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

964 lines
26 KiB
V
Raw Normal View History

2020-01-23 23:04:46 +03:00
// Copyright (c) 2019-2020 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
strings
v.pref
v.builder
)
2019-10-24 12:36:57 +03:00
pub const (
2020-01-31 17:31:15 +03:00
Version = '0.1.25'
)
const (
2019-12-30 14:10:46 +03:00
supported_platforms = ['windows', 'mac', 'macos', 'linux', 'freebsd', 'openbsd', 'netbsd',
2019-12-31 21:53:15 +03:00
'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos']
)
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
}
2020-02-09 12:08:04 +03:00
pub struct V {
pub mut:
mod_file_cacher &builder.ModFileCacher // used during lookup for v.mod to support @VROOT
2019-12-20 00:29:37 +03:00
out_name_c string // name of the temporary C file
files []string // all V files that need to be parsed and compiled
2020-03-20 18:41:18 +03:00
compiled_dir string // contains os.real_path() of the dir of the final file beeing compiled, or the dir itself when doing `v .`
2019-12-20 00:29:37 +03:00
table &Table // table with types, vars, functions etc
cgen &CGen // C code generator
2019-12-31 21:42:16 +03:00
//x64 &x64.Gen
pref &pref.Preferences // all the preferences and settings extracted to a struct for reusability
2019-12-20 00:29:37 +03:00
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
module_lookup_paths []string
2019-12-27 12:16:00 +03:00
2020-02-09 12:08:04 +03:00
v_fmt_all bool // << input set by cmd/tools/vfmt.v
v_fmt_file string // << file given by the user from cmd/tools/vfmt.v
v_fmt_file_result string // >> file with formatted output generated by vlib/compiler/vfmt.v
}
2020-02-09 12:08:04 +03:00
pub fn new_v(pref &pref.Preferences) &V {
2020-03-20 18:41:18 +03:00
rdir := os.real_path(pref.path)
2020-02-09 12:08:04 +03:00
mut out_name_c := get_vtmp_filename(pref.out_name, '.tmp.c')
if pref.is_so {
out_name_c = get_vtmp_filename(pref.out_name, '.tmp.so.c')
}
mut vgen_buf := strings.new_builder(1000)
vgen_buf.writeln('module vgen\nimport strings')
2020-03-08 00:26:26 +03:00
compiled_dir:=if os.is_dir(rdir) { rdir } else { os.dir(rdir) }
2020-02-09 12:08:04 +03:00
return &V{
mod_file_cacher: builder.new_mod_file_cacher()
2020-03-08 00:26:26 +03:00
compiled_dir:compiled_dir// if os.is_dir(rdir) { rdir } else { os.dir(rdir) }
2020-02-09 12:08:04 +03:00
table: new_table(pref.obfuscate)
out_name_c: out_name_c
cgen: new_cgen(out_name_c)
//x64: x64.new_gen(out_name)
pref: pref
vgen_buf: vgen_buf
}
}
// 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() {
// TODO remove
if v.pref.autofree {
/*
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
}
v.table.fns.free()
free(v.table)
2019-10-20 10:19:37 +03:00
//for p in parsers {}
println('done!')
*/
2019-10-20 10:19:37 +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
2020-03-20 18:41:18 +03:00
file_path := if os.is_abs_path(parser.file_path) { parser.file_path } else { os.real_path(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
}
pub fn (v &V) get_file_parser_index(file string) ?int {
2020-03-20 18:41:18 +03:00
file_path := if os.is_abs_path(file) { file } else { os.real_path(file) }
2019-10-25 16:34:12 +03:00
if file_path in v.file_parser_idx {
return v.file_parser_idx[file_path]
}
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)')
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-25 16:34:12 +03:00
// println('matched ' + v.parsers[pidx].file_path + ' with $file')
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) }
return pidx
}
pub fn (v mut V) compile() {
2020-01-03 13:38:26 +03:00
//println('compile()')
// Emily: Stop people on linux from being able to build with msvc
if os.user_os() != 'windows' && v.pref.ccompiler == 'msvc' {
verror('Cannot build with msvc on ${os.user_os()}')
}
mut cgen := v.cgen
cgen.genln('// Generated by V')
2020-03-06 20:53:29 +03:00
if v.pref.verbosity.is_higher_or_equal(.level_three) {
println('all .v files before:')
println(v.files)
}
v.add_v_files_to_compile()
2020-03-06 20:53:29 +03:00
if v.pref.verbosity.is_higher_or_equal(.level_three) {
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
}
println('\nfiles:')
for q in v.files {
println(q)
2019-10-20 10:19:37 +03:00
}
}
*/
2019-12-20 00:29:37 +03:00
// First pass (declarations)
for file in v.files {
v.parse(file, .decl)
}
// Main pass
cgen.pass = .main
if v.pref.is_debug {
$if js {
cgen.genln('const VDEBUG = 1;\n')
2019-12-20 00:29:37 +03:00
} $else {
cgen.genln('#define VDEBUG (1)')
}
}
2019-12-14 02:46:55 +03:00
if v.pref.prealloc {
cgen.genln('#define VPREALLOC (1)')
}
2020-02-09 12:08:04 +03:00
if v.pref.os == .js {
cgen.genln('#define _VJS (1) ')
}
v_hash := vhash()
$if js {
cgen.genln('const V_COMMIT_HASH = "$v_hash";\n')
} $else {
cgen.genln('#ifndef V_COMMIT_HASH')
cgen.genln('#define V_COMMIT_HASH "$v_hash"')
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
}
2020-02-09 12:08:04 +03:00
if v.pref.compile_defines_all.len > 0 {
cgen.genln('')
2020-02-09 12:08:04 +03:00
cgen.genln('// All custom defines : ' + v.pref.compile_defines_all.join(','))
cgen.genln('// Turned ON custom defines: ' + v.pref.compile_defines.join(','))
for cdefine in v.pref.compile_defines {
cgen.genln('#define CUSTOM_DEFINE_${cdefine}')
}
cgen.genln('//')
cgen.genln('')
}
2019-12-27 12:16:00 +03:00
2019-11-14 10:23:44 +03:00
cgen.genln(c_builtin_types)
2019-12-27 12:16: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 {
cgen.genln(bare_c_headers)
2019-11-14 10:23:44 +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')
}
2020-02-09 12:08:04 +03:00
if v.pref.is_live && v.pref.os != .windows {
2019-12-05 14:40:14 +03:00
cgen.includes << '#include <dlfcn.h>'
}
2019-12-20 00:29:37 +03:00
// cgen.genln('/*================================== FNS =================================*/')
cgen.genln('// this line will be replaced with definitions')
mut defs_pos := cgen.lines.len - 1
if defs_pos == -1 {
defs_pos = 0
2019-10-20 10:19:37 +03:00
}
cgen.nogen = q
2019-11-11 17:18:32 +03:00
for i, file in v.files {
v.parse(file, .main)
2019-12-20 00:29:37 +03:00
// if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
// 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
// }
}
// 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())
// free the string builder which held the generated methods
v.vgen_buf.free()
vgen_parser.is_vgen = true
2019-11-29 16:46:43 +03:00
// v.add_parser(vgen_parser)
vgen_parser.parse(.main)
// Generate .vh if we are building a module
if v.pref.build_mode == .build_module {
2020-03-19 15:24:57 +03:00
//generate_vh(v.pref.path)
}
// All definitions
2019-12-20 00:29:37 +03:00
mut def := strings.new_builder(10000) // Avoid unnecessary allocations
def.writeln(cgen.const_defines.join_lines())
$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')
}
def.writeln(cgen.fns.join_lines()) // fn definitions
2019-11-08 06:03:06 +03:00
def.writeln(v.interface_table())
} $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()
2020-03-06 20:53:29 +03:00
if v.pref.verbosity.is_higher_or_equal(.level_three) {
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
}
cgen.save()
v.cc()
2020-02-12 03:16:38 +03:00
//println(v.table.imports)
//println(v.table.modules)
}
2019-12-30 14:10:46 +03:00
pub fn (v mut V) compile2() {
if os.user_os() != 'windows' && v.pref.ccompiler == 'msvc' {
verror('Cannot build with msvc on ${os.user_os()}')
}
//cgen.genln('// Generated by V')
2020-03-31 20:58:44 +03:00
//println('compile2()')
2020-03-06 20:53:29 +03:00
if v.pref.verbosity.is_higher_or_equal(.level_three) {
2019-12-30 14:10:46 +03:00
println('all .v files before:')
println(v.files)
}
// v1 compiler files
//v.add_v_files_to_compile()
2020-01-19 04:44:18 +03:00
//v.files << v.dir
// v2 compiler
v.files << v.get_builtin_files()
v.files << v.get_user_files()
v.set_module_lookup_paths()
2020-03-06 20:53:29 +03:00
if v.pref.verbosity.is_higher_or_equal(.level_three) {
2019-12-30 14:10:46 +03:00
println('all .v files:')
println(v.files)
}
mut b := v.new_v2()
b.build_c(v.files, v.out_name_c)// v.pref.out_name + '.c')
2019-12-30 14:10:46 +03:00
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
}
2020-03-09 04:23:34 +03:00
//v.files << v.v_files_from_dir(os.join_path(v.pref.vlib_path,'builtin','bare'))
2020-02-09 12:08:04 +03:00
v.files << v.pref.path
v.set_module_lookup_paths()
mut b := v.new_v2()
2020-02-03 09:31:54 +03:00
// move all this logic to v2
2020-02-09 12:08:04 +03:00
b.build_x64(v.files, v.pref.out_name)
2019-11-29 20:00:33 +03:00
}
2019-11-22 18:33:02 +03:00
// make v2 from v1
fn (v &V) new_v2() builder.Builder {
mut b := builder.new_builder(v.pref)
b = { b|
2020-02-09 12:08:04 +03:00
os: v.pref.os,
module_path: v_modules_path,
compiled_dir: v.compiled_dir,
module_search_paths: v.module_lookup_paths
}
return b
2020-02-03 09:31:54 +03:00
}
fn (v mut V) generate_init() {
2019-12-20 00:29:37 +03:00
$if js {
return
}
if v.pref.build_mode == .build_module {
nogen := v.cgen.nogen
v.cgen.nogen = false
consts_init_body := v.cgen.consts_init.join_lines()
2020-02-09 12:08:04 +03:00
init_fn_name := mod_gen_name(v.pref.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
}
if !v.pref.is_bare && !v.pref.is_so {
2019-12-20 00:29:37 +03:00
// vlib can't have `init_consts()`
v.cgen.genln('void init() {
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
$call_mod_init_consts
$consts_init_body
builtin__init();
$call_mod_init
}')
2019-12-20 00:29:37 +03:00
}
if !v.pref.is_bare {
v.generate_str_definitions()
}
2019-11-14 10:23:44 +03:00
}
}
pub fn (v mut V) generate_main() {
mut cgen := v.cgen
2019-12-20 00:29:37 +03:00
$if js {
return
}
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.
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')
2020-03-20 18:41:18 +03:00
cgen.lines << '#line $lines_so_far "${cescaped_path(os.real_path(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
// But make sure that there's some code outside of main()
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()...')
v.gen_main_start(true)
cgen.genln('$cgen.fn_main;')
v.gen_main_end('return 0')
}
else if v.v_fmt_file=='' && !v.pref.is_repl && !v.pref.is_so {
verror('function `main` is not declared in the main module\nPlease add: \nfn main(){\n}\n... to your main program .v file, and try again.')
}
2019-12-30 14:10:46 +03:00
}
else if v.pref.is_test {
if v.table.main_exists() {
verror('test files cannot have function `main`')
}
test_fn_names := v.table.all_test_function_names()
if test_fn_names.len == 0 {
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 {
2020-02-09 12:08:04 +03:00
cgen.genln('BenchedTests bt = main__start_testing(${test_fn_names.len},tos3("$v.pref.path"));')
2019-11-18 02:34:46 +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-30 07:23:54 +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);')
}
v.gen_main_end('return g_test_fails > 0')
}
else if v.table.main_exists() && !v.pref.is_so {
v.gen_main_start(true)
cgen.genln(' main__main();')
2019-11-26 13:54:41 +03:00
if !v.pref.is_bare {
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
}
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) {
2020-02-09 12:08:04 +03:00
if v.pref.os == .windows {
if 'glfw' in v.table.imports {
// GUI application
v.cgen.genln('int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance, LPWSTR cmd_line, int show_cmd) { ')
v.cgen.genln(' typedef LPWSTR*(WINAPI *cmd_line_to_argv)(LPCWSTR, int*);')
v.cgen.genln(' HMODULE shell32_module = LoadLibrary(L"shell32.dll");')
v.cgen.genln(' cmd_line_to_argv CommandLineToArgvW = (cmd_line_to_argv)GetProcAddress(shell32_module, "CommandLineToArgvW");')
v.cgen.genln(' int argc;')
v.cgen.genln(' wchar_t** argv = CommandLineToArgvW(cmd_line, &argc);')
} else {
// Console application
2019-12-27 12:16:00 +03:00
v.cgen.genln('int wmain(int argc, wchar_t* argv[], wchar_t* envp[]) { ')
}
} else {
v.cgen.genln('int main(int argc, char** argv) { ')
}
2019-12-04 01:40:26 +03:00
v.cgen.genln(' init();')
if add_os_args && 'os' in v.table.imports {
2020-02-09 12:08:04 +03:00
if v.pref.os == .windows {
v.cgen.genln(' os__args = os__init_os_args_wide(argc, argv);')
} else {
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) {
v.cgen.genln('')
v.cgen.genln(' $return_statement;')
v.cgen.genln('}')
}
pub fn (v &V) v_files_from_dir(dir string) []string {
mut res := []string
if !os.exists(dir) {
if dir == 'compiler' && os.is_dir('vlib') {
println('looks like you are trying to build V with an old command')
2020-02-09 12:08:04 +03:00
println('use `v -o v cmd/v` instead of `v -o v compiler`')
2019-11-29 20:00:33 +03:00
}
verror("$dir doesn't exist")
2019-12-20 00:29:37 +03:00
}
else if !os.is_dir(dir) {
2020-03-24 13:14:11 +03:00
verror("$dir isn't a directory!")
}
2019-12-20 00:29:37 +03:00
mut files := os.ls(dir)or{
panic(err)
}
2020-03-06 20:53:29 +03:00
if v.pref.verbosity.is_higher_or_equal(.level_three) {
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
}
2020-02-09 12:08:04 +03:00
if (file.ends_with('_win.v') || file.ends_with('_windows.v')) && v.pref.os != .windows {
continue
}
2020-02-09 12:08:04 +03:00
if (file.ends_with('_lin.v') || file.ends_with('_linux.v')) && v.pref.os != .linux {
continue
}
2020-02-09 12:08:04 +03:00
if (file.ends_with('_mac.v') || file.ends_with('_darwin.v')) && v.pref.os != .mac {
continue
}
2020-02-09 12:08:04 +03:00
if file.ends_with('_nix.v') && v.pref.os == .windows {
continue
}
if file.ends_with('_android.v') && v.pref.os != .android {
continue
}
if file.ends_with('_freebsd.v') && v.pref.os != .freebsd {
continue
}
if file.ends_with('_solaris.v') && v.pref.os != .solaris {
continue
}
2020-02-09 12:08:04 +03:00
if file.ends_with('_js.v') && v.pref.os != .js {
continue
}
2020-02-09 12:08:04 +03:00
if file.ends_with('_c.v') && v.pref.os == .js {
continue
}
2020-02-09 12:08:04 +03:00
if v.pref.compile_defines_all.len > 0 && file.contains('_d_') {
mut allowed := false
2020-02-09 12:08:04 +03:00
for cdefine in v.pref.compile_defines {
file_postfix := '_d_${cdefine}.v'
if file.ends_with(file_postfix) {
allowed = true
break
}
}
if !allowed {
continue
}
}
2020-03-09 04:23:34 +03:00
res << os.join_path(dir, file)
}
return res
}
// Parses imports, adds necessary libs, and then user files
pub fn (v mut V) add_v_files_to_compile() {
v.set_module_lookup_paths()
mut builtin_files := v.get_builtin_files()
if v.pref.is_bare {
2019-12-20 00:29:37 +03:00
// builtin_files = []
2019-11-29 20:00:33 +03:00
}
// Builtin cache exists? Use it.
if v.pref.is_cache {
2020-03-09 04:23:34 +03:00
builtin_vh := os.join_path(v_modules_path, 'vlib', 'builtin.vh')
if os.exists(builtin_vh) {
v.cached_mods << 'builtin'
builtin_files = [builtin_vh]
}
}
2020-03-06 20:53:29 +03:00
if v.pref.verbosity.is_higher_or_equal(.level_two) {
2019-12-20 00:29:37 +03:00
v.log('v.add_v_files_to_compile > builtin_files: $builtin_files')
}
// 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) }
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) }
v.add_parser(p)
}
// Parse lib imports
v.parse_lib_imports()
2020-03-06 20:53:29 +03:00
if v.pref.verbosity.is_higher_or_equal(.level_three) {
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
2020-03-06 20:53:29 +03:00
// Cached modules are broken currently
/*
if v.pref.vpath != '' && v.pref.build_mode != .build_module && !mod.contains('vweb') {
2020-03-08 00:26:26 +03:00
mod_path := mod.replace('.', os.path_separator)
vh_path := '$v_modules_path${os.path_separator}vlib${os.path_separator}${mod_path}.vh'
if v.pref.is_cache && os.exists(vh_path) {
eprintln('using cached module `$mod`: $vh_path')
v.cached_mods << mod
v.files << vh_path
continue
}
}
2020-03-06 20:53:29 +03:00
*/
// standard module
vfiles := v.get_imported_module_files(mod)
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
}
}
pub fn (v &V) get_builtin_files() []string {
2020-03-06 20:53:29 +03:00
// Lookup for built-in folder in lookup path.
// Assumption: `builtin/` folder implies usable implementation of builtin
for location in v.pref.lookup_path {
2020-03-09 04:23:34 +03:00
if !os.exists(os.join_path(location, 'builtin')) {
2020-03-06 20:53:29 +03:00
continue
}
if v.pref.is_bare {
2020-03-09 04:23:34 +03:00
return v.v_files_from_dir(os.join_path(location, 'builtin', 'bare'))
2020-03-06 20:53:29 +03:00
}
$if js {
2020-03-09 04:23:34 +03:00
return v.v_files_from_dir(os.join_path(location, 'builtin', 'js'))
2020-03-06 20:53:29 +03:00
}
2020-03-09 04:23:34 +03:00
return v.v_files_from_dir(os.join_path(location, 'builtin'))
}
2020-03-06 20:53:29 +03:00
// Panic. We couldn't find the folder.
verror('`builtin/` not included on module lookup path.
Did you forget to add vlib to the path? (Use @vlib for default vlib)')
panic('Unreachable code reached.')
}
// get user files
2019-12-20 00:29:37 +03:00
pub fn (v &V) get_user_files() []string {
2020-02-09 12:08:04 +03:00
mut dir := v.pref.path
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
mut user_files := []string
2020-01-19 04:44:18 +03:00
2020-02-09 12:08:04 +03:00
// See cmd/tools/preludes/README.md for more info about what preludes are
2020-03-08 00:26:26 +03:00
vroot := os.dir(pref.vexe_path())
2020-03-09 04:23:34 +03:00
preludes_path := os.join_path(vroot, 'cmd', 'tools', 'preludes')
if v.pref.is_live {
2020-03-09 04:23:34 +03:00
user_files << os.join_path(preludes_path, 'live_main.v')
}
if v.pref.is_solive {
2020-03-09 04:23:34 +03:00
user_files << os.join_path(preludes_path, 'live_shared.v')
}
if v.pref.is_test {
2020-03-09 04:23:34 +03:00
user_files << os.join_path(preludes_path, 'tests_assertions.v')
}
if v.pref.is_test && v.pref.is_stats {
2020-03-09 04:23:34 +03:00
user_files << os.join_path(preludes_path, 'tests_with_stats.v')
}
2020-01-19 04:44:18 +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')
}
slines := tcontent.trim_space().split_into_lines()
for sline in slines {
line := sline.trim_space()
if line.len > 2 {
if line[0] == `/` && line[1] == `/` {
continue
}
if line.starts_with('module ') && !line.starts_with('module main') {
is_internal_module_test = true
break
}
}
}
}
if is_internal_module_test {
// v volt/slack_test.v: compile all .v files to get the environment
2020-03-20 18:41:18 +03:00
single_test_v_file := os.real_path(dir)
2020-03-06 20:53:29 +03:00
if v.pref.verbosity.is_higher_or_equal(.level_two) {
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
}
user_files << single_test_v_file
2020-03-08 17:43:56 +03:00
dir = os.base_dir(single_test_v_file)
}
is_real_file := os.exists(dir) && !os.is_dir(dir)
if is_real_file && ( dir.ends_with('.v') || dir.ends_with('.vsh') ) {
single_v_file := dir
// Just compile one file and get parent dir
user_files << single_v_file
2020-03-06 20:53:29 +03:00
if v.pref.verbosity.is_higher_or_equal(.level_two) {
2019-12-20 00:29:37 +03:00
v.log('> just compile one file: "${single_v_file}"')
}
}
else {
2020-03-06 20:53:29 +03:00
if v.pref.verbosity.is_higher_or_equal(.level_two) {
2019-12-20 00:29:37 +03:00
v.log('> add all .v files from directory "${dir}" ...')
}
// 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)
}
2020-03-06 20:53:29 +03:00
if v.pref.verbosity.is_higher_or_equal(.level_two) {
v.log('user_files: $user_files')
}
return user_files
}
// 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
}
}
return files
}
// 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
}
2020-02-29 16:23:45 +03:00
import_path := v.parsers[i].find_module_path(mod) or {
v.parsers[i].error_with_token_index('cannot import module "$mod" (not found)\n$err', v.parsers[i].import_table.get_import_tok_idx(mod))
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))
}
// 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
if p_mod != mod {
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`', 0)
}
}
done_imports << mod
}
}
}
pub fn (v &V) log(s string) {
2020-03-06 20:53:29 +03:00
if !v.pref.verbosity.is_higher_or_equal(.level_two) {
return
}
println(s)
}
pub fn verror(s string) {
println('V error: $s')
2020-03-03 02:00:30 +03:00
os.flush()
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)
return tos_clone(buf)
}
pub fn cescaped_path(s string) string {
2019-12-20 00:29:37 +03:00
return s.replace('\\', '\\\\')
}
pub fn os_from_string(os string) pref.OS {
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
}
'msvc' {
// notice that `-os msvc` became `-cc msvc`
verror('use the flag `-cc msvc` to build using msvc')
}
2019-12-20 00:29:37 +03:00
'haiku' {
return .haiku
}
2019-12-31 21:53:15 +03:00
'linux_or_macos' {
return .linux
}
2019-12-20 00:29:37 +03:00
else {
panic('bad os $os')
}}
// println('bad os $os') // todo panic?
return .linux
}
//
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:
vname := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
2020-03-20 18:41:18 +03:00
os.setenv('VEXE', os.real_path([vroot_path, vname].join(os.path_separator)), true)
}
pub fn (v mut V) generate_str_definitions() {
// _STR function can't be defined in vlib
v.cgen.genln('
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);
}
')
}