Skip to content

Commit 62ab1be

Browse files
committed
v.util: add get_build_time/0, supporting https://reproducible-builds.org/docs/source-date-epoch/
1 parent 6e13b02 commit 62ab1be

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

vlib/v/util/util.v

+16-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,20 @@ pub fn tabs(n int) string {
4747
return if n >= 0 && n < const_tabs.len { const_tabs[n] } else { '\t'.repeat(n) }
4848
}
4949

50-
//
50+
// get_build_time returns the current build time, while taking into account SOURCE_DATE_EPOCH
51+
// to support transparent reproducible builds. See also https://reproducible-builds.org/docs/source-date-epoch/
52+
// When SOURCE_DATE_EPOCH is not set, it will return the current UTC time.
53+
pub fn get_build_time() time.Time {
54+
sde := os.getenv('SOURCE_DATE_EPOCH')
55+
if sde == '' {
56+
return time.utc()
57+
}
58+
return time.unix_nanosecond(sde.i64(), 0)
59+
}
60+
61+
// set_vroot_folder sets the VCHILD env variable to 'true', and VEXE to the location of the V executable
62+
// It is called very early by launch_tool/3, so that those env variables can be available by all tools,
63+
// like `v doc`, `v fmt` etc, so they can use them to find how they were started.
5164
pub fn set_vroot_folder(vroot_path string) {
5265
// Preparation for the compiler module:
5366
// VEXE env variable is needed so that compiler.vexe_path() can return it
@@ -61,6 +74,8 @@ pub fn set_vroot_folder(vroot_path string) {
6174
os.setenv('VCHILD', 'true', true)
6275
}
6376

77+
// resolve_vmodroot replaces all occurences of `@VMODROOT` in `str`, with an absolute path,
78+
// formed by resolving, where the nearest `v.mod` is, given the folder `dir`.
6479
pub fn resolve_vmodroot(str string, dir string) !string {
6580
mut mcache := vmod.get_cache()
6681
vmod_file_location := mcache.get_by_folder(dir)

0 commit comments

Comments
 (0)