diff options
Diffstat (limited to 'v_windows/v/cmd/tools/modules')
-rw-r--r-- | v_windows/v/cmd/tools/modules/scripting/scripting.v | 183 | ||||
-rw-r--r-- | v_windows/v/cmd/tools/modules/testing/common.v | 488 | ||||
-rw-r--r-- | v_windows/v/cmd/tools/modules/vgit/vgit.v | 197 | ||||
-rw-r--r-- | v_windows/v/cmd/tools/modules/vhelp/vhelp.v | 14 |
4 files changed, 882 insertions, 0 deletions
diff --git a/v_windows/v/cmd/tools/modules/scripting/scripting.v b/v_windows/v/cmd/tools/modules/scripting/scripting.v new file mode 100644 index 0000000..342b9b7 --- /dev/null +++ b/v_windows/v/cmd/tools/modules/scripting/scripting.v @@ -0,0 +1,183 @@ +module scripting + +import os +import term +import time + +const ( + term_colors = term.can_show_color_on_stdout() +) + +pub fn set_verbose(on bool) { + // setting a global here would be the obvious solution, + // but V does not have globals normally. + if on { + os.setenv('VERBOSE', '1', true) + } else { + os.unsetenv('VERBOSE') + } +} + +pub fn cprint(omessage string) { + mut message := omessage + if scripting.term_colors { + message = term.cyan(message) + } + print(message) +} + +pub fn cprint_strong(omessage string) { + mut message := omessage + if scripting.term_colors { + message = term.bright_green(message) + } + print(message) +} + +pub fn cprintln(omessage string) { + cprint(omessage) + println('') +} + +pub fn cprintln_strong(omessage string) { + cprint_strong(omessage) + println('') +} + +pub fn verbose_trace(label string, message string) { + if os.getenv('VERBOSE').len > 0 { + slabel := '$time.now().format_ss_milli() $label' + cprintln('# ${slabel:-43s} : $message') + } +} + +pub fn verbose_trace_strong(label string, omessage string) { + if os.getenv('VERBOSE').len > 0 { + slabel := '$time.now().format_ss_milli() $label' + mut message := omessage + if scripting.term_colors { + message = term.bright_green(message) + } + cprintln('# ${slabel:-43s} : $message') + } +} + +pub fn verbose_trace_exec_result(x os.Result) { + if os.getenv('VERBOSE').len > 0 { + cprintln('# cmd.exit_code : ${x.exit_code.str():-4s} cmd.output:') + mut lnum := 1 + lines := x.output.split_into_lines() + for oline in lines { + mut line := oline + if scripting.term_colors { + line = term.bright_green(line) + } + cprintln('# ${lnum:3d}: $line') + lnum++ + } + cprintln('# ----------------------------------------------------------------------') + } +} + +fn modfn(mname string, fname string) string { + return '${mname}.$fname' +} + +pub fn chdir(path string) { + verbose_trace_strong(modfn(@MOD, @FN), 'cd $path') + os.chdir(path) or { + verbose_trace(modfn(@MOD, @FN), '## failed.') + return + } +} + +pub fn mkdir(path string) ? { + verbose_trace_strong(modfn(@MOD, @FN), 'mkdir $path') + os.mkdir(path) or { + verbose_trace(modfn(@MOD, @FN), '## failed.') + return err + } +} + +pub fn mkdir_all(path string) ? { + verbose_trace_strong(modfn(@MOD, @FN), 'mkdir -p $path') + os.mkdir_all(path) or { + verbose_trace(modfn(@MOD, @FN), '## failed.') + return err + } +} + +pub fn rmrf(path string) { + verbose_trace_strong(modfn(@MOD, @FN), 'rm -rf $path') + if os.exists(path) { + if os.is_dir(path) { + os.rmdir_all(path) or { panic(err) } + } else { + os.rm(path) or { panic(err) } + } + } +} + +// execute a command, and return a result, or an error, if it failed in any way. +pub fn exec(cmd string) ?os.Result { + verbose_trace_strong(modfn(@MOD, @FN), cmd) + x := os.execute(cmd) + if x.exit_code != 0 { + verbose_trace(modfn(@MOD, @FN), '## failed.') + return error(x.output) + } + verbose_trace_exec_result(x) + return x +} + +// run a command, tracing its results, and returning ONLY its output +pub fn run(cmd string) string { + verbose_trace_strong(modfn(@MOD, @FN), cmd) + x := os.execute(cmd) + if x.exit_code < 0 { + verbose_trace(modfn(@MOD, @FN), '## failed.') + return '' + } + verbose_trace_exec_result(x) + if x.exit_code == 0 { + return x.output.trim_right('\r\n') + } + return '' +} + +pub fn exit_0_status(cmd string) bool { + verbose_trace_strong(modfn(@MOD, @FN), cmd) + x := os.execute(cmd) + if x.exit_code < 0 { + verbose_trace(modfn(@MOD, @FN), '## failed.') + return false + } + verbose_trace_exec_result(x) + if x.exit_code == 0 { + return true + } + return false +} + +pub fn tool_must_exist(toolcmd string) { + verbose_trace(modfn(@MOD, @FN), toolcmd) + if exit_0_status('type $toolcmd') { + return + } + eprintln('Missing tool: $toolcmd') + eprintln('Please try again after you install it.') + exit(1) +} + +pub fn used_tools_must_exist(tools []string) { + for t in tools { + tool_must_exist(t) + } +} + +pub fn show_sizes_of_files(files []string) { + for f in files { + size := os.file_size(f) + println('$size $f') // println('${size:10d} $f') + } +} diff --git a/v_windows/v/cmd/tools/modules/testing/common.v b/v_windows/v/cmd/tools/modules/testing/common.v new file mode 100644 index 0000000..7f9de79 --- /dev/null +++ b/v_windows/v/cmd/tools/modules/testing/common.v @@ -0,0 +1,488 @@ +module testing + +import os +import time +import term +import benchmark +import sync.pool +import v.pref +import v.util.vtest + +const github_job = os.getenv('GITHUB_JOB') + +const show_start = os.getenv('VTEST_SHOW_START') == '1' + +const hide_skips = os.getenv('VTEST_HIDE_SKIP') == '1' + +const hide_oks = os.getenv('VTEST_HIDE_OK') == '1' + +pub struct TestSession { +pub mut: + files []string + skip_files []string + vexe string + vroot string + vtmp_dir string + vargs string + failed bool + benchmark benchmark.Benchmark + rm_binaries bool = true + silent_mode bool + progress_mode bool + root_relative bool // used by CI runs, so that the output is stable everywhere + nmessages chan LogMessage // many publishers, single consumer/printer + nmessage_idx int // currently printed message index + nprint_ended chan int // read to block till printing ends, 1:1 + failed_cmds shared []string +} + +enum MessageKind { + ok + fail + skip + info + sentinel +} + +struct LogMessage { + message string + kind MessageKind +} + +pub fn (mut ts TestSession) add_failed_cmd(cmd string) { + lock ts.failed_cmds { + ts.failed_cmds << cmd + } +} + +pub fn (mut ts TestSession) show_list_of_failed_tests() { + for i, cmd in ts.failed_cmds { + eprintln(term.failed('Failed command ${i + 1}:') + ' $cmd') + } +} + +pub fn (mut ts TestSession) append_message(kind MessageKind, msg string) { + ts.nmessages <- LogMessage{ + message: msg + kind: kind + } +} + +pub fn (mut ts TestSession) print_messages() { + empty := term.header(' ', ' ') + mut print_msg_time := time.new_stopwatch() + for { + // get a message from the channel of messages to be printed: + mut rmessage := <-ts.nmessages + if rmessage.kind == .sentinel { + // a sentinel for stopping the printing thread + if !ts.silent_mode && ts.progress_mode { + eprintln('') + } + ts.nprint_ended <- 0 + return + } + if rmessage.kind != .info { + ts.nmessage_idx++ + } + msg := rmessage.message.replace_each([ + 'TMP1', + '${ts.nmessage_idx:1d}', + 'TMP2', + '${ts.nmessage_idx:2d}', + 'TMP3', + '${ts.nmessage_idx:3d}', + 'TMP4', + '${ts.nmessage_idx:4d}', + ]) + is_ok := rmessage.kind == .ok + // + time_passed := print_msg_time.elapsed().seconds() + if time_passed > 10 && ts.silent_mode && is_ok { + // Even if OK tests are suppressed, + // show *at least* 1 result every 10 seconds, + // otherwise the CI can seem stuck ... + eprintln(msg) + print_msg_time.restart() + continue + } + if ts.progress_mode { + // progress mode, the last line is rewritten many times: + if is_ok && !ts.silent_mode { + print('\r$empty\r$msg') + } else { + // the last \n is needed, so SKIP/FAIL messages + // will not get overwritten by the OK ones + eprint('\r$empty\r$msg\n') + } + continue + } + if !ts.silent_mode || !is_ok { + // normal expanded mode, or failures in -silent mode + eprintln(msg) + continue + } + } +} + +pub fn new_test_session(_vargs string, will_compile bool) TestSession { + mut skip_files := []string{} + if will_compile { + $if msvc { + skip_files << 'vlib/v/tests/const_comptime_eval_before_vinit_test.v' // _constructor used + } + $if solaris { + skip_files << 'examples/gg/gg2.v' + skip_files << 'examples/pico/pico.v' + skip_files << 'examples/sokol/fonts.v' + skip_files << 'examples/sokol/drawing.v' + } + $if macos { + skip_files << 'examples/database/mysql.v' + skip_files << 'examples/database/orm.v' + skip_files << 'examples/database/psql/customer.v' + } + $if windows { + skip_files << 'examples/database/mysql.v' + skip_files << 'examples/database/orm.v' + skip_files << 'examples/websocket/ping.v' // requires OpenSSL + skip_files << 'examples/websocket/client-server/client.v' // requires OpenSSL + skip_files << 'examples/websocket/client-server/server.v' // requires OpenSSL + $if tinyc { + skip_files << 'examples/database/orm.v' // try fix it + } + } + if testing.github_job != 'sokol-shaders-can-be-compiled' { + // These examples need .h files that are produced from the supplied .glsl files, + // using by the shader compiler tools in https://github.com/floooh/sokol-tools-bin/archive/pre-feb2021-api-changes.tar.gz + skip_files << 'examples/sokol/02_cubes_glsl/cube_glsl.v' + skip_files << 'examples/sokol/03_march_tracing_glsl/rt_glsl.v' + skip_files << 'examples/sokol/04_multi_shader_glsl/rt_glsl.v' + skip_files << 'examples/sokol/05_instancing_glsl/rt_glsl.v' + // Skip obj_viewer code in the CI + skip_files << 'examples/sokol/06_obj_viewer/show_obj.v' + } + if testing.github_job != 'ubuntu-tcc' { + skip_files << 'examples/c_interop_wkhtmltopdf.v' // needs installation of wkhtmltopdf from https://github.com/wkhtmltopdf/packaging/releases + // the ttf_test.v is not interactive, but needs X11 headers to be installed, which is done only on ubuntu-tcc for now + skip_files << 'vlib/x/ttf/ttf_test.v' + skip_files << 'vlib/vweb/vweb_app_test.v' // imports the `sqlite` module, which in turn includes sqlite3.h + } + if testing.github_job != 'audio-examples' { + skip_files << 'examples/sokol/sounds/melody.v' + skip_files << 'examples/sokol/sounds/wav_player.v' + skip_files << 'examples/sokol/sounds/simple_sin_tones.v' + } + } + vargs := _vargs.replace('-progress', '').replace('-progress', '') + vexe := pref.vexe_path() + vroot := os.dir(vexe) + new_vtmp_dir := setup_new_vtmp_folder() + if term.can_show_color_on_stderr() { + os.setenv('VCOLORS', 'always', true) + } + return TestSession{ + vexe: vexe + vroot: vroot + skip_files: skip_files + vargs: vargs + vtmp_dir: new_vtmp_dir + silent_mode: _vargs.contains('-silent') + progress_mode: _vargs.contains('-progress') + } +} + +pub fn (mut ts TestSession) init() { + ts.files.sort() + ts.benchmark = benchmark.new_benchmark_no_cstep() +} + +pub fn (mut ts TestSession) add(file string) { + ts.files << file +} + +pub fn (mut ts TestSession) test() { + // Ensure that .tmp.c files generated from compiling _test.v files, + // are easy to delete at the end, *without* affecting the existing ones. + current_wd := os.getwd() + if current_wd == os.wd_at_startup && current_wd == ts.vroot { + ts.root_relative = true + } + // + ts.init() + mut remaining_files := []string{} + for dot_relative_file in ts.files { + relative_file := dot_relative_file.replace('./', '') + file := os.real_path(relative_file) + $if windows { + if file.contains('sqlite') || file.contains('httpbin') { + continue + } + } + $if !macos { + if file.contains('customer') { + continue + } + } + $if msvc { + if file.contains('asm') { + continue + } + } + remaining_files << dot_relative_file + } + remaining_files = vtest.filter_vtest_only(remaining_files, fix_slashes: false) + ts.files = remaining_files + ts.benchmark.set_total_expected_steps(remaining_files.len) + mut pool_of_test_runners := pool.new_pool_processor(callback: worker_trunner) + // for handling messages across threads + ts.nmessages = chan LogMessage{cap: 10000} + ts.nprint_ended = chan int{cap: 0} + ts.nmessage_idx = 0 + go ts.print_messages() + pool_of_test_runners.set_shared_context(ts) + pool_of_test_runners.work_on_pointers(unsafe { remaining_files.pointers() }) + ts.benchmark.stop() + ts.append_message(.sentinel, '') // send the sentinel + _ := <-ts.nprint_ended // wait for the stop of the printing thread + eprintln(term.h_divider('-')) + // cleanup generated .tmp.c files after successfull tests: + if ts.benchmark.nfail == 0 { + if ts.rm_binaries { + os.rmdir_all(ts.vtmp_dir) or { panic(err) } + } + } + ts.show_list_of_failed_tests() +} + +fn worker_trunner(mut p pool.PoolProcessor, idx int, thread_id int) voidptr { + mut ts := &TestSession(p.get_shared_context()) + tmpd := ts.vtmp_dir + show_stats := '-stats' in ts.vargs.split(' ') + // tls_bench is used to format the step messages/timings + mut tls_bench := &benchmark.Benchmark(p.get_thread_context(idx)) + if isnil(tls_bench) { + tls_bench = benchmark.new_benchmark_pointer() + tls_bench.set_total_expected_steps(ts.benchmark.nexpected_steps) + p.set_thread_context(idx, tls_bench) + } + tls_bench.no_cstep = true + dot_relative_file := p.get_item<string>(idx) + mut relative_file := dot_relative_file.replace('./', '') + mut cmd_options := [ts.vargs] + if relative_file.contains('global') && !ts.vargs.contains('fmt') { + cmd_options << ' -enable-globals' + } + if ts.root_relative { + relative_file = relative_file.replace(ts.vroot + os.path_separator, '') + } + file := os.real_path(relative_file) + normalised_relative_file := relative_file.replace('\\', '/') + // Ensure that the generated binaries will be stored in the temporary folder. + // Remove them after a test passes/fails. + fname := os.file_name(file) + generated_binary_fname := if os.user_os() == 'windows' { + fname.replace('.v', '.exe') + } else { + fname.replace('.v', '') + } + generated_binary_fpath := os.join_path(tmpd, generated_binary_fname) + if os.exists(generated_binary_fpath) { + if ts.rm_binaries { + os.rm(generated_binary_fpath) or { panic(err) } + } + } + if !ts.vargs.contains('fmt') { + cmd_options << ' -o "$generated_binary_fpath"' + } + cmd := '"$ts.vexe" ' + cmd_options.join(' ') + ' "$file"' + ts.benchmark.step() + tls_bench.step() + if relative_file.replace('\\', '/') in ts.skip_files { + ts.benchmark.skip() + tls_bench.skip() + if !testing.hide_skips { + ts.append_message(.skip, tls_bench.step_message_skip(normalised_relative_file)) + } + return pool.no_result + } + if show_stats { + ts.append_message(.ok, term.h_divider('-')) + status := os.system(cmd) + if status == 0 { + ts.benchmark.ok() + tls_bench.ok() + } else { + ts.failed = true + ts.benchmark.fail() + tls_bench.fail() + ts.add_failed_cmd(cmd) + return pool.no_result + } + } else { + if testing.show_start { + ts.append_message(.info, ' starting $relative_file ...') + } + r := os.execute(cmd) + if r.exit_code < 0 { + ts.failed = true + ts.benchmark.fail() + tls_bench.fail() + ts.append_message(.fail, tls_bench.step_message_fail(normalised_relative_file)) + ts.add_failed_cmd(cmd) + return pool.no_result + } + if r.exit_code != 0 { + ts.failed = true + ts.benchmark.fail() + tls_bench.fail() + ending_newline := if r.output.ends_with('\n') { '\n' } else { '' } + ts.append_message(.fail, tls_bench.step_message_fail('$normalised_relative_file\n$r.output.trim_space()$ending_newline')) + ts.add_failed_cmd(cmd) + } else { + ts.benchmark.ok() + tls_bench.ok() + if !testing.hide_oks { + ts.append_message(.ok, tls_bench.step_message_ok(normalised_relative_file)) + } + } + } + if os.exists(generated_binary_fpath) { + if ts.rm_binaries { + os.rm(generated_binary_fpath) or { panic(err) } + } + } + return pool.no_result +} + +pub fn vlib_should_be_present(parent_dir string) { + vlib_dir := os.join_path(parent_dir, 'vlib') + if !os.is_dir(vlib_dir) { + eprintln('$vlib_dir is missing, it must be next to the V executable') + exit(1) + } +} + +pub fn v_build_failing(zargs string, folder string) bool { + return v_build_failing_skipped(zargs, folder, []) +} + +pub fn prepare_test_session(zargs string, folder string, oskipped []string, main_label string) TestSession { + vexe := pref.vexe_path() + parent_dir := os.dir(vexe) + vlib_should_be_present(parent_dir) + vargs := zargs.replace(vexe, '') + eheader(main_label) + if vargs.len > 0 { + eprintln('v compiler args: "$vargs"') + } + mut session := new_test_session(vargs, true) + files := os.walk_ext(os.join_path(parent_dir, folder), '.v') + mut mains := []string{} + mut skipped := oskipped.clone() + next_file: for f in files { + fnormalised := f.replace('\\', '/') + // NB: a `testdata` folder, is the preferred name of a folder, containing V code, + // that you *do not want* the test framework to find incidentally for various reasons, + // for example module import tests, or subtests, that are compiled/run by other parent tests + // in specific configurations, etc. + if fnormalised.contains('testdata/') || fnormalised.contains('modules/') + || f.contains('preludes/') { + continue + } + $if windows { + // skip pico and process/command examples on windows + if fnormalised.ends_with('examples/pico/pico.v') + || fnormalised.ends_with('examples/process/command.v') { + continue + } + } + c := os.read_file(f) or { panic(err) } + maxc := if c.len > 300 { 300 } else { c.len } + start := c[0..maxc] + if start.contains('module ') && !start.contains('module main') { + skipped_f := f.replace(os.join_path(parent_dir, ''), '') + skipped << skipped_f + } + for skip_prefix in oskipped { + if f.starts_with(skip_prefix) { + continue next_file + } + } + mains << f + } + session.files << mains + session.skip_files << skipped + return session +} + +pub fn v_build_failing_skipped(zargs string, folder string, oskipped []string) bool { + main_label := 'Building $folder ...' + finish_label := 'building $folder' + mut session := prepare_test_session(zargs, folder, oskipped, main_label) + session.test() + eprintln(session.benchmark.total_message(finish_label)) + return session.failed +} + +pub fn build_v_cmd_failed(cmd string) bool { + res := os.execute(cmd) + if res.exit_code < 0 { + return true + } + if res.exit_code != 0 { + eprintln('') + eprintln(res.output) + return true + } + return false +} + +pub fn building_any_v_binaries_failed() bool { + eheader('Building V binaries...') + eprintln('VFLAGS is: "' + os.getenv('VFLAGS') + '"') + vexe := pref.vexe_path() + parent_dir := os.dir(vexe) + vlib_should_be_present(parent_dir) + os.chdir(parent_dir) or { panic(err) } + mut failed := false + v_build_commands := ['$vexe -o v_g -g cmd/v', '$vexe -o v_prod_g -prod -g cmd/v', + '$vexe -o v_cg -cg cmd/v', '$vexe -o v_prod_cg -prod -cg cmd/v', + '$vexe -o v_prod -prod cmd/v', + ] + mut bmark := benchmark.new_benchmark() + for cmd in v_build_commands { + bmark.step() + if build_v_cmd_failed(cmd) { + bmark.fail() + failed = true + eprintln(bmark.step_message_fail('command: $cmd . See details above ^^^^^^^')) + eprintln('') + continue + } + bmark.ok() + if !testing.hide_oks { + eprintln(bmark.step_message_ok('command: $cmd')) + } + } + bmark.stop() + eprintln(term.h_divider('-')) + eprintln(bmark.total_message('building v binaries')) + return failed +} + +pub fn eheader(msg string) { + eprintln(term.header_left(msg, '-')) +} + +pub fn header(msg string) { + println(term.header_left(msg, '-')) +} + +pub fn setup_new_vtmp_folder() string { + now := time.sys_mono_now() + new_vtmp_dir := os.join_path(os.temp_dir(), 'v', 'test_session_$now') + os.mkdir_all(new_vtmp_dir) or { panic(err) } + os.setenv('VTMP', new_vtmp_dir, true) + return new_vtmp_dir +} diff --git a/v_windows/v/cmd/tools/modules/vgit/vgit.v b/v_windows/v/cmd/tools/modules/vgit/vgit.v new file mode 100644 index 0000000..efa2e8a --- /dev/null +++ b/v_windows/v/cmd/tools/modules/vgit/vgit.v @@ -0,0 +1,197 @@ +module vgit + +import os +import flag +import scripting + +pub fn check_v_commit_timestamp_before_self_rebuilding(v_timestamp int) { + if v_timestamp >= 1561805697 { + return + } + eprintln('##################################################################') + eprintln('# WARNING: v self rebuilding, before 5b7a1e8 (2019-06-29 12:21) #') + eprintln('# required the v executable to be built *inside* #') + eprintln('# the toplevel compiler/ folder. #') + eprintln('# #') + eprintln('# That is not supported by this tool. #') + eprintln('# You will have to build it manually there. #') + eprintln('##################################################################') +} + +pub fn validate_commit_exists(commit string) { + if commit.len == 0 { + return + } + cmd := "git cat-file -t '$commit' " + if !scripting.exit_0_status(cmd) { + eprintln('Commit: "$commit" does not exist in the current repository.') + exit(3) + } +} + +pub fn line_to_timestamp_and_commit(line string) (int, string) { + parts := line.split(' ') + return parts[0].int(), parts[1] +} + +pub fn normalized_workpath_for_commit(workdir string, commit string) string { + nc := 'v_at_' + commit.replace('^', '_').replace('-', '_').replace('/', '_') + return os.real_path(workdir + os.path_separator + nc) +} + +fn get_current_folder_commit_hash() string { + vline := scripting.run('git rev-list -n1 --timestamp HEAD') + _, v_commithash := line_to_timestamp_and_commit(vline) + return v_commithash +} + +pub fn prepare_vc_source(vcdir string, cdir string, commit string) (string, string) { + scripting.chdir(cdir) + // Building a historic v with the latest vc is not always possible ... + // It is more likely, that the vc *at the time of the v commit*, + // or slightly before that time will be able to build the historic v: + vline := scripting.run('git rev-list -n1 --timestamp "$commit" ') + v_timestamp, v_commithash := line_to_timestamp_and_commit(vline) + scripting.verbose_trace(@FN, 'v_timestamp: $v_timestamp | v_commithash: $v_commithash') + check_v_commit_timestamp_before_self_rebuilding(v_timestamp) + scripting.chdir(vcdir) + scripting.run('git checkout --quiet master') + // + mut vccommit := '' + vcbefore_subject_match := scripting.run('git rev-list HEAD -n1 --timestamp --grep=${v_commithash[0..7]} ') + scripting.verbose_trace(@FN, 'vcbefore_subject_match: $vcbefore_subject_match') + if vcbefore_subject_match.len > 3 { + _, vccommit = line_to_timestamp_and_commit(vcbefore_subject_match) + } else { + scripting.verbose_trace(@FN, 'the v commit did not match anything in the vc log; try --timestamp instead.') + vcbefore := scripting.run('git rev-list HEAD -n1 --timestamp --before=$v_timestamp ') + _, vccommit = line_to_timestamp_and_commit(vcbefore) + } + scripting.verbose_trace(@FN, 'vccommit: $vccommit') + scripting.run('git checkout --quiet "$vccommit" ') + scripting.run('wc *.c') + scripting.chdir(cdir) + return v_commithash, vccommit +} + +pub fn clone_or_pull(remote_git_url string, local_worktree_path string) { + // NB: after clone_or_pull, the current repo branch is === HEAD === master + if os.is_dir(local_worktree_path) && os.is_dir(os.join_path(local_worktree_path, '.git')) { + // Already existing ... Just pulling in this case is faster usually. + scripting.run('git -C "$local_worktree_path" checkout --quiet master') + scripting.run('git -C "$local_worktree_path" pull --quiet ') + } else { + // Clone a fresh + scripting.run('git clone --quiet "$remote_git_url" "$local_worktree_path" ') + } +} + +pub struct VGitContext { +pub: + cc string = 'cc' // what compiler to use + workdir string = '/tmp' // the base working folder + commit_v string = 'master' // the commit-ish that needs to be prepared + path_v string // where is the local working copy v repo + path_vc string // where is the local working copy vc repo + v_repo_url string // the remote v repo URL + vc_repo_url string // the remote vc repo URL +pub mut: + // these will be filled by vgitcontext.compile_oldv_if_needed() + commit_v__hash string // the git commit of the v repo that should be prepared + commit_vc_hash string // the git commit of the vc repo, corresponding to commit_v__hash + vexename string // v or v.exe + vexepath string // the full absolute path to the prepared v/v.exe + vvlocation string // v.v or compiler/ or cmd/v, depending on v version + make_fresh_tcc bool // whether to do 'make fresh_tcc' before compiling an old V. +} + +pub fn (mut vgit_context VGitContext) compile_oldv_if_needed() { + vgit_context.vexename = if os.user_os() == 'windows' { 'v.exe' } else { 'v' } + vgit_context.vexepath = os.real_path(os.join_path(vgit_context.path_v, vgit_context.vexename)) + mut command_for_building_v_from_c_source := '' + mut command_for_selfbuilding := '' + if 'windows' == os.user_os() { + command_for_building_v_from_c_source = '$vgit_context.cc -std=c99 -municode -w -o cv.exe "$vgit_context.path_vc/v_win.c" ' + command_for_selfbuilding = './cv.exe -o $vgit_context.vexename {SOURCE}' + } else { + command_for_building_v_from_c_source = '$vgit_context.cc -std=gnu11 -w -o cv "$vgit_context.path_vc/v.c" -lm -lpthread' + command_for_selfbuilding = './cv -o $vgit_context.vexename {SOURCE}' + } + scripting.chdir(vgit_context.workdir) + clone_or_pull(vgit_context.v_repo_url, vgit_context.path_v) + clone_or_pull(vgit_context.vc_repo_url, vgit_context.path_vc) + scripting.chdir(vgit_context.path_v) + scripting.run('git checkout --quiet $vgit_context.commit_v') + if os.is_dir(vgit_context.path_v) && os.exists(vgit_context.vexepath) { + // already compiled, so no need to compile v again + vgit_context.commit_v__hash = get_current_folder_commit_hash() + return + } + v_commithash, vccommit_before := prepare_vc_source(vgit_context.path_vc, vgit_context.path_v, + 'HEAD') + vgit_context.commit_v__hash = v_commithash + vgit_context.commit_vc_hash = vccommit_before + if os.exists('cmd/v') { + vgit_context.vvlocation = 'cmd/v' + } else { + vgit_context.vvlocation = if os.exists('v.v') { 'v.v' } else { 'compiler' } + } + if os.is_dir(vgit_context.path_v) && os.exists(vgit_context.vexepath) { + // already compiled, so no need to compile v again + return + } + // Recompilation is needed. Just to be sure, clean up everything first. + scripting.run('git clean -xf') + if vgit_context.make_fresh_tcc { + scripting.run('make fresh_tcc') + } + scripting.run(command_for_building_v_from_c_source) + build_cmd := command_for_selfbuilding.replace('{SOURCE}', vgit_context.vvlocation) + scripting.run(build_cmd) + // At this point, there exists a file vgit_context.vexepath + // which should be a valid working V executable. +} + +pub struct VGitOptions { +pub mut: + workdir string // the working folder (typically /tmp), where the tool will write + v_repo_url string // the url of the V repository. It can be a local folder path, if you want to eliminate network operations... + vc_repo_url string // the url of the vc repository. It can be a local folder path, if you want to eliminate network operations... + show_help bool // whether to show the usage screen + verbose bool // should the tool be much more verbose +} + +pub fn add_common_tool_options(mut context VGitOptions, mut fp flag.FlagParser) []string { + tdir := os.temp_dir() + context.workdir = os.real_path(fp.string('workdir', `w`, context.workdir, 'A writable base folder. Default: $tdir')) + context.v_repo_url = fp.string('vrepo', 0, context.v_repo_url, 'The url of the V repository. You can clone it locally too. See also --vcrepo below.') + context.vc_repo_url = fp.string('vcrepo', 0, context.vc_repo_url, 'The url of the vc repository. You can clone it +${flag.space}beforehand, and then just give the local folder +${flag.space}path here. That will eliminate the network ops +${flag.space}done by this tool, which is useful, if you want +${flag.space}to script it/run it in a restrictive vps/docker. +') + context.show_help = fp.bool('help', `h`, false, 'Show this help screen.') + context.verbose = fp.bool('verbose', `v`, false, 'Be more verbose.') + if context.show_help { + println(fp.usage()) + exit(0) + } + if context.verbose { + scripting.set_verbose(true) + } + if os.is_dir(context.v_repo_url) { + context.v_repo_url = os.real_path(context.v_repo_url) + } + if os.is_dir(context.vc_repo_url) { + context.vc_repo_url = os.real_path(context.vc_repo_url) + } + commits := fp.finalize() or { + eprintln('Error: $err') + exit(1) + } + for commit in commits { + validate_commit_exists(commit) + } + return commits +} diff --git a/v_windows/v/cmd/tools/modules/vhelp/vhelp.v b/v_windows/v/cmd/tools/modules/vhelp/vhelp.v new file mode 100644 index 0000000..347ba75 --- /dev/null +++ b/v_windows/v/cmd/tools/modules/vhelp/vhelp.v @@ -0,0 +1,14 @@ +module vhelp + +import os + +pub fn show_topic(topic string) { + vexe := os.real_path(os.getenv('VEXE')) + vroot := os.dir(vexe) + target_topic := os.join_path(vroot, 'cmd', 'v', 'help', '${topic}.txt') + content := os.read_file(target_topic) or { + eprintln('Unknown topic: $topic') + exit(1) + } + println(content) +} |