2022-01-04 10:21:08 +01:00
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
2020-02-03 07:02:54 +01:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module pref
2021-02-05 08:05:13 +01:00
// import v.ast // TODO this results in a compiler bug
2020-05-16 22:45:38 +02:00
import os . cmdline
import os
2020-10-26 17:28:35 +01:00
import v . vcache
2020-11-20 09:25:59 +01:00
import rand
2020-05-16 22:45:38 +02:00
2020-02-03 07:02:54 +01:00
pub enum BuildMode {
// `v program.v'
// Build user code only, and add pre-compiled vlib (`cc program.o builtin.o os.o...`)
2020-07-04 15:19:54 +02:00
default_mode // `v -lib ~/v/os`
2020-02-03 07:02:54 +01:00
// build any module (generate os.o + os.vh)
build_module
}
2021-04-15 19:26:51 +02:00
pub enum AssertFailureMode {
default
aborts
backtraces
}
2021-03-20 14:16:36 +01:00
pub enum GarbageCollectionMode {
no_gc
2021-03-30 09:36:22 +02:00
boehm_full // full garbage collection mode
boehm_incr // incremental garbage colletion mode
2021-04-14 00:40:26 +02:00
boehm_full_opt // full garbage collection mode
boehm_incr_opt // incremental garbage colletion mode
2021-03-30 09:36:22 +02:00
boehm_leak // leak detection mode (makes `gc_check_leaks()` work)
2021-03-20 14:16:36 +01:00
}
2020-05-10 11:26:57 +02:00
pub enum OutputMode {
stdout
silent
}
2020-05-20 19:33:29 +02:00
pub enum ColorOutput {
auto
always
never
2020-05-22 02:22:56 +02:00
}
2020-05-20 19:33:29 +02:00
2020-03-06 18:53:29 +01:00
pub enum Backend {
2020-07-04 15:19:54 +02:00
c // The (default) C backend
2021-07-19 14:55:03 +02:00
js_node // The JavaScript NodeJS backend
js_browser // The JavaScript browser backend
js_freestanding // The JavaScript freestanding backend
2021-04-26 15:39:38 +02:00
native // The Native backend
2021-12-08 09:09:10 +01:00
interpret // Interpret the ast
2020-03-06 18:53:29 +01:00
}
2021-07-19 14:55:03 +02:00
pub fn ( b Backend ) is_js ( ) bool {
2021-12-08 09:09:10 +01:00
return b in [
. js_node ,
. js_browser ,
. js_freestanding ,
]
2021-07-19 14:55:03 +02:00
}
2020-06-18 22:01:00 +02:00
pub enum CompilerType {
2020-09-18 00:58:54 +02:00
gcc
2020-06-18 22:01:00 +02:00
tinyc
clang
mingw
msvc
2020-09-18 00:58:54 +02:00
cplusplus
2020-06-18 22:01:00 +02:00
}
2021-03-17 01:43:17 +01:00
pub enum Arch {
_auto
amd64 // aka x86_64
2021-04-26 18:01:42 +02:00
arm64 // 64-bit arm
arm32 // 32-bit arm
2021-03-17 01:43:17 +01:00
rv64 // 64-bit risc-v
rv32 // 32-bit risc-v
i386
2021-10-19 11:11:54 +02:00
js_node
js_browser
js_freestanding
2021-06-12 21:15:08 +02:00
_max
2021-03-17 01:43:17 +01:00
}
2020-05-16 22:45:38 +02:00
const (
2020-09-30 07:27:24 +02:00
list_of_flags_with_param = [ ' o ' , ' d ' , ' d e f i n e ' , ' b ' , ' b a c k e n d ' , ' c c ' , ' o s ' , ' t a r g e t - o s ' , ' c f ' ,
2021-03-17 01:43:17 +01:00
' c f l a g s ' , ' p a t h ' , ' a r c h ' ]
2020-05-16 22:45:38 +02:00
)
2022-05-02 00:59:17 +02:00
[ heap ; minify ]
2020-02-03 07:02:54 +01:00
pub struct Preferences {
pub mut :
2021-01-12 04:38:43 +01:00
os OS // the OS to compile for
backend Backend
build_mode BuildMode
2021-03-17 01:43:17 +01:00
arch Arch
2021-01-12 04:38:43 +01:00
output_mode OutputMode = . stdout
2020-07-04 15:19:54 +02:00
// verbosity VerboseLevel
2021-01-12 04:38:43 +01:00
is_verbose bool
2020-02-09 10:08:04 +01:00
// nofmt bool // disable vfmt
2022-05-03 08:17:53 +02:00
is_glibc bool // if GLIBC will be linked
is_musl bool // if MUSL will be linked
is_test bool // `v test string_test.v`
is_script bool // single file mode (`v program.v`), main function can be skipped
is_vsh bool // v script (`file.vsh`) file, the `os` module should be made global
is_livemain bool // main program that contains live/hot code
is_liveshared bool // a shared library, that will be used in a -live main program
is_shared bool // an ordinary shared library, -shared, no matter if it is live or not
is_o bool // building an .o file
is_prof bool // benchmark every function
is_prod bool // use "-O2"
is_repl bool
is_run bool
is_debug bool // turned on by -g or -cg, it tells v to pass -g to the C backend compiler.
is_vlines bool // turned on by -g (it slows down .tmp.c generation slightly).
is_stats bool // `v -stats file_test.v` will produce more detailed statistics for the tests that were run
is_fmt bool
is_vet bool
is_vweb bool // skip _ var warning in templates
is_ios_simulator bool
is_apk bool // build as Android .apk format
is_help bool // -h, -help or --help was passed
is_cstrict bool // turn on more C warnings; slightly slower
2022-01-26 18:52:08 +01:00
test_runner string // can be 'simple' (fastest, but much less detailed), 'tap', 'normal'
profile_file string // the profile results will be stored inside profile_file
profile_no_inline bool // when true, [inline] functions would not be profiled
2022-04-10 09:21:58 +02:00
profile_fns [ ] string // when set, profiling will be off by default, but inside these functions (and what they call) it will be on.
2022-01-26 18:52:08 +01:00
translated bool // `v translate doom.v` are we running V code translated from C? allow globals, ++ expressions, etc
obfuscate bool // `v -obf program.v`, renames functions to "f_XXX"
2022-03-06 18:01:22 +01:00
// Note: passing -cg instead of -g will set is_vlines to false and is_debug to true, thus making v generate cleaner C files,
2020-02-03 07:02:54 +01:00
// which are sometimes easier to debug / inspect manually than the .tmp.c files by plain -g (when/if v line number generation breaks).
2021-07-28 15:41:32 +02:00
sanitize bool // use Clang's new "-fsanitize" option
sourcemap bool // JS Backend: -sourcemap will create a source map - default false
sourcemap_inline bool = true // JS Backend: -sourcemap-inline will embed the source map in the generated JaaScript file - currently default true only implemented
sourcemap_src_included bool // JS Backend: -sourcemap-src-included includes V source code in source map - default false
show_cc bool // -showcc, print cc command
show_c_output bool // -show-c-output, print all cc output even if the code was compiled correctly
show_callgraph bool // -show-callgraph, print the program callgraph, in a Graphviz DOT format to stdout
show_depgraph bool // -show-depgraph, print the program module dependency graph, in a Graphviz DOT format to stdout
dump_c_flags string // `-dump-c-flags file.txt` - let V store all C flags, passed to the backend C compiler in `file.txt`, one C flag/value per line.
2022-05-23 18:48:27 +02:00
dump_modules string // `-dump-modules modules.txt` - let V store all V modules, that were used by the compiled program in `modules.txt`, one module per line.
dump_files string // `-dump-files files.txt` - let V store all V or .template file paths, that were used by the compiled program in `files.txt`, one path per line.
2021-07-28 15:41:32 +02:00
use_cache bool // when set, use cached modules to speed up subsequent compilations, at the cost of slower initial ones (while the modules are cached)
retry_compilation bool = true // retry the compilation with another C compiler, if tcc fails.
2020-03-06 18:53:29 +01:00
// TODO Convert this into a []string
2021-01-12 04:38:43 +01:00
cflags string // Additional options which will be passed to the C compiler.
2020-02-03 07:02:54 +01:00
// For example, passing -cflags -Os will cause the C compiler to optimize the generated binaries for size.
// You could pass several -cflags XXX arguments. They will be merged with each other.
// You can also quote several options at the same time: -cflags '-Os -fno-inline-small-functions'.
2021-01-12 04:38:43 +01:00
m64 bool // true = generate 64-bit code, defaults to x64
ccompiler string // the name of the C compiler used
ccompiler_type CompilerType // the type of the C compiler used
third_party_option string
building_v bool
autofree bool // `v -manualfree` => false, `v -autofree` => true; false by default for now.
2021-01-08 13:56:55 +01:00
// Disabling `free()` insertion results in better performance in some applications (e.g. compilers)
2021-07-28 15:41:32 +02:00
compress bool // when set, use `upx` to compress the generated executable
2020-02-09 10:08:04 +01:00
// generating_vh bool
2022-05-03 08:17:53 +02:00
no_builtin bool // Skip adding the `builtin` module implicitly. The generated C code may not compile.
enable_globals bool // allow __global for low level code
2021-10-23 11:53:53 +02:00
is_bare bool // set by -freestanding
bare_builtin_dir string // Set by -bare-builtin-dir xyz/ . The xyz/ module should contain implementations of malloc, memset, etc, that are used by the rest of V's `builtin` module. That option is only useful with -freestanding (i.e. when is_bare is true).
2021-04-09 22:24:25 +02:00
no_preludes bool // Prevents V from generating preludes in resulting .c files
custom_prelude string // Contents of custom V prelude that will be prepended before code in resulting .c files
2022-05-28 20:16:48 +02:00
cmain string // The name of the generated C main function. Useful with framework like code, that uses macros to re-define `main`, like SDL2 does. When set, V will always generate `int THE_NAME(int ___argc, char** ___argv){`, *no matter* the platform.
2021-04-09 22:24:25 +02:00
lookup_path [ ] string
2021-10-23 11:53:53 +02:00
output_cross_c bool // true, when the user passed `-os cross`
2021-12-15 14:47:34 +01:00
output_es5 bool
2021-04-09 22:24:25 +02:00
prealloc bool
vroot string
out_name_c string // full os.real_path to the generated .tmp.c file; set by builder.
out_name string
path string // Path to file/folder to compile
2022-05-03 08:17:53 +02:00
//
2021-12-20 16:22:02 +01:00
run_only [ ] string // VTEST_ONLY_FN and -run-only accept comma separated glob patterns.
// Only test_ functions that match these patterns will be run. -run-only is valid only for _test.v files.
2022-05-03 08:17:53 +02:00
//
// -d vfmt and -d another=0 for `$if vfmt { will execute }` and `$if another ? { will NOT get here }`
compile_defines [ ] string // just ['vfmt']
compile_defines_all [ ] string // contains both: ['vfmt','another']
//
run_args [ ] string // `v run x.v 1 2 3` => `1 2 3`
printfn_list [ ] string // a list of generated function names, whose source should be shown, for debugging
print_v_files bool // when true, just print the list of all parsed .v files then stop.
skip_running bool // when true, do no try to run the produced file (set by b.cc(), when -o x.c or -o x.js)
skip_warnings bool // like C's "-w", forces warnings to be ignored.
warn_impure_v bool // -Wimpure-v, force a warning for JS.fn()/C.fn(), outside of .js.v/.c.v files. TODO: turn to an error by default
warns_are_errors bool // -W, like C's "-Werror", treat *every* warning is an error
fatal_errors bool // unconditionally exit after the first error with exit(1)
reuse_tmpc bool // do not use random names for .tmp.c and .tmp.c.rsp files, and do not remove them
no_rsp bool // when true, pass C backend options directly on the CLI (do not use `.rsp` files for them, some older C compilers do not support them)
no_std bool // when true, do not pass -std=gnu99(linux)/-std=c99 to the C backend
//
no_parallel bool // do not use threads when compiling; slower, but more portable and sometimes less buggy
only_check_syntax bool // when true, just parse the files, then stop, before running checker
check_only bool // same as only_check_syntax, but also runs the checker
experimental bool // enable experimental features
skip_unused bool // skip generating C code for functions, that are not used
show_timings bool // show how much time each compiler stage took
//
2020-05-20 19:33:29 +02:00
use_color ColorOutput // whether the warnings/errors should use ANSI color escapes.
2022-05-03 08:17:53 +02:00
cleanup_files [ ] string // list of temporary *.tmp.c and *.tmp.c.rsp files. Cleaned up on successfull builds.
build_options [ ] string // list of options, that should be passed down to `build-module`, if needed for -usecache
2020-10-26 17:28:35 +01:00
cache_manager vcache . CacheManager
2021-03-30 09:36:22 +02:00
gc_mode GarbageCollectionMode = . no_gc // .no_gc, .boehm, .boehm_leak, ...
2022-05-03 08:17:53 +02:00
assert_failure_mode AssertFailureMode // whether to call abort() or print_backtrace() after an assertion failure
2021-08-15 12:41:51 +02:00
message_limit int = 100 // the maximum amount of warnings/errors/notices that will be accumulated
2022-05-03 08:17:53 +02:00
nofloat bool // for low level code, like kernels: replaces f32 with u32 and f64 with u64
2021-03-15 05:22:22 +01:00
// checker settings:
2021-06-13 01:04:08 +02:00
checker_match_exhaustive_cutoff_limit int = 12
2022-04-28 07:46:33 +02:00
thread_stack_size int = 8388608 // Change with `-thread-stack-size 4194304`. Note: on macos it was 524288, which is too small for more complex programs with many nested callexprs.
2020-02-03 07:02:54 +01:00
}
2020-03-06 18:53:29 +01:00
2021-02-18 11:08:14 +01:00
pub fn parse_args ( known_external_commands [ ] string , args [ ] string ) ( & Preferences , string ) {
2021-12-12 20:10:43 +01:00
return parse_args_and_show_errors ( known_external_commands , args , false )
}
2022-05-03 08:17:53 +02:00
[ if linux ]
fn detect_musl ( mut res Preferences ) {
res . is_glibc = true
res . is_musl = false
if os . exists ( ' / e t c / a l p i n e - r e l e a s e ' ) {
res . is_musl = true
res . is_glibc = false
return
}
my_libs := os . walk_ext ( ' / p r o c / s e l f / m a p _ f i l e s / ' , ' ' ) . map ( os . real_path ( it ) )
if my_libs . any ( it . contains ( ' m u s l ' ) ) {
res . is_musl = true
res . is_glibc = false
}
}
2021-12-12 20:10:43 +01:00
pub fn parse_args_and_show_errors ( known_external_commands [ ] string , args [ ] string , show_output bool ) ( & Preferences , string ) {
2020-07-04 15:19:54 +02:00
mut res := & Preferences { }
2022-05-03 08:17:53 +02:00
detect_musl ( mut res )
2020-11-29 14:53:49 +01:00
$ if x64 {
res . m64 = true // follow V model by default
}
2021-12-20 16:22:02 +01:00
res . run_only = os . getenv ( ' V T E S T _ O N L Y _ F N ' ) . split_any ( ' , ' )
2020-05-16 22:45:38 +02:00
mut command := ' '
2022-05-18 06:03:00 +02:00
mut command_pos := - 1
2020-05-16 22:45:38 +02:00
// for i, arg in args {
for i := 0 ; i < args . len ; i ++ {
arg := args [ i ]
2021-03-14 08:37:38 +01:00
current_args := args [ i .. ] . clone ( )
2020-05-16 22:45:38 +02:00
match arg {
2020-08-19 11:00:15 +02:00
' - a p k ' {
res . is_apk = true
2020-10-25 02:09:07 +02:00
res . build_options << arg
2020-08-19 11:00:15 +02:00
}
2021-03-17 01:43:17 +01:00
' - a r c h ' {
target_arch := cmdline . option ( current_args , ' - a r c h ' , ' ' )
i ++
target_arch_kind := arch_from_string ( target_arch ) or {
2021-03-20 14:16:36 +01:00
eprintln ( ' u n k n o w n a r c h i t e c t u r e t a r g e t ` $ target_arch ` ' )
2021-03-17 01:43:17 +01:00
exit ( 1 )
}
res . arch = target_arch_kind
res . build_options << ' $ arg $ target_arch '
}
2021-04-15 19:26:51 +02:00
' - a s s e r t ' {
assert_mode := cmdline . option ( current_args , ' - a s s e r t ' , ' ' )
match assert_mode {
' a b o r t s ' {
res . assert_failure_mode = . aborts
}
' b a c k t r a c e s ' {
res . assert_failure_mode = . backtraces
}
else {
eprintln ( ' u n k n o w n a s s e r t m o d e ` - g c $ assert_mode ` , s u p p o r t e d m o d e s a r e : ` ' )
eprintln ( ' ` - a s s e r t a b o r t s ` . . . . c a l l s a b o r t ( ) a f t e r a s s e r t i o n f a i l u r e ' )
eprintln ( ' ` - a s s e r t b a c k t r a c e s ` . . . . c a l l s p r i n t _ b a c k t r a c e ( ) a f t e r a s s e r t i o n f a i l u r e ' )
exit ( 1 )
}
}
2021-04-15 19:29:58 +02:00
i ++
2021-04-15 19:26:51 +02:00
}
2020-07-14 17:45:44 +02:00
' - s h o w - t i m i n g s ' {
res . show_timings = true
}
2020-07-12 21:44:38 +02:00
' - c h e c k - s y n t a x ' {
res . only_check_syntax = true
}
2021-09-07 06:17:53 +02:00
' - c h e c k ' {
res . check_only = true
}
2021-03-02 11:22:10 +01:00
' - h ' , ' - h e l p ' , ' - - h e l p ' {
2022-03-06 18:01:22 +01:00
// Note: help is *very important*, just respond to all variations:
2021-03-02 11:22:10 +01:00
res . is_help = true
}
2020-05-16 22:45:38 +02:00
' - v ' {
2022-05-18 06:03:00 +02:00
if command_pos != - 1 {
// a -v flag after the command, is intended for the command, not for V itself
continue
}
2020-09-29 03:13:54 +02:00
// `-v` flag is for setting verbosity, but without any args it prints the version, like Clang
if args . len > 1 {
res . is_verbose = true
} else {
command = ' v e r s i o n '
command_pos = i
}
2020-05-16 22:45:38 +02:00
}
2020-10-18 19:02:47 +02:00
' - p r o g r e s s ' {
// processed by testing tools in cmd/tools/modules/testing/common.v
2020-10-18 18:00:32 +02:00
}
2020-12-08 17:52:24 +01:00
' - W i m p u r e - v ' {
res . warn_impure_v = true
}
2020-12-04 19:34:05 +01:00
' - W f a t a l - e r r o r s ' {
res . fatal_errors = true
}
2020-05-16 22:45:38 +02:00
' - s i l e n t ' {
res . output_mode = . silent
}
2021-04-15 13:55:36 +02:00
' - c s t r i c t ' {
res . is_cstrict = true
}
2021-09-10 19:54:41 +02:00
' - n o f l o a t ' {
res . nofloat = true
2021-09-13 17:34:58 +02:00
res . compile_defines_all << ' n o f l o a t ' // so that `$if nofloat? {` works
2021-09-10 19:54:41 +02:00
}
2021-03-20 14:16:36 +01:00
' - g c ' {
gc_mode := cmdline . option ( current_args , ' - g c ' , ' ' )
match gc_mode {
2021-06-15 14:43:00 +02:00
' ' , ' n o n e ' {
2021-03-20 14:16:36 +01:00
res . gc_mode = . no_gc
}
2021-03-30 09:36:22 +02:00
' b o e h m _ f u l l ' {
res . gc_mode = . boehm_full
2021-09-13 17:40:32 +02:00
res . parse_define ( ' g c b o e h m ' )
res . parse_define ( ' g c b o e h m _ f u l l ' )
2021-03-30 09:36:22 +02:00
}
' b o e h m _ i n c r ' {
res . gc_mode = . boehm_incr
2021-09-13 17:40:32 +02:00
res . parse_define ( ' g c b o e h m ' )
res . parse_define ( ' g c b o e h m _ i n c r ' )
2021-03-30 09:36:22 +02:00
}
2021-04-14 00:40:26 +02:00
' b o e h m _ f u l l _ o p t ' {
res . gc_mode = . boehm_full_opt
2021-09-13 17:40:32 +02:00
res . parse_define ( ' g c b o e h m ' )
res . parse_define ( ' g c b o e h m _ f u l l ' )
res . parse_define ( ' g c b o e h m _ o p t ' )
2021-04-14 00:40:26 +02:00
}
' b o e h m _ i n c r _ o p t ' {
res . gc_mode = . boehm_incr_opt
2021-09-13 17:40:32 +02:00
res . parse_define ( ' g c b o e h m ' )
res . parse_define ( ' g c b o e h m _ i n c r ' )
res . parse_define ( ' g c b o e h m _ o p t ' )
2021-04-14 00:40:26 +02:00
}
2021-03-20 14:16:36 +01:00
' b o e h m ' {
2021-06-15 14:43:00 +02:00
res . gc_mode = . boehm_full_opt // default mode
2021-09-13 17:40:32 +02:00
res . parse_define ( ' g c b o e h m ' )
res . parse_define ( ' g c b o e h m _ f u l l ' )
res . parse_define ( ' g c b o e h m _ o p t ' )
2021-03-20 14:16:36 +01:00
}
2021-03-25 16:52:33 +01:00
' b o e h m _ l e a k ' {
res . gc_mode = . boehm_leak
2021-09-13 17:40:32 +02:00
res . parse_define ( ' g c b o e h m ' )
res . parse_define ( ' g c b o e h m _ l e a k ' )
2021-03-25 16:52:33 +01:00
}
2021-03-20 14:16:36 +01:00
else {
2021-04-14 00:40:26 +02:00
eprintln ( ' u n k n o w n g a r b a g e c o l l e c t i o n m o d e ` - g c $ gc_mode ` , s u p p o r t e d m o d e s a r e : ` ' )
2021-06-15 14:43:00 +02:00
eprintln ( ' ` - g c b o e h m ` . . . . . . . . . . . . d e f a u l t G C - m o d e ( c u r r e n t l y ` b o e h m _ f u l l _ o p t ` ) ' )
2021-04-14 00:40:26 +02:00
eprintln ( ' ` - g c b o e h m _ f u l l ` . . . . . . . c l a s s i c f u l l c o l l e c t i o n ' )
eprintln ( ' ` - g c b o e h m _ i n c r ` . . . . . . . i n c r e m e n t a l c o l l e c t i o n ' )
eprintln ( ' ` - g c b o e h m _ f u l l _ o p t ` . . . o p t i m i z e d c l a s s i c f u l l c o l l e c t i o n ' )
eprintln ( ' ` - g c b o e h m _ i n c r _ o p t ` . . . o p t i m i z e d i n c r e m e n t a l c o l l e c t i o n ' )
eprintln ( ' ` - g c b o e h m _ l e a k ` . . . . . . . l e a k d e t e c t i o n ( f o r d e b u g g i n g ) ' )
2021-06-15 14:43:00 +02:00
eprintln ( ' ` - g c n o n e ` . . . . . . . . . . . . . n o g a r b a g e c o l l e c t i o n ' )
2021-03-20 14:16:36 +01:00
exit ( 1 )
}
}
i ++
}
2020-08-11 23:08:19 +02:00
' - g ' {
res . is_debug = true
res . is_vlines = true
2020-10-25 02:09:07 +02:00
res . build_options << arg
2020-08-11 23:08:19 +02:00
}
2020-05-16 22:45:38 +02:00
' - c g ' {
res . is_debug = true
2020-08-11 23:08:19 +02:00
res . is_vlines = false
2020-10-25 02:09:07 +02:00
res . build_options << arg
2020-05-16 22:45:38 +02:00
}
2021-02-20 17:53:22 +01:00
' - d e b u g - t c c ' {
res . ccompiler = ' t c c '
res . build_options << ' $ arg " $ res . ccompiler " '
res . retry_compilation = false
res . show_cc = true
res . show_c_output = true
}
2021-06-10 07:33:46 +02:00
' - s o u r c e m a p ' {
res . sourcemap = true
}
' - s o u r c e m a p - s r c - i n c l u d e d ' {
res . sourcemap_src_included = true
}
' - s o u r c e m a p - i n l i n e ' {
res . sourcemap_inline = true
}
2020-05-16 22:45:38 +02:00
' - r e p l ' {
res . is_repl = true
}
' - l i v e ' {
res . is_livemain = true
}
' - s h a r e d l i v e ' {
res . is_liveshared = true
res . is_shared = true
}
' - s h a r e d ' {
res . is_shared = true
}
2021-04-28 06:44:28 +02:00
' - - e n a b l e - g l o b a l s ' {
2021-12-12 20:10:43 +01:00
eprintln_cond ( show_output , ' ` - - e n a b l e - g l o b a l s ` f l a g i s d e p r e c a t e d , p l e a s e u s e ` - e n a b l e - g l o b a l s ` i n s t e a d ' )
2021-04-28 06:44:28 +02:00
res . enable_globals = true
}
' - e n a b l e - g l o b a l s ' {
2020-05-16 22:45:38 +02:00
res . enable_globals = true
}
' - a u t o f r e e ' {
res . autofree = true
2020-10-25 02:09:07 +02:00
res . build_options << arg
2020-05-16 22:45:38 +02:00
}
2021-01-08 13:56:55 +01:00
' - m a n u a l f r e e ' {
res . autofree = false
res . build_options << arg
}
2021-02-05 09:42:52 +01:00
' - s k i p - u n u s e d ' {
res . skip_unused = true
}
2020-05-16 22:45:38 +02:00
' - c o m p r e s s ' {
res . compress = true
}
' - f r e e s t a n d i n g ' {
res . is_bare = true
2020-10-25 02:09:07 +02:00
res . build_options << arg
2020-05-16 22:45:38 +02:00
}
2020-12-28 08:39:21 +01:00
' - n o - r e t r y - c o m p i l a t i o n ' {
res . retry_compilation = false
}
2022-05-03 08:17:53 +02:00
' - m u s l ' {
res . is_musl = true
res . is_glibc = false
res . build_options << arg
}
' - g l i b c ' {
res . is_musl = false
res . is_glibc = true
res . build_options << arg
}
2021-10-23 11:53:53 +02:00
' - n o - b u i l t i n ' {
res . no_builtin = true
res . build_options << arg
}
2020-05-26 14:12:18 +02:00
' - n o - p r e l u d e s ' {
res . no_preludes = true
2020-10-25 02:09:07 +02:00
res . build_options << arg
2020-05-26 14:12:18 +02:00
}
2020-05-16 22:45:38 +02:00
' - p r o f ' , ' - p r o f i l e ' {
2020-12-11 08:28:34 +01:00
res . profile_file = cmdline . option ( current_args , arg , ' - ' )
2020-05-16 22:45:38 +02:00
res . is_prof = true
2020-10-25 02:09:07 +02:00
res . build_options << ' $ arg $ res . profile_file '
2020-05-16 22:45:38 +02:00
i ++
}
2022-01-26 18:52:08 +01:00
' - p r o f i l e - f n s ' {
profile_fns := cmdline . option ( current_args , arg , ' ' ) . split ( ' , ' )
if profile_fns . len > 0 {
res . profile_fns << profile_fns
}
i ++
}
2020-05-16 22:45:38 +02:00
' - p r o f i l e - n o - i n l i n e ' {
res . profile_no_inline = true
}
' - p r o d ' {
res . is_prod = true
2020-10-25 02:09:07 +02:00
res . build_options << arg
2020-05-16 22:45:38 +02:00
}
2020-12-21 17:46:26 +01:00
' - s a n i t i z e ' {
res . sanitize = true
res . build_options << arg
}
2020-07-28 22:27:38 +02:00
' - s i m u l a t o r ' {
res . is_ios_simulator = true
}
2020-05-16 22:45:38 +02:00
' - s t a t s ' {
res . is_stats = true
}
2021-02-02 11:24:25 +01:00
' - o b f ' , ' - o b f u s c a t e ' {
2020-05-16 22:45:38 +02:00
res . obfuscate = true
}
' - t r a n s l a t e d ' {
res . translated = true
}
2020-11-29 14:53:49 +01:00
' - m 3 2 ' , ' - m 6 4 ' {
res . m64 = arg [ 2 ] == ` 6 `
res . cflags += ' $ arg '
}
2021-05-28 08:07:06 +02:00
' - c o l o r ' {
res . use_color = . always
}
2020-05-20 19:33:29 +02:00
' - n o c o l o r ' {
2020-05-26 14:12:18 +02:00
res . use_color = . never
2020-05-20 19:33:29 +02:00
}
2020-05-16 22:45:38 +02:00
' - s h o w c c ' {
res . show_cc = true
}
2020-11-06 15:26:59 +01:00
' - s h o w - c - o u t p u t ' {
res . show_c_output = true
}
2021-07-24 07:49:50 +02:00
' - s h o w - c a l l g r a p h ' {
res . show_callgraph = true
}
2021-07-28 15:41:32 +02:00
' - s h o w - d e p g r a p h ' {
res . show_depgraph = true
}
2021-12-20 16:22:02 +01:00
' - r u n - o n l y ' {
res . run_only = cmdline . option ( current_args , arg , os . getenv ( ' V T E S T _ O N L Y _ F N ' ) ) . split_any ( ' , ' )
i ++
}
2021-12-16 14:59:46 +01:00
' - t e s t - r u n n e r ' {
res . test_runner = cmdline . option ( current_args , arg , res . test_runner )
i ++
}
2021-01-29 17:05:09 +01:00
' - d u m p - c - f l a g s ' {
res . dump_c_flags = cmdline . option ( current_args , arg , ' - ' )
i ++
}
2022-05-23 18:48:27 +02:00
' - d u m p - m o d u l e s ' {
res . dump_modules = cmdline . option ( current_args , arg , ' - ' )
i ++
}
' - d u m p - f i l e s ' {
res . dump_files = cmdline . option ( current_args , arg , ' - ' )
i ++
}
2020-07-14 00:27:47 +02:00
' - e x p e r i m e n t a l ' {
res . experimental = true
}
2020-05-16 22:45:38 +02:00
' - u s e c a c h e ' {
res . use_cache = true
}
2020-12-01 04:05:41 +01:00
' - n o c a c h e ' {
2020-12-28 08:39:21 +01:00
res . use_cache = false
2020-12-01 04:05:41 +01:00
}
2020-07-11 11:25:53 +02:00
' - p r e a l l o c ' {
res . prealloc = true
2020-10-25 02:09:07 +02:00
res . build_options << arg
2020-07-11 11:25:53 +02:00
}
2021-09-28 09:28:04 +02:00
' - n o - p a r a l l e l ' {
res . no_parallel = true
2020-05-22 02:22:56 +02:00
}
2021-04-26 15:39:38 +02:00
' - n a t i v e ' {
res . backend = . native
2020-10-25 02:09:07 +02:00
res . build_options << arg
2020-05-16 22:45:38 +02:00
}
2021-12-08 09:09:10 +01:00
' - i n t e r p r e t ' {
res . backend = . interpret
}
2020-10-24 19:29:24 +02:00
' - W ' {
res . warns_are_errors = true
}
2021-08-15 09:38:39 +02:00
' - n o - r s p ' {
res . no_rsp = true
}
2021-08-15 10:05:06 +02:00
' - n o - s t d ' {
res . no_std = true
}
2020-11-27 20:16:24 +01:00
' - k e e p c ' {
2020-10-24 19:29:24 +02:00
res . reuse_tmpc = true
}
2020-05-16 22:45:38 +02:00
' - w ' {
res . skip_warnings = true
}
2021-04-04 16:05:06 +02:00
' - w a t c h ' {
2021-04-28 11:23:23 +02:00
eprintln ( ' T h e - w a t c h o p t i o n i s d e p r e c a t e d . P l e a s e u s e t h e w a t c h c o m m a n d ` v w a t c h f i l e . v ` i n s t e a d . ' )
exit ( 1 )
2021-04-04 16:05:06 +02:00
}
2021-03-04 13:38:05 +01:00
' - p r i n t - v - f i l e s ' {
2020-05-16 22:45:38 +02:00
res . print_v_files = true
}
' - o s ' {
target_os := cmdline . option ( current_args , ' - o s ' , ' ' )
i ++
2020-07-04 15:19:54 +02:00
target_os_kind := os_from_string ( target_os ) or {
2020-05-16 22:45:38 +02:00
if target_os == ' c r o s s ' {
res . output_cross_c = true
continue
}
2020-05-26 14:12:18 +02:00
eprintln ( ' u n k n o w n o p e r a t i n g s y s t e m t a r g e t ` $ target_os ` ' )
2020-05-16 22:45:38 +02:00
exit ( 1 )
}
2022-01-08 10:06:32 +01:00
if target_os_kind == . wasm32 {
res . is_bare = true
}
2020-05-16 22:45:38 +02:00
res . os = target_os_kind
2020-10-25 02:09:07 +02:00
res . build_options << ' $ arg $ target_os '
2020-05-16 22:45:38 +02:00
}
' - p r i n t f n ' {
2021-03-05 12:19:39 +01:00
res . printfn_list << cmdline . option ( current_args , ' - p r i n t f n ' , ' ' ) . split ( ' , ' )
2020-05-16 22:45:38 +02:00
i ++
}
' - c f l a g s ' {
res . cflags += ' ' + cmdline . option ( current_args , ' - c f l a g s ' , ' ' )
2020-10-25 02:09:07 +02:00
res . build_options << ' $ arg " $ res . cflags . trim_space ( ) " '
2020-05-16 22:45:38 +02:00
i ++
}
2021-05-28 08:07:06 +02:00
' - d ' , ' - d e f i n e ' {
2020-05-16 22:45:38 +02:00
if current_args . len > 1 {
define := current_args [ 1 ]
2021-09-13 17:40:32 +02:00
res . parse_define ( define )
2020-05-16 22:45:38 +02:00
}
i ++
}
2021-08-15 12:46:50 +02:00
' - e r r o r - l i m i t ' , ' - m e s s a g e - l i m i t ' {
2021-08-15 12:41:51 +02:00
res . message_limit = cmdline . option ( current_args , arg , ' 5 ' ) . int ( )
2021-08-14 18:49:21 +02:00
i ++
}
2022-04-28 07:46:33 +02:00
' - t h r e a d - s t a c k - s i z e ' {
res . thread_stack_size = cmdline . option ( current_args , arg , res . thread_stack_size . str ( ) ) . int ( )
i ++
}
2020-05-16 22:45:38 +02:00
' - c c ' {
res . ccompiler = cmdline . option ( current_args , ' - c c ' , ' c c ' )
2020-10-25 02:09:07 +02:00
res . build_options << ' $ arg " $ res . ccompiler " '
2022-05-03 08:17:53 +02:00
if res . ccompiler == ' m u s l - g c c ' {
res . is_musl = true
res . is_glibc = false
}
2020-05-16 22:45:38 +02:00
i ++
}
2021-03-15 05:22:22 +01:00
' - c h e c k e r - m a t c h - e x h a u s t i v e - c u t o f f - l i m i t ' {
res . checker_match_exhaustive_cutoff_limit = cmdline . option ( current_args ,
arg , ' 1 0 ' ) . int ( )
i ++
}
2021-05-28 08:07:06 +02:00
' - o ' , ' - o u t p u t ' {
res . out_name = cmdline . option ( current_args , arg , ' ' )
2020-11-08 17:45:32 +01:00
if res . out_name . ends_with ( ' . j s ' ) {
2021-07-19 14:55:03 +02:00
res . backend = . js_node
2021-09-03 11:16:07 +02:00
res . output_cross_c = true
2021-11-29 03:00:20 +01:00
} else if res . out_name . ends_with ( ' . o ' ) {
res . is_o = true
2020-11-08 17:45:32 +01:00
}
2020-11-29 16:18:49 +01:00
if ! os . is_abs_path ( res . out_name ) {
res . out_name = os . join_path ( os . getwd ( ) , res . out_name )
}
2020-05-16 22:45:38 +02:00
i ++
}
2021-05-28 08:07:06 +02:00
' - b ' , ' - b a c k e n d ' {
sbackend := cmdline . option ( current_args , arg , ' c ' )
2020-10-25 02:09:07 +02:00
res . build_options << ' $ arg $ sbackend '
2020-12-04 12:25:23 +01:00
b := backend_from_string ( sbackend ) or { continue }
2021-07-19 14:55:03 +02:00
if b . is_js ( ) {
res . output_cross_c = true
}
2020-05-16 22:45:38 +02:00
res . backend = b
i ++
}
2021-12-15 14:47:34 +01:00
' - e s 5 ' {
res . output_es5 = true
}
2020-05-26 03:17:52 +02:00
' - p a t h ' {
path := cmdline . option ( current_args , ' - p a t h ' , ' ' )
2020-10-25 02:09:07 +02:00
res . build_options << ' $ arg " $ path " '
2020-08-15 21:07:38 +02:00
res . lookup_path = path . replace ( ' | ' , os . path_delimiter ) . split ( os . path_delimiter )
2020-05-26 03:17:52 +02:00
i ++
}
2021-04-09 22:24:25 +02:00
' - b a r e - b u i l t i n - d i r ' {
bare_builtin_dir := cmdline . option ( current_args , arg , ' ' )
res . build_options << ' $ arg " $ bare_builtin_dir " '
res . bare_builtin_dir = bare_builtin_dir
i ++
}
2020-05-26 14:12:18 +02:00
' - c u s t o m - p r e l u d e ' {
path := cmdline . option ( current_args , ' - c u s t o m - p r e l u d e ' , ' ' )
2020-10-25 02:09:07 +02:00
res . build_options << ' $ arg $ path '
2020-05-26 14:12:18 +02:00
prelude := os . read_file ( path ) or {
eprintln ( ' c a n n o t o p e n c u s t o m p r e l u d e f i l e : $ err ' )
exit ( 1 )
}
res . custom_prelude = prelude
i ++
}
2022-05-28 20:16:48 +02:00
' - c m a i n ' {
res . cmain = cmdline . option ( current_args , ' - c m a i n ' , ' ' )
i ++
}
2020-05-16 22:45:38 +02:00
else {
2021-02-18 08:42:00 +01:00
if command == ' b u i l d ' && is_source_file ( arg ) {
2020-12-31 15:24:55 +01:00
eprintln ( ' U s e ` v $ arg ` i n s t e a d . ' )
exit ( 1 )
}
2022-04-10 09:21:58 +02:00
if arg . len != 0 && arg [ 0 ] == ` - ` {
2021-01-25 10:26:20 +01:00
if arg [ 1 .. ] in pref . list_of_flags_with_param {
2020-09-29 03:13:54 +02:00
// skip parameter
2020-05-16 22:45:38 +02:00
i ++
2020-09-29 03:13:54 +02:00
continue
}
} else {
if command == ' ' {
command = arg
command_pos = i
2020-12-29 20:16:20 +01:00
if command == ' r u n ' {
break
}
2021-02-18 11:08:14 +01:00
} else if is_source_file ( command ) && is_source_file ( arg )
&& command ! in known_external_commands {
2021-02-18 08:42:00 +01:00
eprintln ( ' T o o m a n y t a r g e t s . S p e c i f y j u s t o n e t a r g e t : < t a r g e t . v | t a r g e t _ d i r e c t o r y > . ' )
exit ( 1 )
2020-05-16 22:45:38 +02:00
}
continue
}
2020-09-29 03:13:54 +02:00
if arg in [ ' - V ' , ' - v e r s i o n ' , ' - - v e r s i o n ' ] {
command = ' v e r s i o n '
2020-05-16 22:45:38 +02:00
command_pos = i
2020-09-29 03:13:54 +02:00
continue
}
2021-03-14 08:37:38 +01:00
if command != ' ' && command != ' b u i l d - m o d u l e ' {
2020-10-01 22:30:39 +02:00
// arguments for e.g. fmt should be checked elsewhere
2020-09-29 03:13:54 +02:00
continue
2020-05-16 22:45:38 +02:00
}
2021-03-14 08:37:38 +01:00
extension := if command . len == 0 { ' ' } else { ' f o r c o m m a n d ` $ command ` ' }
eprintln ( ' U n k n o w n a r g u m e n t ` $ arg ` $ extension ' )
2020-09-29 03:13:54 +02:00
exit ( 1 )
2020-05-16 22:45:38 +02:00
}
}
}
2021-03-07 10:09:17 +01:00
if res . is_debug {
2021-09-13 17:40:32 +02:00
res . parse_define ( ' d e b u g ' )
2021-03-07 10:09:17 +01:00
}
2021-10-07 18:22:08 +02:00
if command == ' r u n ' && res . is_prod && os . is_atty ( 1 ) > 0 {
2022-03-06 18:01:22 +01:00
eprintln_cond ( show_output , " N o t e : b u i l d i n g a n o p t i m i z e d b i n a r y t a k e s m u c h l o n g e r . I t s h o u l d n ' t b e u s e d w i t h ` v r u n ` . " )
2021-12-12 20:10:43 +01:00
eprintln_cond ( show_output , ' U s e ` v r u n ` w i t h o u t o p t i m i z a t i o n , o r b u i l d a n o p t i m i z e d b i n a r y w i t h - p r o d f i r s t , t h e n r u n i t s e p a r a t e l y . ' )
2021-10-07 15:13:58 +02:00
}
2021-06-12 23:45:17 +02:00
2020-12-21 08:35:24 +01:00
// res.use_cache = true
2020-06-05 09:59:26 +02:00
if command != ' d o c ' && res . out_name . ends_with ( ' . v ' ) {
eprintln ( ' C a n n o t s a v e o u t p u t b i n a r y i n a . v f i l e . ' )
exit ( 1 )
}
2021-07-30 02:24:36 +02:00
if command == ' r u n ' {
2020-05-16 22:45:38 +02:00
res . is_run = true
2020-05-25 12:30:12 +02:00
if command_pos + 2 > args . len {
2020-05-16 22:45:38 +02:00
eprintln ( ' v r u n : n o v f i l e s l i s t e d ' )
exit ( 1 )
}
res . path = args [ command_pos + 1 ]
res . run_args = args [ command_pos + 2 .. ]
2020-11-20 09:25:59 +01:00
if res . path == ' - ' {
tmp_file_path := rand . ulid ( )
mut tmp_exe_file_path := res . out_name
mut output_option := ' '
if tmp_exe_file_path == ' ' {
tmp_exe_file_path = ' $ { tmp_file_path } . e x e '
2022-01-22 21:35:18 +01:00
output_option = ' - o $ { os . quoted_path ( tmp_exe_file_path ) } '
2020-11-20 09:25:59 +01:00
}
tmp_v_file_path := ' $ { tmp_file_path } . v '
2021-01-30 11:38:54 +01:00
contents := os . get_raw_lines_joined ( )
2020-11-20 09:25:59 +01:00
os . write_file ( tmp_v_file_path , contents ) or {
panic ( ' F a i l e d t o c r e a t e t e m p o r a r y f i l e $ tmp_v_file_path ' )
}
run_options := cmdline . options_before ( args , [ ' r u n ' ] ) . join ( ' ' )
command_options := cmdline . options_after ( args , [ ' r u n ' ] ) [ 1 .. ] . join ( ' ' )
vexe := vexe_path ( )
2022-01-22 21:35:18 +01:00
tmp_cmd := ' $ { os . quoted_path ( vexe ) } $ output_option $ run_options r u n $ { os . quoted_path ( tmp_v_file_path ) } $ command_options '
2020-11-20 09:25:59 +01:00
//
res . vrun_elog ( ' t m p _ c m d : $ tmp_cmd ' )
tmp_result := os . system ( tmp_cmd )
res . vrun_elog ( ' e x i t c o d e : $ tmp_result ' )
//
if output_option . len != 0 {
res . vrun_elog ( ' r e m o v e t m p e x e f i l e : $ tmp_exe_file_path ' )
2021-03-06 20:04:51 +01:00
os . rm ( tmp_exe_file_path ) or { }
2020-11-20 09:25:59 +01:00
}
res . vrun_elog ( ' r e m o v e t m p v f i l e : $ tmp_v_file_path ' )
2021-09-17 19:56:33 +02:00
os . rm ( tmp_v_file_path ) or { }
2020-11-20 09:25:59 +01:00
exit ( tmp_result )
}
2020-07-21 18:36:58 +02:00
must_exist ( res . path )
2021-01-23 09:33:22 +01:00
if ! res . path . ends_with ( ' . v ' ) && os . is_executable ( res . path ) && os . is_file ( res . path )
&& os . is_file ( res . path + ' . v ' ) {
2021-12-12 20:10:43 +01:00
eprintln_cond ( show_output , ' I t l o o k s l i k e y o u w a n t e d t o r u n " $ { res . path } . v " , s o w e w e n t a h e a d a n d d i d t h a t s i n c e " $ res . path " i s a n e x e c u t a b l e . ' )
2020-11-19 16:23:55 +01:00
res . path += ' . v '
}
2021-07-30 02:24:36 +02:00
} else if is_source_file ( command ) {
res . path = command
2020-05-16 22:45:38 +02:00
}
2021-04-14 07:50:50 +02:00
if ! res . is_bare && res . bare_builtin_dir != ' ' {
2021-12-12 20:10:43 +01:00
eprintln_cond ( show_output , ' ` - b a r e - b u i l t i n - d i r ` m u s t b e u s e d w i t h ` - f r e e s t a n d i n g ` ' )
2021-04-14 07:50:50 +02:00
}
2021-11-08 14:18:11 +01:00
if command . ends_with ( ' . v s h ' ) {
// `v build.vsh gcc` is the same as `v run build.vsh gcc`,
// i.e. compiling, then running the script, passing the args
// after it to the script:
res . is_run = true
res . path = command
res . run_args = args [ command_pos + 1 .. ]
2021-12-08 09:09:10 +01:00
} else if command == ' i n t e r p r e t ' {
res . backend = . interpret
if command_pos + 2 > args . len {
eprintln ( ' v i n t e r p r e t : n o v f i l e s l i s t e d ' )
exit ( 1 )
}
res . path = args [ command_pos + 1 ]
res . run_args = args [ command_pos + 2 .. ]
must_exist ( res . path )
if ! res . path . ends_with ( ' . v ' ) && os . is_executable ( res . path ) && os . is_file ( res . path )
&& os . is_file ( res . path + ' . v ' ) {
eprintln ( ' I t l o o k s l i k e y o u w a n t e d t o r u n " $ { res . path } . v " , s o w e w e n t a h e a d a n d d i d t h a t s i n c e " $ res . path " i s a n e x e c u t a b l e . ' )
res . path += ' . v '
}
2021-11-08 14:18:11 +01:00
}
2020-05-16 22:45:38 +02:00
if command == ' b u i l d - m o d u l e ' {
res . build_mode = . build_module
2021-06-04 17:30:43 +02:00
if command_pos + 1 >= args . len {
eprintln ( ' v b u i l d - m o d u l e : n o m o d u l e s p e c i f i e d ' )
exit ( 1 )
}
2020-05-16 22:45:38 +02:00
res . path = args [ command_pos + 1 ]
}
2020-10-25 02:09:07 +02:00
// keep only the unique res.build_options:
mut m := map [ string ] string { }
for x in res . build_options {
m [ x ] = ' '
}
res . build_options = m . keys ( )
// eprintln('>> res.build_options: $res.build_options')
2020-05-16 22:45:38 +02:00
res . fill_with_defaults ( )
return res , command
}
2021-12-12 20:10:43 +01:00
pub fn eprintln_cond ( condition bool , s string ) {
if ! condition {
return
}
eprintln ( s )
}
2020-12-05 12:03:12 +01:00
pub fn ( pref & Preferences ) vrun_elog ( s string ) {
2020-11-20 09:25:59 +01:00
if pref . is_verbose {
eprintln ( ' > v r u n - , $ s ' )
}
}
2021-07-31 13:40:58 +02:00
pub fn ( pref & Preferences ) should_output_to_stdout ( ) bool {
return pref . out_name . ends_with ( ' / - ' ) || pref . out_name . ends_with ( r '\-' )
}
2021-03-17 01:43:17 +01:00
pub fn arch_from_string ( arch_str string ) ? Arch {
match arch_str {
' a m d 6 4 ' , ' x 8 6 _ 6 4 ' , ' x 6 4 ' , ' x 8 6 ' { // amd64 recommended
return Arch . amd64
}
2021-04-26 18:01:42 +02:00
' a a r c h 6 4 ' , ' a r m 6 4 ' { // arm64 recommended
2021-03-17 01:43:17 +01:00
2021-04-26 18:01:42 +02:00
return Arch . arm64
2021-03-17 01:43:17 +01:00
}
2021-04-26 18:01:42 +02:00
' a a r c h 3 2 ' , ' a r m 3 2 ' , ' a r m ' { // arm32 recommended
2021-03-17 01:43:17 +01:00
2021-04-26 18:01:42 +02:00
return Arch . arm32
2021-03-17 01:43:17 +01:00
}
' r v 6 4 ' , ' r i s c v 6 4 ' , ' r i s c - v 6 4 ' , ' r i s c v ' , ' r i s c - v ' { // rv64 recommended
return Arch . rv64
}
' r v 3 2 ' , ' r i s c v 3 2 ' { // rv32 recommended
return Arch . rv32
}
' x 8 6 _ 3 2 ' , ' x 3 2 ' , ' i 3 8 6 ' , ' I A - 3 2 ' , ' i a - 3 2 ' , ' i a 3 2 ' { // i386 recommended
return Arch . i386
}
2021-10-19 11:11:54 +02:00
' j s ' , ' j s _ n o d e ' {
return Arch . js_node
}
' j s _ b r o w s e r ' {
return Arch . js_browser
}
' j s _ f r e e s t a n d i n g ' {
return Arch . js_freestanding
}
2021-03-17 01:43:17 +01:00
' ' {
return . _auto
}
else {
return error ( ' i n v a l i d a r c h : $ arch_str ' )
}
}
}
2020-07-21 18:36:58 +02:00
fn must_exist ( path string ) {
if ! os . exists ( path ) {
eprintln ( ' v e x p e c t s t h a t ` $ path ` e x i s t s , b u t i t d o e s n o t ' )
exit ( 1 )
2020-07-23 06:58:44 +02:00
}
2020-07-21 18:36:58 +02:00
}
2021-02-18 08:42:00 +01:00
[ inline ]
fn is_source_file ( path string ) bool {
return path . ends_with ( ' . v ' ) || os . exists ( path )
}
2020-03-06 18:53:29 +01:00
pub fn backend_from_string ( s string ) ? Backend {
match s {
2020-07-04 15:19:54 +02:00
' c ' { return . c }
2021-07-19 14:55:03 +02:00
' j s ' { return . js_node }
' j s _ n o d e ' { return . js_node }
' j s _ b r o w s e r ' { return . js_browser }
' j s _ f r e e s t a n d i n g ' { return . js_freestanding }
2021-04-26 15:39:38 +02:00
' n a t i v e ' { return . native }
2021-12-08 09:09:10 +01:00
' i n t e r p r e t ' { return . interpret }
2020-07-04 15:19:54 +02:00
else { return error ( ' U n k n o w n b a c k e n d t y p e $ s ' ) }
2020-03-06 18:53:29 +01:00
}
}
2020-05-16 22:57:55 +02:00
2020-09-18 00:58:54 +02:00
// Helper function to convert string names to CC enum
pub fn cc_from_string ( cc_str string ) CompilerType {
2020-09-29 02:53:59 +02:00
if cc_str . len == 0 {
return . gcc
}
// TODO
2021-03-14 08:37:38 +01:00
normalized_cc := cc_str . replace ( ' \\ ' , ' / ' )
normalized_cc_array := normalized_cc . split ( ' / ' )
last_elem := normalized_cc_array . last ( )
cc := last_elem . all_before ( ' . ' )
2021-03-23 09:38:56 +01:00
if cc . contains ( ' + + ' ) {
2020-09-29 02:53:59 +02:00
return . cplusplus
}
2021-03-23 09:38:56 +01:00
if cc . contains ( ' t c c ' ) || cc . contains ( ' t i n y c ' ) {
2020-09-29 02:53:59 +02:00
return . tinyc
}
2021-03-23 09:38:56 +01:00
if cc . contains ( ' c l a n g ' ) {
2020-09-29 02:53:59 +02:00
return . clang
}
2021-03-23 09:38:56 +01:00
if cc . contains ( ' m i n g w ' ) {
2020-09-29 02:53:59 +02:00
return . mingw
}
2021-03-23 09:38:56 +01:00
if cc . contains ( ' m s v c ' ) {
2020-09-29 02:53:59 +02:00
return . msvc
}
2020-09-18 00:58:54 +02:00
return . gcc
}
2021-03-17 01:43:17 +01:00
pub fn get_host_arch ( ) Arch {
2022-03-06 18:01:22 +01:00
// Note: we can not use `$if arch` here, because V skips cgen for the non
2021-06-12 21:15:08 +02:00
// current comptime branches by default, so there is a bootstrapping
// problem => the __V_architecture macro is used to resolve it.
// TODO: think about how to solve it for non C backends, perhaps we
// need a comptime `$if native {` too, and/or a mechanism to always
// generate all branches for specific functions?
if C . __V_architecture <= int ( Arch . _auto ) || C . __V_architecture >= int ( Arch . _max ) {
2021-06-13 00:44:52 +02:00
return Arch . amd64
2021-03-17 01:43:17 +01:00
}
2021-06-12 21:15:08 +02:00
return Arch ( C . __V_architecture )
2021-03-17 01:43:17 +01:00
}
2021-09-13 17:40:32 +02:00
fn ( mut prefs Preferences ) parse_define ( define string ) {
2020-07-04 15:19:54 +02:00
define_parts := define . split ( ' = ' )
2022-06-01 08:08:18 +02:00
prefs . diagnose_deprecated_defines ( define_parts )
2021-08-15 07:35:26 +02:00
if ! ( prefs . is_debug && define == ' d e b u g ' ) {
prefs . build_options << ' - d $ define '
}
2020-07-04 15:19:54 +02:00
if define_parts . len == 1 {
prefs . compile_defines << define
prefs . compile_defines_all << define
return
}
if define_parts . len == 2 {
prefs . compile_defines_all << define_parts [ 0 ]
match define_parts [ 1 ] {
' 0 ' { }
' 1 ' {
prefs . compile_defines << define_parts [ 0 ]
}
else {
2021-01-23 09:33:22 +01:00
println (
' V e r r o r : U n k n o w n d e f i n e a r g u m e n t v a l u e ` $ { define_parts [ 1 ] } ` f o r $ { define_parts [ 0 ] } . ' +
2020-10-02 05:16:16 +02:00
' E x p e c t e d ` 0 ` o r ` 1 ` . ' )
2020-07-04 15:19:54 +02:00
exit ( 1 )
}
}
return
}
println ( ' V e r r o r : U n k n o w n d e f i n e a r g u m e n t : $ { define } . E x p e c t e d a t m o s t o n e ` = ` . ' )
exit ( 1 )
2020-05-16 22:57:55 +02:00
}
2022-06-01 08:08:18 +02:00
fn ( mut prefs Preferences ) diagnose_deprecated_defines ( define_parts [ ] string ) {
if define_parts [ 0 ] == ' f o r c e _ e m b e d _ f i l e ' {
eprintln ( ' - d f o r c e _ e m b e d _ f i l e w a s d e p r e c a t e d i n 2 0 2 2 / 0 6 / 0 1 . N o w \$ e m b e d _ f i l e ( f i l e ) a l w a y s e m b e d s t h e f i l e , u n l e s s y o u p a s s ` - d e m b e d _ o n l y _ m e t a d a t a ` . ' )
}
}