diff --git a/vlib/compiler/vtest.v b/tools/vtest.v similarity index 94% rename from vlib/compiler/vtest.v rename to tools/vtest.v index 65e61343c5..9da1284de1 100644 --- a/vlib/compiler/vtest.v +++ b/tools/vtest.v @@ -1,4 +1,4 @@ -module compiler +module main import ( os @@ -22,7 +22,15 @@ pub fn new_test_sesion(vargs string) TestSession { } } -pub fn test_v() { +fn vexe_path() string { + // NB: tools extracted from v require that the first + // argument to them to be the v executable location. + // They are usually launched by vlib/compiler/vtools.v, + // launch_tool/1 , which provides it. + return os.args[1] +} + +pub fn main() { args := os.args if args.last() == 'test' { println('Usage:') diff --git a/v.v b/v.v index a1d3cba807..4a638a5d42 100755 --- a/v.v +++ b/v.v @@ -58,7 +58,7 @@ fn main() { return } else if 'test' in commands { - compiler.test_v() + compiler.launch_tool('vtest') return } // Generate the docs and exit diff --git a/vlib/compiler/vtools.v b/vlib/compiler/vtools.v new file mode 100644 index 0000000000..b492b1bd5d --- /dev/null +++ b/vlib/compiler/vtools.v @@ -0,0 +1,43 @@ +module compiler + +import os + +pub fn launch_tool(tname string){ + vexe := vexe_path() + vroot := os.dir(vexe) + mut oargs := os.args + oargs[0] = vexe // make it more explicit + tool_exe := os.realpath('$vroot/tools/$tname') + tool_source := os.realpath('$vroot/tools/${tname}.v') + ////////////////////////////////////////////////////// + tool_args := oargs.join(' ') + tool_command := '$tool_exe $tool_args' + //println('Launching: "$tool_command" ...') + + mut tool_should_be_recompiled := false + if !os.file_exists( tool_exe ) { + // fresh checkout + tool_should_be_recompiled = true + }else{ + if os.file_last_mod_unix( tool_exe ) <= os.file_last_mod_unix( vexe ) { + // v was recompiled, maybe after v up ... + // rebuild the tool too just in case + tool_should_be_recompiled = true + } + if os.file_last_mod_unix( tool_exe ) <= os.file_last_mod_unix( tool_source ) { + // the user changed the source code of the tool + tool_should_be_recompiled = true + } + } + + if tool_should_be_recompiled { + compilation_command := '$vexe -prod $tool_source' + //println('Compiling $tname with: "$compilation_command"') + tool_compilation := os.exec(compilation_command) or { panic(err) } + if tool_compilation.exit_code != 0 { + panic('V tool "$tool_source" could not be compiled.') + } + } + + exit( os.system(tool_command) ) +}