diff --git a/compiler/main.v b/compiler/main.v index f06b5673ee..b507be1935 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -100,13 +100,13 @@ mut: building_v bool } - fn main() { // There's no `flags` module yet, so args have to be parsed manually args := env_vflags_and_os_args() // Print the version and exit. if '-v' in args || '--version' in args || 'version' in args { - println('V $Version') + versionhash := vhash() + println('V $Version $versionhash') return } if '-h' in args || '--help' in args || 'help' in args { @@ -231,8 +231,14 @@ fn (v mut V) compile() { cgen.genln('#define VDEBUG (1) ') } - cgen.genln(CommonCHeaders) + if v.pref.building_v { + cgen.genln('#ifndef V_COMMIT_HASH') + cgen.genln('#define V_COMMIT_HASH "' + vhash() + '"') + cgen.genln('#endif') + } + cgen.genln(CommonCHeaders) + v.generate_hotcode_reloading_declarations() imports_json := v.table.imports.contains('json') @@ -1008,3 +1014,14 @@ pub fn cerror(s string) { os.flush_stdout() exit(1) } + +// TODO: this must be uncommented on stage 2, after V_COMMIT_HASH is always present and preserved +fn vhash() string { + /* + mut buf := [50]byte + buf[0] = 0 + C.snprintf(buf, 50, '%s', C.V_COMMIT_HASH ) + return tos_clone(buf) + */ + return '0000000' +} diff --git a/compiler/repl.v b/compiler/repl.v index 776cb0e9ed..a1105aa5b7 100644 --- a/compiler/repl.v +++ b/compiler/repl.v @@ -51,8 +51,9 @@ fn (r mut Repl) function_call(line string) bool { } fn repl_help() { +versionhash := vhash() println(' -V $Version +V $Version $versionhash help Displays this information. Ctrl-C, Ctrl-D, exit Exits the REPL. clear Clears the screen. @@ -60,7 +61,8 @@ V $Version } fn run_repl() []string { - println('V $Version') + versionhash := vhash() + println('V $Version $versionhash') println('Use Ctrl-C or `exit` to exit') file := '.vrepl.v' temp_file := '.vrepl_temp.v' diff --git a/compiler/scanner.v b/compiler/scanner.v index 3d875c0f62..a22c77cb14 100644 --- a/compiler/scanner.v +++ b/compiler/scanner.v @@ -401,6 +401,7 @@ fn (s mut Scanner) scan() ScanRes { // @FILE => will be substituted with the path of the V source file // @LINE => will be substituted with the V line number where it appears (as a string). // @COLUMN => will be substituted with the column where it appears (as a string). + // @VHASH => will be substituted with the shortened commit hash of the V compiler (as a string). // This allows things like this: // println( 'file: ' + @FILE + ' | line: ' + @LINE + ' | fn: ' + @FN) // ... which is useful while debugging/tracing @@ -408,6 +409,7 @@ fn (s mut Scanner) scan() ScanRes { if name == 'FILE' { return scan_res(.str, os.realpath(s.file_path)) } if name == 'LINE' { return scan_res(.str, (s.line_nr+1).str()) } if name == 'COLUMN' { return scan_res(.str, (s.current_column()).str()) } + if name == 'VHASH' { return scan_res(.str, vhash()) } if !is_key(name) { s.error('@ must be used before keywords (e.g. `@type string`)') }