11#!/usr/ bin/ env - S v
22
33// This script is used to build the v-analyzer binary.
4- // Usage:
5- // v build.vsh [debug|dev|release]
6- // By default, just `v build.vsh` will use debug mode.
4+ // Usage: `v build.vsh [debug|dev|release]`
5+ // By default, doing just `v build.vsh` will use debug mode.
76import os
87import cli
98import term
109import time
1110import src.metadata
1211
12+ const vexe = @VEXE
1313const bin_path = './bin/ v- analyzer' + $if windows { '.exe' } $else { '' }
1414const build_commit = os.execute('git rev- parse -- short HEAD').output.trim_space()
15- const build_datetime = time.now().format_ss()
15+ const build_time = time.now()
16+ const build_datetime = build_time.format_ss()
17+ const gcheck = term.green('✓')
1618
1719enum ReleaseMode {
1820 release
1921 debug
2022 dev
2123}
2224
23- fn errorln (msg string) {
25+ fn eline (msg string) {
2426 eprintln('${term.red('[ERROR]')} ${msg}')
2527}
2628
2729fn (m ReleaseMode) compile_cmd() string {
28- base_build_cmd : = '"${ @VEXE}" "${ @VMODROOT}" - o "${ bin_path}" - no- parallel'
30+ base_build_cmd : = '${os.quoted_path( @VEXE)} ${os.quoted_path( @VMODROOT)} - o ${quoted_path( bin_path)} - no- parallel'
2931 cc : = if v : = os.getenv_opt('CC') {
3032 '- cc ${v}'
3133 } else {
@@ -47,7 +49,7 @@ fn (m ReleaseMode) compile_cmd() string {
4749 ''
4850 }
4951 libbacktrace : = $if windows { '' } $else { '- d use_libbacktrace' }
50- build_cmd : = '${base_build_cmd} ${cc} ${cflags}'
52+ build_cmd : = '${base_build_cmd} ${cc} ${cflags}'.trim_space()
5153 mut resulting_cmd : = match m {
5254 .release { '${build_cmd} - prod' }
5355 .debug { '${build_cmd} - g ${libbacktrace}' }
@@ -62,38 +64,51 @@ fn (m ReleaseMode) compile_cmd() string {
6264 return resulting_cmd
6365}
6466
65- fn prepare_output_dir() {
66- if os.exists('./bin') {
67- return
67+ fn prepare_output_dir() string {
68+ output_dir : = './bin'
69+ if os.exists(output_dir) {
70+ return output_dir
6871 }
69- os.mkdir('./bin') or { errorln('Failed to create output directory: ${err}') }
72+ os.mkdir(output_dir) or { eline('Failed to create output directory: ${err}') }
73+ return output_dir
7074}
7175
7276fn build(mode ReleaseMode, explicit_debug bool ) {
73- println('Building v- analyzer at commit: ${build_commit}, build time: ${build_datetime} ...')
77+ vexe_version : = os.execute('${os.quoted_path(vexe)} version').output.trim_space()
78+ println('${gcheck} Building with ${vexe_version} .')
79+ println('${gcheck} Building v- analyzer at commit: ${build_commit} .')
80+ println('${gcheck} Building start time: ${build_datetime} .')
7481
75- prepare_output_dir()
76- println('${term.green('✓') } Prepared output directory')
82+ odir : = prepare_output_dir()
83+ println('${gcheck } Prepared output directory `${odir}` . ')
7784
7885 cmd : = mode.compile_cmd()
79- println('Building v- analyzer in ${term.bold(mode.str())} mode, using: ${cmd}')
86+ println('Building v- analyzer in ${term.bold(mode.str())} mode, using: ')
87+ println(' ${cmd}')
8088 if mode == .release {
81- println('This may take a while .. .')
89+ println('This may take 1 - 2 minutes... Please wait .')
8290 }
8391
8492 if ! explicit_debug && mode == .debug {
85- println('To build in ${term.bold('release')} mode, run ${term.bold('v build.vsh release')}')
86- println('Release mode is recommended for production use. At runtime, it is about 30 - 40 % faster than debug mode.')
93+ println('')
94+ println('Note: to build in ${term.bold('release')} mode, run `${term.bold('v build.vsh release')}` .')
95+ println(' Release mode is recommended for production use.')
96+ println(' At runtime, it is about 30 - 40 % faster than debug mode.')
97+ println('')
8798 }
8899
89100 os.execute_opt(cmd) or {
90- errorln ('Failed to build v- analyzer')
101+ eline ('Failed to build v- analyzer')
91102 eprintln(err)
92103 exit(1 )
93104 }
94105
95- println('${term.green('✓')} Successfully built v- analyzer! ')
96- println('Binary is located at ${term.bold(abs_path(bin_path))}')
106+ final_path : = abs_path(bin_path)
107+ nbytes : = os.file_size(final_path)
108+ println('The binary size in bytes is: ${nbytes} .')
109+ println('The binary is located at ${term.bold(final_path)} .')
110+ elapsed_ms : = f64((time.now() - build_time).milliseconds())
111+ println('${gcheck} Successfully built v- analyzer, in ${elapsed_ms / 1000.0 : 5 .3f}s .')
97112}
98113
99114// main program:
0 commit comments