2019-11-01 02:15:03 +03:00
|
|
|
module compiler
|
|
|
|
|
2019-12-23 13:09:22 +03:00
|
|
|
import (
|
|
|
|
os
|
|
|
|
filepath
|
|
|
|
)
|
2019-11-01 02:15:03 +03:00
|
|
|
|
2020-01-08 19:15:05 +03:00
|
|
|
pub fn launch_tool(tname string, cmdname string) {
|
2019-12-23 13:02:50 +03:00
|
|
|
is_verbose := '-verbose' in os.args || '--verbose' in os.args
|
2019-11-01 02:15:03 +03:00
|
|
|
vexe := vexe_path()
|
2019-12-23 13:09:22 +03:00
|
|
|
vroot := filepath.dir(vexe)
|
2019-12-23 13:02:50 +03:00
|
|
|
set_vroot_folder( vroot ) // needed by tools to find back v
|
2020-01-08 19:15:05 +03:00
|
|
|
mut tname_index := os.args.index(cmdname)
|
|
|
|
if tname_index == -1 {
|
|
|
|
tname_index = os.args.len
|
|
|
|
}
|
|
|
|
mut compilation_options := os.args[1..tname_index].clone()
|
2019-12-23 13:02:50 +03:00
|
|
|
tool_args := os.args[1..].join(' ')
|
2019-11-01 02:15:03 +03:00
|
|
|
tool_exe := os.realpath('$vroot/tools/$tname')
|
|
|
|
tool_source := os.realpath('$vroot/tools/${tname}.v')
|
2019-11-17 14:36:05 +03:00
|
|
|
tool_command := '"$tool_exe" $tool_args'
|
2019-12-23 13:02:50 +03:00
|
|
|
if is_verbose {
|
|
|
|
eprintln('launch_tool vexe : $vroot')
|
|
|
|
eprintln('launch_tool vroot : $vroot')
|
|
|
|
eprintln('launch_tool tool_args : $tool_args')
|
|
|
|
eprintln('launch_tool tool_command: $tool_command')
|
|
|
|
}
|
2019-11-01 02:15:03 +03:00
|
|
|
mut tool_should_be_recompiled := false
|
2019-12-20 00:29:37 +03:00
|
|
|
if !os.exists(tool_exe) {
|
2019-11-01 02:15:03 +03:00
|
|
|
// fresh checkout
|
|
|
|
tool_should_be_recompiled = true
|
2019-12-23 13:02:50 +03:00
|
|
|
} else {
|
2019-12-20 00:29:37 +03:00
|
|
|
if os.file_last_mod_unix(tool_exe) <= os.file_last_mod_unix(vexe) {
|
2019-11-01 02:15:03 +03:00
|
|
|
// v was recompiled, maybe after v up ...
|
|
|
|
// rebuild the tool too just in case
|
|
|
|
tool_should_be_recompiled = true
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
if os.file_last_mod_unix(tool_exe) <= os.file_last_mod_unix(tool_source) {
|
2019-11-01 02:15:03 +03:00
|
|
|
// the user changed the source code of the tool
|
|
|
|
tool_should_be_recompiled = true
|
|
|
|
}
|
|
|
|
}
|
2019-12-23 13:02:50 +03:00
|
|
|
|
|
|
|
if is_verbose {
|
|
|
|
eprintln('launch_tool tool_should_be_recompiled: $tool_should_be_recompiled')
|
|
|
|
}
|
|
|
|
|
2019-11-01 02:15:03 +03:00
|
|
|
if tool_should_be_recompiled {
|
2020-01-08 19:15:05 +03:00
|
|
|
if tname == 'vfmt' { compilation_options << ['-d', 'vfmt'] }
|
|
|
|
compilation_args := compilation_options.join(' ')
|
|
|
|
compilation_command := '"$vexe" $compilation_args "$tool_source"'
|
2019-12-23 13:02:50 +03:00
|
|
|
if is_verbose {
|
|
|
|
eprintln('Compiling $tname with: "$compilation_command"')
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
2019-12-23 13:02:50 +03:00
|
|
|
tool_compilation := os.exec(compilation_command) or { panic(err) }
|
2019-11-01 02:15:03 +03:00
|
|
|
if tool_compilation.exit_code != 0 {
|
2019-11-07 16:17:32 +03:00
|
|
|
panic('V tool "$tool_source" could not be compiled\n' + tool_compilation.output)
|
2019-11-01 02:15:03 +03:00
|
|
|
}
|
|
|
|
}
|
2019-12-23 13:02:50 +03:00
|
|
|
if is_verbose {
|
|
|
|
eprintln('launch_tool running tool command: $tool_command ...')
|
|
|
|
}
|
|
|
|
|
|
|
|
exit( os.system(tool_command) )
|
2019-11-01 02:15:03 +03:00
|
|
|
}
|