1 | module main |
2 | |
3 | import os |
4 | import os.cmdline |
5 | import v.util.recompilation |
6 | |
7 | const is_debug = os.args.contains('-debug') |
8 | |
9 | // support a renamed `v` executable too: |
10 | const vexe = os.getenv_opt('VEXE') or { @VEXE } |
11 | |
12 | const vroot = os.dir(vexe) |
13 | |
14 | fn main() { |
15 | // make testing `v up` easier, by providing a way to force `v self` to fail, |
16 | // to test the fallback logic: |
17 | if os.getenv('VSELF_SHOULD_FAIL') != '' { |
18 | eprintln('v self failed') |
19 | exit(1) |
20 | } |
21 | vexe_name := os.file_name(vexe) |
22 | short_v_name := vexe_name.all_before('.') |
23 | // |
24 | recompilation.must_be_enabled(vroot, 'Please install V from source, to use `${vexe_name} self` .') |
25 | os.chdir(vroot)! |
26 | os.setenv('VCOLORS', 'always', true) |
27 | mut args := os.args[1..].filter(it != 'self') |
28 | if args.len == 0 || ('-cc' !in args && '-prod' !in args) { |
29 | // compiling by default, i.e. `v self`: |
30 | uos := os.user_os() |
31 | uname := os.uname() |
32 | if uos == 'macos' && uname.machine == 'arm64' { |
33 | // Apple silicon, like m1, m2 etc |
34 | // Use tcc by default for V, since tinycc is much faster and also |
35 | // it already supports compiling many programs like V itself, that do not depend on inlined objective-C code |
36 | args << '-cc tcc' |
37 | } |
38 | } |
39 | jargs := args.join(' ') |
40 | obinary := cmdline.option(args, '-o', '') |
41 | sargs := if obinary != '' { jargs } else { '${jargs} -o v2' } |
42 | cmd := '${os.quoted_path(vexe)} ${sargs} ${os.quoted_path('cmd/v')}' |
43 | options := if args.len > 0 { '(${sargs})' } else { '' } |
44 | println('V self compiling ${options}...') |
45 | compile(vroot, cmd) |
46 | if obinary != '' { |
47 | // When -o was given, there is no need to backup/rename the original. |
48 | // The user just wants an independent copy of v, and so we are done. |
49 | return |
50 | } |
51 | backup_old_version_and_rename_newer(short_v_name) or { panic(err.msg()) } |
52 | println('V built successfully as executable "${vexe_name}".') |
53 | } |
54 | |
55 | fn compile(vroot string, cmd string) { |
56 | result := os.execute_or_exit(cmd) |
57 | if result.exit_code != 0 { |
58 | eprintln('cannot compile to `${vroot}`: \n${result.output}') |
59 | exit(1) |
60 | } |
61 | if result.output.len > 0 { |
62 | println(result.output.trim_space()) |
63 | } |
64 | } |
65 | |
66 | fn list_folder(short_v_name string, bmessage string, message string) { |
67 | if !is_debug { |
68 | return |
69 | } |
70 | if bmessage != '' { |
71 | println(bmessage) |
72 | } |
73 | if os.user_os() == 'windows' { |
74 | os.system('dir ${short_v_name}*.exe') |
75 | } else { |
76 | os.system('ls -lartd ${short_v_name}*') |
77 | } |
78 | println(message) |
79 | } |
80 | |
81 | fn backup_old_version_and_rename_newer(short_v_name string) !bool { |
82 | mut errors := []string{} |
83 | short_v_file := if os.user_os() == 'windows' { '${short_v_name}.exe' } else { '${short_v_name}' } |
84 | short_v2_file := if os.user_os() == 'windows' { 'v2.exe' } else { 'v2' } |
85 | short_bak_file := if os.user_os() == 'windows' { 'v_old.exe' } else { 'v_old' } |
86 | v_file := os.real_path(short_v_file) |
87 | v2_file := os.real_path(short_v2_file) |
88 | bak_file := os.real_path(short_bak_file) |
89 | |
90 | list_folder(short_v_name, 'before:', 'removing ${bak_file} ...') |
91 | if os.exists(bak_file) { |
92 | os.rm(bak_file) or { errors << 'failed removing ${bak_file}: ${err.msg()}' } |
93 | } |
94 | |
95 | list_folder(short_v_name, '', 'moving ${v_file} to ${bak_file} ...') |
96 | os.mv(v_file, bak_file) or { errors << err.msg() } |
97 | |
98 | list_folder(short_v_name, '', 'removing ${v_file} ...') |
99 | os.rm(v_file) or {} |
100 | |
101 | list_folder(short_v_name, '', 'moving ${v2_file} to ${v_file} ...') |
102 | os.mv_by_cp(v2_file, v_file) or { panic(err.msg()) } |
103 | |
104 | list_folder(short_v_name, 'after:', '') |
105 | |
106 | if errors.len > 0 { |
107 | eprintln('backup errors:\n >> ' + errors.join('\n >> ')) |
108 | } |
109 | return true |
110 | } |