v/cmd/v/simple_tool.v

79 lines
2.1 KiB
V
Raw Normal View History

2020-02-09 09:08:04 +00:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module main
2019-12-23 10:09:22 +00:00
import (
2020-02-09 09:08:04 +00:00
compiler
2019-12-23 10:09:22 +00:00
filepath
2020-02-09 09:08:04 +00:00
os
2020-02-20 10:33:01 +00:00
v.pref
2019-12-23 10:09:22 +00:00
)
2020-02-09 09:08:04 +00:00
fn launch_tool(is_verbose bool, tname string, cmdname string) {
2020-02-20 10:33:01 +00:00
vexe := pref.vexe_path()
2019-12-23 10:09:22 +00:00
vroot := filepath.dir(vexe)
2020-02-09 09:08:04 +00:00
compiler.set_vroot_folder(vroot)
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()
tool_args := os.args[1..].join(' ')
tool_exe := path_of_executable(os.realpath('$vroot/cmd/tools/$tname'))
2020-02-09 09:08:04 +00:00
tool_source := os.realpath('$vroot/cmd/tools/${tname}.v')
tool_command := '"$tool_exe" $tool_args'
2020-02-09 09:08:04 +00: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')
2020-02-09 09:08:04 +00:00
}
mut should_compile := false
2019-12-19 21:29:37 +00:00
if !os.exists(tool_exe) {
2020-02-09 09:08:04 +00:00
should_compile = true
} else {
2019-12-19 21:29:37 +00:00
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
2020-02-09 09:08:04 +00:00
should_compile = true
}
2019-12-19 21:29:37 +00:00
if os.file_last_mod_unix(tool_exe) <= os.file_last_mod_unix(tool_source) {
// the user changed the source code of the tool
2020-02-09 09:08:04 +00:00
should_compile = true
}
}
if is_verbose {
2020-02-09 09:08:04 +00:00
eprintln('launch_tool should_compile: $should_compile')
}
2020-02-09 09:08:04 +00:00
if should_compile {
if tname == 'vfmt' {
compilation_options << ['-d', 'vfmt']
}
compilation_args := compilation_options.join(' ')
compilation_command := '"$vexe" $compilation_args "$tool_source"'
if is_verbose {
2020-02-20 10:33:01 +00:00
eprintln('Compiling $tname with: "$compilation_command"')
2019-12-19 21:29:37 +00:00
}
tool_compilation := os.exec(compilation_command) or { panic(err) }
if tool_compilation.exit_code != 0 {
panic('V tool "$tool_source" could not be compiled\n' + tool_compilation.output)
}
}
if is_verbose {
eprintln('launch_tool running tool command: $tool_command ...')
}
2020-02-20 10:33:01 +00:00
2020-02-09 09:08:04 +00:00
exit(os.system(tool_command))
}
fn path_of_executable(path string) string {
$if windows {
return path + '.exe'
}
return path
}