2019-06-23 05:21:30 +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.
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
module main
|
|
|
|
|
2019-08-29 01:52:32 +03:00
|
|
|
import (
|
|
|
|
os
|
|
|
|
strings
|
2019-09-16 17:29:06 +03:00
|
|
|
benchmark
|
2019-08-29 01:52:32 +03:00
|
|
|
)
|
2019-06-22 21:20:28 +03:00
|
|
|
|
|
|
|
const (
|
2019-09-30 22:39:52 +03:00
|
|
|
Version = '0.1.21'
|
2019-06-22 21:20:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
enum BuildMode {
|
|
|
|
// `v program.v'
|
|
|
|
// Build user code only, and add pre-compiled vlib (`cc program.o builtin.o os.o...`)
|
2019-07-12 08:23:16 +03:00
|
|
|
default_mode
|
2019-06-22 21:20:28 +03:00
|
|
|
// `v -lib ~/v/os`
|
|
|
|
// build any module (generate os.o + os.vh)
|
2019-09-10 17:36:14 +03:00
|
|
|
build_module
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2019-09-27 04:00:48 +03:00
|
|
|
supported_platforms = ['windows', 'mac', 'linux', 'freebsd', 'openbsd',
|
2019-09-27 00:23:27 +03:00
|
|
|
'netbsd', 'dragonfly', 'msvc', 'android', 'js', 'solaris']
|
2019-06-22 21:20:28 +03:00
|
|
|
)
|
|
|
|
|
2019-07-07 23:30:15 +03:00
|
|
|
enum OS {
|
|
|
|
mac
|
|
|
|
linux
|
|
|
|
windows
|
2019-08-17 22:19:37 +03:00
|
|
|
freebsd
|
|
|
|
openbsd
|
|
|
|
netbsd
|
|
|
|
dragonfly
|
2019-09-29 04:54:12 +03:00
|
|
|
msvc // TODO not an OS
|
2019-10-04 15:48:09 +03:00
|
|
|
js // TODO
|
2019-09-27 00:30:41 +03:00
|
|
|
android
|
|
|
|
solaris
|
2019-06-23 07:34:41 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
|
|
|
|
enum Pass {
|
2019-07-16 18:59:07 +03:00
|
|
|
// A very short pass that only looks at imports in the beginning of
|
|
|
|
// each file
|
2019-07-07 23:30:15 +03:00
|
|
|
imports
|
2019-07-16 18:59:07 +03:00
|
|
|
// First pass, only parses and saves declarations (fn signatures,
|
|
|
|
// consts, types).
|
2019-06-22 21:20:28 +03:00
|
|
|
// Skips function bodies.
|
2019-07-16 18:59:07 +03:00
|
|
|
// We need this because in V things can be used before they are
|
|
|
|
// declared.
|
2019-07-07 23:30:15 +03:00
|
|
|
decl
|
2019-06-22 21:20:28 +03:00
|
|
|
// Second pass, parses function bodies and generates C or machine code.
|
|
|
|
main
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
struct V {
|
|
|
|
mut:
|
2019-10-04 15:48:09 +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?)
|
|
|
|
table &Table // table with types, vars, functions etc
|
|
|
|
cgen &CGen // C code generator
|
2019-09-13 14:25:05 +03:00
|
|
|
pref &Preferences // all the preferences and settings extracted to a struct for reusability
|
2019-10-04 15:48:09 +03:00
|
|
|
lang_dir string // "~/code/v"
|
|
|
|
out_name string // "program.exe"
|
2019-06-25 15:24:12 +03:00
|
|
|
vroot string
|
2019-10-04 15:48:09 +03:00
|
|
|
mod string // module being built with -lib
|
2019-09-20 03:01:52 +03:00
|
|
|
parsers []Parser
|
2019-09-29 04:54:12 +03:00
|
|
|
vgen_buf strings.Builder // temporary buffer for generated V code (.str() etc)
|
2019-10-12 01:17:37 +03:00
|
|
|
cached_mods []string
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-30 23:44:15 +03:00
|
|
|
struct Preferences {
|
|
|
|
mut:
|
2019-10-04 15:48:09 +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 // for hot code reloading
|
|
|
|
is_so bool
|
|
|
|
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
|
2019-10-12 13:54:01 +03:00
|
|
|
|
|
|
|
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_cache bool // turns on v usage of the module cache to speed up compilation.
|
|
|
|
|
2019-10-07 08:51:26 +03:00
|
|
|
is_stats bool // `v -stats file_test.v` will produce more detailed statistics for the tests that were run
|
2019-10-04 15:48:09 +03:00
|
|
|
no_auto_free bool // `v -nofree` disable automatic `free()` insertion for better performance in some applications (e.g. compilers)
|
2019-07-18 20:00:49 +03:00
|
|
|
cflags string // Additional options which will be passed to the C compiler.
|
2019-08-18 17:25:33 +03: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'.
|
2019-10-04 15:48:09 +03:00
|
|
|
ccompiler string // the name of the used C compiler
|
|
|
|
building_v bool
|
|
|
|
autofree bool
|
|
|
|
compress bool
|
2019-10-12 22:14:16 +03:00
|
|
|
//skip_builtin bool // Skips re-compilation of the builtin module
|
2019-10-04 15:48:09 +03:00
|
|
|
// to increase compilation time.
|
|
|
|
// This is on by default, since a vast majority of users do not
|
|
|
|
// work on the builtin module itself.
|
2019-06-30 23:44:15 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn main() {
|
2019-07-03 14:20:43 +03:00
|
|
|
// There's no `flags` module yet, so args have to be parsed manually
|
2019-07-16 18:07:28 +03:00
|
|
|
args := env_vflags_and_os_args()
|
2019-06-22 21:20:28 +03:00
|
|
|
// Print the version and exit.
|
2019-07-01 22:51:51 +03:00
|
|
|
if '-v' in args || '--version' in args || 'version' in args {
|
2019-09-11 01:12:46 +03:00
|
|
|
version_hash := vhash()
|
|
|
|
println('V $Version $version_hash')
|
2019-06-22 21:20:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if '-h' in args || '--help' in args || 'help' in args {
|
|
|
|
println(HelpText)
|
2019-06-23 03:40:36 +03:00
|
|
|
return
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-24 18:42:44 +03:00
|
|
|
if 'translate' in args {
|
2019-08-17 22:19:37 +03:00
|
|
|
println('Translating C to V will be available in V 0.3')
|
|
|
|
return
|
|
|
|
}
|
2019-07-31 05:40:38 +03:00
|
|
|
if 'up' in args {
|
2019-08-17 22:19:37 +03:00
|
|
|
update_v()
|
|
|
|
return
|
|
|
|
}
|
2019-08-01 02:34:28 +03:00
|
|
|
if 'get' in args {
|
2019-09-11 13:35:03 +03:00
|
|
|
println('use `v install` to install modules from vpm.vlang.io ')
|
2019-08-17 22:19:37 +03:00
|
|
|
return
|
|
|
|
}
|
2019-08-27 19:35:48 +03:00
|
|
|
if 'symlink' in args {
|
|
|
|
create_symlink()
|
|
|
|
return
|
|
|
|
}
|
2019-08-01 02:34:28 +03:00
|
|
|
if 'install' in args {
|
2019-09-10 11:22:16 +03:00
|
|
|
install_v(args)
|
2019-08-10 13:21:31 +03:00
|
|
|
return
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
|
|
|
// TODO quit if the compiler is too old
|
2019-07-02 01:00:27 +03:00
|
|
|
// u := os.file_last_mod_unix('v')
|
2019-06-22 23:00:38 +03:00
|
|
|
// If there's no tmp path with current version yet, the user must be using a pre-built package
|
2019-09-29 04:54:12 +03:00
|
|
|
//
|
2019-06-22 21:20:28 +03:00
|
|
|
// Just fmt and exit
|
2019-08-17 22:19:37 +03:00
|
|
|
if 'fmt' in args {
|
2019-09-11 01:49:56 +03:00
|
|
|
vfmt(args)
|
2019-06-22 21:20:28 +03:00
|
|
|
return
|
|
|
|
}
|
2019-10-09 06:01:43 +03:00
|
|
|
if 'test' in args {
|
|
|
|
test_v()
|
2019-09-16 17:29:06 +03:00
|
|
|
return
|
|
|
|
}
|
2019-10-09 06:01:43 +03:00
|
|
|
// Construct the V object from command line arguments
|
|
|
|
mut v := new_v(args)
|
2019-06-30 23:44:15 +03:00
|
|
|
if v.pref.is_verbose {
|
2019-06-22 21:20:28 +03:00
|
|
|
println(args)
|
|
|
|
}
|
|
|
|
// Generate the docs and exit
|
2019-08-17 22:19:37 +03:00
|
|
|
if 'doc' in args {
|
2019-06-30 23:44:15 +03:00
|
|
|
// v.gen_doc_html_for_module(args.last())
|
2019-06-23 11:12:09 +03:00
|
|
|
exit(0)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
|
2019-08-06 19:04:55 +03:00
|
|
|
if 'run' in args {
|
2019-08-09 23:37:31 +03:00
|
|
|
// always recompile for now, too error prone to skip recompilation otherwise
|
|
|
|
// for example for -repl usage, especially when piping lines to v
|
2019-08-17 22:19:37 +03:00
|
|
|
v.compile()
|
2019-08-06 19:04:55 +03:00
|
|
|
v.run_compiled_executable_and_exit()
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
|
2019-09-04 11:50:44 +03:00
|
|
|
// No args? REPL
|
|
|
|
if args.len < 2 || (args.len == 2 && args[1] == '-') || 'runrepl' in args {
|
|
|
|
run_repl()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-07 08:51:26 +03:00
|
|
|
mut tmark := benchmark.new_benchmark()
|
|
|
|
v.compile()
|
|
|
|
if v.pref.is_stats {
|
|
|
|
tmark.stop()
|
|
|
|
println( 'compilation took: ' + tmark.total_duration().str() + 'ms')
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
|
2019-08-06 19:04:55 +03:00
|
|
|
if v.pref.is_test {
|
|
|
|
v.run_compiled_executable_and_exit()
|
|
|
|
}
|
2019-09-09 16:22:39 +03:00
|
|
|
|
2019-09-17 22:41:58 +03:00
|
|
|
// 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)
|
2019-09-19 17:25:00 +03:00
|
|
|
for _, f in v.table.fns {
|
2019-09-23 20:34:08 +03:00
|
|
|
//f.local_vars.free()
|
2019-09-19 17:25:00 +03:00
|
|
|
f.args.free()
|
|
|
|
//f.defer_text.free()
|
|
|
|
}
|
|
|
|
v.table.fns.free()
|
2019-09-17 22:41:58 +03:00
|
|
|
free(v.table)
|
2019-10-04 15:48:09 +03:00
|
|
|
//for p in parsers {}
|
2019-09-17 22:41:58 +03:00
|
|
|
println('done!')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-09-20 03:01:52 +03:00
|
|
|
fn (v mut V) add_parser(parser Parser) {
|
2019-10-04 15:48:09 +03:00
|
|
|
v.parsers << parser
|
|
|
|
}
|
|
|
|
|
2019-10-08 02:32:12 +03:00
|
|
|
fn (v &V) get_file_parser_index(file string) ?int {
|
2019-10-04 15:48:09 +03:00
|
|
|
for i, p in v.parsers {
|
2019-10-13 03:05:11 +03:00
|
|
|
if os.realpath(p.file_path_id) == os.realpath(file) {
|
2019-10-05 09:52:50 +03:00
|
|
|
return i
|
2019-10-04 15:48:09 +03:00
|
|
|
}
|
2019-10-05 07:10:28 +03:00
|
|
|
}
|
2019-10-08 02:32:12 +03:00
|
|
|
return error('parser for "$file" not found')
|
|
|
|
}
|
|
|
|
|
|
|
|
// find existing parser or create new one. returns v.parsers index
|
|
|
|
fn (v mut V) parse(file string, pass Pass) int {
|
|
|
|
//println('parse($file, $pass)')
|
|
|
|
pidx := v.get_file_parser_index(file) or {
|
|
|
|
mut p := v.new_parser_from_file(file)
|
|
|
|
p.parse(pass)
|
|
|
|
//if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
|
|
|
|
v.add_parser(p)
|
|
|
|
return v.parsers.len-1
|
|
|
|
}
|
|
|
|
v.parsers[pidx].parse(pass)
|
|
|
|
//if v.parsers[i].pref.autofree { v.parsers[i].scanner.text.free() free(v.parsers[i].scanner) }
|
|
|
|
return pidx
|
2019-09-20 03:01:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-30 23:44:15 +03:00
|
|
|
fn (v mut V) compile() {
|
2019-08-12 14:17:26 +03:00
|
|
|
// Emily: Stop people on linux from being able to build with msvc
|
|
|
|
if os.user_os() != 'windows' && v.os == .msvc {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror('Cannot build with msvc on ${os.user_os()}')
|
2019-08-12 14:17:26 +03:00
|
|
|
}
|
2019-06-30 23:44:15 +03:00
|
|
|
mut cgen := v.cgen
|
2019-06-22 21:20:28 +03:00
|
|
|
cgen.genln('// Generated by V')
|
2019-09-03 12:10:51 +03:00
|
|
|
if v.pref.is_verbose {
|
|
|
|
println('all .v files before:')
|
|
|
|
println(v.files)
|
|
|
|
}
|
2019-08-01 02:34:28 +03:00
|
|
|
v.add_v_files_to_compile()
|
2019-09-14 23:48:30 +03:00
|
|
|
if v.pref.is_verbose || v.pref.is_debug {
|
2019-06-22 21:20:28 +03:00
|
|
|
println('all .v files:')
|
2019-06-30 23:44:15 +03:00
|
|
|
println(v.files)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-28 22:26:44 +03:00
|
|
|
/*
|
2019-09-26 05:28:43 +03:00
|
|
|
if v.pref.is_debug {
|
|
|
|
println('\nparsers:')
|
|
|
|
for q in v.parsers {
|
|
|
|
println(q.file_name)
|
|
|
|
}
|
|
|
|
println('\nfiles:')
|
|
|
|
for q in v.files {
|
|
|
|
println(q)
|
|
|
|
}
|
|
|
|
}
|
2019-09-28 22:26:44 +03:00
|
|
|
*/
|
2019-06-22 21:20:28 +03:00
|
|
|
// First pass (declarations)
|
2019-06-30 23:44:15 +03:00
|
|
|
for file in v.files {
|
2019-10-04 15:48:09 +03:00
|
|
|
v.parse(file, .decl)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-12 01:17:37 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// Main pass
|
2019-07-29 19:21:36 +03:00
|
|
|
cgen.pass = Pass.main
|
2019-08-16 08:50:36 +03:00
|
|
|
if v.pref.is_debug {
|
2019-09-14 23:48:30 +03:00
|
|
|
$if js {
|
|
|
|
cgen.genln('const VDEBUG = 1;\n')
|
|
|
|
} $else {
|
|
|
|
cgen.genln('#define VDEBUG (1)')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v.os == .js {
|
|
|
|
cgen.genln('#define _VJS (1) ')
|
2019-08-16 08:50:36 +03:00
|
|
|
}
|
2019-08-23 03:10:37 +03:00
|
|
|
|
2019-09-10 23:08:48 +03:00
|
|
|
if v.pref.building_v {
|
|
|
|
cgen.genln('#ifndef V_COMMIT_HASH')
|
|
|
|
cgen.genln('#define V_COMMIT_HASH "' + vhash() + '"')
|
|
|
|
cgen.genln('#endif')
|
|
|
|
}
|
2019-10-07 01:31:01 +03:00
|
|
|
q := cgen.nogen // TODO hack
|
|
|
|
cgen.nogen = false
|
2019-09-14 23:48:30 +03:00
|
|
|
$if js {
|
|
|
|
cgen.genln(js_headers)
|
|
|
|
} $else {
|
|
|
|
cgen.genln(CommonCHeaders)
|
|
|
|
}
|
2019-08-23 13:43:31 +03:00
|
|
|
v.generate_hotcode_reloading_declarations()
|
2019-09-26 05:28:43 +03:00
|
|
|
// We need the cjson header for all the json decoding that will be done in
|
|
|
|
// default mode
|
2019-10-07 01:31:01 +03:00
|
|
|
imports_json := 'json' in v.table.imports
|
2019-07-02 01:00:27 +03:00
|
|
|
if v.pref.build_mode == .default_mode {
|
2019-06-22 21:20:28 +03:00
|
|
|
if imports_json {
|
2019-06-23 10:03:52 +03:00
|
|
|
cgen.genln('#include "cJSON.h"')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-07 01:31:01 +03:00
|
|
|
if v.pref.build_mode == .default_mode {
|
2019-06-22 21:20:28 +03:00
|
|
|
// If we declare these for all modes, then when running `v a.v` we'll get
|
|
|
|
// `/usr/bin/ld: multiple definition of 'total_m'`
|
2019-09-16 18:29:04 +03:00
|
|
|
$if !js {
|
2019-10-07 08:51:26 +03:00
|
|
|
cgen.genln('int g_test_oks = 0;')
|
|
|
|
cgen.genln('int g_test_fails = 0;')
|
2019-09-16 18:29:04 +03:00
|
|
|
}
|
2019-09-26 05:28:43 +03:00
|
|
|
if imports_json {
|
2019-08-17 22:19:37 +03:00
|
|
|
cgen.genln('
|
2019-06-22 21:20:28 +03:00
|
|
|
#define js_get(object, key) cJSON_GetObjectItemCaseSensitive((object), (key))
|
|
|
|
')
|
|
|
|
}
|
|
|
|
}
|
2019-09-11 15:07:18 +03:00
|
|
|
if '-debug_alloc' in os.args {
|
2019-06-22 21:20:28 +03:00
|
|
|
cgen.genln('#define DEBUG_ALLOC 1')
|
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
//cgen.genln('/*================================== FNS =================================*/')
|
2019-06-22 21:20:28 +03:00
|
|
|
cgen.genln('this line will be replaced with definitions')
|
2019-10-07 01:31:01 +03:00
|
|
|
mut defs_pos := cgen.lines.len - 1
|
|
|
|
if defs_pos == -1 {
|
|
|
|
defs_pos = 0
|
|
|
|
}
|
|
|
|
cgen.nogen = q
|
2019-06-30 23:44:15 +03:00
|
|
|
for file in v.files {
|
2019-10-04 15:48:09 +03:00
|
|
|
v.parse(file, .main)
|
2019-09-24 22:30:30 +03:00
|
|
|
//if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
|
2019-06-22 21:20:28 +03:00
|
|
|
// Format all files (don't format automatically generated vlib headers)
|
2019-06-30 23:44:15 +03:00
|
|
|
if !v.pref.nofmt && !file.contains('/vlib/') {
|
2019-06-22 21:20:28 +03:00
|
|
|
// new vfmt is not ready yet
|
|
|
|
}
|
|
|
|
}
|
2019-10-04 15:48:09 +03:00
|
|
|
// Generate .vh if we are building a module
|
|
|
|
if v.pref.build_mode == .build_module {
|
|
|
|
v.generate_vh()
|
|
|
|
}
|
2019-10-12 00:04:42 +03:00
|
|
|
|
2019-09-27 14:02:01 +03:00
|
|
|
// parse generated V code (str() methods etc)
|
2019-10-04 15:48:09 +03:00
|
|
|
mut vgen_parser := v.new_parser_from_string(v.vgen_buf.str(), 'vgen')
|
2019-09-27 14:02:01 +03:00
|
|
|
// free the string builder which held the generated methods
|
|
|
|
v.vgen_buf.free()
|
2019-09-26 05:28:43 +03:00
|
|
|
vgen_parser.parse(.main)
|
2019-10-05 09:52:50 +03:00
|
|
|
// v.parsers.add(vgen_parser)
|
2019-10-12 01:17:37 +03:00
|
|
|
|
2019-10-07 01:31:01 +03:00
|
|
|
// All definitions
|
|
|
|
mut def := strings.new_builder(10000)// Avoid unnecessary allocations
|
2019-09-14 23:48:30 +03:00
|
|
|
$if !js {
|
2019-10-07 01:31:01 +03:00
|
|
|
def.writeln(cgen.includes.join_lines())
|
|
|
|
def.writeln(cgen.typedefs.join_lines())
|
|
|
|
def.writeln(v.type_definitions())
|
|
|
|
def.writeln('\nstring _STR(const char*, ...);\n')
|
|
|
|
def.writeln('\nstring _STR_TMP(const char*, ...);\n')
|
|
|
|
def.writeln(cgen.fns.join_lines()) // fn definitions
|
2019-09-14 23:48:30 +03:00
|
|
|
} $else {
|
2019-10-07 01:31:01 +03:00
|
|
|
def.writeln(v.type_definitions())
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
2019-10-07 01:31:01 +03:00
|
|
|
def.writeln(cgen.consts.join_lines())
|
|
|
|
def.writeln(cgen.thread_args.join_lines())
|
2019-06-30 23:44:15 +03:00
|
|
|
if v.pref.is_prof {
|
2019-10-07 01:31:01 +03:00
|
|
|
def.writeln('; // Prof counters:')
|
|
|
|
def.writeln(v.prof_counters())
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-07 01:31:01 +03:00
|
|
|
cgen.lines[defs_pos] = def.str()
|
2019-10-12 01:17:37 +03:00
|
|
|
v.generate_init()
|
2019-09-14 23:48:30 +03:00
|
|
|
v.generate_main()
|
|
|
|
v.generate_hot_reload_code()
|
2019-08-19 01:18:07 +03:00
|
|
|
if v.pref.is_verbose {
|
|
|
|
v.log('flags=')
|
2019-09-06 15:12:04 +03:00
|
|
|
for flag in v.get_os_cflags() {
|
|
|
|
println(' * ' + flag.format())
|
|
|
|
}
|
2019-08-19 01:18:07 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
$if js {
|
2019-09-28 20:42:29 +03:00
|
|
|
cgen.genln('main__main();')
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
cgen.save()
|
2019-08-19 01:18:07 +03:00
|
|
|
v.cc()
|
|
|
|
}
|
|
|
|
|
2019-10-12 01:17:37 +03:00
|
|
|
fn (v mut V) generate_init() {
|
|
|
|
$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()
|
|
|
|
init_fn_name := mod_gen_name(v.mod) + '__init_consts'
|
2019-10-12 04:35:33 +03:00
|
|
|
v.cgen.genln('void ${init_fn_name}();\nvoid ${init_fn_name}() {\n$consts_init_body\n}')
|
2019-10-12 01:17:37 +03:00
|
|
|
v.cgen.nogen = nogen
|
|
|
|
}
|
2019-10-07 01:31:01 +03:00
|
|
|
if v.pref.build_mode == .default_mode {
|
2019-10-12 01:17:37 +03:00
|
|
|
mut call_mod_init := ''
|
|
|
|
mut call_mod_init_consts := ''
|
2019-10-12 08:41:41 +03:00
|
|
|
if 'builtin' in v.cached_mods {
|
2019-10-12 09:53:42 +03:00
|
|
|
v.cgen.genln('void builtin__init_consts();')
|
2019-10-12 08:41:41 +03:00
|
|
|
call_mod_init_consts += 'builtin__init_consts();\n'
|
|
|
|
}
|
2019-10-12 01:17:37 +03:00
|
|
|
for mod in v.table.imports {
|
|
|
|
init_fn_name := mod_gen_name(mod) + '__init'
|
2019-10-12 03:38:56 +03:00
|
|
|
if v.table.known_fn(init_fn_name) {
|
|
|
|
call_mod_init += '${init_fn_name}();\n'
|
|
|
|
}
|
2019-10-12 01:17:37 +03:00
|
|
|
if mod in v.cached_mods {
|
2019-10-12 09:53:42 +03:00
|
|
|
v.cgen.genln('void ${init_fn_name}_consts();')
|
2019-10-12 01:17:37 +03:00
|
|
|
call_mod_init_consts += '${init_fn_name}_consts();\n'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
consts_init_body := v.cgen.consts_init.join_lines()
|
2019-06-22 21:20:28 +03:00
|
|
|
// vlib can't have `init_consts()`
|
2019-10-12 01:17:37 +03:00
|
|
|
v.cgen.genln('void init() {
|
2019-08-18 17:25:33 +03:00
|
|
|
g_str_buf=malloc(1000);
|
2019-10-12 01:17:37 +03:00
|
|
|
$call_mod_init_consts
|
2019-08-17 22:19:37 +03:00
|
|
|
$consts_init_body
|
2019-10-12 01:17:37 +03:00
|
|
|
builtin__init();
|
|
|
|
$call_mod_init
|
2019-08-06 15:02:51 +03:00
|
|
|
}')
|
2019-06-22 21:20:28 +03:00
|
|
|
// _STR function can't be defined in vlib
|
2019-10-12 01:17:37 +03:00
|
|
|
v.cgen.genln('
|
2019-06-22 21:20:28 +03:00
|
|
|
string _STR(const char *fmt, ...) {
|
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, fmt);
|
2019-08-17 22:19:37 +03:00
|
|
|
size_t len = vsnprintf(0, 0, fmt, argptr) + 1;
|
2019-06-22 21:20:28 +03:00
|
|
|
va_end(argptr);
|
2019-08-17 22:19:37 +03:00
|
|
|
byte* buf = malloc(len);
|
2019-06-22 21:20:28 +03:00
|
|
|
va_start(argptr, fmt);
|
2019-08-25 01:12:11 +03:00
|
|
|
vsprintf((char *)buf, fmt, argptr);
|
2019-06-22 21:20:28 +03:00
|
|
|
va_end(argptr);
|
2019-08-17 22:19:37 +03:00
|
|
|
#ifdef DEBUG_ALLOC
|
|
|
|
puts("_STR:");
|
|
|
|
puts(buf);
|
|
|
|
#endif
|
2019-06-22 21:20:28 +03:00
|
|
|
return tos2(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
string _STR_TMP(const char *fmt, ...) {
|
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, fmt);
|
2019-08-25 01:12:11 +03:00
|
|
|
//size_t len = vsnprintf(0, 0, fmt, argptr) + 1;
|
2019-06-22 21:20:28 +03:00
|
|
|
va_end(argptr);
|
|
|
|
va_start(argptr, fmt);
|
2019-08-25 01:12:11 +03:00
|
|
|
vsprintf((char *)g_str_buf, fmt, argptr);
|
2019-06-22 21:20:28 +03:00
|
|
|
va_end(argptr);
|
2019-08-17 22:19:37 +03:00
|
|
|
#ifdef DEBUG_ALLOC
|
|
|
|
//puts("_STR_TMP:");
|
|
|
|
//puts(g_str_buf);
|
|
|
|
#endif
|
2019-06-22 21:20:28 +03:00
|
|
|
return tos2(g_str_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
')
|
|
|
|
}
|
2019-10-12 01:17:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (v mut V) generate_main() {
|
|
|
|
mut cgen := v.cgen
|
|
|
|
$if js { return }
|
|
|
|
|
2019-10-12 13:54:01 +03:00
|
|
|
if v.pref.is_vlines {
|
|
|
|
///// 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('')
|
|
|
|
cgen.genln('////////////////// Reset the file/line numbers //////////')
|
|
|
|
cgen.lines << '#line $lines_so_far "${cescaped_path(os.realpath(cgen.out_path))}"'
|
|
|
|
cgen.genln('')
|
|
|
|
}
|
2019-08-19 01:18:07 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// Make sure the main function exists
|
|
|
|
// Obviously we don't need it in libraries
|
2019-09-10 18:19:29 +03:00
|
|
|
if v.pref.build_mode != .build_module {
|
2019-06-30 23:44:15 +03:00
|
|
|
if !v.table.main_exists() && !v.pref.is_test {
|
2019-06-22 21:20:28 +03:00
|
|
|
// It can be skipped in single file programs
|
2019-06-30 23:44:15 +03:00
|
|
|
if v.pref.is_script {
|
2019-06-28 13:41:09 +03:00
|
|
|
//println('Generating main()...')
|
2019-09-28 20:42:29 +03:00
|
|
|
v.gen_main_start(true)
|
2019-08-10 01:56:44 +03:00
|
|
|
cgen.genln('$cgen.fn_main;')
|
2019-09-28 20:42:29 +03:00
|
|
|
v.gen_main_end('return 0')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-09-28 20:42:29 +03:00
|
|
|
verror('function `main` is not declared in the main module')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-30 23:44:15 +03:00
|
|
|
else if v.pref.is_test {
|
2019-09-22 09:48:15 +03:00
|
|
|
if v.table.main_exists() {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror('test files cannot have function `main`')
|
2019-09-28 20:42:29 +03:00
|
|
|
}
|
2019-09-22 09:48:15 +03:00
|
|
|
if !v.table.has_at_least_one_test_fn() {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror('test files need to have at least one test function')
|
2019-09-28 20:42:29 +03:00
|
|
|
}
|
|
|
|
// Generate a C `main`, which calls every single test function
|
|
|
|
v.gen_main_start(false)
|
2019-10-07 08:51:26 +03:00
|
|
|
|
|
|
|
if v.pref.is_stats { cgen.genln('BenchedTests bt = main__start_testing();') }
|
|
|
|
|
2019-08-19 01:18:07 +03:00
|
|
|
for _, f in v.table.fns {
|
2019-09-28 20:42:29 +03:00
|
|
|
if f.name.starts_with('main__test_') {
|
2019-10-07 08:51:26 +03:00
|
|
|
if v.pref.is_stats { cgen.genln('BenchedTests_testing_step_start(&bt, tos3("$f.name"));') }
|
|
|
|
cgen.genln('$f.name();')
|
|
|
|
if v.pref.is_stats { cgen.genln('BenchedTests_testing_step_end(&bt);') }
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-07 08:51:26 +03:00
|
|
|
if v.pref.is_stats { cgen.genln('BenchedTests_end_testing(&bt);') }
|
|
|
|
v.gen_main_end('return g_test_fails > 0')
|
2019-09-28 20:42:29 +03:00
|
|
|
}
|
|
|
|
else if v.table.main_exists() {
|
|
|
|
v.gen_main_start(true)
|
|
|
|
cgen.genln(' main__main();')
|
|
|
|
v.gen_main_end('return 0')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-06 19:04:55 +03:00
|
|
|
}
|
|
|
|
|
2019-09-28 20:42:29 +03:00
|
|
|
fn (v mut V) gen_main_start(add_os_args bool){
|
|
|
|
v.cgen.genln('int main(int argc, char** argv) { ')
|
2019-10-12 01:17:37 +03:00
|
|
|
v.cgen.genln(' init();')
|
2019-09-28 20:42:29 +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('')
|
|
|
|
}
|
|
|
|
fn (v mut V) gen_main_end(return_statement string){
|
|
|
|
v.cgen.genln('')
|
|
|
|
v.cgen.genln(' $return_statement;')
|
|
|
|
v.cgen.genln('}')
|
|
|
|
}
|
|
|
|
|
2019-08-06 19:04:55 +03:00
|
|
|
fn final_target_out_name(out_name string) string {
|
2019-10-12 00:54:25 +03:00
|
|
|
$if windows {
|
|
|
|
return out_name.replace('/', '\\') + '.exe'
|
|
|
|
}
|
|
|
|
return if out_name.starts_with('/') {
|
2019-08-06 19:04:55 +03:00
|
|
|
out_name
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
'./' + out_name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (v V) run_compiled_executable_and_exit() {
|
|
|
|
if v.pref.is_verbose {
|
2019-08-17 22:19:37 +03:00
|
|
|
println('============ running $v.out_name ============')
|
|
|
|
}
|
2019-09-06 15:12:04 +03:00
|
|
|
mut cmd := '"' + final_target_out_name(v.out_name).replace('.exe','') + '"'
|
2019-08-06 19:04:55 +03:00
|
|
|
if os.args.len > 3 {
|
|
|
|
cmd += ' ' + os.args.right(3).join(' ')
|
|
|
|
}
|
|
|
|
if v.pref.is_test {
|
2019-06-26 17:11:25 +03:00
|
|
|
ret := os.system(cmd)
|
2019-06-22 21:20:28 +03:00
|
|
|
if ret != 0 {
|
2019-06-23 11:12:09 +03:00
|
|
|
exit(1)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-08-06 19:04:55 +03:00
|
|
|
if v.pref.is_run {
|
|
|
|
ret := os.system(cmd)
|
2019-08-17 22:19:37 +03:00
|
|
|
// TODO: make the runner wrapping as transparent as possible
|
|
|
|
// (i.e. use execve when implemented). For now though, the runner
|
2019-10-08 02:34:55 +03:00
|
|
|
// just returns the same exit code as the child process.
|
|
|
|
exit( ret )
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-08-06 19:04:55 +03:00
|
|
|
exit(0)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-30 23:44:15 +03:00
|
|
|
fn (v &V) v_files_from_dir(dir string) []string {
|
2019-06-22 21:20:28 +03:00
|
|
|
mut res := []string
|
2019-06-23 19:12:21 +03:00
|
|
|
if !os.file_exists(dir) {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror('$dir doesn\'t exist')
|
2019-06-23 19:12:21 +03:00
|
|
|
} else if !os.dir_exists(dir) {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror('$dir isn\'t a directory')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-23 12:09:23 +03:00
|
|
|
mut files := os.ls(dir)
|
2019-06-30 23:44:15 +03:00
|
|
|
if v.pref.is_verbose {
|
2019-06-22 21:20:28 +03:00
|
|
|
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-07-24 00:23:13 +03:00
|
|
|
if file.ends_with('_win.v') && (v.os != .windows && v.os != .msvc) {
|
2019-06-22 21:20:28 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
if file.ends_with('_lin.v') && v.os != .linux {
|
2019-06-22 21:20:28 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
if file.ends_with('_mac.v') && v.os != .mac {
|
2019-07-15 20:19:53 +03:00
|
|
|
continue
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-07-24 00:23:13 +03:00
|
|
|
if file.ends_with('_nix.v') && (v.os == .windows || v.os == .msvc) {
|
2019-08-17 22:19:37 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
if file.ends_with('_js.v') && v.os != .js {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if file.ends_with('_c.v') && v.os == .js {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-12 22:31:05 +03:00
|
|
|
res << '$dir${os.path_separator}$file'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parses imports, adds necessary libs, and then user files
|
2019-08-01 02:34:28 +03:00
|
|
|
fn (v mut V) add_v_files_to_compile() {
|
2019-10-07 01:31:01 +03:00
|
|
|
mut builtin_files := v.get_builtin_files()
|
|
|
|
// Builtin cache exists? Use it.
|
2019-10-12 22:31:05 +03:00
|
|
|
builtin_vh := '$v_modules_path${os.path_separator}vlib${os.path_separator}builtin.vh'
|
2019-10-12 13:54:01 +03:00
|
|
|
if v.pref.is_cache && os.file_exists(builtin_vh) {
|
2019-10-12 08:41:41 +03:00
|
|
|
v.cached_mods << 'builtin'
|
2019-10-07 01:31:01 +03:00
|
|
|
builtin_files = [builtin_vh]
|
|
|
|
}
|
2019-10-04 15:48:09 +03:00
|
|
|
// Parse builtin imports
|
2019-10-07 01:31:01 +03:00
|
|
|
for file in builtin_files {
|
2019-10-04 15:48:09 +03:00
|
|
|
// add builtins first
|
|
|
|
v.files << file
|
|
|
|
mut p := v.new_parser_from_file(file)
|
|
|
|
p.parse(.imports)
|
|
|
|
//if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
|
2019-10-05 09:52:50 +03:00
|
|
|
v.add_parser(p)
|
2019-10-04 15:48:09 +03:00
|
|
|
}
|
|
|
|
// Parse user imports
|
|
|
|
for file in v.get_user_files() {
|
|
|
|
mut p := v.new_parser_from_file(file)
|
|
|
|
p.parse(.imports)
|
|
|
|
//if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
|
2019-10-05 09:52:50 +03:00
|
|
|
v.add_parser(p)
|
2019-10-04 15:48:09 +03:00
|
|
|
}
|
|
|
|
// Parse lib imports
|
|
|
|
v.parse_lib_imports()
|
|
|
|
if v.pref.is_verbose {
|
|
|
|
v.log('imports:')
|
|
|
|
println(v.table.imports)
|
|
|
|
}
|
2019-10-07 01:31:01 +03:00
|
|
|
// 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
|
|
|
|
}
|
2019-10-04 15:48:09 +03:00
|
|
|
|
|
|
|
// use cached built module if exists
|
2019-10-12 01:17:37 +03:00
|
|
|
if v.pref.build_mode != .build_module && !mod.contains('vweb') {
|
2019-10-12 22:31:05 +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-10-12 13:54:01 +03:00
|
|
|
if v.pref.is_cache && os.file_exists(vh_path) {
|
2019-10-07 01:31:01 +03:00
|
|
|
println('using cached module `$mod`: $vh_path')
|
2019-10-12 01:17:37 +03:00
|
|
|
v.cached_mods << mod
|
2019-10-07 01:31:01 +03:00
|
|
|
v.files << vh_path
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-10-04 15:48:09 +03:00
|
|
|
// standard module
|
2019-10-08 02:32:12 +03:00
|
|
|
mod_path := v.find_module_path(mod) or { verror(err) break }
|
|
|
|
vfiles := v.v_files_from_dir(mod_path)
|
2019-10-04 15:48:09 +03:00
|
|
|
for file in vfiles {
|
|
|
|
v.files << file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add remaining main files last
|
|
|
|
for _, fit in v.table.file_imports {
|
|
|
|
if fit.module_name != 'main' { continue }
|
2019-10-13 03:05:11 +03:00
|
|
|
v.files << fit.file_path_id
|
2019-10-04 15:48:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (v &V) get_builtin_files() []string {
|
2019-10-07 01:31:01 +03:00
|
|
|
// .vh cache exists? Use it
|
|
|
|
|
2019-10-04 15:48:09 +03:00
|
|
|
$if js {
|
2019-10-12 22:31:05 +03:00
|
|
|
return v.v_files_from_dir('$v.vroot${os.path_separator}vlib${os.path_separator}builtin${os.path_separator}js')
|
2019-10-04 15:48:09 +03:00
|
|
|
}
|
2019-10-12 22:31:05 +03:00
|
|
|
return v.v_files_from_dir('$v.vroot${os.path_separator}vlib${os.path_separator}builtin')
|
2019-10-04 15:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get user files
|
|
|
|
fn (v &V) get_user_files() []string {
|
2019-10-05 09:52:50 +03:00
|
|
|
mut dir := v.dir
|
2019-10-04 15:48:09 +03:00
|
|
|
v.log('get_v_files($dir)')
|
2019-06-22 21:20:28 +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
|
2019-10-07 08:51:26 +03:00
|
|
|
|
|
|
|
if v.pref.is_test && v.pref.is_stats {
|
2019-10-12 22:31:05 +03:00
|
|
|
user_files << [v.vroot, 'vlib', 'benchmark', 'tests', 'always_imported.v'].join( os.path_separator )
|
2019-10-07 08:51:26 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// v volt/slack_test.v: compile all .v files to get the environment
|
|
|
|
// I need to implement user packages! TODO
|
|
|
|
is_test_with_imports := dir.ends_with('_test.v') &&
|
2019-10-12 22:31:05 +03:00
|
|
|
(dir.contains('${os.path_separator}volt') || dir.contains('${os.path_separator}c2volt'))// TODO
|
2019-06-22 21:20:28 +03:00
|
|
|
if is_test_with_imports {
|
|
|
|
user_files << dir
|
2019-10-12 22:31:05 +03:00
|
|
|
pos := dir.last_index(os.path_separator)
|
|
|
|
dir = dir.left(pos) + os.path_separator// TODO WHY IS THIS .neEDED?
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
if dir.ends_with('.v') {
|
|
|
|
// Just compile one file and get parent dir
|
|
|
|
user_files << dir
|
2019-10-12 22:31:05 +03:00
|
|
|
dir = dir.all_before('${os.path_separator}')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-09-14 23:48:30 +03:00
|
|
|
// Add .v files from the directory being compiled
|
2019-06-30 23:44:15 +03:00
|
|
|
files := v.v_files_from_dir(dir)
|
2019-06-22 21:20:28 +03:00
|
|
|
for file in files {
|
|
|
|
user_files << file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if user_files.len == 0 {
|
2019-06-23 11:12:09 +03:00
|
|
|
println('No input .v files')
|
|
|
|
exit(1)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-30 23:44:15 +03:00
|
|
|
if v.pref.is_verbose {
|
|
|
|
v.log('user_files:')
|
2019-06-22 21:20:28 +03:00
|
|
|
println(user_files)
|
|
|
|
}
|
2019-10-04 15:48:09 +03:00
|
|
|
return user_files
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse deps from already parsed builtin/user files
|
|
|
|
fn (v mut V) parse_lib_imports() {
|
|
|
|
mut done_fits := []string
|
2019-10-05 07:10:28 +03:00
|
|
|
mut done_imports := []string
|
2019-10-04 15:48:09 +03:00
|
|
|
for {
|
2019-10-05 07:10:28 +03:00
|
|
|
for _, fit in v.table.file_imports {
|
2019-10-13 03:05:11 +03:00
|
|
|
if fit.file_path_id in done_fits { continue }
|
2019-10-05 07:10:28 +03:00
|
|
|
for _, mod in fit.imports {
|
2019-10-13 07:11:40 +03:00
|
|
|
if mod in done_imports { continue }
|
2019-10-08 02:32:12 +03:00
|
|
|
import_path := v.find_module_path(mod) or {
|
2019-10-13 03:05:11 +03:00
|
|
|
pidx := v.get_file_parser_index(fit.file_path_id) or { verror(err) break }
|
2019-10-08 02:32:12 +03:00
|
|
|
v.parsers[pidx].error_with_token_index('cannot import module "$mod" (not found)', fit.get_import_tok_idx(mod))
|
|
|
|
break
|
|
|
|
}
|
2019-10-05 07:10:28 +03:00
|
|
|
vfiles := v.v_files_from_dir(import_path)
|
|
|
|
if vfiles.len == 0 {
|
2019-10-13 03:05:11 +03:00
|
|
|
pidx := v.get_file_parser_index(fit.file_path_id) or { verror(err) break }
|
2019-10-08 02:32:12 +03:00
|
|
|
v.parsers[pidx].error_with_token_index('cannot import module "$mod" (no .v files in "$import_path")', fit.get_import_tok_idx(mod))
|
2019-10-05 07:10:28 +03:00
|
|
|
}
|
|
|
|
// Add all imports referenced by these libs
|
|
|
|
for file in vfiles {
|
2019-10-05 09:52:50 +03:00
|
|
|
pid := v.parse(file, .imports)
|
|
|
|
p_mod := v.parsers[pid].import_table.module_name
|
|
|
|
if p_mod != mod {
|
2019-10-13 03:05:11 +03:00
|
|
|
v.parsers[pid].error_with_token_index('bad module definition: $fit.file_path_id imports module "$mod" but $file is defined as module `$p_mod`', 1)
|
2019-10-05 09:52:50 +03:00
|
|
|
}
|
2019-09-30 13:40:19 +03:00
|
|
|
}
|
2019-10-13 07:11:40 +03:00
|
|
|
done_imports << mod
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-13 03:05:11 +03:00
|
|
|
done_fits << fit.file_path_id
|
2019-10-05 07:10:28 +03:00
|
|
|
}
|
|
|
|
if v.table.file_imports.size == done_fits.len { break}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-04 15:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// return resolved dep graph (order deps)
|
|
|
|
fn (v &V) resolve_deps() &DepGraph {
|
2019-09-03 19:11:21 +03:00
|
|
|
mut dep_graph := new_dep_graph()
|
2019-08-05 10:49:52 +03:00
|
|
|
dep_graph.from_import_tables(v.table.file_imports)
|
2019-07-21 18:53:35 +03:00
|
|
|
deps_resolved := dep_graph.resolve()
|
|
|
|
if !deps_resolved.acyclic {
|
|
|
|
deps_resolved.display()
|
2019-10-04 15:48:09 +03:00
|
|
|
verror('import cycle detected')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-04 15:48:09 +03:00
|
|
|
return deps_resolved
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_arg(joined_args, arg, def string) string {
|
2019-10-09 06:01:43 +03:00
|
|
|
return get_param_after(joined_args, '-$arg', def)
|
2019-08-27 21:08:54 +03:00
|
|
|
}
|
|
|
|
|
2019-10-09 06:01:43 +03:00
|
|
|
fn get_param_after(joined_args, arg, def string) string {
|
2019-08-27 21:08:54 +03:00
|
|
|
key := '$arg '
|
2019-06-22 21:20:28 +03:00
|
|
|
mut pos := joined_args.index(key)
|
|
|
|
if pos == -1 {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
pos += key.len
|
|
|
|
mut space := joined_args.index_after(' ', pos)
|
|
|
|
if space == -1 {
|
|
|
|
space = joined_args.len
|
|
|
|
}
|
|
|
|
res := joined_args.substr(pos, space)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-06-30 23:44:15 +03:00
|
|
|
fn (v &V) log(s string) {
|
|
|
|
if !v.pref.is_verbose {
|
2019-06-22 21:20:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
println(s)
|
|
|
|
}
|
|
|
|
|
2019-09-01 22:51:16 +03:00
|
|
|
fn new_v(args[]string) &V {
|
2019-10-12 05:04:56 +03:00
|
|
|
// Create modules dirs if they are missing
|
|
|
|
if !os.dir_exists(v_modules_path) {
|
|
|
|
os.mkdir(v_modules_path)
|
2019-10-12 22:31:05 +03:00
|
|
|
os.mkdir('$v_modules_path${os.path_separator}cache')
|
2019-10-12 05:04:56 +03:00
|
|
|
}
|
|
|
|
|
2019-09-27 14:02:01 +03:00
|
|
|
mut vgen_buf := strings.new_builder(1000)
|
|
|
|
vgen_buf.writeln('module main\nimport strings')
|
2019-09-26 05:28:43 +03:00
|
|
|
|
2019-08-27 21:08:54 +03:00
|
|
|
joined_args := args.join(' ')
|
|
|
|
target_os := get_arg(joined_args, 'os', '')
|
|
|
|
mut out_name := get_arg(joined_args, 'o', 'a.out')
|
2019-10-05 07:10:28 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
mut dir := args.last()
|
2019-09-11 15:07:18 +03:00
|
|
|
if 'run' in args {
|
2019-10-09 06:01:43 +03:00
|
|
|
dir = get_param_after(joined_args, 'run', '')
|
2019-06-26 15:13:02 +03:00
|
|
|
}
|
2019-10-12 22:31:05 +03:00
|
|
|
if dir.ends_with(os.path_separator) {
|
|
|
|
dir = dir.all_before_last(os.path_separator)
|
2019-09-06 13:08:50 +03:00
|
|
|
}
|
2019-10-12 22:31:05 +03:00
|
|
|
if dir.starts_with('.$os.path_separator') {
|
2019-10-12 08:41:41 +03:00
|
|
|
dir = dir.right(2)
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
if args.len < 2 {
|
|
|
|
dir = ''
|
|
|
|
}
|
|
|
|
// build mode
|
2019-07-02 01:00:27 +03:00
|
|
|
mut build_mode := BuildMode.default_mode
|
2019-08-17 22:19:37 +03:00
|
|
|
mut mod := ''
|
|
|
|
if joined_args.contains('build module ') {
|
2019-09-10 18:19:29 +03:00
|
|
|
build_mode = .build_module
|
2019-09-14 23:48:30 +03:00
|
|
|
// v build module ~/v/os => os.o
|
2019-10-12 12:48:57 +03:00
|
|
|
mod_path := if dir.contains('vlib') {
|
2019-10-12 22:31:05 +03:00
|
|
|
dir.all_after('vlib'+os.path_separator)
|
2019-10-12 01:17:37 +03:00
|
|
|
}
|
2019-10-12 12:48:57 +03:00
|
|
|
else if dir.starts_with('.\\') || dir.starts_with('./') {
|
|
|
|
dir.right(2)
|
|
|
|
}
|
2019-10-12 22:31:05 +03:00
|
|
|
else if dir.starts_with(os.path_separator) {
|
|
|
|
dir.all_after(os.path_separator)
|
2019-09-06 13:08:50 +03:00
|
|
|
} else {
|
2019-10-12 12:48:57 +03:00
|
|
|
dir
|
2019-09-03 12:10:51 +03:00
|
|
|
}
|
2019-10-12 22:31:05 +03:00
|
|
|
mod = mod_path.replace(os.path_separator, '.')
|
2019-09-03 12:10:51 +03:00
|
|
|
println('Building module "${mod}" (dir="$dir")...')
|
2019-07-02 01:00:27 +03:00
|
|
|
//out_name = '$TmpPath/vlib/${base}.o'
|
2019-10-12 01:17:37 +03:00
|
|
|
out_name = mod
|
2019-06-22 21:20:28 +03:00
|
|
|
// Cross compiling? Use separate dirs for each os
|
2019-10-04 15:48:09 +03:00
|
|
|
/*
|
2019-06-22 21:20:28 +03:00
|
|
|
if target_os != os.user_os() {
|
|
|
|
os.mkdir('$TmpPath/vlib/$target_os')
|
|
|
|
out_name = '$TmpPath/vlib/$target_os/${base}.o'
|
2019-08-17 22:19:37 +03:00
|
|
|
println('target_os=$target_os user_os=${os.user_os()}')
|
2019-07-02 01:00:27 +03:00
|
|
|
println('!Cross compiling $out_name')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-10-04 15:48:09 +03:00
|
|
|
*/
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
is_test := dir.ends_with('_test.v')
|
|
|
|
is_script := dir.ends_with('.v')
|
|
|
|
if is_script && !os.file_exists(dir) {
|
2019-06-23 11:12:09 +03:00
|
|
|
println('`$dir` does not exist')
|
|
|
|
exit(1)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// No -o provided? foo.v => foo
|
2019-10-12 03:03:37 +03:00
|
|
|
if out_name == 'a.out' && dir.ends_with('.v') && dir != '.v' {
|
2019-06-22 21:20:28 +03:00
|
|
|
out_name = dir.left(dir.len - 2)
|
|
|
|
}
|
|
|
|
// if we are in `/foo` and run `v .`, the executable should be `foo`
|
|
|
|
if dir == '.' && out_name == 'a.out' {
|
2019-10-12 22:31:05 +03:00
|
|
|
base := os.getwd().all_after(os.path_separator)
|
2019-06-22 21:20:28 +03:00
|
|
|
out_name = base.trim_space()
|
|
|
|
}
|
2019-07-07 23:30:15 +03:00
|
|
|
mut _os := OS.mac
|
2019-06-22 21:20:28 +03:00
|
|
|
// No OS specifed? Use current system
|
|
|
|
if target_os == '' {
|
|
|
|
$if linux {
|
2019-08-17 22:19:37 +03:00
|
|
|
_os = .linux
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
$if mac {
|
2019-07-07 23:30:15 +03:00
|
|
|
_os = .mac
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
$if windows {
|
2019-07-07 23:30:15 +03:00
|
|
|
_os = .windows
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-15 18:24:40 +03:00
|
|
|
$if freebsd {
|
2019-08-17 22:19:37 +03:00
|
|
|
_os = .freebsd
|
2019-07-15 18:24:40 +03:00
|
|
|
}
|
2019-07-15 21:18:05 +03:00
|
|
|
$if openbsd {
|
2019-08-17 22:19:37 +03:00
|
|
|
_os = .openbsd
|
2019-07-15 21:18:05 +03:00
|
|
|
}
|
|
|
|
$if netbsd {
|
2019-08-17 22:19:37 +03:00
|
|
|
_os = .netbsd
|
2019-07-15 21:18:05 +03:00
|
|
|
}
|
|
|
|
$if dragonfly {
|
2019-08-17 22:19:37 +03:00
|
|
|
_os = .dragonfly
|
2019-07-15 21:18:05 +03:00
|
|
|
}
|
2019-09-27 00:30:41 +03:00
|
|
|
$if solaris {
|
|
|
|
_os = .solaris
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-10-12 00:54:25 +03:00
|
|
|
_os = os_from_string(target_os)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-25 02:38:58 +03:00
|
|
|
// Location of all vlib files
|
2019-08-17 22:19:37 +03:00
|
|
|
vroot := os.dir(os.executable())
|
|
|
|
//println('VROOT=$vroot')
|
|
|
|
// v.exe's parent directory should contain vlib
|
2019-09-06 15:12:04 +03:00
|
|
|
if !os.dir_exists(vroot) || !os.dir_exists(vroot + '/vlib/builtin') {
|
2019-09-30 23:56:53 +03:00
|
|
|
println('vlib not found, downloading it...')
|
2019-10-10 23:20:41 +03:00
|
|
|
/*
|
2019-09-30 23:56:53 +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-10-10 23:20:41 +03:00
|
|
|
*/
|
|
|
|
println('vlib not found. It should be next to the V executable. ')
|
|
|
|
println('Go to https://vlang.io to install V.')
|
|
|
|
exit(1)
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-10-12 08:41:41 +03:00
|
|
|
// println('out_name:$out_name')
|
2019-10-05 07:10:28 +03:00
|
|
|
mut out_name_c := os.realpath('${out_name}.tmp.c')
|
2019-07-16 19:15:14 +03:00
|
|
|
|
2019-09-22 10:56:02 +03:00
|
|
|
cflags := get_cmdline_cflags(args)
|
2019-07-16 19:15:14 +03:00
|
|
|
|
2019-09-11 01:08:23 +03:00
|
|
|
rdir := os.realpath( dir )
|
|
|
|
rdir_name := os.filename( rdir )
|
|
|
|
|
2019-09-11 15:07:18 +03:00
|
|
|
obfuscate := '-obf' in args
|
|
|
|
is_repl := '-repl' in args
|
2019-06-30 23:03:17 +03:00
|
|
|
pref := &Preferences {
|
2019-06-22 21:20:28 +03:00
|
|
|
is_test: is_test
|
|
|
|
is_script: is_script
|
2019-09-11 15:07:18 +03:00
|
|
|
is_so: '-shared' in args
|
|
|
|
is_prod: '-prod' in args
|
2019-09-16 17:29:06 +03:00
|
|
|
is_verbose: '-verbose' in args || '--verbose' in args
|
2019-10-12 13:54:01 +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-07 08:51:26 +03:00
|
|
|
is_stats: '-stats' in args
|
2019-06-30 23:03:17 +03:00
|
|
|
obfuscate: obfuscate
|
2019-09-11 15:07:18 +03:00
|
|
|
is_prof: '-prof' in args
|
|
|
|
is_live: '-live' in args
|
|
|
|
sanitize: '-sanitize' in args
|
|
|
|
nofmt: '-nofmt' in args
|
|
|
|
show_c_cmd: '-show_c_cmd' in args
|
|
|
|
translated: 'translated' in args
|
|
|
|
is_run: 'run' in args
|
2019-09-17 22:41:58 +03:00
|
|
|
autofree: '-autofree' in args
|
2019-09-19 15:52:38 +03:00
|
|
|
compress: '-compress' in args
|
2019-09-08 17:19:42 +03:00
|
|
|
is_repl: is_repl
|
2019-06-30 23:03:17 +03:00
|
|
|
build_mode: build_mode
|
2019-07-18 20:00:49 +03:00
|
|
|
cflags: cflags
|
2019-08-23 11:42:48 +03:00
|
|
|
ccompiler: find_c_compiler()
|
2019-09-13 18:59:17 +03:00
|
|
|
building_v: !is_repl && (rdir_name == 'compiler' || dir.contains('vlib'))
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-07-08 04:42:36 +03:00
|
|
|
if pref.is_verbose || pref.is_debug {
|
|
|
|
println('C compiler=$pref.ccompiler')
|
|
|
|
}
|
2019-07-15 10:09:23 +03:00
|
|
|
if pref.is_so {
|
2019-10-12 22:31:05 +03:00
|
|
|
out_name_c = out_name.all_after(os.path_separator) + '_shared_lib.c'
|
2019-06-30 23:03:17 +03:00
|
|
|
}
|
2019-08-31 16:38:13 +03:00
|
|
|
return &V{
|
2019-06-30 23:03:17 +03:00
|
|
|
os: _os
|
|
|
|
out_name: out_name
|
|
|
|
dir: dir
|
2019-08-17 22:19:37 +03:00
|
|
|
lang_dir: vroot
|
2019-06-30 23:03:17 +03:00
|
|
|
table: new_table(obfuscate)
|
|
|
|
out_name_c: out_name_c
|
|
|
|
cgen: new_cgen(out_name_c)
|
2019-08-17 22:19:37 +03:00
|
|
|
vroot: vroot
|
2019-06-30 23:03:17 +03:00
|
|
|
pref: pref
|
2019-08-17 22:19:37 +03:00
|
|
|
mod: mod
|
2019-09-27 14:02:01 +03:00
|
|
|
vgen_buf: vgen_buf
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 18:07:28 +03:00
|
|
|
fn env_vflags_and_os_args() []string {
|
|
|
|
mut args := []string
|
|
|
|
vflags := os.getenv('VFLAGS')
|
|
|
|
if '' != vflags {
|
2019-08-18 17:25:33 +03:00
|
|
|
args << os.args[0]
|
|
|
|
args << vflags.split(' ')
|
|
|
|
if os.args.len > 1 {
|
|
|
|
args << os.args.right(1)
|
|
|
|
}
|
2019-10-05 07:10:28 +03:00
|
|
|
} else{
|
2019-08-18 17:25:33 +03:00
|
|
|
args << os.args
|
2019-07-16 18:07:28 +03:00
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|
2019-07-31 05:40:38 +03:00
|
|
|
|
|
|
|
fn update_v() {
|
2019-08-17 22:19:37 +03:00
|
|
|
println('Updating V...')
|
|
|
|
vroot := os.dir(os.executable())
|
2019-08-08 08:30:05 +03:00
|
|
|
s := os.exec('git -C "$vroot" pull --rebase origin master') or {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror(err)
|
2019-08-29 03:30:17 +03:00
|
|
|
return
|
2019-08-08 08:30:05 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
println(s.output)
|
|
|
|
$if windows {
|
2019-09-15 04:33:08 +03:00
|
|
|
v_backup_file := '$vroot/v_old.exe'
|
|
|
|
if os.file_exists( v_backup_file ) {
|
|
|
|
os.rm( v_backup_file )
|
|
|
|
}
|
|
|
|
os.mv('$vroot/v.exe', v_backup_file)
|
2019-09-19 13:11:25 +03:00
|
|
|
s2 := os.exec('"$vroot/make.bat"') or {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror(err)
|
2019-08-29 03:30:17 +03:00
|
|
|
return
|
2019-08-08 08:30:05 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
println(s2.output)
|
|
|
|
} $else {
|
2019-08-08 08:30:05 +03:00
|
|
|
s2 := os.exec('make -C "$vroot"') or {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror(err)
|
2019-08-29 03:30:17 +03:00
|
|
|
return
|
2019-08-08 08:30:05 +03:00
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
println(s2.output)
|
|
|
|
}
|
|
|
|
}
|
2019-08-01 02:34:28 +03:00
|
|
|
|
2019-09-11 01:49:56 +03:00
|
|
|
fn vfmt(args[]string) {
|
|
|
|
file := args.last()
|
|
|
|
if !os.file_exists(file) {
|
|
|
|
println('"$file" does not exist')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
if !file.ends_with('.v') {
|
|
|
|
println('v fmt can only be used on .v files')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
println('vfmt is temporarily disabled')
|
|
|
|
}
|
|
|
|
|
2019-09-10 11:22:16 +03:00
|
|
|
fn install_v(args[]string) {
|
|
|
|
if args.len < 3 {
|
|
|
|
println('usage: v install [module] [module] [...]')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
names := args.slice(2, args.len)
|
|
|
|
vexec := os.executable()
|
|
|
|
vroot := os.dir(vexec)
|
|
|
|
vget := '$vroot/tools/vget'
|
|
|
|
if true {
|
|
|
|
//println('Building vget...')
|
|
|
|
os.chdir(vroot + '/tools')
|
2019-09-29 04:54:12 +03:00
|
|
|
vget_compilation := os.exec('$vexec -o $vget vget.v') or {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror(err)
|
2019-09-10 11:22:16 +03:00
|
|
|
return
|
|
|
|
}
|
2019-09-29 04:54:12 +03:00
|
|
|
if vget_compilation.exit_code != 0 {
|
|
|
|
verror( vget_compilation.output )
|
2019-09-10 11:22:16 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vgetresult := os.exec('$vget ' + names.join(' ')) or {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror(err)
|
2019-09-10 11:22:16 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if vgetresult.exit_code != 0 {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror( vgetresult.output )
|
2019-09-10 11:22:16 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 19:35:48 +03:00
|
|
|
fn create_symlink() {
|
|
|
|
vexe := os.executable()
|
|
|
|
link_path := '/usr/local/bin/v'
|
2019-08-31 16:38:13 +03:00
|
|
|
ret := os.system('ln -sf $vexe $link_path')
|
|
|
|
if ret == 0 {
|
|
|
|
println('symlink "$link_path" has been created')
|
|
|
|
} else {
|
|
|
|
println('failed to create symlink "$link_path", '+
|
|
|
|
'make sure you run with sudo')
|
|
|
|
}
|
2019-08-27 19:35:48 +03:00
|
|
|
}
|
|
|
|
|
2019-09-24 00:40:34 +03:00
|
|
|
pub fn verror(s string) {
|
2019-08-29 03:30:17 +03:00
|
|
|
println('V error: $s')
|
|
|
|
os.flush_stdout()
|
|
|
|
exit(1)
|
|
|
|
}
|
2019-09-10 23:08:48 +03:00
|
|
|
|
|
|
|
fn vhash() string {
|
|
|
|
mut buf := [50]byte
|
|
|
|
buf[0] = 0
|
2019-09-16 21:26:05 +03:00
|
|
|
C.snprintf(*char(buf), 50, '%s', C.V_COMMIT_HASH )
|
2019-09-09 16:22:39 +03:00
|
|
|
return tos_clone(buf)
|
2019-09-10 23:08:48 +03:00
|
|
|
}
|
2019-10-12 00:04:42 +03:00
|
|
|
|
|
|
|
fn cescaped_path(s string) string {
|
|
|
|
return s.replace('\\','\\\\')
|
|
|
|
}
|
2019-10-12 00:54:25 +03:00
|
|
|
|
|
|
|
fn os_from_string(os string) OS {
|
|
|
|
switch os {
|
|
|
|
case 'linux': return .linux
|
|
|
|
case 'windows': return .windows
|
|
|
|
case 'mac': return .mac
|
|
|
|
case 'freebsd': return .freebsd
|
|
|
|
case 'openbsd': return .openbsd
|
|
|
|
case 'netbsd': return .netbsd
|
|
|
|
case 'dragonfly': return .dragonfly
|
|
|
|
case 'msvc': return .msvc
|
|
|
|
case 'js': return .js
|
|
|
|
case 'solaris': return .solaris
|
|
|
|
case 'android': return .android
|
|
|
|
}
|
|
|
|
println('bad os $os') // todo panic?
|
|
|
|
return .linux
|
|
|
|
}
|