2020-04-07 00:44:19 +02: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 builder
|
|
|
|
|
2020-05-05 14:31:23 +02:00
|
|
|
import time
|
2020-04-25 17:49:16 +02:00
|
|
|
import os
|
|
|
|
import v.pref
|
2020-07-25 09:32:29 +02:00
|
|
|
import v.util
|
2020-04-07 00:44:19 +02:00
|
|
|
|
|
|
|
fn get_vtmp_folder() string {
|
2020-07-06 14:08:38 +02:00
|
|
|
mut vtmp := os.getenv('VTMP')
|
|
|
|
if vtmp.len > 0 {
|
|
|
|
return vtmp
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
2020-07-06 14:08:38 +02:00
|
|
|
vtmp = os.join_path(os.temp_dir(), 'v')
|
|
|
|
if !os.exists(vtmp) || !os.is_dir(vtmp) {
|
|
|
|
os.mkdir_all(vtmp)
|
|
|
|
}
|
|
|
|
os.setenv('VTMP', vtmp, true)
|
2020-04-07 00:44:19 +02:00
|
|
|
return vtmp
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_vtmp_filename(base_file_name, postfix string) string {
|
|
|
|
vtmp := get_vtmp_folder()
|
2020-04-07 01:02:48 +02:00
|
|
|
return os.real_path(os.join_path(vtmp, os.file_name(os.real_path(base_file_name)) + postfix))
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compile(command string, pref &pref.Preferences) {
|
2020-08-03 17:19:36 +02:00
|
|
|
odir := os.base_dir(pref.out_name)
|
|
|
|
// When pref.out_name is just the name of an executable, i.e. `./v -o executable main.v`
|
|
|
|
// without a folder component, just use the current folder instead:
|
|
|
|
mut output_folder := odir
|
|
|
|
if odir.len == pref.out_name.len {
|
|
|
|
output_folder = os.getwd()
|
2020-08-25 13:32:22 +02:00
|
|
|
}
|
|
|
|
os.is_writable_folder(output_folder) or {
|
2020-08-03 17:19:36 +02:00
|
|
|
// An early error here, is better than an unclear C error later:
|
|
|
|
verror(err)
|
|
|
|
exit(1)
|
|
|
|
}
|
2020-04-07 00:44:19 +02:00
|
|
|
// Construct the V object from command line arguments
|
|
|
|
mut b := new_builder(pref)
|
|
|
|
if pref.is_verbose {
|
|
|
|
println('builder.compile() pref:')
|
2020-04-10 18:11:43 +02:00
|
|
|
// println(pref)
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
2020-05-30 09:20:54 +02:00
|
|
|
mut sw := time.new_stopwatch({})
|
2020-04-15 23:16:49 +02:00
|
|
|
match pref.backend {
|
2020-04-26 09:17:13 +02:00
|
|
|
.c { b.compile_c() }
|
|
|
|
.js { b.compile_js() }
|
|
|
|
.x64 { b.compile_x64() }
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
|
|
|
if pref.is_stats {
|
2020-07-25 09:32:29 +02:00
|
|
|
println('compilation took: ${util.bold(sw.elapsed().milliseconds().str())} ms')
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
2020-05-08 14:31:03 +02:00
|
|
|
// running does not require the parsers anymore
|
2020-07-17 19:14:12 +02:00
|
|
|
unsafe {
|
|
|
|
b.myfree()
|
|
|
|
}
|
2020-04-07 00:44:19 +02:00
|
|
|
if pref.is_test || pref.is_run {
|
2020-04-07 03:21:32 +02:00
|
|
|
b.run_compiled_executable_and_exit()
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
2020-05-07 17:37:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Temporary, will be done by -autofree
|
2020-08-09 11:22:11 +02:00
|
|
|
[unsafe]
|
2020-05-07 17:37:10 +02:00
|
|
|
fn (mut b Builder) myfree() {
|
|
|
|
// for file in b.parsed_files {
|
|
|
|
// }
|
2020-07-17 19:14:12 +02:00
|
|
|
unsafe {
|
|
|
|
b.parsed_files.free()
|
|
|
|
}
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 17:49:16 +02:00
|
|
|
fn (mut b Builder) run_compiled_executable_and_exit() {
|
2020-05-08 14:31:03 +02:00
|
|
|
if b.pref.skip_running {
|
|
|
|
return
|
|
|
|
}
|
2020-07-28 22:27:38 +02:00
|
|
|
if b.pref.os == .ios && b.pref.is_ios_simulator == false {
|
|
|
|
panic('Running iOS apps on physical devices is not yet supported. Please run in the simulator using the -simulator flag.')
|
|
|
|
}
|
2020-04-07 00:44:19 +02:00
|
|
|
if b.pref.is_verbose {
|
|
|
|
println('============ running $b.pref.out_name ============')
|
|
|
|
}
|
2020-07-28 22:27:38 +02:00
|
|
|
if b.pref.os == .ios {
|
|
|
|
device := '"iPhone SE (2nd generation)"'
|
|
|
|
os.exec('xcrun simctl boot $device')
|
|
|
|
bundle_name := b.pref.out_name.split('/').last()
|
|
|
|
display_name := if b.pref.display_name != '' { b.pref.display_name } else { bundle_name }
|
|
|
|
os.exec('xcrun simctl install $device $display_name\.app')
|
|
|
|
bundle_id := if b.pref.bundle_id != '' { b.pref.bundle_id } else { 'app.vlang.$bundle_name' }
|
|
|
|
os.exec('xcrun simctl launch $device $bundle_id')
|
|
|
|
} else {
|
|
|
|
mut cmd := '"$b.pref.out_name"'
|
|
|
|
if b.pref.backend == .js {
|
|
|
|
cmd = 'node "${b.pref.out_name}.js"'
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
2020-07-28 22:27:38 +02:00
|
|
|
for arg in b.pref.run_args {
|
|
|
|
// Determine if there are spaces in the parameters
|
|
|
|
if arg.index_byte(` `) > 0 {
|
|
|
|
cmd += ' "' + arg + '"'
|
|
|
|
} else {
|
|
|
|
cmd += ' ' + arg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if b.pref.is_verbose {
|
|
|
|
println('command to run executable: $cmd')
|
|
|
|
}
|
|
|
|
if b.pref.is_test {
|
|
|
|
ret := os.system(cmd)
|
|
|
|
if ret != 0 {
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if b.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.
|
|
|
|
exit(ret)
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'strings' => 'VROOT/vlib/strings'
|
|
|
|
// 'installed_mod' => '~/.vmodules/installed_mod'
|
|
|
|
// 'local_mod' => '/path/to/current/dir/local_mod'
|
2020-04-25 17:49:16 +02:00
|
|
|
fn (mut v Builder) set_module_lookup_paths() {
|
2020-04-07 00:44:19 +02:00
|
|
|
// Module search order:
|
|
|
|
// 0) V test files are very commonly located right inside the folder of the
|
|
|
|
// module, which they test. Adding the parent folder of the module folder
|
|
|
|
// with the _test.v files, *guarantees* that the tested module can be found
|
|
|
|
// without needing to set custom options/flags.
|
|
|
|
// 1) search in the *same* directory, as the compiled final v program source
|
|
|
|
// (i.e. the . in `v .` or file.v in `v file.v`)
|
|
|
|
// 2) search in the modules/ in the same directory.
|
|
|
|
// 3) search in the provided paths
|
|
|
|
// By default, these are what (3) contains:
|
|
|
|
// 3.1) search in vlib/
|
|
|
|
// 3.2) search in ~/.vmodules/ (i.e. modules installed with vpm)
|
|
|
|
v.module_search_paths = []
|
|
|
|
if v.pref.is_test {
|
2020-04-25 17:49:16 +02:00
|
|
|
v.module_search_paths << os.base_dir(v.compiled_dir) // pdir of _test.v
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
|
|
|
v.module_search_paths << v.compiled_dir
|
|
|
|
x := os.join_path(v.compiled_dir, 'modules')
|
|
|
|
if v.pref.is_verbose {
|
|
|
|
println('x: "$x"')
|
|
|
|
}
|
|
|
|
v.module_search_paths << os.join_path(v.compiled_dir, 'modules')
|
|
|
|
v.module_search_paths << v.pref.lookup_path
|
|
|
|
if v.pref.is_verbose {
|
2020-04-11 08:57:31 +02:00
|
|
|
v.log('v.module_search_paths:')
|
2020-04-07 00:44:19 +02:00
|
|
|
println(v.module_search_paths)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 01:02:48 +02:00
|
|
|
pub fn (v Builder) get_builtin_files() []string {
|
2020-07-14 18:27:11 +02:00
|
|
|
/*
|
|
|
|
// if v.pref.build_mode == .build_module && v.pref.path == 'vlib/builtin' { // .contains('builtin/' + location {
|
|
|
|
if v.pref.build_mode == .build_module && v.pref.path == 'vlib/strconv' { // .contains('builtin/' + location {
|
2020-04-10 18:11:43 +02:00
|
|
|
// We are already building builtin.o, no need to import them again
|
|
|
|
if v.pref.is_verbose {
|
|
|
|
println('skipping builtin modules for builtin.o')
|
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
2020-07-14 18:27:11 +02:00
|
|
|
*/
|
2020-08-15 21:07:38 +02:00
|
|
|
v.log('v.pref.lookup_path: $v.pref.lookup_path')
|
2020-04-07 00:44:19 +02: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-07-04 23:37:41 +02:00
|
|
|
if os.exists(os.join_path(location, 'builtin')) {
|
|
|
|
mut builtin_files := []string{}
|
|
|
|
if v.pref.is_bare {
|
|
|
|
builtin_files << v.v_files_from_dir(os.join_path(location, 'builtin', 'bare'))
|
|
|
|
} else if v.pref.backend == .js {
|
|
|
|
builtin_files << v.v_files_from_dir(os.join_path(location, 'builtin', 'js'))
|
|
|
|
} else {
|
|
|
|
builtin_files << v.v_files_from_dir(os.join_path(location, 'builtin'))
|
|
|
|
}
|
|
|
|
if v.pref.backend == .c {
|
|
|
|
// TODO JavaScript backend doesn't handle os for now
|
2020-08-14 21:57:32 +02:00
|
|
|
if v.pref.is_vsh && os.exists(os.join_path(location, 'os')) {
|
2020-07-04 23:37:41 +02:00
|
|
|
builtin_files << v.v_files_from_dir(os.join_path(location, 'os'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return builtin_files
|
2020-04-07 00:44:19 +02: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.')
|
|
|
|
}
|
|
|
|
|
2020-07-14 18:27:11 +02:00
|
|
|
pub fn (v &Builder) get_user_files() []string {
|
2020-08-30 08:55:31 +02:00
|
|
|
if v.pref.path in ['vlib/builtin', 'vlib/strconv', 'vlib/strings', 'vlib/hash', 'vlib/time'] {
|
2020-07-16 19:01:22 +02:00
|
|
|
// This means we are building a builtin module with `v build-module vlib/strings` etc
|
|
|
|
// get_builtin_files() has already added the files in this module,
|
|
|
|
// do nothing here to avoid duplicate definition errors.
|
2020-07-14 18:27:11 +02:00
|
|
|
v.log('Skipping user files.')
|
2020-06-11 18:23:13 +02:00
|
|
|
return []
|
2020-07-04 23:37:41 +02:00
|
|
|
}
|
2020-04-07 00:44:19 +02:00
|
|
|
mut dir := v.pref.path
|
|
|
|
v.log('get_v_files($dir)')
|
|
|
|
// 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
|
2020-04-26 09:17:13 +02:00
|
|
|
mut user_files := []string{}
|
2020-04-07 00:44:19 +02:00
|
|
|
// See cmd/tools/preludes/README.md for more info about what preludes are
|
|
|
|
vroot := os.dir(pref.vexe_path())
|
|
|
|
preludes_path := os.join_path(vroot, 'cmd', 'tools', 'preludes')
|
2020-05-03 17:59:11 +02:00
|
|
|
if v.pref.is_livemain || v.pref.is_liveshared {
|
|
|
|
user_files << os.join_path(preludes_path, 'live.v')
|
2020-05-07 17:37:10 +02:00
|
|
|
}
|
2020-05-01 19:34:27 +02:00
|
|
|
if v.pref.is_livemain {
|
2020-04-07 00:44:19 +02:00
|
|
|
user_files << os.join_path(preludes_path, 'live_main.v')
|
|
|
|
}
|
2020-05-01 19:34:27 +02:00
|
|
|
if v.pref.is_liveshared {
|
2020-04-07 00:44:19 +02:00
|
|
|
user_files << os.join_path(preludes_path, 'live_shared.v')
|
|
|
|
}
|
|
|
|
if v.pref.is_test {
|
|
|
|
user_files << os.join_path(preludes_path, 'tests_assertions.v')
|
|
|
|
}
|
|
|
|
if v.pref.is_test && v.pref.is_stats {
|
|
|
|
user_files << os.join_path(preludes_path, 'tests_with_stats.v')
|
|
|
|
}
|
2020-04-25 12:05:31 +02:00
|
|
|
if v.pref.is_prof {
|
|
|
|
user_files << os.join_path(preludes_path, 'profiled_program.v')
|
|
|
|
}
|
2020-04-07 00:44:19 +02:00
|
|
|
is_test := dir.ends_with('_test.v')
|
2020-04-29 16:44:50 +02:00
|
|
|
if v.pref.is_run && is_test {
|
2020-04-29 14:52:30 +02:00
|
|
|
println('use `v x_test.v` instead of `v run x_test.v`')
|
|
|
|
exit(1)
|
|
|
|
}
|
2020-04-07 00:44:19 +02:00
|
|
|
mut is_internal_module_test := false
|
|
|
|
if is_test {
|
|
|
|
tcontent := os.read_file(dir) or {
|
2020-05-27 20:38:09 +02:00
|
|
|
verror('$dir does not exist')
|
|
|
|
exit(0)
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2020-07-01 00:53:53 +02:00
|
|
|
if line.starts_with('module ') {
|
2020-04-07 00:44:19 +02:00
|
|
|
is_internal_module_test = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_internal_module_test {
|
|
|
|
// v volt/slack_test.v: compile all .v files to get the environment
|
|
|
|
single_test_v_file := os.real_path(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 .')
|
|
|
|
}
|
|
|
|
user_files << single_test_v_file
|
|
|
|
dir = os.base_dir(single_test_v_file)
|
|
|
|
}
|
2020-08-25 14:59:07 +02:00
|
|
|
does_exist := os.exists(dir)
|
|
|
|
if !does_exist {
|
|
|
|
verror("$dir doesn't exist")
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
is_real_file := does_exist && !os.is_dir(dir)
|
2020-08-25 13:32:22 +02:00
|
|
|
if is_real_file && (dir.ends_with('.v') || dir.ends_with('.vsh') || dir.ends_with('.vv')) {
|
2020-04-07 00:44:19 +02:00
|
|
|
single_v_file := dir
|
|
|
|
// Just compile one file and get parent dir
|
|
|
|
user_files << single_v_file
|
|
|
|
if v.pref.is_verbose {
|
2020-07-04 23:37:41 +02:00
|
|
|
v.log('> just compile one file: "$single_v_file"')
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
2020-08-25 13:32:22 +02:00
|
|
|
} else if os.is_dir(dir) {
|
2020-04-07 00:44:19 +02:00
|
|
|
if v.pref.is_verbose {
|
2020-07-04 23:37:41 +02:00
|
|
|
v.log('> add all .v files from directory "$dir" ...')
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
|
|
|
// Add .v files from the directory being compiled
|
2020-04-17 21:17:19 +02:00
|
|
|
user_files << v.v_files_from_dir(dir)
|
2020-08-25 13:32:22 +02:00
|
|
|
} else {
|
|
|
|
println('usage: `v file.v` or `v directory`')
|
|
|
|
ext := os.file_ext(dir)
|
|
|
|
println('unknown file extension `$ext`')
|
|
|
|
exit(1)
|
2020-04-07 00:44:19 +02:00
|
|
|
}
|
|
|
|
if user_files.len == 0 {
|
|
|
|
println('No input .v files')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
if v.pref.is_verbose {
|
|
|
|
v.log('user_files: $user_files')
|
|
|
|
}
|
|
|
|
return user_files
|
|
|
|
}
|