all: implement @VCURRENTHASH to replace C.V_CURRENT_COMMIT_HASH (#19514)

This commit is contained in:
Spydr 2023-10-05 17:14:36 +02:00 committed by GitHub
parent 1512486d01
commit 32bb8cf86d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 20 additions and 13 deletions

View file

@ -5533,6 +5533,9 @@ that are substituted at compile time:
- `@VEXEROOT` => will be substituted with the *folder*, - `@VEXEROOT` => will be substituted with the *folder*,
where the V executable is (as a string). where the V executable is (as a string).
- `@VHASH` => replaced with the shortened commit hash of the V compiler (as a string). - `@VHASH` => replaced with the shortened commit hash of the V compiler (as a string).
- `@VCURRENTHASH` => Similar to `@VHASH`, but changes when the compiler is
recompiled on a different commit (after local modifications, or after
using git bisect etc).
- `@VMOD_FILE` => replaced with the contents of the nearest v.mod file (as a string). - `@VMOD_FILE` => replaced with the contents of the nearest v.mod file (as a string).
- `@VMODROOT` => will be substituted with the *folder*, - `@VMODROOT` => will be substituted with the *folder*,
where the nearest v.mod file is (as a string). where the nearest v.mod file is (as a string).

View file

@ -50,7 +50,7 @@ fn panic_debug(line_no int, file string, mod string, fn_name string, s string) {
eprintln(' function: ${fn_name}()') eprintln(' function: ${fn_name}()')
eprintln(' message: ${s}') eprintln(' message: ${s}')
eprintln(' file: ${file}:${line_no}') eprintln(' file: ${file}:${line_no}')
eprintln(' v hash: ${@VHASH}') eprintln(' v hash: ${@VHASH}') // TODO: use @VCURRENTHASH when bootstrapped
eprintln('=========================================') eprintln('=========================================')
$if native { $if native {
C.exit(1) // TODO: native backtraces C.exit(1) // TODO: native backtraces
@ -104,7 +104,7 @@ pub fn panic(s string) {
} $else { } $else {
eprint('V panic: ') eprint('V panic: ')
eprintln(s) eprintln(s)
eprintln('v hash: ${@VHASH}') eprintln('v hash: ${@VHASH}') // TODO: use @VCURRENTHASH when bootstrapped
$if native { $if native {
C.exit(1) // TODO: native backtraces C.exit(1) // TODO: native backtraces
} $else $if exit_after_panic_message ? { } $else $if exit_after_panic_message ? {

View file

@ -133,6 +133,8 @@ mut:
goto_labels map[string]ast.GotoLabel // to check for unused goto labels goto_labels map[string]ast.GotoLabel // to check for unused goto labels
enum_data_type ast.Type enum_data_type ast.Type
fn_return_type ast.Type fn_return_type ast.Type
v_current_commit_hash string // same as V_CURRENT_COMMIT_HASH
} }
pub fn new_checker(table &ast.Table, pref_ &pref.Preferences) &Checker { pub fn new_checker(table &ast.Table, pref_ &pref.Preferences) &Checker {
@ -145,6 +147,7 @@ pub fn new_checker(table &ast.Table, pref_ &pref.Preferences) &Checker {
pref: pref_ pref: pref_
timers: util.new_timers(should_print: timers_should_print, label: 'checker') timers: util.new_timers(should_print: timers_should_print, label: 'checker')
match_exhaustive_cutoff_limit: pref_.checker_match_exhaustive_cutoff_limit match_exhaustive_cutoff_limit: pref_.checker_match_exhaustive_cutoff_limit
v_current_commit_hash: version.githash(pref_.building_v)
} }
} }
@ -3368,6 +3371,9 @@ fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type {
.vhash { .vhash {
node.val = version.vhash() node.val = version.vhash()
} }
.v_current_hash {
node.val = c.v_current_commit_hash
}
.vmod_file { .vmod_file {
// cache the vmod content, do not read it many times // cache the vmod content, do not read it many times
if c.vmod_file_content.len == 0 { if c.vmod_file_content.len == 0 {

View file

@ -282,9 +282,8 @@ fn (e Eval) error(msg string) {
} }
fn (e Eval) panic(s string) { fn (e Eval) panic(s string) {
commithash := unsafe { tos5(&char(C.V_CURRENT_COMMIT_HASH)) }
eprintln('V panic: ${s}') eprintln('V panic: ${s}')
eprintln('V hash: ${commithash}') eprintln('V hash: ${@VCURRENTHASH}')
e.print_backtrace() e.print_backtrace()
exit(1) exit(1)
} }

View file

@ -5,7 +5,6 @@ import v.ast
import v.token import v.token
import v.pref import v.pref
import v.util import v.util
import v.util.version
import v.depgraph import v.depgraph
import encoding.base64 import encoding.base64
import v.gen.js.sourcemap import v.gen.js.sourcemap
@ -247,7 +246,7 @@ pub fn gen(files []&ast.File, table &ast.Table, pref_ &pref.Preferences) string
// deps_resolved := graph.resolve() // deps_resolved := graph.resolve()
// nodes := deps_resolved.nodes // nodes := deps_resolved.nodes
mut out := g.definitions.str() + g.hashes() mut out := g.definitions.str()
if !g.pref.output_es5 { if !g.pref.output_es5 {
out += '\nlet wasmExportObject;\n' out += '\nlet wasmExportObject;\n'
@ -491,12 +490,6 @@ pub fn (mut g JsGen) init() {
g.definitions.writeln('function ReturnException(val) { this.val = val; }') g.definitions.writeln('function ReturnException(val) { this.val = val; }')
} }
pub fn (g JsGen) hashes() string {
mut res := '// V_COMMIT_HASH ${version.vhash()}\n'
res += '// V_CURRENT_COMMIT_HASH ${version.githash(g.pref.building_v)}\n'
return res
}
[noreturn] [noreturn]
fn verror(msg string) { fn verror(msg string) {
eprintln('jsgen error: ${msg}') eprintln('jsgen error: ${msg}')

View file

@ -8,6 +8,9 @@ import v.util
fn (mut g Gen) expr(node ast.Expr) { fn (mut g Gen) expr(node ast.Expr) {
match node { match node {
ast.AtExpr {
g.allocate_string(g.comptime_at(node), 3, .rel32)
}
ast.ParExpr { ast.ParExpr {
g.expr(node.expr) g.expr(node.expr)
} }

View file

@ -352,6 +352,7 @@ fn (mut p Parser) at() ast.AtExpr {
'@FILE_LINE' { token.AtKind.file_path_line_nr } '@FILE_LINE' { token.AtKind.file_path_line_nr }
'@LOCATION' { token.AtKind.location } '@LOCATION' { token.AtKind.location }
'@COLUMN' { token.AtKind.column_nr } '@COLUMN' { token.AtKind.column_nr }
'@VCURRENTHASH' { token.AtKind.v_current_hash }
'@VHASH' { token.AtKind.vhash } '@VHASH' { token.AtKind.vhash }
'@VMOD_FILE' { token.AtKind.vmod_file } '@VMOD_FILE' { token.AtKind.vmod_file }
'@VEXE' { token.AtKind.vexe_path } '@VEXE' { token.AtKind.vexe_path }

View file

@ -173,6 +173,7 @@ pub enum AtKind {
line_nr line_nr
column_nr column_nr
vhash vhash
v_current_hash
vmod_file vmod_file
vmodroot_path vmodroot_path
vroot_path // obsolete vroot_path // obsolete
@ -187,7 +188,8 @@ pub const (
.unsigned_right_shift_assign] .unsigned_right_shift_assign]
valid_at_tokens = ['@VROOT', '@VMODROOT', '@VEXEROOT', '@FN', '@METHOD', '@MOD', '@STRUCT', valid_at_tokens = ['@VROOT', '@VMODROOT', '@VEXEROOT', '@FN', '@METHOD', '@MOD', '@STRUCT',
'@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VMOD_FILE', '@FILE_LINE', '@LOCATION'] '@VEXE', '@FILE', '@LINE', '@COLUMN', '@VHASH', '@VCURRENTHASH', '@VMOD_FILE', '@FILE_LINE',
'@LOCATION']
token_str = build_token_str() token_str = build_token_str()