-
Notifications
You must be signed in to change notification settings - Fork 16
/
build.vsh
140 lines (121 loc) · 3.78 KB
/
build.vsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env -S v
// This script is used to build the v-analyzer binary.
// Usage:
// v build.vsh [debug|dev|release]
// By default, just `v build.vsh` will use debug mode.
import os
import cli
import term
import time
import src.metadata
const bin_path = './bin/v-analyzer' + $if windows { '.exe' } $else { '' }
const build_commit = os.execute('git rev-parse --short HEAD').output.trim_space()
const build_datetime = time.now().format_ss()
enum ReleaseMode {
release
debug
dev
}
fn errorln(msg string) {
eprintln('${term.red('[ERROR]')} ${msg}')
}
fn (m ReleaseMode) compile_cmd() string {
base_build_cmd := '"${@VEXE}" "${@VMODROOT}" -o "${bin_path}" -no-parallel'
cc := if v := os.getenv_opt('CC') {
'-cc ${v}'
} else {
$if windows {
// TCC cannot build tree-sitter on Windows.
'-cc gcc'
} $else {
// Let `-prod` toggle the appropriate production compiler.
''
}
}
cflags := $if cross_compile_macos_arm64 ? {
'-cflags "-target arm64-apple-darwin"'
} $else $if cross_compile_macos_x86_64 ? {
'-cflags "-target x86_64-apple-darwin"'
} $else $if linux {
if m == .release { '-cflags -static' } else { '' }
} $else {
''
}
libbacktrace := $if windows { '' } $else { '-d use_libbacktrace' }
build_cmd := '${base_build_cmd} ${cc} ${cflags}'
return match m {
.release { '${build_cmd} -prod' }
.debug { '${build_cmd} -g ${libbacktrace}' }
.dev { '${build_cmd} -d show_ast_on_hover -g ${libbacktrace}' }
}
}
fn prepare_output_dir() {
if os.exists('./bin') {
return
}
os.mkdir('./bin') or { errorln('Failed to create output directory: ${err}') }
}
fn build(mode ReleaseMode, explicit_debug bool) {
println('Building v-analyzer at commit: ${build_commit}, build time: ${build_datetime} ...')
prepare_output_dir()
println('${term.green('✓')} Prepared output directory')
cmd := mode.compile_cmd()
println('Building v-analyzer in ${term.bold(mode.str())} mode, using: ${cmd}')
if mode == .release {
println('This may take a while...')
}
if !explicit_debug && mode == .debug {
println('To build in ${term.bold('release')} mode, run ${term.bold('v build.vsh release')}')
println('Release mode is recommended for production use. At runtime, it is about 30-40% faster than debug mode.')
}
os.execute_opt(cmd) or {
errorln('Failed to build v-analyzer')
eprintln(err)
exit(1)
}
println('${term.green('✓')} Successfully built v-analyzer!')
println('Binary is located at ${term.bold(abs_path(bin_path))}')
}
// main program:
os.setenv('BUILD_DATETIME', build_datetime, true)
os.setenv('BUILD_COMMIT', build_commit, true)
mut cmd := cli.Command{
name: 'v-analyzer-builder'
version: metadata.manifest.version
description: 'Builds the v-analyzer binary.'
posix_mode: true
execute: fn (_ cli.Command) ! {
build(.debug, false)
}
}
// debug builds the v-analyzer binary in debug mode.
// This is the default mode.
// Thanks to -d use_libbacktrace, the binary will print beautiful stack traces,
// which is very useful for debugging.
cmd.add_command(cli.Command{
name: 'debug'
description: 'Builds the v-analyzer binary in debug mode.'
execute: fn (_ cli.Command) ! {
build(.debug, true)
}
})
// dev builds the v-analyzer binary in development mode.
// In this mode, additional development features are enabled.
cmd.add_command(cli.Command{
name: 'dev'
description: 'Builds the v-analyzer binary in development mode.'
execute: fn (_ cli.Command) ! {
build(.dev, false)
}
})
// release builds the v-analyzer binary in release mode.
// This is the recommended mode for production use.
// It is about 30-40% faster than debug mode.
cmd.add_command(cli.Command{
name: 'release'
description: 'Builds the v-analyzer binary in release mode.'
execute: fn (_ cli.Command) ! {
build(.release, false)
}
})
cmd.parse(os.args)