From d51019dd770d91011ea122548810d974c26e4dd0 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 20 Feb 2020 18:33:01 +0800 Subject: [PATCH] unify vexe_path --- cmd/tools/modules/testing/common.v | 31 ++++++++++++------------------ cmd/tools/vtest-compiler.v | 3 ++- cmd/v/compile_options.v | 20 +++++++++---------- cmd/v/flag.v | 10 ---------- cmd/v/simple_tool.v | 7 ++++--- cmd/v/symlink.v | 8 +++++--- vlib/compiler/cc.v | 2 +- vlib/compiler/main.v | 12 +----------- vlib/compiler/module_header.v | 2 +- vlib/v/doc/doc.v | 14 ++------------ vlib/v/pref/default.v | 3 +-- 11 files changed, 39 insertions(+), 73 deletions(-) diff --git a/cmd/tools/modules/testing/common.v b/cmd/tools/modules/testing/common.v index fd6ffb9b4c..72116a1eee 100644 --- a/cmd/tools/modules/testing/common.v +++ b/cmd/tools/modules/testing/common.v @@ -7,6 +7,7 @@ import ( filepath runtime sync + v.pref ) pub struct TestSession { @@ -16,7 +17,7 @@ pub mut: vargs string failed bool benchmark benchmark.Benchmark - + ntask int // writing to this should be locked by mu. ntask_mtx &sync.Mutex waitgroup &sync.WaitGroup @@ -25,25 +26,17 @@ pub mut: pub fn new_test_session(vargs string) TestSession { return TestSession{ - vexe: vexe_path() + vexe: pref.vexe_path() vargs: vargs - + ntask: 0 ntask_mtx: sync.new_mutex() waitgroup: sync.new_waitgroup() - + show_ok_tests: !vargs.contains('-silent') } } -pub fn vexe_path() string { - // NB: tools extracted from v require that the VEXE - // environment variable contains the path to the v executable location. - // They are usually launched by cmd/v/simple_tool.v, - // launch_tool/1 , which provides it. - return os.getenv('VEXE') -} - pub fn (ts mut TestSession) init() { ts.benchmark = benchmark.new_benchmark() } @@ -76,7 +69,7 @@ pub fn (ts mut TestSession) test() { } remaining_files << dot_relative_file } - + ts.files = remaining_files ts.benchmark.set_total_expected_steps(remaining_files.len) @@ -107,7 +100,7 @@ fn process_in_thread(ts mut TestSession){ fn (ts mut TestSession) process_files() { tmpd := os.tmpdir() show_stats := '-stats' in ts.vargs.split(' ') - + mut tls_bench := benchmark.new_benchmark() // tls_bench is used to format the step messages/timings tls_bench.set_total_expected_steps( ts.benchmark.nexpected_steps ) for { @@ -116,11 +109,11 @@ fn (ts mut TestSession) process_files() { ts.ntask++ idx := ts.ntask-1 ts.ntask_mtx.unlock() - + if idx >= ts.files.len { break } tls_bench.cstep = idx - - dot_relative_file := ts.files[ idx ] + + dot_relative_file := ts.files[ idx ] relative_file := dot_relative_file.replace('./', '') file := os.realpath(relative_file) // Ensure that the generated binaries will be stored in the temporary folder. @@ -192,7 +185,7 @@ pub fn vlib_should_be_present(parent_dir string) { pub fn v_build_failing(zargs string, folder string) bool { main_label := 'Building $folder ...' finish_label := 'building $folder' - vexe := vexe_path() + vexe := pref.vexe_path() parent_dir := filepath.dir(vexe) vlib_should_be_present(parent_dir) vargs := zargs.replace(vexe, '') @@ -233,7 +226,7 @@ pub fn build_v_cmd_failed(cmd string) bool { pub fn building_any_v_binaries_failed() bool { eheader('Building V binaries...') eprintln('VFLAGS is: "' + os.getenv('VFLAGS') + '"') - vexe := testing.vexe_path() + vexe := pref.vexe_path() parent_dir := filepath.dir(vexe) testing.vlib_should_be_present(parent_dir) os.chdir(parent_dir) diff --git a/cmd/tools/vtest-compiler.v b/cmd/tools/vtest-compiler.v index 6fc64590ca..8dc22d2002 100644 --- a/cmd/tools/vtest-compiler.v +++ b/cmd/tools/vtest-compiler.v @@ -5,6 +5,7 @@ import ( testing benchmark filepath + v.pref ) pub const ( @@ -18,7 +19,7 @@ fn main() { } fn v_test_compiler(vargs string) { - vexe := testing.vexe_path() + vexe := pref.vexe_path() parent_dir := filepath.dir(vexe) testing.vlib_should_be_present(parent_dir) // Changing the current directory is needed for some of the compiler tests, diff --git a/cmd/v/compile_options.v b/cmd/v/compile_options.v index 69e5c78391..d94ded7f73 100644 --- a/cmd/v/compile_options.v +++ b/cmd/v/compile_options.v @@ -24,7 +24,7 @@ pub fn new_v(args []string) &compiler.V { panic(err) } } - vroot := filepath.dir(vexe_path()) + vroot := filepath.dir(pref.vexe_path()) // optional, custom modules search path user_mod_path := cmdline.option(args, '-user_mod_path', '') vlib_path := cmdline.option(args, '-vlib-path', '') @@ -108,7 +108,7 @@ pub fn new_v(args []string) &compiler.V { } is_repl := '-repl' in args ccompiler := cmdline.option(args, '-cc', '') - mut pref := &pref.Preferences{ + mut prefs := &pref.Preferences{ os: pref.os_from_string(target_os) is_so: '-shared' in args is_solive: '-solive' in args @@ -155,20 +155,20 @@ pub fn new_v(args []string) &compiler.V { compile_defines_all: compile_defines_all mod: mod } - if pref.is_verbose || pref.is_debug { - println('C compiler=$pref.ccompiler') + if prefs.is_verbose || prefs.is_debug { + println('C compiler=$prefs.ccompiler') } $if !linux { - if pref.is_bare && !out_name.ends_with('.c') { + if prefs.is_bare && !out_name.ends_with('.c') { println('V error: -freestanding only works on Linux for now') os.flush_stdout() exit(1) } } - pref.fill_with_defaults() + prefs.fill_with_defaults() // v.exe's parent directory should contain vlib - if !os.is_dir(pref.vlib_path) || !os.is_dir(pref.vlib_path + filepath.separator + 'builtin') { + if !os.is_dir(prefs.vlib_path) || !os.is_dir(prefs.vlib_path + filepath.separator + 'builtin') { // println('vlib not found, downloading it...') /* ret := os.system('git clone --depth=1 https://github.com/vlang/v .') @@ -180,16 +180,16 @@ pub fn new_v(args []string) &compiler.V { */ println('vlib not found. It should be next to the V executable.') println('Go to https://vlang.io to install V.') - println('(os.executable=${os.executable()} vlib_path=$pref.vlib_path vexe_path=${vexe_path()}') + println('(os.executable=${os.executable()} vlib_path=$prefs.vlib_path vexe_path=${pref.vexe_path()}') exit(1) } - if pref.is_script && !os.exists(dir) { + if prefs.is_script && !os.exists(dir) { println('`$dir` does not exist') exit(1) } - return compiler.new_v(pref) + return compiler.new_v(prefs) } fn find_c_compiler_thirdparty_options(args []string) string { diff --git a/cmd/v/flag.v b/cmd/v/flag.v index 9427b99b25..4f1f308fd4 100644 --- a/cmd/v/flag.v +++ b/cmd/v/flag.v @@ -66,13 +66,3 @@ fn join_flags_and_argument() []string { return non_empty(os.args) } - -fn vexe_path() string { - vexe := os.getenv('VEXE') - if vexe != '' { - return vexe - } - real_vexe_path := os.realpath(os.executable()) - os.setenv('VEXE', real_vexe_path, true) - return real_vexe_path -} diff --git a/cmd/v/simple_tool.v b/cmd/v/simple_tool.v index d39d6b47cb..e69e463d7a 100644 --- a/cmd/v/simple_tool.v +++ b/cmd/v/simple_tool.v @@ -7,10 +7,11 @@ import ( compiler filepath os + v.pref ) fn launch_tool(is_verbose bool, tname string, cmdname string) { - vexe := vexe_path() + vexe := pref.vexe_path() vroot := filepath.dir(vexe) compiler.set_vroot_folder(vroot) @@ -55,7 +56,7 @@ fn launch_tool(is_verbose bool, tname string, cmdname string) { compilation_args := compilation_options.join(' ') compilation_command := '"$vexe" $compilation_args "$tool_source"' if is_verbose { - eprintln('Compiling $tname with: "$compilation_command"') + eprintln('Compiling $tname with: "$compilation_command"') } tool_compilation := os.exec(compilation_command) or { panic(err) } if tool_compilation.exit_code != 0 { @@ -65,7 +66,7 @@ fn launch_tool(is_verbose bool, tname string, cmdname string) { if is_verbose { eprintln('launch_tool running tool command: $tool_command ...') } - + exit(os.system(tool_command)) } diff --git a/cmd/v/symlink.v b/cmd/v/symlink.v index f35cc39d2b..d36333c87b 100644 --- a/cmd/v/symlink.v +++ b/cmd/v/symlink.v @@ -3,13 +3,16 @@ // that can be found in the LICENSE file. module main -import os +import ( + os + v.pref +) fn create_symlink() { $if windows { return } - vexe := vexe_path() + vexe := pref.vexe_path() mut link_path := '/usr/local/bin/v' mut ret := os.exec('ln -sf $vexe $link_path') or { panic(err) } if ret.exit_code == 0 { @@ -28,4 +31,3 @@ fn create_symlink() { println('Failed to create symlink "$link_path". Try again with sudo.') } } - diff --git a/vlib/compiler/cc.v b/vlib/compiler/cc.v index db3194f57c..8e42ee53df 100644 --- a/vlib/compiler/cc.v +++ b/vlib/compiler/cc.v @@ -30,7 +30,7 @@ fn (v mut V) cc() { return } v.build_thirdparty_obj_files() - vexe := vexe_path() + vexe := pref.vexe_path() vdir := filepath.dir(vexe) // Just create a C/JavaScript file and exit // for example: `v -o v.c compiler` diff --git a/vlib/compiler/main.v b/vlib/compiler/main.v index c947bb69c3..d5b17e8c01 100644 --- a/vlib/compiler/main.v +++ b/vlib/compiler/main.v @@ -733,7 +733,7 @@ pub fn (v &V) get_user_files() []string { mut user_files := []string // See cmd/tools/preludes/README.md for more info about what preludes are - vroot := filepath.dir(vexe_path()) + vroot := filepath.dir(pref.vexe_path()) preludes_path := filepath.join(vroot,'cmd','tools','preludes') if v.pref.is_live { user_files << filepath.join(preludes_path,'live_main.v') @@ -844,16 +844,6 @@ pub fn (v &V) log(s string) { println(s) } -pub fn vexe_path() string { - vexe := os.getenv('VEXE') - if '' != vexe { - return vexe - } - real_vexe_path := os.realpath(os.executable()) - os.setenv('VEXE', real_vexe_path, true) - return real_vexe_path -} - pub fn verror(s string) { println('V error: $s') os.flush_stdout() diff --git a/vlib/compiler/module_header.v b/vlib/compiler/module_header.v index 24bda0faa1..44c96e2a42 100644 --- a/vlib/compiler/module_header.v +++ b/vlib/compiler/module_header.v @@ -28,7 +28,7 @@ mut: // `mod` == "vlib/os" fn generate_vh(mod string) { println('\n\n\n\nGenerating a V header file for module `$mod`') - vexe := vexe_path() + vexe := pref.vexe_path() full_mod_path := filepath.join(filepath.dir(vexe),mod) dir := if mod.starts_with('vlib') { '$compiler.v_modules_path${filepath.separator}$mod' } else { mod } path := dir + '.vh' diff --git a/vlib/v/doc/doc.v b/vlib/v/doc/doc.v index 025c23138d..55d1b85066 100644 --- a/vlib/v/doc/doc.v +++ b/vlib/v/doc/doc.v @@ -3,7 +3,7 @@ module doc import ( strings // v.builder - // v.pref + v.pref v.table v.parser v.ast @@ -25,7 +25,7 @@ pub fn doc(mod string, table &table.Table) string { table: table mod: mod } - mods_path := filepath.dir(vexe_path()) + '/vlib' + mods_path := filepath.dir(pref.vexe_path()) + '/vlib' path := filepath.join(mods_path,mod).replace('.', filepath.separator) if !os.exists(path) { println('module "$mod" not found') @@ -92,13 +92,3 @@ fn (d mut Doc) print_methods() { } } } - -pub fn vexe_path() string { - vexe := os.getenv('VEXE') - if '' != vexe { - return vexe - } - real_vexe_path := os.realpath(os.executable()) - os.setenv('VEXE', real_vexe_path, true) - return real_vexe_path -} diff --git a/vlib/v/pref/default.v b/vlib/v/pref/default.v index c56ffd159c..ff648079b2 100644 --- a/vlib/v/pref/default.v +++ b/vlib/v/pref/default.v @@ -66,8 +66,7 @@ fn default_c_compiler() string { return 'cc' } -//TODO Remove code duplication -fn vexe_path() string { +pub fn vexe_path() string { vexe := os.getenv('VEXE') if vexe != '' { return vexe