From 1d78914a8f21f2f015c49cd231d93b941f64862d Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 28 May 2020 18:36:04 +0200 Subject: [PATCH] parser: support @VROOT in #include too, make it use absolute paths --- vlib/v/parser/comptime.v | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 043451b5d2..16f3abeca7 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -3,6 +3,7 @@ // that can be found in the LICENSE file. module parser +import os import v.ast import v.pref import v.vmod @@ -13,9 +14,20 @@ const ( 'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos'] ) +fn (mut p Parser)resolve_vroot(flag string) string { + vmod_file_location := vmod.mod_file_cacher.get_by_folder(p.file_name_dir) + if vmod_file_location.vmod_file.len == 0 { + // There was no actual v.mod file found. + p.error('To use @VROOT, you need' + ' to have a "v.mod" file in ${p.file_name_dir},' + + ' or in one of its parent folders.') + } + vmod_path := vmod_file_location.vmod_folder + return flag.replace('@VROOT', os.real_path(vmod_path)) +} + // // #include, #flag, #v fn (mut p Parser) hash() ast.HashStmt { - val := p.tok.lit + mut val := p.tok.lit p.next() if p.pref.backend == .js { if !p.file_name.ends_with('.js.v') { @@ -25,18 +37,19 @@ fn (mut p Parser) hash() ast.HashStmt { p.error('Hash statements are not allowed in the main module. Please place them in a separate module.') } } + if val.starts_with('include') { + mut flag := val[8..] + if flag.contains('@VROOT') { + vroot := p.resolve_vroot(flag) + val = 'include $vroot' + } + } if val.starts_with('flag') { // #flag linux -lm mut flag := val[5..] // expand `@VROOT` to its absolute path if flag.contains('@VROOT') { - vmod_file_location := vmod.mod_file_cacher.get_by_folder(p.file_name_dir) - if vmod_file_location.vmod_file.len == 0 { - // There was no actual v.mod file found. - p.error('To use @VROOT, you need' + ' to have a "v.mod" file in ${p.file_name_dir},' + - ' or in one of its parent folders.') - } - flag = flag.replace('@VROOT', vmod_file_location.vmod_folder) + flag = p.resolve_vroot(flag) } for deprecated in ['@VMOD', '@VMODULE', '@VPATH', '@VLIB_PATH'] { if flag.contains(deprecated) {