1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v2: remove duplication & slight cleanup. share prefs with v1

This commit is contained in:
joe-conigliaro
2020-02-03 22:09:17 +11:00
committed by GitHub
parent f1f8a2e4dd
commit 21b6dace8f
7 changed files with 60 additions and 171 deletions

View File

@ -7,7 +7,7 @@ import (
os
strings
filepath
v.builder
v.pref
//compiler.x64
time
)
@ -21,7 +21,7 @@ struct Parser {
// the #include directives in the parsed .v file
file_pcguard string
v &V
pref &Preferences // Preferences shared from V struct
pref &pref.Preferences // Preferences shared from V struct
mut:
scanner &Scanner
tokens []Token
@ -36,7 +36,7 @@ mut:
table &Table
import_table ImportTable // Holds imports for just the file being parsed
pass Pass
os builder.OS
os pref.OS
inside_const bool
expr_var Var
has_immutable_field bool

View File

@ -8,6 +8,7 @@ import (
os.cmdline
strings
filepath
v.pref
v.builder
)
@ -15,15 +16,6 @@ pub const (
Version = '0.1.25'
)
enum BuildMode {
// `v program.v'
// Build user code only, and add pre-compiled vlib (`cc program.o builtin.o os.o...`)
default_mode
// `v -lib ~/v/os`
// build any module (generate os.o + os.vh)
build_module
}
const (
supported_platforms = ['windows', 'mac', 'macos', 'linux', 'freebsd', 'openbsd', 'netbsd',
'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos']
@ -45,7 +37,7 @@ enum Pass {
struct V {
pub mut:
os builder.OS // the OS to build for
os pref.OS // the OS to build for
out_name_c string // name of the temporary C file
files []string // all V files that need to be parsed and compiled
dir string // directory (or file) being compiled (TODO rename to path?)
@ -53,7 +45,7 @@ pub mut:
table &Table // table with types, vars, functions etc
cgen &CGen // C code generator
//x64 &x64.Gen
pref &Preferences // all the preferences and settings extracted to a struct for reusability
pref &pref.Preferences // all the preferences and settings extracted to a struct for reusability
lang_dir string // "~/code/v"
out_name string // "program.exe"
vroot string
@ -74,59 +66,6 @@ pub mut:
v_fmt_file_result string // >> file with formatted output generated by vlib/compiler/vfmt.v
}
struct Preferences {
pub mut:
build_mode BuildMode
// nofmt bool // disable vfmt
is_test bool // `v test string_test.v`
is_script bool // single file mode (`v program.v`), main function can be skipped
is_live bool // main program that contains live/hot code
is_solive bool // a shared library, that will be used in a -live main program
is_so bool // an ordinary shared library, -shared, no matter if it is live or not
is_prof bool // benchmark every function
translated bool // `v translate doom.v` are we running V code translated from C? allow globals, ++ expressions, etc
is_prod bool // use "-O2"
is_verbose bool // print extra information with `v.log()`
obfuscate bool // `v -obf program.v`, renames functions to "f_XXX"
is_repl bool
is_run bool
show_c_cmd bool // `v -show_c_cmd` prints the C command to build program.v.c
sanitize bool // use Clang's new "-fsanitize" option
is_debug bool // false by default, turned on by -g or -cg, it tells v to pass -g to the C backend compiler.
is_vlines bool // turned on by -g, false by default (it slows down .tmp.c generation slightly).
is_keep_c bool // -keep_c , tell v to leave the generated .tmp.c alone (since by default v will delete them after c backend finishes)
// NB: passing -cg instead of -g will set is_vlines to false and is_g to true, thus making v generate cleaner C files,
// which are sometimes easier to debug / inspect manually than the .tmp.c files by plain -g (when/if v line number generation breaks).
is_pretty_c bool // -pretty_c , tell v to run clang-format -i over the produced C file, before it is compiled. Use with -keep_c or with -o x.c .
is_cache bool // turns on v usage of the module cache to speed up compilation.
is_stats bool // `v -stats file_test.v` will produce more detailed statistics for the tests that were run
no_auto_free bool // `v -nofree` disable automatic `free()` insertion for better performance in some applications (e.g. compilers)
cflags string // Additional options which will be passed to the C compiler.
// 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'.
ccompiler string // the name of the used C compiler
building_v bool
autofree bool
compress bool
// skip_builtin bool // Skips re-compilation of the builtin module
// to increase compilation time.
// This is on by default, since a vast majority of users do not
// work on the builtin module itself.
// generating_vh bool
fast bool // use tcc/x64 codegen
enable_globals bool // allow __global for low level code
// is_fmt bool
is_bare bool
user_mod_path string // `v -user_mod_path /Users/user/modules` adds a new lookup path for imported modules
vlib_path string
vpath string
x64 bool
output_cross_c bool
prealloc bool
v2 bool
}
// Should be called by main at the end of the compilation process, to cleanup
pub fn (v &V) finalize_compilation() {
// TODO remove
@ -379,7 +318,7 @@ pub fn (v mut V) compile2() {
println('all .v files:')
println(v.files)
}
mut b := builder.new_builder(v.v2_prefs())
mut b := v.new_v2()
b.build_c(v.files, v.out_name)
v.cc()
}
@ -391,23 +330,22 @@ pub fn (v mut V) compile_x64() {
}
//v.files << v.v_files_from_dir(filepath.join(v.pref.vlib_path,'builtin','bare'))
v.files << v.dir
mut b := builder.new_builder(v.v2_prefs())
v.set_module_lookup_paths()
mut b := v.new_v2()
// move all this logic to v2
b.build_x64(v.files, v.out_name)
}
// make v2 prefs from v1
fn (v &V) v2_prefs() builder.Preferences {
return builder.Preferences{
os: v.os
vpath: v.pref.vpath
vlib_path: v.pref.vlib_path
mod_path: v_modules_path
compile_dir: v.compiled_dir
user_mod_path: v.pref.user_mod_path
is_test: v.pref.is_test
is_verbose: v.pref.is_verbose,
// make v2 from v1
fn (v &V) new_v2() builder.Builder {
mut b := builder.new_builder(v.pref)
b = { b|
os: v.os,
module_path: v_modules_path,
compiled_dir: v.compiled_dir,
module_search_paths: v.module_lookup_paths
}
return b
}
fn (v mut V) generate_init() {
@ -956,7 +894,7 @@ pub fn new_v(args []string) &V {
}
// build mode
mut build_mode := BuildMode.default_mode
mut build_mode := pref.BuildMode.default_mode
mut mod := ''
joined_args := args.join(' ')
if joined_args.contains('build module ') {
@ -1014,7 +952,7 @@ pub fn new_v(args []string) &V {
}
}
}
mut _os := builder.OS.mac
mut _os := pref.OS.mac
// No OS specifed? Use current system
if target_os == '' {
$if linux {
@ -1078,7 +1016,7 @@ pub fn new_v(args []string) &V {
}
obfuscate := '-obf' in args
is_repl := '-repl' in args
pref := &Preferences{
pref := &pref.Preferences{
is_test: is_test
is_script: is_script
is_so: '-shared' in args
@ -1225,7 +1163,7 @@ pub fn cescaped_path(s string) string {
return s.replace('\\', '\\\\')
}
pub fn os_from_string(os string) builder.OS {
pub fn os_from_string(os string) pref.OS {
match os {
'linux' {
return .linux