2019-10-13 16:37:43 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module compiler
|
|
|
|
|
|
|
|
import (
|
|
|
|
os
|
2019-12-24 05:43:31 +03:00
|
|
|
os.cmdline
|
2019-10-13 16:37:43 +03:00
|
|
|
strings
|
2019-11-01 19:29:51 +03:00
|
|
|
filepath
|
2019-11-19 09:53:52 +03:00
|
|
|
compiler.x64
|
2019-10-13 16:37:43 +03:00
|
|
|
)
|
|
|
|
|
2019-10-24 12:36:57 +03:00
|
|
|
pub const (
|
2019-11-29 20:00:33 +03:00
|
|
|
Version = '0.1.23'
|
2019-10-13 16:37:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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 (
|
2019-12-20 00:29:37 +03:00
|
|
|
supported_platforms = ['windows', 'mac', 'macos', 'linux', 'freebsd', 'openbsd', 'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku']
|
2019-10-13 16:37:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
enum OS {
|
|
|
|
mac
|
|
|
|
linux
|
|
|
|
windows
|
|
|
|
freebsd
|
|
|
|
openbsd
|
|
|
|
netbsd
|
|
|
|
dragonfly
|
2019-12-20 00:29:37 +03:00
|
|
|
js // TODO
|
2019-10-13 16:37:43 +03:00
|
|
|
android
|
|
|
|
solaris
|
2019-11-24 19:39:04 +03:00
|
|
|
haiku
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Pass {
|
|
|
|
// A very short pass that only looks at imports in the beginning of
|
|
|
|
// each file
|
|
|
|
imports
|
|
|
|
// First pass, only parses and saves declarations (fn signatures,
|
|
|
|
// consts, types).
|
|
|
|
// Skips function bodies.
|
|
|
|
// We need this because in V things can be used before they are
|
|
|
|
// declared.
|
|
|
|
decl
|
|
|
|
// Second pass, parses function bodies and generates C or machine code.
|
|
|
|
main
|
|
|
|
}
|
|
|
|
|
|
|
|
struct V {
|
|
|
|
pub mut:
|
2019-12-20 00:29:37 +03:00
|
|
|
os 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?)
|
|
|
|
compiled_dir string // contains os.realpath() of the dir of the final file beeing compiled, or the dir itself when doing `v .`
|
|
|
|
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
|
|
|
|
lang_dir string // "~/code/v"
|
|
|
|
out_name string // "program.exe"
|
|
|
|
vroot string
|
|
|
|
mod string // module being built with -lib
|
|
|
|
parsers []Parser // file parsers
|
|
|
|
vgen_buf strings.Builder // temporary buffer for generated V code (.str() etc)
|
|
|
|
file_parser_idx map[string]int // map absolute file path to v.parsers index
|
|
|
|
gen_parser_idx map[string]int
|
|
|
|
cached_mods []string
|
2019-12-13 19:28:39 +03:00
|
|
|
module_lookup_paths []string
|
2019-12-23 13:09:00 +03: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']
|
2019-12-24 05:43:31 +03:00
|
|
|
|
|
|
|
v_fmt_all bool // << input set by tools/vfmt.v
|
|
|
|
v_fmt_file string // << file given by the user from tools/vfmt.v
|
|
|
|
v_fmt_file_result string // >> file with formatted output generated by vlib/compiler/vfmt.v
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Preferences {
|
|
|
|
pub mut:
|
2019-12-20 00:29:37 +03:00
|
|
|
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)
|
2019-10-13 16:37:43 +03:00
|
|
|
// 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).
|
2019-12-20 00:29:37 +03:00
|
|
|
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
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should be called by main at the end of the compilation process, to cleanup
|
2019-12-20 00:29:37 +03:00
|
|
|
pub fn (v &V) finalize_compilation() {
|
2019-10-13 16:37:43 +03:00
|
|
|
// TODO remove
|
|
|
|
if v.pref.autofree {
|
2019-10-20 19:59:53 +03:00
|
|
|
/*
|
2019-10-13 16:37:43 +03:00
|
|
|
println('started freeing v struct')
|
|
|
|
v.table.typesmap.free()
|
|
|
|
v.table.obf_ids.free()
|
|
|
|
v.cgen.lines.free()
|
|
|
|
free(v.cgen)
|
|
|
|
for _, f in v.table.fns {
|
|
|
|
//f.local_vars.free()
|
|
|
|
f.args.free()
|
|
|
|
//f.defer_text.free()
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
v.table.fns.free()
|
|
|
|
free(v.table)
|
2019-10-20 10:19:37 +03:00
|
|
|
//for p in parsers {}
|
2019-10-13 16:37:43 +03:00
|
|
|
println('done!')
|
2019-10-20 19:59:53 +03:00
|
|
|
*/
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
|
2019-10-25 16:34:12 +03:00
|
|
|
pub fn (v mut V) add_parser(parser Parser) int {
|
2019-12-21 03:53:58 +03:00
|
|
|
pidx := v.parsers.len
|
2019-12-20 00:29:37 +03:00
|
|
|
v.parsers << parser
|
2019-12-22 00:54:37 +03:00
|
|
|
file_path := if filepath.is_abs(parser.file_path) { parser.file_path } else { os.realpath(parser.file_path) }
|
2019-12-21 03:53:58 +03:00
|
|
|
v.file_parser_idx[file_path] = pidx
|
2019-12-20 00:29:37 +03:00
|
|
|
return pidx
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (v &V) get_file_parser_index(file string) ?int {
|
2019-12-22 00:54:37 +03:00
|
|
|
file_path := if filepath.is_abs(file) { file } else { os.realpath(file) }
|
2019-10-25 16:34:12 +03:00
|
|
|
if file_path in v.file_parser_idx {
|
|
|
|
return v.file_parser_idx[file_path]
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
return error('parser for "$file" not found')
|
|
|
|
}
|
|
|
|
|
|
|
|
// find existing parser or create new one. returns v.parsers index
|
|
|
|
pub fn (v mut V) parse(file string, pass Pass) int {
|
2019-12-20 00:29:37 +03:00
|
|
|
// println('parse($file, $pass)')
|
2019-10-13 16:37:43 +03:00
|
|
|
pidx := v.get_file_parser_index(file) or {
|
|
|
|
mut p := v.new_parser_from_file(file)
|
|
|
|
p.parse(pass)
|
2019-12-20 00:29:37 +03:00
|
|
|
// if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
|
2019-10-25 16:34:12 +03:00
|
|
|
return v.add_parser(p)
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
2019-10-25 16:34:12 +03:00
|
|
|
// println('matched ' + v.parsers[pidx].file_path + ' with $file')
|
2019-10-13 16:37:43 +03:00
|
|
|
v.parsers[pidx].parse(pass)
|
2019-12-20 00:29:37 +03:00
|
|
|
// if v.parsers[i].pref.autofree { v.parsers[i].scanner.text.free() free(v.parsers[i].scanner) }
|
2019-10-13 16:37:43 +03:00
|
|
|
return pidx
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (v mut V) compile() {
|
|
|
|
// Emily: Stop people on linux from being able to build with msvc
|
2019-10-14 10:36:29 +03:00
|
|
|
if os.user_os() != 'windows' && v.pref.ccompiler == 'msvc' {
|
2019-10-13 16:37:43 +03:00
|
|
|
verror('Cannot build with msvc on ${os.user_os()}')
|
|
|
|
}
|
|
|
|
mut cgen := v.cgen
|
|
|
|
cgen.genln('// Generated by V')
|
|
|
|
if v.pref.is_verbose {
|
|
|
|
println('all .v files before:')
|
|
|
|
println(v.files)
|
|
|
|
}
|
|
|
|
v.add_v_files_to_compile()
|
2019-12-11 18:41:25 +03:00
|
|
|
if v.pref.is_verbose {
|
2019-10-13 16:37:43 +03:00
|
|
|
println('all .v files:')
|
|
|
|
println(v.files)
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
if v.pref.is_debug {
|
|
|
|
println('\nparsers:')
|
|
|
|
for q in v.parsers {
|
|
|
|
println(q.file_name)
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
println('\nfiles:')
|
|
|
|
for q in v.files {
|
|
|
|
println(q)
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
*/
|
2019-12-20 00:29:37 +03:00
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
// First pass (declarations)
|
|
|
|
for file in v.files {
|
|
|
|
v.parse(file, .decl)
|
|
|
|
}
|
|
|
|
// Main pass
|
2019-10-30 15:04:25 +03:00
|
|
|
cgen.pass = .main
|
2019-10-13 16:37:43 +03:00
|
|
|
if v.pref.is_debug {
|
|
|
|
$if js {
|
|
|
|
cgen.genln('const VDEBUG = 1;\n')
|
2019-12-20 00:29:37 +03:00
|
|
|
} $else {
|
2019-10-13 16:37:43 +03:00
|
|
|
cgen.genln('#define VDEBUG (1)')
|
|
|
|
}
|
|
|
|
}
|
2019-12-14 02:46:55 +03:00
|
|
|
if v.pref.prealloc {
|
|
|
|
cgen.genln('#define VPREALLOC (1)')
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
if v.os == .js {
|
|
|
|
cgen.genln('#define _VJS (1) ')
|
|
|
|
}
|
2019-10-21 14:14:28 +03:00
|
|
|
v_hash := vhash()
|
|
|
|
$if js {
|
|
|
|
cgen.genln('const V_COMMIT_HASH = "$v_hash";\n')
|
|
|
|
} $else {
|
2019-10-13 16:37:43 +03:00
|
|
|
cgen.genln('#ifndef V_COMMIT_HASH')
|
2019-10-21 14:14:28 +03:00
|
|
|
cgen.genln('#define V_COMMIT_HASH "$v_hash"')
|
2019-10-13 16:37:43 +03:00
|
|
|
cgen.genln('#endif')
|
|
|
|
}
|
|
|
|
q := cgen.nogen // TODO hack
|
|
|
|
cgen.nogen = false
|
|
|
|
$if js {
|
|
|
|
cgen.genln(js_headers)
|
|
|
|
} $else {
|
2019-11-16 11:10:38 +03:00
|
|
|
if !v.pref.is_bare {
|
2019-12-20 00:29:37 +03:00
|
|
|
cgen.genln('#include <inttypes.h>') // int64_t etc
|
|
|
|
}
|
|
|
|
else {
|
2019-11-16 11:10:38 +03:00
|
|
|
cgen.genln('#include <stdint.h>')
|
2019-11-29 20:00:33 +03:00
|
|
|
}
|
2019-12-23 13:09:00 +03:00
|
|
|
|
|
|
|
if v.compile_defines_all.len > 0 {
|
|
|
|
cgen.genln('')
|
|
|
|
cgen.genln('// All custom defines : ' + v.compile_defines_all.join(','))
|
|
|
|
cgen.genln('// Turned ON custom defines: ' + v.compile_defines.join(','))
|
|
|
|
for cdefine in v.compile_defines {
|
|
|
|
cgen.genln('#define CUSTOM_DEFINE_${cdefine}')
|
|
|
|
}
|
|
|
|
cgen.genln('//')
|
|
|
|
cgen.genln('')
|
|
|
|
}
|
|
|
|
|
2019-11-14 10:23:44 +03:00
|
|
|
cgen.genln(c_builtin_types)
|
2019-12-23 13:09:00 +03:00
|
|
|
|
2019-11-14 10:23:44 +03:00
|
|
|
if !v.pref.is_bare {
|
|
|
|
cgen.genln(c_headers)
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-12-01 11:27:36 +03:00
|
|
|
cgen.genln(bare_c_headers)
|
2019-11-14 10:23:44 +03:00
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
v.generate_hotcode_reloading_declarations()
|
|
|
|
// We need the cjson header for all the json decoding that will be done in
|
|
|
|
// default mode
|
|
|
|
imports_json := 'json' in v.table.imports
|
|
|
|
if v.pref.build_mode == .default_mode {
|
|
|
|
if imports_json {
|
|
|
|
cgen.genln('#include "cJSON.h"')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v.pref.build_mode == .default_mode {
|
|
|
|
// If we declare these for all modes, then when running `v a.v` we'll get
|
|
|
|
// `/usr/bin/ld: multiple definition of 'total_m'`
|
|
|
|
$if !js {
|
|
|
|
cgen.genln('int g_test_oks = 0;')
|
|
|
|
cgen.genln('int g_test_fails = 0;')
|
|
|
|
}
|
|
|
|
if imports_json {
|
|
|
|
cgen.genln('
|
|
|
|
#define js_get(object, key) cJSON_GetObjectItemCaseSensitive((object), (key))
|
|
|
|
')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if '-debug_alloc' in os.args {
|
|
|
|
cgen.genln('#define DEBUG_ALLOC 1')
|
|
|
|
}
|
2019-12-05 14:40:14 +03:00
|
|
|
if v.pref.is_live && v.os != .windows {
|
|
|
|
cgen.includes << '#include <dlfcn.h>'
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
// cgen.genln('/*================================== FNS =================================*/')
|
2019-10-22 08:02:31 +03:00
|
|
|
cgen.genln('// this line will be replaced with definitions')
|
2019-10-13 16:37:43 +03:00
|
|
|
mut defs_pos := cgen.lines.len - 1
|
|
|
|
if defs_pos == -1 {
|
|
|
|
defs_pos = 0
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
cgen.nogen = q
|
2019-11-11 17:18:32 +03:00
|
|
|
for i, file in v.files {
|
2019-10-13 16:37:43 +03:00
|
|
|
v.parse(file, .main)
|
2019-12-20 00:29:37 +03:00
|
|
|
// if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
|
2019-10-13 16:37:43 +03:00
|
|
|
// Format all files (don't format automatically generated vlib headers)
|
2019-12-20 00:29:37 +03:00
|
|
|
// if !v.pref.nofmt && !file.contains('/vlib/') {
|
|
|
|
// new vfmt is not ready yet
|
|
|
|
// }
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
2019-11-22 08:22:11 +03:00
|
|
|
// add parser generated V code (str() methods etc)
|
2019-10-25 16:34:12 +03:00
|
|
|
mut vgen_parser := v.new_parser_from_string(v.vgen_buf.str())
|
2019-10-13 16:37:43 +03:00
|
|
|
// free the string builder which held the generated methods
|
|
|
|
v.vgen_buf.free()
|
2019-11-22 08:22:11 +03:00
|
|
|
vgen_parser.is_vgen = true
|
2019-11-29 16:46:43 +03:00
|
|
|
// v.add_parser(vgen_parser)
|
|
|
|
vgen_parser.parse(.main)
|
2019-11-22 08:22:11 +03:00
|
|
|
// Generate .vh if we are building a module
|
|
|
|
if v.pref.build_mode == .build_module {
|
|
|
|
generate_vh(v.dir)
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
// All definitions
|
2019-12-20 00:29:37 +03:00
|
|
|
mut def := strings.new_builder(10000) // Avoid unnecessary allocations
|
2019-12-06 02:38:35 +03:00
|
|
|
def.writeln(cgen.const_defines.join_lines())
|
2019-10-13 16:37:43 +03:00
|
|
|
$if !js {
|
|
|
|
def.writeln(cgen.includes.join_lines())
|
|
|
|
def.writeln(cgen.typedefs.join_lines())
|
|
|
|
def.writeln(v.type_definitions())
|
2019-11-14 10:23:44 +03:00
|
|
|
if !v.pref.is_bare {
|
|
|
|
def.writeln('\nstring _STR(const char*, ...);\n')
|
|
|
|
def.writeln('\nstring _STR_TMP(const char*, ...);\n')
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
def.writeln(cgen.fns.join_lines()) // fn definitions
|
2019-11-08 06:03:06 +03:00
|
|
|
def.writeln(v.interface_table())
|
2019-10-13 16:37:43 +03:00
|
|
|
} $else {
|
|
|
|
def.writeln(v.type_definitions())
|
|
|
|
}
|
|
|
|
def.writeln(cgen.consts.join_lines())
|
|
|
|
def.writeln(cgen.thread_args.join_lines())
|
|
|
|
if v.pref.is_prof {
|
|
|
|
def.writeln('; // Prof counters:')
|
|
|
|
def.writeln(v.prof_counters())
|
|
|
|
}
|
|
|
|
cgen.lines[defs_pos] = def.str()
|
|
|
|
v.generate_init()
|
|
|
|
v.generate_main()
|
|
|
|
v.generate_hot_reload_code()
|
|
|
|
if v.pref.is_verbose {
|
|
|
|
v.log('flags=')
|
|
|
|
for flag in v.get_os_cflags() {
|
|
|
|
println(' * ' + flag.format())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$if js {
|
|
|
|
cgen.genln('main__main();')
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
cgen.save()
|
|
|
|
v.cc()
|
|
|
|
}
|
|
|
|
|
2019-11-22 18:33:02 +03:00
|
|
|
pub fn (v mut V) compile_x64() {
|
|
|
|
$if !linux {
|
|
|
|
println('v -x64 can only generate Linux binaries for now')
|
2019-12-20 00:29:37 +03:00
|
|
|
println('You are not on a Linux system, so you will not ' + 'be able to run the resulting executable')
|
2019-11-29 20:00:33 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
v.files << v.v_files_from_dir(filepath.join(v.pref.vlib_path,'builtin','bare'))
|
2019-11-22 18:33:02 +03:00
|
|
|
v.files << v.dir
|
|
|
|
v.x64.generate_elf_header()
|
|
|
|
for f in v.files {
|
|
|
|
v.parse(f, .decl)
|
|
|
|
}
|
|
|
|
for f in v.files {
|
|
|
|
v.parse(f, .main)
|
|
|
|
}
|
|
|
|
v.x64.generate_elf_footer()
|
2019-11-29 20:00:33 +03:00
|
|
|
}
|
2019-11-22 18:33:02 +03:00
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
fn (v mut V) generate_init() {
|
2019-12-20 00:29:37 +03:00
|
|
|
$if js {
|
|
|
|
return
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
if v.pref.build_mode == .build_module {
|
|
|
|
nogen := v.cgen.nogen
|
|
|
|
v.cgen.nogen = false
|
|
|
|
consts_init_body := v.cgen.consts_init.join_lines()
|
|
|
|
init_fn_name := mod_gen_name(v.mod) + '__init_consts'
|
|
|
|
v.cgen.genln('void ${init_fn_name}();\nvoid ${init_fn_name}() {\n$consts_init_body\n}')
|
|
|
|
v.cgen.nogen = nogen
|
|
|
|
}
|
|
|
|
if v.pref.build_mode == .default_mode {
|
|
|
|
mut call_mod_init := ''
|
|
|
|
mut call_mod_init_consts := ''
|
|
|
|
if 'builtin' in v.cached_mods {
|
|
|
|
v.cgen.genln('void builtin__init_consts();')
|
|
|
|
call_mod_init_consts += 'builtin__init_consts();\n'
|
|
|
|
}
|
|
|
|
for mod in v.table.imports {
|
|
|
|
init_fn_name := mod_gen_name(mod) + '__init'
|
|
|
|
if v.table.known_fn(init_fn_name) {
|
|
|
|
call_mod_init += '${init_fn_name}();\n'
|
|
|
|
}
|
|
|
|
if mod in v.cached_mods {
|
|
|
|
v.cgen.genln('void ${init_fn_name}_consts();')
|
|
|
|
call_mod_init_consts += '${init_fn_name}_consts();\n'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
consts_init_body := v.cgen.consts_init.join_lines()
|
2019-12-20 00:29:37 +03:00
|
|
|
if v.pref.is_bare {
|
|
|
|
// vlib can't have init_consts()
|
|
|
|
v.cgen.genln('
|
2019-12-04 01:40:26 +03:00
|
|
|
void init() {
|
|
|
|
$call_mod_init_consts
|
|
|
|
$consts_init_body
|
|
|
|
builtin__init();
|
|
|
|
$call_mod_init
|
|
|
|
}
|
|
|
|
')
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
2019-11-14 10:23:44 +03:00
|
|
|
if !v.pref.is_bare {
|
2019-12-20 00:29:37 +03:00
|
|
|
// vlib can't have `init_consts()`
|
|
|
|
v.cgen.genln('void init() {
|
2019-10-13 16:37:43 +03:00
|
|
|
g_str_buf=malloc(1000);
|
2019-12-14 02:28:15 +03:00
|
|
|
#if VPREALLOC
|
2019-12-11 23:58:51 +03:00
|
|
|
g_m2_buf = malloc(50 * 1000 * 1000);
|
|
|
|
g_m2_ptr = g_m2_buf;
|
2019-12-11 18:41:25 +03:00
|
|
|
puts("allocated 50 mb");
|
|
|
|
#endif
|
2019-10-13 16:37:43 +03:00
|
|
|
$call_mod_init_consts
|
|
|
|
$consts_init_body
|
|
|
|
builtin__init();
|
|
|
|
$call_mod_init
|
|
|
|
}')
|
2019-12-20 00:29:37 +03:00
|
|
|
// _STR function can't be defined in vlib
|
|
|
|
v.cgen.genln('
|
2019-10-13 16:37:43 +03:00
|
|
|
string _STR(const char *fmt, ...) {
|
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, fmt);
|
|
|
|
size_t len = vsnprintf(0, 0, fmt, argptr) + 1;
|
|
|
|
va_end(argptr);
|
|
|
|
byte* buf = malloc(len);
|
|
|
|
va_start(argptr, fmt);
|
|
|
|
vsprintf((char *)buf, fmt, argptr);
|
|
|
|
va_end(argptr);
|
|
|
|
#ifdef DEBUG_ALLOC
|
|
|
|
puts("_STR:");
|
|
|
|
puts(buf);
|
|
|
|
#endif
|
|
|
|
return tos2(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
string _STR_TMP(const char *fmt, ...) {
|
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, fmt);
|
|
|
|
//size_t len = vsnprintf(0, 0, fmt, argptr) + 1;
|
|
|
|
va_end(argptr);
|
|
|
|
va_start(argptr, fmt);
|
|
|
|
vsprintf((char *)g_str_buf, fmt, argptr);
|
|
|
|
va_end(argptr);
|
|
|
|
#ifdef DEBUG_ALLOC
|
|
|
|
//puts("_STR_TMP:");
|
|
|
|
//puts(g_str_buf);
|
|
|
|
#endif
|
|
|
|
return tos2(g_str_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
')
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
2019-11-14 10:23:44 +03:00
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (v mut V) generate_main() {
|
|
|
|
mut cgen := v.cgen
|
2019-12-20 00:29:37 +03:00
|
|
|
$if js {
|
|
|
|
return
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
if v.pref.is_vlines {
|
2019-11-14 10:23:44 +03:00
|
|
|
// After this point, the v files are compiled.
|
|
|
|
// The rest is auto generated code, which will not have
|
|
|
|
// different .v source file/line numbers.
|
2019-10-13 16:37:43 +03:00
|
|
|
lines_so_far := cgen.lines.join('\n').count('\n') + 5
|
|
|
|
cgen.genln('')
|
2019-11-14 10:23:44 +03:00
|
|
|
cgen.genln('// Reset the file/line numbers')
|
2019-10-13 16:37:43 +03:00
|
|
|
cgen.lines << '#line $lines_so_far "${cescaped_path(os.realpath(cgen.out_path))}"'
|
|
|
|
cgen.genln('')
|
|
|
|
}
|
|
|
|
// Make sure the main function exists
|
|
|
|
// Obviously we don't need it in libraries
|
|
|
|
if v.pref.build_mode != .build_module {
|
|
|
|
if !v.table.main_exists() && !v.pref.is_test {
|
|
|
|
// It can be skipped in single file programs
|
2019-11-18 00:40:59 +03:00
|
|
|
// But make sure that there's some code outside of main()
|
2019-11-18 04:53:46 +03:00
|
|
|
if (v.pref.is_script && cgen.fn_main.trim_space() != '') || v.pref.is_repl {
|
2019-12-20 00:29:37 +03:00
|
|
|
// println('Generating main()...')
|
2019-10-13 16:37:43 +03:00
|
|
|
v.gen_main_start(true)
|
|
|
|
cgen.genln('$cgen.fn_main;')
|
|
|
|
v.gen_main_end('return 0')
|
|
|
|
}
|
2019-11-18 02:34:46 +03:00
|
|
|
else if !v.pref.is_repl {
|
2019-10-13 16:37:43 +03:00
|
|
|
verror('function `main` is not declared in the main module')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if v.pref.is_test {
|
|
|
|
if v.table.main_exists() {
|
|
|
|
verror('test files cannot have function `main`')
|
|
|
|
}
|
2019-12-14 15:57:28 +03:00
|
|
|
test_fn_names := v.table.all_test_function_names()
|
|
|
|
if test_fn_names.len == 0 {
|
2019-10-13 16:37:43 +03:00
|
|
|
verror('test files need to have at least one test function')
|
|
|
|
}
|
|
|
|
// Generate a C `main`, which calls every single test function
|
|
|
|
v.gen_main_start(false)
|
2019-11-18 02:34:46 +03:00
|
|
|
if v.pref.is_stats {
|
2019-12-09 17:25:56 +03:00
|
|
|
cgen.genln('BenchedTests bt = main__start_testing();')
|
2019-11-18 02:34:46 +03:00
|
|
|
}
|
2019-12-14 15:57:28 +03:00
|
|
|
for tfname in test_fn_names {
|
2019-12-20 00:29:37 +03:00
|
|
|
if v.pref.is_stats {
|
|
|
|
cgen.genln('BenchedTests_testing_step_start(&bt, tos3("$tfname"));')
|
|
|
|
}
|
2019-12-14 15:57:28 +03:00
|
|
|
cgen.genln('$tfname ();')
|
2019-12-20 00:29:37 +03:00
|
|
|
if v.pref.is_stats {
|
|
|
|
cgen.genln('BenchedTests_testing_step_end(&bt);')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v.pref.is_stats {
|
|
|
|
cgen.genln('BenchedTests_end_testing(&bt);')
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
v.gen_main_end('return g_test_fails > 0')
|
|
|
|
}
|
|
|
|
else if v.table.main_exists() {
|
|
|
|
v.gen_main_start(true)
|
|
|
|
cgen.genln(' main__main();')
|
2019-11-26 13:54:41 +03:00
|
|
|
if !v.pref.is_bare {
|
|
|
|
cgen.genln('free(g_str_buf);')
|
2019-12-14 02:28:15 +03:00
|
|
|
cgen.genln('#if VPREALLOC')
|
2019-12-11 23:58:51 +03:00
|
|
|
cgen.genln('free(g_m2_buf);')
|
|
|
|
cgen.genln('puts("freed mem buf");')
|
|
|
|
cgen.genln('#endif')
|
2019-11-26 13:54:41 +03:00
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
v.gen_main_end('return 0')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 00:29:37 +03:00
|
|
|
pub fn (v mut V) gen_main_start(add_os_args bool) {
|
2019-10-13 16:37:43 +03:00
|
|
|
v.cgen.genln('int main(int argc, char** argv) { ')
|
2019-12-04 01:40:26 +03:00
|
|
|
v.cgen.genln(' init();')
|
2019-10-13 16:37:43 +03:00
|
|
|
if add_os_args && 'os' in v.table.imports {
|
|
|
|
v.cgen.genln(' os__args = os__init_os_args(argc, (byteptr*)argv);')
|
|
|
|
}
|
|
|
|
v.generate_hotcode_reloading_main_caller()
|
|
|
|
v.cgen.genln('')
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
|
|
|
|
pub fn (v mut V) gen_main_end(return_statement string) {
|
2019-10-13 16:37:43 +03:00
|
|
|
v.cgen.genln('')
|
|
|
|
v.cgen.genln(' $return_statement;')
|
|
|
|
v.cgen.genln('}')
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn final_target_out_name(out_name string) string {
|
|
|
|
$if windows {
|
|
|
|
return out_name.replace('/', '\\') + '.exe'
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
return if out_name.starts_with('/') { out_name } else { './' + out_name }
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (v V) run_compiled_executable_and_exit() {
|
2019-10-21 14:14:28 +03:00
|
|
|
args := env_vflags_and_os_args()
|
2019-10-13 16:37:43 +03:00
|
|
|
if v.pref.is_verbose {
|
|
|
|
println('============ running $v.out_name ============')
|
2019-10-20 10:19:37 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
mut cmd := '"' + final_target_out_name(v.out_name).replace('.exe', '') + '"'
|
2019-12-24 05:43:31 +03:00
|
|
|
args_after_no_options := cmdline.only_non_options( cmdline.after(args,['run','test']) )
|
|
|
|
if args_after_no_options.len > 1 {
|
|
|
|
cmd += ' ' + args_after_no_options[1..].join(' ')
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
if v.pref.is_test {
|
|
|
|
ret := os.system(cmd)
|
|
|
|
if ret != 0 {
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v.pref.is_run {
|
|
|
|
ret := os.system(cmd)
|
|
|
|
// TODO: make the runner wrapping as transparent as possible
|
|
|
|
// (i.e. use execve when implemented). For now though, the runner
|
|
|
|
// just returns the same exit code as the child process.
|
2019-12-20 00:29:37 +03:00
|
|
|
exit(ret)
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (v &V) v_files_from_dir(dir string) []string {
|
|
|
|
mut res := []string
|
2019-12-04 23:03:12 +03:00
|
|
|
if !os.exists(dir) {
|
|
|
|
if dir == 'compiler' && os.is_dir('vlib') {
|
2019-10-18 09:56:54 +03:00
|
|
|
println('looks like you are trying to build V with an old command')
|
2019-11-05 01:32:21 +03:00
|
|
|
println('use `v -o v v.v` instead of `v -o v compiler`')
|
2019-11-29 20:00:33 +03:00
|
|
|
}
|
2019-10-18 09:56:54 +03:00
|
|
|
verror("$dir doesn't exist")
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
|
|
|
else if !os.is_dir(dir) {
|
2019-10-14 05:18:48 +03:00
|
|
|
verror("$dir isn't a directory")
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
mut files := os.ls(dir)or{
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
if v.pref.is_verbose {
|
|
|
|
println('v_files_from_dir ("$dir")')
|
|
|
|
}
|
|
|
|
files.sort()
|
|
|
|
for file in files {
|
|
|
|
if !file.ends_with('.v') && !file.ends_with('.vh') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if file.ends_with('_test.v') {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-23 12:25:00 +03:00
|
|
|
if (file.ends_with('_win.v') || file.ends_with('_windows.v')) && v.os != .windows {
|
2019-10-13 16:37:43 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-10-23 12:25:00 +03:00
|
|
|
if (file.ends_with('_lin.v') || file.ends_with('_linux.v')) && v.os != .linux {
|
2019-10-13 16:37:43 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-10-23 12:25:00 +03:00
|
|
|
if (file.ends_with('_mac.v') || file.ends_with('_darwin.v')) && v.os != .mac {
|
2019-10-13 16:37:43 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-10-14 10:36:29 +03:00
|
|
|
if file.ends_with('_nix.v') && v.os == .windows {
|
2019-10-13 16:37:43 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if file.ends_with('_js.v') && v.os != .js {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if file.ends_with('_c.v') && v.os == .js {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-23 13:09:00 +03:00
|
|
|
if v.compile_defines_all.len > 0 && file.contains('_d_') {
|
|
|
|
mut allowed := false
|
|
|
|
for cdefine in v.compile_defines {
|
|
|
|
file_postfix := '_d_${cdefine}.v'
|
|
|
|
if file.ends_with(file_postfix) {
|
|
|
|
allowed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !allowed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 19:28:39 +03:00
|
|
|
res << filepath.join(dir,file)
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses imports, adds necessary libs, and then user files
|
|
|
|
pub fn (v mut V) add_v_files_to_compile() {
|
2019-12-13 19:28:39 +03:00
|
|
|
v.set_module_lookup_paths()
|
2019-10-13 16:37:43 +03:00
|
|
|
mut builtin_files := v.get_builtin_files()
|
2019-11-14 07:15:17 +03:00
|
|
|
if v.pref.is_bare {
|
2019-12-20 00:29:37 +03:00
|
|
|
// builtin_files = []
|
2019-11-29 20:00:33 +03:00
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
// Builtin cache exists? Use it.
|
2019-12-13 19:28:39 +03:00
|
|
|
if v.pref.is_cache {
|
|
|
|
builtin_vh := filepath.join(v_modules_path,'vlib','builtin.vh')
|
|
|
|
if os.exists(builtin_vh) {
|
|
|
|
v.cached_mods << 'builtin'
|
|
|
|
builtin_files = [builtin_vh]
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
if v.pref.is_verbose {
|
|
|
|
v.log('v.add_v_files_to_compile > builtin_files: $builtin_files')
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
// Parse builtin imports
|
|
|
|
for file in builtin_files {
|
|
|
|
// add builtins first
|
|
|
|
v.files << file
|
|
|
|
mut p := v.new_parser_from_file(file)
|
|
|
|
p.parse(.imports)
|
2019-12-20 00:29:37 +03:00
|
|
|
// if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
|
2019-10-13 16:37:43 +03:00
|
|
|
v.add_parser(p)
|
|
|
|
}
|
|
|
|
// Parse user imports
|
|
|
|
for file in v.get_user_files() {
|
|
|
|
mut p := v.new_parser_from_file(file)
|
|
|
|
p.parse(.imports)
|
2019-10-15 18:08:46 +03:00
|
|
|
if p.v_script {
|
|
|
|
v.log('imports0:')
|
|
|
|
println(v.table.imports)
|
|
|
|
println(v.files)
|
2019-10-25 16:34:12 +03:00
|
|
|
p.register_import('os', 0)
|
2019-10-15 18:08:46 +03:00
|
|
|
p.table.imports << 'os'
|
|
|
|
p.table.register_module('os')
|
2019-11-29 20:00:33 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
// if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
|
2019-10-13 16:37:43 +03:00
|
|
|
v.add_parser(p)
|
|
|
|
}
|
|
|
|
// Parse lib imports
|
|
|
|
v.parse_lib_imports()
|
|
|
|
if v.pref.is_verbose {
|
|
|
|
v.log('imports:')
|
|
|
|
println(v.table.imports)
|
|
|
|
}
|
|
|
|
// resolve deps and add imports in correct order
|
|
|
|
imported_mods := v.resolve_deps().imports()
|
|
|
|
for mod in imported_mods {
|
|
|
|
if mod == 'builtin' || mod == 'main' {
|
|
|
|
// builtin already added
|
|
|
|
// main files will get added last
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// use cached built module if exists
|
2019-11-21 15:06:19 +03:00
|
|
|
if v.pref.vpath != '' && v.pref.build_mode != .build_module && !mod.contains('vweb') {
|
2019-10-13 16:37:43 +03:00
|
|
|
mod_path := mod.replace('.', os.path_separator)
|
|
|
|
vh_path := '$v_modules_path${os.path_separator}vlib${os.path_separator}${mod_path}.vh'
|
2019-12-04 23:03:12 +03:00
|
|
|
if v.pref.is_cache && os.exists(vh_path) {
|
2019-10-18 06:16:33 +03:00
|
|
|
eprintln('using cached module `$mod`: $vh_path')
|
2019-10-13 16:37:43 +03:00
|
|
|
v.cached_mods << mod
|
|
|
|
v.files << vh_path
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// standard module
|
2019-10-14 04:07:59 +03:00
|
|
|
vfiles := v.get_imported_module_files(mod)
|
2019-10-13 16:37:43 +03:00
|
|
|
for file in vfiles {
|
|
|
|
v.files << file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add remaining main files last
|
2019-10-25 16:34:12 +03:00
|
|
|
for p in v.parsers {
|
2019-12-20 00:29:37 +03:00
|
|
|
if p.mod != 'main' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if p.is_vgen {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-25 16:34:12 +03:00
|
|
|
v.files << p.file_path
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (v &V) get_builtin_files() []string {
|
|
|
|
// .vh cache exists? Use it
|
2019-11-15 02:04:40 +03:00
|
|
|
if v.pref.is_bare {
|
2019-12-20 00:29:37 +03:00
|
|
|
return v.v_files_from_dir(filepath.join(v.pref.vlib_path,'builtin','bare'))
|
2019-11-15 02:04:40 +03:00
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
$if js {
|
2019-12-20 00:29:37 +03:00
|
|
|
return v.v_files_from_dir(filepath.join(v.pref.vlib_path,'builtin','js'))
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
return v.v_files_from_dir(filepath.join(v.pref.vlib_path,'builtin'))
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get user files
|
2019-12-20 00:29:37 +03:00
|
|
|
pub fn (v &V) get_user_files() []string {
|
2019-10-13 16:37:43 +03:00
|
|
|
mut dir := v.dir
|
|
|
|
v.log('get_v_files($dir)')
|
2019-10-15 18:08:46 +03:00
|
|
|
// Need to store user files separately, because they have to be added after
|
|
|
|
// libs, but we dont know which libs need to be added yet
|
2019-10-13 16:37:43 +03:00
|
|
|
mut user_files := []string
|
2019-12-09 13:48:41 +03:00
|
|
|
preludes_path := filepath.join(v.pref.vlib_path,'compiler','preludes')
|
|
|
|
if v.pref.is_live {
|
|
|
|
user_files << filepath.join(preludes_path,'live_main.v')
|
|
|
|
}
|
|
|
|
if v.pref.is_solive {
|
|
|
|
user_files << filepath.join(preludes_path,'live_shared.v')
|
2019-10-30 12:15:33 +03:00
|
|
|
}
|
2019-12-09 13:48:41 +03:00
|
|
|
if v.pref.is_test {
|
|
|
|
user_files << filepath.join(preludes_path,'tests_assertions.v')
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
if v.pref.is_test && v.pref.is_stats {
|
2019-12-09 13:48:41 +03:00
|
|
|
user_files << filepath.join(preludes_path,'tests_with_stats.v')
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
2019-12-14 15:57:28 +03:00
|
|
|
is_test := dir.ends_with('_test.v')
|
|
|
|
mut is_internal_module_test := false
|
|
|
|
if is_test {
|
2019-12-20 00:29:37 +03:00
|
|
|
tcontent := os.read_file(dir)or{
|
|
|
|
panic('$dir does not exist')
|
|
|
|
}
|
2019-12-14 15:57:28 +03:00
|
|
|
if tcontent.contains('module ') && !tcontent.contains('module main') {
|
|
|
|
is_internal_module_test = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_internal_module_test {
|
|
|
|
// v volt/slack_test.v: compile all .v files to get the environment
|
|
|
|
single_test_v_file := os.realpath(dir)
|
|
|
|
if v.pref.is_verbose {
|
|
|
|
v.log('> Compiling an internal module _test.v file $single_test_v_file .')
|
|
|
|
v.log('> That brings in all other ordinary .v files in the same module too .')
|
2019-12-12 21:44:52 +03:00
|
|
|
}
|
2019-12-14 15:57:28 +03:00
|
|
|
user_files << single_test_v_file
|
2019-12-23 13:09:22 +03:00
|
|
|
dir = filepath.basedir(single_test_v_file)
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
2019-10-15 18:08:46 +03:00
|
|
|
if dir.ends_with('.v') || dir.ends_with('.vsh') {
|
2019-12-13 19:28:39 +03:00
|
|
|
single_v_file := dir
|
2019-10-13 16:37:43 +03:00
|
|
|
// Just compile one file and get parent dir
|
2019-12-13 19:28:39 +03:00
|
|
|
user_files << single_v_file
|
2019-12-20 00:29:37 +03:00
|
|
|
if v.pref.is_verbose {
|
|
|
|
v.log('> just compile one file: "${single_v_file}"')
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-12-20 00:29:37 +03:00
|
|
|
if v.pref.is_verbose {
|
|
|
|
v.log('> add all .v files from directory "${dir}" ...')
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
// Add .v files from the directory being compiled
|
|
|
|
files := v.v_files_from_dir(dir)
|
|
|
|
for file in files {
|
|
|
|
user_files << file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if user_files.len == 0 {
|
|
|
|
println('No input .v files')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
if v.pref.is_verbose {
|
2019-12-13 19:28:39 +03:00
|
|
|
v.log('user_files: $user_files')
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
return user_files
|
|
|
|
}
|
|
|
|
|
2019-10-14 04:07:59 +03:00
|
|
|
// get module files from already parsed imports
|
|
|
|
fn (v &V) get_imported_module_files(mod string) []string {
|
|
|
|
mut files := []string
|
2019-10-25 16:34:12 +03:00
|
|
|
for p in v.parsers {
|
|
|
|
if p.mod == mod {
|
|
|
|
files << p.file_path
|
2019-10-14 04:07:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
// parse deps from already parsed builtin/user files
|
|
|
|
pub fn (v mut V) parse_lib_imports() {
|
|
|
|
mut done_imports := []string
|
2019-12-20 00:29:37 +03:00
|
|
|
for i in 0 .. v.parsers.len {
|
2019-10-25 16:34:12 +03:00
|
|
|
for _, mod in v.parsers[i].import_table.imports {
|
2019-12-20 00:29:37 +03:00
|
|
|
if mod in done_imports {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
import_path := v.find_module_path(mod) or {
|
2019-12-20 00:29:37 +03:00
|
|
|
v.parsers[i].error_with_token_index('cannot import module "$mod" (not found)', v.parsers[i].import_table.get_import_tok_idx(mod))
|
2019-10-13 16:37:43 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
vfiles := v.v_files_from_dir(import_path)
|
|
|
|
if vfiles.len == 0 {
|
2019-12-20 00:29:37 +03:00
|
|
|
v.parsers[i].error_with_token_index('cannot import module "$mod" (no .v files in "$import_path")', v.parsers[i].import_table.get_import_tok_idx(mod))
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
// Add all imports referenced by these libs
|
|
|
|
for file in vfiles {
|
2019-10-25 16:34:12 +03:00
|
|
|
pidx := v.parse(file, .imports)
|
|
|
|
p_mod := v.parsers[pidx].mod
|
2019-10-13 16:37:43 +03:00
|
|
|
if p_mod != mod {
|
2019-12-20 00:29:37 +03:00
|
|
|
v.parsers[pidx].error_with_token_index('bad module definition: ${v.parsers[pidx].file_path} imports module "$mod" but $file is defined as module `$p_mod`', 1)
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
done_imports << mod
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:06:19 +03:00
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
pub fn (v &V) log(s string) {
|
|
|
|
if !v.pref.is_verbose {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
println(s)
|
|
|
|
}
|
|
|
|
|
2019-12-20 00:29:37 +03:00
|
|
|
pub fn new_v(args []string) &V {
|
2019-10-13 16:37:43 +03:00
|
|
|
// Create modules dirs if they are missing
|
2019-12-04 23:03:12 +03:00
|
|
|
if !os.is_dir(v_modules_path) {
|
2019-12-20 00:29:37 +03:00
|
|
|
os.mkdir(v_modules_path)or{
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
os.mkdir('$v_modules_path${os.path_separator}cache')or{
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
2019-11-28 13:04:57 +03:00
|
|
|
// optional, custom modules search path
|
2019-12-24 05:43:31 +03:00
|
|
|
user_mod_path := cmdline.option(args, '-user_mod_path', '')
|
2019-11-01 14:16:58 +03:00
|
|
|
// Location of all vlib files
|
2019-12-23 13:09:22 +03:00
|
|
|
vroot := filepath.dir(vexe_path())
|
2019-12-24 05:43:31 +03:00
|
|
|
vlib_path := cmdline.option(args, '-vlib-path', filepath.join(vroot,'vlib'))
|
|
|
|
vpath := cmdline.option(args, '-vpath', v_modules_path)
|
2019-10-13 16:37:43 +03:00
|
|
|
mut vgen_buf := strings.new_builder(1000)
|
2019-11-02 13:17:56 +03:00
|
|
|
vgen_buf.writeln('module vgen\nimport strings')
|
2019-12-24 05:43:31 +03:00
|
|
|
target_os := cmdline.option(args, '-os', '')
|
|
|
|
mut out_name := cmdline.option(args, '-o', 'a.out')
|
2019-10-13 16:37:43 +03:00
|
|
|
mut dir := args.last()
|
|
|
|
if 'run' in args {
|
2019-12-24 05:43:31 +03:00
|
|
|
args_after_run := cmdline.only_non_options( cmdline.after(args,['run']) )
|
|
|
|
dir = if args_after_run.len>0 { args_after_run[0] } else { '' }
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
if dir.ends_with(os.path_separator) {
|
|
|
|
dir = dir.all_before_last(os.path_separator)
|
|
|
|
}
|
|
|
|
if dir.starts_with('.$os.path_separator') {
|
2019-10-27 10:03:15 +03:00
|
|
|
dir = dir[2..]
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
if args.len < 2 {
|
|
|
|
dir = ''
|
|
|
|
}
|
2019-12-24 05:43:31 +03:00
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
// build mode
|
|
|
|
mut build_mode := BuildMode.default_mode
|
2019-12-24 05:43:31 +03:00
|
|
|
mut mod := ''
|
|
|
|
joined_args := args.join(' ')
|
2019-10-13 16:37:43 +03:00
|
|
|
if joined_args.contains('build module ') {
|
|
|
|
build_mode = .build_module
|
2019-11-01 14:16:58 +03:00
|
|
|
os.chdir(vroot)
|
2019-10-13 16:37:43 +03:00
|
|
|
// v build module ~/v/os => os.o
|
2019-12-20 00:29:37 +03:00
|
|
|
mod_path := if dir.contains('vlib') { dir.all_after('vlib' + os.path_separator) } else if dir.starts_with('.\\') || dir.starts_with('./') { dir[2..] } else if dir.starts_with(os.path_separator) { dir.all_after(os.path_separator) } else { dir }
|
2019-10-13 16:37:43 +03:00
|
|
|
mod = mod_path.replace(os.path_separator, '.')
|
|
|
|
println('Building module "${mod}" (dir="$dir")...')
|
2019-12-20 00:29:37 +03:00
|
|
|
// out_name = '$TmpPath/vlib/${base}.o'
|
2019-11-16 21:49:55 +03:00
|
|
|
if !out_name.ends_with('.c') {
|
|
|
|
out_name = mod
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
// Cross compiling? Use separate dirs for each os
|
|
|
|
/*
|
|
|
|
if target_os != os.user_os() {
|
2019-11-23 19:55:18 +03:00
|
|
|
os.mkdir('$TmpPath/vlib/$target_os') or { panic(err) }
|
2019-10-13 16:37:43 +03:00
|
|
|
out_name = '$TmpPath/vlib/$target_os/${base}.o'
|
|
|
|
println('target_os=$target_os user_os=${os.user_os()}')
|
|
|
|
println('!Cross compiling $out_name')
|
|
|
|
}
|
|
|
|
*/
|
2019-12-20 00:29:37 +03:00
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
is_test := dir.ends_with('_test.v')
|
2019-10-15 18:08:46 +03:00
|
|
|
is_script := dir.ends_with('.v') || dir.ends_with('.vsh')
|
2019-12-04 23:03:12 +03:00
|
|
|
if is_script && !os.exists(dir) {
|
2019-10-13 16:37:43 +03:00
|
|
|
println('`$dir` does not exist')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
// No -o provided? foo.v => foo
|
|
|
|
if out_name == 'a.out' && dir.ends_with('.v') && dir != '.v' {
|
2019-10-27 10:03:15 +03:00
|
|
|
out_name = dir[..dir.len - 2]
|
2019-10-22 21:29:32 +03:00
|
|
|
// Building V? Use v2, since we can't overwrite a running
|
|
|
|
// executable on Windows + the precompiled V is more
|
2019-10-23 08:18:44 +03:00
|
|
|
// optimized.
|
2019-12-04 23:03:12 +03:00
|
|
|
if out_name == 'v' && os.is_dir('vlib/compiler') {
|
2019-10-22 21:29:32 +03:00
|
|
|
println('Saving the resulting V executable in `./v2`')
|
2019-12-20 00:29:37 +03:00
|
|
|
println('Use `v -o v v.v` if you want to replace current ' + 'V executable.')
|
2019-10-22 21:29:32 +03:00
|
|
|
out_name = 'v2'
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
// if we are in `/foo` and run `v .`, the executable should be `foo`
|
|
|
|
if dir == '.' && out_name == 'a.out' {
|
|
|
|
base := os.getwd().all_after(os.path_separator)
|
|
|
|
out_name = base.trim_space()
|
|
|
|
}
|
2019-10-30 20:53:30 +03:00
|
|
|
// `v -o dir/exec`, create "dir/" if it doesn't exist
|
|
|
|
if out_name.contains(os.path_separator) {
|
|
|
|
d := out_name.all_before_last(os.path_separator)
|
2019-12-04 23:03:12 +03:00
|
|
|
if !os.is_dir(d) {
|
2019-10-30 20:53:30 +03:00
|
|
|
println('creating a new directory "$d"')
|
2019-12-20 00:29:37 +03:00
|
|
|
os.mkdir(d)or{
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-11-29 20:00:33 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
mut _os := OS.mac
|
|
|
|
// No OS specifed? Use current system
|
|
|
|
if target_os == '' {
|
|
|
|
$if linux {
|
|
|
|
_os = .linux
|
|
|
|
}
|
2019-12-03 16:29:24 +03:00
|
|
|
$if macos {
|
2019-10-13 16:37:43 +03:00
|
|
|
_os = .mac
|
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
_os = .windows
|
|
|
|
}
|
|
|
|
$if freebsd {
|
|
|
|
_os = .freebsd
|
|
|
|
}
|
|
|
|
$if openbsd {
|
|
|
|
_os = .openbsd
|
|
|
|
}
|
|
|
|
$if netbsd {
|
|
|
|
_os = .netbsd
|
|
|
|
}
|
|
|
|
$if dragonfly {
|
|
|
|
_os = .dragonfly
|
|
|
|
}
|
|
|
|
$if solaris {
|
|
|
|
_os = .solaris
|
|
|
|
}
|
2019-12-03 11:26:47 +03:00
|
|
|
$if haiku {
|
|
|
|
_os = .haiku
|
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
_os = os_from_string(target_os)
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
// println('VROOT=$vroot')
|
2019-10-13 16:37:43 +03:00
|
|
|
// v.exe's parent directory should contain vlib
|
2019-12-04 23:03:12 +03:00
|
|
|
if !os.is_dir(vlib_path) || !os.is_dir(vlib_path + os.path_separator + 'builtin') {
|
2019-12-20 00:29:37 +03:00
|
|
|
// println('vlib not found, downloading it...')
|
2019-10-13 16:37:43 +03:00
|
|
|
/*
|
|
|
|
ret := os.system('git clone --depth=1 https://github.com/vlang/v .')
|
|
|
|
if ret != 0 {
|
|
|
|
println('failed to `git clone` vlib')
|
|
|
|
println('make sure you are online and have git installed')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
*/
|
2019-12-02 11:37:35 +03:00
|
|
|
println('vlib not found. It should be next to the V executable.')
|
2019-10-13 16:37:43 +03:00
|
|
|
println('Go to https://vlang.io to install V.')
|
2019-12-02 12:16:55 +03:00
|
|
|
println('(os.executable=${os.executable()} vlib_path=$vlib_path vexe_path=${vexe_path()}')
|
2019-10-13 16:37:43 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
2019-11-21 15:06:19 +03:00
|
|
|
mut out_name_c := get_vtmp_filename(out_name, '.tmp.c')
|
2019-12-24 05:43:31 +03:00
|
|
|
cflags := cmdline.many_values(args, '-cflags').join(' ')
|
2019-12-23 13:09:00 +03:00
|
|
|
|
2019-12-24 05:43:31 +03:00
|
|
|
defines := cmdline.many_values(args, '-d')
|
2019-12-23 13:09:00 +03:00
|
|
|
compile_defines, compile_defines_all := parse_defines( defines )
|
|
|
|
|
2019-11-21 15:06:19 +03:00
|
|
|
rdir := os.realpath(dir)
|
2019-12-23 13:09:22 +03:00
|
|
|
rdir_name := filepath.filename(rdir)
|
2019-11-19 09:53:52 +03:00
|
|
|
if '-bare' in args {
|
|
|
|
verror('use -freestanding instead of -bare')
|
2019-11-29 20:00:33 +03:00
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
obfuscate := '-obf' in args
|
|
|
|
is_repl := '-repl' in args
|
2019-12-20 00:29:37 +03:00
|
|
|
pref := &Preferences{
|
2019-10-13 16:37:43 +03:00
|
|
|
is_test: is_test
|
|
|
|
is_script: is_script
|
|
|
|
is_so: '-shared' in args
|
2019-12-09 13:48:41 +03:00
|
|
|
is_solive: '-solive' in args
|
2019-10-13 16:37:43 +03:00
|
|
|
is_prod: '-prod' in args
|
|
|
|
is_verbose: '-verbose' in args || '--verbose' in args
|
2019-12-20 00:29:37 +03:00
|
|
|
is_debug: '-g' in args || '-cg' in args
|
|
|
|
is_vlines: '-g' in args && !('-cg' in args)
|
|
|
|
is_keep_c: '-keep_c' in args
|
|
|
|
is_cache: '-cache' in args
|
2019-10-13 16:37:43 +03:00
|
|
|
is_stats: '-stats' in args
|
|
|
|
obfuscate: obfuscate
|
|
|
|
is_prof: '-prof' in args
|
|
|
|
is_live: '-live' in args
|
|
|
|
sanitize: '-sanitize' in args
|
2019-12-20 00:29:37 +03:00
|
|
|
// nofmt: '-nofmt' in args
|
2019-12-22 01:27:03 +03:00
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
show_c_cmd: '-show_c_cmd' in args
|
|
|
|
translated: 'translated' in args
|
|
|
|
is_run: 'run' in args
|
|
|
|
autofree: '-autofree' in args
|
|
|
|
compress: '-compress' in args
|
2019-11-05 02:43:52 +03:00
|
|
|
enable_globals: '--enable-globals' in args
|
2019-11-01 00:37:24 +03:00
|
|
|
fast: '-fast' in args
|
2019-11-19 09:53:52 +03:00
|
|
|
is_bare: '-freestanding' in args
|
|
|
|
x64: '-x64' in args
|
2019-12-04 02:01:17 +03:00
|
|
|
output_cross_c: '-output-cross-platform-c' in args
|
2019-12-14 02:28:15 +03:00
|
|
|
prealloc: '-prealloc' in args
|
2019-10-13 16:37:43 +03:00
|
|
|
is_repl: is_repl
|
|
|
|
build_mode: build_mode
|
|
|
|
cflags: cflags
|
|
|
|
ccompiler: find_c_compiler()
|
2019-12-20 00:29:37 +03:00
|
|
|
building_v: !is_repl && (rdir_name == 'compiler' || rdir_name == 'v.v' || dir.contains('vlib'))
|
|
|
|
// is_fmt: comptime_define == 'vfmt'
|
2019-12-22 01:27:03 +03:00
|
|
|
|
2019-11-28 13:04:57 +03:00
|
|
|
user_mod_path: user_mod_path
|
2019-11-21 15:06:19 +03:00
|
|
|
vlib_path: vlib_path
|
|
|
|
vpath: vpath
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
if pref.is_verbose || pref.is_debug {
|
|
|
|
println('C compiler=$pref.ccompiler')
|
|
|
|
}
|
|
|
|
if pref.is_so {
|
2019-12-20 00:29:37 +03:00
|
|
|
out_name_c = get_vtmp_filename(out_name, '.tmp.so.c')
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
2019-11-14 06:50:21 +03:00
|
|
|
$if !linux {
|
2019-11-14 07:08:11 +03:00
|
|
|
if pref.is_bare && !out_name.ends_with('.c') {
|
2019-11-29 22:49:05 +03:00
|
|
|
verror('-freestanding only works on Linux for now')
|
2019-11-29 20:00:33 +03:00
|
|
|
}
|
2019-11-14 06:50:21 +03:00
|
|
|
}
|
2019-10-13 16:37:43 +03:00
|
|
|
return &V{
|
|
|
|
os: _os
|
|
|
|
out_name: out_name
|
|
|
|
dir: dir
|
2019-12-23 13:09:22 +03:00
|
|
|
compiled_dir: if os.is_dir(rdir) { rdir } else { filepath.dir(rdir) }
|
2019-10-13 16:37:43 +03:00
|
|
|
lang_dir: vroot
|
|
|
|
table: new_table(obfuscate)
|
|
|
|
out_name_c: out_name_c
|
|
|
|
cgen: new_cgen(out_name_c)
|
2019-11-19 09:53:52 +03:00
|
|
|
x64: x64.new_gen(out_name)
|
2019-10-13 16:37:43 +03:00
|
|
|
vroot: vroot
|
|
|
|
pref: pref
|
|
|
|
mod: mod
|
2019-12-23 13:09:00 +03:00
|
|
|
vgen_buf: vgen_buf
|
|
|
|
compile_defines: compile_defines
|
|
|
|
compile_defines_all: compile_defines_all
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 16:00:10 +03:00
|
|
|
fn non_empty(a []string) []string {
|
|
|
|
return a.filter(it.len != 0)
|
|
|
|
}
|
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
pub fn env_vflags_and_os_args() []string {
|
2019-10-21 14:14:28 +03:00
|
|
|
vosargs := os.getenv('VOSARGS')
|
2019-12-20 00:29:37 +03:00
|
|
|
if '' != vosargs {
|
|
|
|
return non_empty(vosargs.split(' '))
|
|
|
|
}
|
2019-10-21 14:14:28 +03:00
|
|
|
mut args := []string
|
|
|
|
vflags := os.getenv('VFLAGS')
|
|
|
|
if '' != vflags {
|
|
|
|
args << os.args[0]
|
|
|
|
args << vflags.split(' ')
|
|
|
|
if os.args.len > 1 {
|
2019-10-27 10:03:15 +03:00
|
|
|
args << os.args[1..]
|
2019-10-21 14:14:28 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-10-21 14:14:28 +03:00
|
|
|
args << os.args
|
|
|
|
}
|
2019-12-17 16:00:10 +03:00
|
|
|
return non_empty(args)
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_symlink() {
|
2019-12-20 00:29:37 +03:00
|
|
|
$if windows {
|
|
|
|
return
|
|
|
|
}
|
2019-11-21 04:34:08 +03:00
|
|
|
vexe := vexe_path()
|
2019-10-13 16:37:43 +03:00
|
|
|
link_path := '/usr/local/bin/v'
|
|
|
|
ret := os.system('ln -sf $vexe $link_path')
|
|
|
|
if ret == 0 {
|
2019-11-21 04:34:08 +03:00
|
|
|
println('Symlink "$link_path" has been created')
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-11-21 04:34:08 +03:00
|
|
|
println('Failed to create symlink "$link_path". Try again with sudo.')
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 14:14:28 +03:00
|
|
|
pub fn vexe_path() string {
|
|
|
|
vexe := os.getenv('VEXE')
|
2019-12-20 00:29:37 +03:00
|
|
|
if '' != vexe {
|
|
|
|
return vexe
|
|
|
|
}
|
2019-11-21 04:34:08 +03:00
|
|
|
real_vexe_path := os.realpath(os.executable())
|
|
|
|
os.setenv('VEXE', real_vexe_path, true)
|
|
|
|
return real_vexe_path
|
2019-10-21 14:14:28 +03:00
|
|
|
}
|
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
pub fn verror(s string) {
|
|
|
|
println('V error: $s')
|
|
|
|
os.flush_stdout()
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn vhash() string {
|
|
|
|
mut buf := [50]byte
|
|
|
|
buf[0] = 0
|
2019-12-20 00:29:37 +03:00
|
|
|
C.snprintf(charptr(buf), 50, '%s', C.V_COMMIT_HASH)
|
2019-10-13 16:37:43 +03:00
|
|
|
return tos_clone(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cescaped_path(s string) string {
|
2019-12-20 00:29:37 +03:00
|
|
|
return s.replace('\\', '\\\\')
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn os_from_string(os string) OS {
|
2019-10-24 19:19:03 +03:00
|
|
|
match os {
|
2019-12-20 00:29:37 +03:00
|
|
|
'linux' {
|
|
|
|
return .linux
|
|
|
|
}
|
|
|
|
'windows' {
|
|
|
|
return .windows
|
|
|
|
}
|
|
|
|
'mac' {
|
|
|
|
return .mac
|
|
|
|
}
|
|
|
|
'macos' {
|
|
|
|
return .mac
|
|
|
|
}
|
|
|
|
'freebsd' {
|
|
|
|
return .freebsd
|
|
|
|
}
|
|
|
|
'openbsd' {
|
|
|
|
return .openbsd
|
|
|
|
}
|
|
|
|
'netbsd' {
|
|
|
|
return .netbsd
|
|
|
|
}
|
|
|
|
'dragonfly' {
|
|
|
|
return .dragonfly
|
|
|
|
}
|
|
|
|
'js' {
|
|
|
|
return .js
|
|
|
|
}
|
|
|
|
'solaris' {
|
|
|
|
return .solaris
|
|
|
|
}
|
|
|
|
'android' {
|
|
|
|
return .android
|
|
|
|
}
|
2019-10-24 19:19:03 +03:00
|
|
|
'msvc' {
|
2019-10-14 10:36:29 +03:00
|
|
|
// notice that `-os msvc` became `-cc msvc`
|
|
|
|
verror('use the flag `-cc msvc` to build using msvc')
|
2019-10-13 16:37:43 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
'haiku' {
|
|
|
|
return .haiku
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
panic('bad os $os')
|
|
|
|
}}
|
|
|
|
// println('bad os $os') // todo panic?
|
2019-10-13 16:37:43 +03:00
|
|
|
return .linux
|
|
|
|
}
|
2019-10-21 14:14:28 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
pub fn set_vroot_folder(vroot_path string) {
|
|
|
|
// Preparation for the compiler module:
|
|
|
|
// VEXE env variable is needed so that compiler.vexe_path()
|
|
|
|
// can return it later to whoever needs it:
|
2019-12-06 15:24:53 +03:00
|
|
|
vname := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
|
2019-12-20 00:29:37 +03:00
|
|
|
os.setenv('VEXE', os.realpath([vroot_path, vname].join(os.path_separator)), true)
|
2019-10-21 14:14:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_v_compiler_with_args(args []string) &V {
|
|
|
|
vexe := vexe_path()
|
|
|
|
mut allargs := [vexe]
|
|
|
|
allargs << args
|
|
|
|
os.setenv('VOSARGS', allargs.join(' '), true)
|
|
|
|
return new_v(allargs)
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
|