2019-08-23 12:05:16 +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-10-13 16:37:43 +03:00
|
|
|
module compiler
|
2019-08-23 12:05:16 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
os
|
|
|
|
time
|
2019-11-01 19:29:51 +03:00
|
|
|
filepath
|
2019-08-23 12:05:16 +03:00
|
|
|
)
|
|
|
|
|
2019-10-25 18:53:45 +03:00
|
|
|
fn todo() {
|
2019-11-28 16:49:35 +03:00
|
|
|
|
|
|
|
}
|
2019-10-25 18:53:45 +03:00
|
|
|
|
2019-11-08 21:15:14 +03:00
|
|
|
fn no_mingw_installed() bool {
|
|
|
|
$if !windows {
|
|
|
|
panic('no_mingw_installed() can only run on Windows')
|
|
|
|
}
|
|
|
|
os.exec('gcc -v') or {
|
|
|
|
println('mingw not found, trying to build with msvc...')
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-08-23 12:05:16 +03:00
|
|
|
fn (v mut V) cc() {
|
2019-09-06 15:12:04 +03:00
|
|
|
v.build_thirdparty_obj_files()
|
2019-10-21 14:14:28 +03:00
|
|
|
vexe := vexe_path()
|
2019-11-01 14:16:58 +03:00
|
|
|
vdir := os.dir(vexe)
|
2019-09-14 23:48:30 +03:00
|
|
|
// Just create a C/JavaScript file and exit
|
2019-10-09 23:38:33 +03:00
|
|
|
// for example: `v -o v.c compiler`
|
2019-09-14 23:48:30 +03:00
|
|
|
if v.out_name.ends_with('.c') || v.out_name.ends_with('.js') {
|
2019-10-25 21:57:32 +03:00
|
|
|
// Translating V code to JS by launching vjs.
|
|
|
|
// Using a separate process for V.js is for performance mostly,
|
|
|
|
// to avoid constant is_js checks.
|
2019-09-14 23:48:30 +03:00
|
|
|
$if !js {
|
|
|
|
if v.out_name.ends_with('.js') {
|
|
|
|
vjs_path := vexe + 'js'
|
|
|
|
if !os.file_exists(vjs_path) {
|
|
|
|
println('V.js compiler not found, building...')
|
2019-10-25 21:57:32 +03:00
|
|
|
// Build V.js. Specifying `-os js` makes V include
|
|
|
|
// only _js.v files and ignore _c.v files.
|
2019-11-01 14:16:58 +03:00
|
|
|
ret := os.system('$vexe -o $vjs_path -os js $vdir/v.v')
|
2019-09-14 23:48:30 +03:00
|
|
|
if ret == 0 {
|
|
|
|
println('Done.')
|
|
|
|
} else {
|
|
|
|
println('Failed.')
|
|
|
|
exit(1)
|
2019-11-28 16:49:35 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
ret := os.system('$vjs_path -o $v.out_name $v.dir')
|
|
|
|
if ret == 0 {
|
|
|
|
println('Done. Run it with `node $v.out_name`')
|
2019-09-16 19:02:10 +03:00
|
|
|
println('JS backend is at a very early stage.')
|
2019-11-28 16:49:35 +03:00
|
|
|
}
|
2019-09-14 23:48:30 +03:00
|
|
|
}
|
|
|
|
}
|
2019-11-28 16:49:35 +03:00
|
|
|
|
2019-11-19 12:55:03 +03:00
|
|
|
// v.out_name_c may be on a different partition than v.out_name
|
|
|
|
os.mv_by_cp(v.out_name_c, v.out_name) or { panic(err) }
|
2019-08-30 00:07:54 +03:00
|
|
|
exit(0)
|
|
|
|
}
|
2019-08-23 12:05:16 +03:00
|
|
|
// Cross compiling for Windows
|
|
|
|
if v.os == .windows {
|
|
|
|
$if !windows {
|
|
|
|
v.cc_windows_cross()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$if windows {
|
2019-11-08 21:15:14 +03:00
|
|
|
if v.pref.ccompiler == 'msvc' || no_mingw_installed() {
|
2019-08-23 12:05:16 +03:00
|
|
|
v.cc_msvc()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-11-17 01:14:05 +03:00
|
|
|
// arguments for the C compiler
|
|
|
|
mut a := [v.pref.cflags, '-std=gnu11',
|
|
|
|
'-Wall',
|
|
|
|
'-Wextra',
|
|
|
|
// TODO : activate -Werror once no warnings remain
|
|
|
|
// '-Werror',
|
|
|
|
// TODO : try and remove the below workaround options when the corresponding
|
|
|
|
// warnings are totally fixed/removed
|
|
|
|
'-Wno-unused-variable',
|
2019-11-18 04:53:46 +03:00
|
|
|
//'-Wno-unused-but-set-variable',
|
2019-11-17 01:14:05 +03:00
|
|
|
'-Wno-unused-parameter',
|
|
|
|
'-Wno-unused-result',
|
|
|
|
'-Wno-missing-braces',
|
|
|
|
'-Wno-unused-label']
|
2019-10-12 16:05:24 +03:00
|
|
|
// TCC on Linux by default, unless -cc was provided
|
|
|
|
// TODO if -cc = cc, TCC is still used, default compiler should be
|
|
|
|
// used instead.
|
2019-11-01 00:37:24 +03:00
|
|
|
if v.pref.fast {
|
|
|
|
$if linux {
|
|
|
|
$if !android {
|
|
|
|
tcc_3rd := '$vdir/thirdparty/tcc/bin/tcc'
|
|
|
|
//println('tcc third "$tcc_3rd"')
|
|
|
|
tcc_path := '/var/tmp/tcc/bin/tcc'
|
|
|
|
if os.file_exists(tcc_3rd) && !os.file_exists(tcc_path) {
|
|
|
|
//println('moving tcc')
|
|
|
|
// if there's tcc in thirdparty/, that means this is
|
|
|
|
// a prebuilt V_linux.zip.
|
|
|
|
// Until the libtcc1.a bug is fixed, we neeed to move
|
|
|
|
// it to /var/tmp/
|
|
|
|
os.system('mv $vdir/thirdparty/tcc /var/tmp/')
|
|
|
|
}
|
|
|
|
if v.pref.ccompiler == 'cc' && os.file_exists(tcc_path) {
|
|
|
|
// TODO tcc bug, needs an empty libtcc1.a fila
|
2019-11-23 19:55:18 +03:00
|
|
|
//os.mkdir('/var/tmp/tcc/lib/tcc/') or { panic(err) }
|
2019-11-01 00:37:24 +03:00
|
|
|
//os.create('/var/tmp/tcc/lib/tcc/libtcc1.a')
|
|
|
|
v.pref.ccompiler = tcc_path
|
2019-11-17 01:14:05 +03:00
|
|
|
a << '-m64'
|
2019-11-01 00:37:24 +03:00
|
|
|
}
|
2019-10-12 16:05:24 +03:00
|
|
|
}
|
2019-11-01 00:37:24 +03:00
|
|
|
} $else {
|
|
|
|
verror('-fast is only supported on Linux right now')
|
2019-11-28 16:49:35 +03:00
|
|
|
}
|
2019-10-31 20:32:34 +03:00
|
|
|
}
|
2019-09-03 12:10:51 +03:00
|
|
|
//linux_host := os.user_os() == 'linux'
|
2019-08-23 12:05:16 +03:00
|
|
|
v.log('cc() isprod=$v.pref.is_prod outname=$v.out_name')
|
2019-09-03 16:09:43 +03:00
|
|
|
|
2019-08-23 12:05:16 +03:00
|
|
|
if v.pref.is_so {
|
|
|
|
a << '-shared -fPIC '// -Wl,-z,defs'
|
|
|
|
v.out_name = v.out_name + '.so'
|
|
|
|
}
|
2019-11-14 10:23:44 +03:00
|
|
|
if v.pref.is_bare {
|
2019-12-01 11:27:36 +03:00
|
|
|
a << '-fno-stack-protector -static -ffreestanding -nostdlib $vdir/vlib/os/bare/bare.S'
|
2019-11-28 16:49:35 +03:00
|
|
|
}
|
2019-09-10 17:36:14 +03:00
|
|
|
if v.pref.build_mode == .build_module {
|
2019-10-04 15:48:09 +03:00
|
|
|
// Create the modules & out directory if it's not there.
|
2019-10-12 08:41:41 +03:00
|
|
|
mut out_dir := if v.dir.starts_with('vlib') {
|
2019-11-28 16:49:35 +03:00
|
|
|
'$v_modules_path${os.path_separator}cache${os.path_separator}$v.dir'
|
2019-10-12 08:41:41 +03:00
|
|
|
} else {
|
2019-10-12 22:31:05 +03:00
|
|
|
'$v_modules_path${os.path_separator}$v.dir'
|
2019-10-12 08:41:41 +03:00
|
|
|
}
|
2019-10-12 22:31:05 +03:00
|
|
|
pdir := out_dir.all_before_last(os.path_separator)
|
2019-10-12 08:41:41 +03:00
|
|
|
if !os.dir_exists(pdir) {
|
|
|
|
os.mkdir_all(pdir)
|
2019-09-13 20:47:17 +03:00
|
|
|
}
|
2019-10-04 15:48:09 +03:00
|
|
|
v.out_name = '${out_dir}.o' //v.out_name
|
2019-09-03 12:10:51 +03:00
|
|
|
println('Building ${v.out_name}...')
|
|
|
|
}
|
2019-09-29 04:54:12 +03:00
|
|
|
|
2019-10-12 13:54:01 +03:00
|
|
|
debug_mode := v.pref.is_debug
|
|
|
|
mut debug_options := '-g'
|
2019-09-26 21:58:08 +03:00
|
|
|
mut optimization_options := '-O2'
|
2019-12-03 13:58:24 +03:00
|
|
|
|
|
|
|
mut guessed_compiler := v.pref.ccompiler
|
|
|
|
if guessed_compiler == 'cc' && v.pref.is_prod {
|
|
|
|
// deliberately guessing only for -prod builds for performance reasons
|
|
|
|
if ccversion := os.exec('cc --version') {
|
|
|
|
if ccversion.exit_code == 0 {
|
|
|
|
if ccversion.output.contains('This is free software;')
|
|
|
|
&& ccversion.output.contains('Free Software Foundation, Inc.') {
|
|
|
|
guessed_compiler = 'gcc'
|
|
|
|
}
|
|
|
|
if ccversion.output.contains('clang version '){
|
|
|
|
guessed_compiler = 'clang'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.pref.ccompiler.contains('clang') || guessed_compiler == 'clang' {
|
2019-10-12 13:54:01 +03:00
|
|
|
if debug_mode {
|
2019-11-01 13:20:26 +03:00
|
|
|
debug_options = '-g -O0 -no-pie'
|
2019-09-26 21:58:08 +03:00
|
|
|
}
|
|
|
|
optimization_options = '-O3 -flto'
|
|
|
|
}
|
2019-12-03 13:58:24 +03:00
|
|
|
if v.pref.ccompiler.contains('gcc') || guessed_compiler == 'gcc' {
|
2019-10-12 13:54:01 +03:00
|
|
|
if debug_mode {
|
2019-11-01 13:20:26 +03:00
|
|
|
debug_options = '-g3 -no-pie'
|
2019-09-26 21:58:08 +03:00
|
|
|
}
|
|
|
|
optimization_options = '-O3 -fno-strict-aliasing -flto'
|
|
|
|
}
|
2019-11-28 16:49:35 +03:00
|
|
|
|
2019-10-12 13:54:01 +03:00
|
|
|
if debug_mode {
|
|
|
|
a << debug_options
|
|
|
|
}
|
2019-08-23 12:05:16 +03:00
|
|
|
if v.pref.is_prod {
|
2019-09-26 21:58:08 +03:00
|
|
|
a << optimization_options
|
2019-08-23 12:05:16 +03:00
|
|
|
}
|
2019-11-28 16:49:35 +03:00
|
|
|
|
2019-10-12 13:54:01 +03:00
|
|
|
if debug_mode && os.user_os() != 'windows'{
|
2019-08-27 23:29:13 +03:00
|
|
|
a << ' -rdynamic ' // needed for nicer symbolic backtraces
|
|
|
|
}
|
|
|
|
|
2019-10-14 08:41:46 +03:00
|
|
|
if v.pref.ccompiler != 'msvc' && v.os != .freebsd {
|
2019-11-01 14:16:58 +03:00
|
|
|
a << '-Werror=implicit-function-declaration'
|
2019-08-23 23:55:45 +03:00
|
|
|
}
|
2019-08-23 12:05:16 +03:00
|
|
|
|
|
|
|
for f in v.generate_hotcode_reloading_compiler_flags() {
|
|
|
|
a << f
|
|
|
|
}
|
|
|
|
|
2019-10-10 02:59:33 +03:00
|
|
|
mut libs := ''// builtin.o os.o http.o etc
|
2019-09-10 17:36:14 +03:00
|
|
|
if v.pref.build_mode == .build_module {
|
2019-08-23 12:05:16 +03:00
|
|
|
a << '-c'
|
|
|
|
}
|
2019-10-12 13:54:01 +03:00
|
|
|
else if v.pref.is_cache {
|
2019-11-01 19:29:51 +03:00
|
|
|
builtin_o_path := filepath.join(v_modules_path, 'cache', 'vlib', 'builtin.o')
|
2019-11-01 00:47:11 +03:00
|
|
|
a << builtin_o_path.replace('builtin.o', 'strconv.o') // TODO hack no idea why this is needed
|
2019-10-10 15:00:18 +03:00
|
|
|
if os.file_exists(builtin_o_path) {
|
|
|
|
libs = builtin_o_path
|
|
|
|
} else {
|
2019-10-10 22:27:22 +03:00
|
|
|
println('$builtin_o_path not found... building module builtin')
|
2019-10-12 22:31:05 +03:00
|
|
|
os.system('$vexe build module vlib${os.path_separator}builtin')
|
2019-10-10 15:00:18 +03:00
|
|
|
}
|
2019-08-23 12:05:16 +03:00
|
|
|
for imp in v.table.imports {
|
2019-10-12 01:17:37 +03:00
|
|
|
if imp.contains('vweb') { continue } // not working
|
|
|
|
if imp == 'webview' { continue }
|
2019-11-28 16:49:35 +03:00
|
|
|
|
2019-10-12 22:31:05 +03:00
|
|
|
imp_path := imp.replace('.', os.path_separator)
|
|
|
|
path := '$v_modules_path${os.path_separator}cache${os.path_separator}vlib${os.path_separator}${imp_path}.o'
|
2019-10-12 22:14:16 +03:00
|
|
|
//println('adding ${imp_path}.o')
|
2019-10-10 02:59:33 +03:00
|
|
|
if os.file_exists(path) {
|
|
|
|
libs += ' ' + path
|
2019-10-10 03:48:11 +03:00
|
|
|
} else {
|
2019-10-10 22:27:22 +03:00
|
|
|
println('$path not found... building module $imp')
|
2019-11-01 00:57:16 +03:00
|
|
|
if path.ends_with('vlib/ui.o') {
|
|
|
|
println('copying ui...')
|
2019-11-04 02:59:18 +03:00
|
|
|
os.cp('$vdir/thirdparty/ui/ui.o', path) or { panic('error copying ui files') }
|
|
|
|
os.cp('$vdir/thirdparty/ui/ui.vh', v_modules_path +
|
2019-11-04 02:38:49 +03:00
|
|
|
'/vlib/ui.vh') or { panic('error copying ui files') }
|
2019-11-28 16:49:35 +03:00
|
|
|
|
2019-11-01 00:57:16 +03:00
|
|
|
} else {
|
|
|
|
os.system('$vexe build module vlib${os.path_separator}$imp_path')
|
|
|
|
}
|
2019-11-28 16:49:35 +03:00
|
|
|
}
|
2019-11-01 00:57:16 +03:00
|
|
|
if path.ends_with('vlib/ui.o') {
|
|
|
|
a << '-framework Cocoa -framework Carbon'
|
|
|
|
}
|
2019-08-23 12:05:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if v.pref.sanitize {
|
|
|
|
a << '-fsanitize=leak'
|
|
|
|
}
|
2019-09-03 12:10:51 +03:00
|
|
|
// Cross compiling linux TODO
|
|
|
|
/*
|
2019-09-29 04:54:12 +03:00
|
|
|
sysroot := '/tmp/lld/linuxroot/'
|
2019-08-23 12:05:16 +03:00
|
|
|
if v.os == .linux && !linux_host {
|
|
|
|
// Build file.o
|
|
|
|
a << '-c --sysroot=$sysroot -target x86_64-linux-gnu'
|
|
|
|
// Right now `out_name` can be `file`, not `file.o`
|
|
|
|
if !v.out_name.ends_with('.o') {
|
|
|
|
v.out_name = v.out_name + '.o'
|
|
|
|
}
|
|
|
|
}
|
2019-09-03 12:10:51 +03:00
|
|
|
*/
|
2019-08-23 12:05:16 +03:00
|
|
|
// Cross compiling windows
|
2019-09-03 12:10:51 +03:00
|
|
|
//
|
2019-08-23 12:05:16 +03:00
|
|
|
// Output executable name
|
2019-09-06 19:30:55 +03:00
|
|
|
a << '-o "$v.out_name"'
|
2019-08-23 12:05:16 +03:00
|
|
|
if os.dir_exists(v.out_name) {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror('\'$v.out_name\' is a directory')
|
2019-08-23 12:05:16 +03:00
|
|
|
}
|
2019-09-07 19:19:17 +03:00
|
|
|
// macOS code can include objective C TODO remove once objective C is replaced with C
|
2019-08-23 12:05:16 +03:00
|
|
|
if v.os == .mac {
|
|
|
|
a << '-x objective-c'
|
|
|
|
}
|
|
|
|
// The C file we are compiling
|
|
|
|
a << '"$v.out_name_c"'
|
|
|
|
if v.os == .mac {
|
|
|
|
a << '-x none'
|
|
|
|
}
|
|
|
|
// Min macos version is mandatory I think?
|
|
|
|
if v.os == .mac {
|
|
|
|
a << '-mmacosx-version-min=10.7'
|
|
|
|
}
|
2019-09-08 12:02:03 +03:00
|
|
|
cflags := v.get_os_cflags()
|
2019-09-11 19:26:35 +03:00
|
|
|
|
2019-09-11 13:34:19 +03:00
|
|
|
// add .o files
|
2019-09-23 00:51:59 +03:00
|
|
|
a << cflags.c_options_only_object_files()
|
2019-11-28 16:49:35 +03:00
|
|
|
|
2019-09-11 13:34:19 +03:00
|
|
|
// add all flags (-I -l -L etc) not .o files
|
2019-09-23 00:51:59 +03:00
|
|
|
a << cflags.c_options_without_object_files()
|
2019-11-28 16:49:35 +03:00
|
|
|
|
2019-08-23 12:05:16 +03:00
|
|
|
a << libs
|
|
|
|
// Without these libs compilation will fail on Linux
|
|
|
|
// || os.user_os() == 'linux'
|
2019-12-03 11:26:47 +03:00
|
|
|
if !v.pref.is_bare && v.pref.build_mode != .build_module && v.os in [ .linux, .freebsd,
|
|
|
|
.openbsd, .netbsd, .dragonfly, .solaris, .haiku ] {
|
2019-08-23 12:05:16 +03:00
|
|
|
a << '-lm -lpthread '
|
|
|
|
// -ldl is a Linux only thing. BSDs have it in libc.
|
|
|
|
if v.os == .linux {
|
|
|
|
a << ' -ldl '
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 22:00:59 +03:00
|
|
|
|
2019-12-01 11:27:36 +03:00
|
|
|
if !v.pref.is_bare && v.os == .js && os.user_os() == 'linux' {
|
2019-09-16 22:00:59 +03:00
|
|
|
a << '-lm'
|
|
|
|
}
|
2019-11-28 16:49:35 +03:00
|
|
|
|
2019-08-23 12:05:16 +03:00
|
|
|
args := a.join(' ')
|
2019-10-25 18:49:49 +03:00
|
|
|
start:
|
2019-10-25 18:53:45 +03:00
|
|
|
todo()
|
|
|
|
// TODO remove
|
2019-08-23 12:05:16 +03:00
|
|
|
cmd := '${v.pref.ccompiler} $args'
|
|
|
|
// Run
|
|
|
|
if v.pref.show_c_cmd || v.pref.is_verbose {
|
|
|
|
println('\n==========')
|
|
|
|
println(cmd)
|
|
|
|
}
|
|
|
|
ticks := time.ticks()
|
2019-11-08 21:15:14 +03:00
|
|
|
res := os.exec(cmd) or {
|
|
|
|
// C compilation failed.
|
|
|
|
// If we are on Windows, try msvc
|
|
|
|
println('C compilation failed.')
|
|
|
|
/*
|
|
|
|
if os.user_os() == 'windows' && v.pref.ccompiler != 'msvc' {
|
|
|
|
println('Trying to build with MSVC')
|
|
|
|
v.cc_msvc()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*/
|
2019-11-14 10:23:44 +03:00
|
|
|
verror(err)
|
2019-11-08 21:15:14 +03:00
|
|
|
return
|
|
|
|
}
|
2019-08-23 12:05:16 +03:00
|
|
|
if res.exit_code != 0 {
|
2019-10-25 18:53:45 +03:00
|
|
|
// the command could not be found by the system
|
2019-08-23 12:05:16 +03:00
|
|
|
if res.exit_code == 127 {
|
2019-10-25 18:49:49 +03:00
|
|
|
$if linux {
|
2019-10-25 18:53:45 +03:00
|
|
|
// TCC problems on linux? Try GCC.
|
2019-10-25 20:11:53 +03:00
|
|
|
if v.pref.ccompiler.contains('tcc') {
|
2019-10-25 18:49:49 +03:00
|
|
|
v.pref.ccompiler = 'cc'
|
|
|
|
goto start
|
2019-11-28 16:49:35 +03:00
|
|
|
}
|
2019-10-25 18:49:49 +03:00
|
|
|
}
|
2019-11-28 16:49:35 +03:00
|
|
|
|
2019-10-25 18:53:45 +03:00
|
|
|
verror('C compiler error, while attempting to run: \n' +
|
|
|
|
'-----------------------------------------------------------\n' +
|
|
|
|
'$cmd\n' +
|
|
|
|
'-----------------------------------------------------------\n' +
|
|
|
|
'Probably your C compiler is missing. \n' +
|
2019-11-28 16:49:35 +03:00
|
|
|
'Please reinstall it, or make it available in your PATH.\n\n' +
|
|
|
|
missing_compiler_info())
|
2019-08-23 12:05:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if v.pref.is_debug {
|
|
|
|
println(res.output)
|
|
|
|
} else {
|
2019-08-29 03:30:17 +03:00
|
|
|
partial_output := res.output.limit(200).trim_right('\r\n')
|
|
|
|
print(partial_output)
|
|
|
|
if res.output.len > partial_output.len {
|
2019-10-12 13:54:01 +03:00
|
|
|
println('...\n(Use `v -g` to print the entire error message)\n')
|
2019-08-29 03:30:17 +03:00
|
|
|
}else{
|
|
|
|
println('')
|
2019-08-23 12:05:16 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 00:40:34 +03:00
|
|
|
verror('C error. This should never happen. ' +
|
2019-10-23 13:03:14 +03:00
|
|
|
'\nPlease create a GitHub issue: https://github.com/vlang/v/issues/new/choose')
|
2019-08-23 12:05:16 +03:00
|
|
|
}
|
|
|
|
diff := time.ticks() - ticks
|
|
|
|
// Print the C command
|
|
|
|
if v.pref.show_c_cmd || v.pref.is_verbose {
|
|
|
|
println('${v.pref.ccompiler} took $diff ms')
|
|
|
|
println('=========\n')
|
|
|
|
}
|
|
|
|
// Link it if we are cross compiling and need an executable
|
2019-09-03 12:10:51 +03:00
|
|
|
/*
|
2019-08-23 12:05:16 +03:00
|
|
|
if v.os == .linux && !linux_host && v.pref.build_mode != .build {
|
|
|
|
v.out_name = v.out_name.replace('.o', '')
|
|
|
|
obj_file := v.out_name + '.o'
|
|
|
|
println('linux obj_file=$obj_file out_name=$v.out_name')
|
|
|
|
ress := os.exec('/usr/local/Cellar/llvm/8.0.0/bin/ld.lld --sysroot=$sysroot ' +
|
|
|
|
'-v -o $v.out_name ' +
|
|
|
|
'-m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 ' +
|
|
|
|
'/usr/lib/x86_64-linux-gnu/crt1.o ' +
|
|
|
|
'$sysroot/lib/x86_64-linux-gnu/libm-2.28.a ' +
|
|
|
|
'/usr/lib/x86_64-linux-gnu/crti.o ' +
|
|
|
|
obj_file +
|
|
|
|
' /usr/lib/x86_64-linux-gnu/libc.so ' +
|
|
|
|
'/usr/lib/x86_64-linux-gnu/crtn.o') or {
|
2019-09-24 00:40:34 +03:00
|
|
|
verror(err)
|
2019-08-29 03:30:17 +03:00
|
|
|
return
|
2019-08-23 12:05:16 +03:00
|
|
|
}
|
|
|
|
println(ress.output)
|
|
|
|
println('linux cross compilation done. resulting binary: "$v.out_name"')
|
|
|
|
}
|
2019-09-03 12:10:51 +03:00
|
|
|
*/
|
2019-10-12 13:54:01 +03:00
|
|
|
if !v.pref.is_keep_c && v.out_name_c != 'v.c' {
|
2019-10-09 07:02:45 +03:00
|
|
|
os.rm(v.out_name_c)
|
2019-08-23 12:05:16 +03:00
|
|
|
}
|
2019-09-19 15:52:38 +03:00
|
|
|
if v.pref.compress {
|
2019-09-19 16:05:17 +03:00
|
|
|
$if windows {
|
|
|
|
println('-compress does not work on Windows for now')
|
|
|
|
return
|
2019-11-28 16:49:35 +03:00
|
|
|
}
|
2019-09-19 15:52:38 +03:00
|
|
|
ret := os.system('strip $v.out_name')
|
|
|
|
if ret != 0 {
|
|
|
|
println('strip failed')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret2 := os.system('upx --lzma -qqq $v.out_name')
|
|
|
|
if ret2 != 0 {
|
|
|
|
println('upx failed')
|
2019-12-03 16:29:24 +03:00
|
|
|
$if macos {
|
2019-09-19 15:52:38 +03:00
|
|
|
println('install upx with `brew install upx`')
|
2019-11-28 16:49:35 +03:00
|
|
|
}
|
2019-09-19 15:52:38 +03:00
|
|
|
$if linux {
|
|
|
|
println('install upx\n' +
|
|
|
|
'for example, on Debian/Ubuntu run `sudo apt install upx`')
|
2019-11-28 16:49:35 +03:00
|
|
|
}
|
2019-09-19 15:52:38 +03:00
|
|
|
$if windows {
|
|
|
|
// :)
|
2019-11-28 16:49:35 +03:00
|
|
|
}
|
2019-09-19 15:52:38 +03:00
|
|
|
}
|
2019-11-28 16:49:35 +03:00
|
|
|
}
|
2019-08-23 12:05:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn (c mut V) cc_windows_cross() {
|
|
|
|
if !c.out_name.ends_with('.exe') {
|
|
|
|
c.out_name = c.out_name + '.exe'
|
|
|
|
}
|
|
|
|
mut args := '-o $c.out_name -w -L. '
|
2019-09-08 12:02:03 +03:00
|
|
|
cflags := c.get_os_cflags()
|
2019-08-23 12:05:16 +03:00
|
|
|
// -I flags
|
2019-10-14 08:41:46 +03:00
|
|
|
args += if c.pref.ccompiler == 'msvc' {
|
|
|
|
cflags.c_options_before_target_msvc()
|
|
|
|
} else {
|
|
|
|
cflags.c_options_before_target()
|
|
|
|
}
|
2019-08-23 12:05:16 +03:00
|
|
|
mut libs := ''
|
|
|
|
if c.pref.build_mode == .default_mode {
|
2019-09-29 04:54:12 +03:00
|
|
|
libs = '"$v_modules_path/vlib/builtin.o"'
|
2019-08-23 12:05:16 +03:00
|
|
|
if !os.file_exists(libs) {
|
2019-09-03 12:10:51 +03:00
|
|
|
println('`$libs` not found')
|
2019-08-23 12:05:16 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
for imp in c.table.imports {
|
2019-09-29 04:54:12 +03:00
|
|
|
libs += ' "$v_modules_path/vlib/${imp}.o"'
|
2019-08-23 12:05:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
args += ' $c.out_name_c '
|
2019-10-14 08:41:46 +03:00
|
|
|
args += if c.pref.ccompiler == 'msvc' {
|
|
|
|
cflags.c_options_after_target_msvc()
|
|
|
|
} else {
|
|
|
|
cflags.c_options_after_target()
|
|
|
|
}
|
2019-08-23 12:05:16 +03:00
|
|
|
println('Cross compiling for Windows...')
|
2019-09-29 04:54:12 +03:00
|
|
|
winroot := '$v_modules_path/winroot'
|
2019-08-23 12:05:16 +03:00
|
|
|
if !os.dir_exists(winroot) {
|
|
|
|
winroot_url := 'https://github.com/vlang/v/releases/download/v0.1.10/winroot.zip'
|
2019-08-28 14:35:48 +03:00
|
|
|
println('"$winroot" not found.')
|
2019-09-29 04:54:12 +03:00
|
|
|
println('Download it from $winroot_url and save it in $v_modules_path')
|
2019-08-28 14:35:48 +03:00
|
|
|
println('Unzip it afterwards.\n')
|
|
|
|
println('winroot.zip contains all library and header files needed '+
|
|
|
|
'to cross-compile for Windows.')
|
2019-11-28 16:49:35 +03:00
|
|
|
exit(1)
|
2019-08-23 12:05:16 +03:00
|
|
|
}
|
|
|
|
mut obj_name := c.out_name
|
|
|
|
obj_name = obj_name.replace('.exe', '')
|
|
|
|
obj_name = obj_name.replace('.o.o', '.o')
|
|
|
|
include := '-I $winroot/include '
|
2019-09-29 04:54:12 +03:00
|
|
|
cmd := 'clang -o $obj_name -w $include -m32 -c -target x86_64-win32 $v_modules_path/$c.out_name_c'
|
2019-08-23 12:05:16 +03:00
|
|
|
if c.pref.show_c_cmd {
|
|
|
|
println(cmd)
|
|
|
|
}
|
|
|
|
if os.system(cmd) != 0 {
|
|
|
|
println('Cross compilation for Windows failed. Make sure you have clang installed.')
|
|
|
|
exit(1)
|
|
|
|
}
|
2019-09-10 18:19:29 +03:00
|
|
|
if c.pref.build_mode != .build_module {
|
2019-08-23 12:05:16 +03:00
|
|
|
link_cmd := 'lld-link $obj_name $winroot/lib/libcmt.lib ' +
|
|
|
|
'$winroot/lib/libucrt.lib $winroot/lib/kernel32.lib $winroot/lib/libvcruntime.lib ' +
|
|
|
|
'$winroot/lib/uuid.lib'
|
|
|
|
if c.pref.show_c_cmd {
|
|
|
|
println(link_cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.system(link_cmd) != 0 {
|
|
|
|
println('Cross compilation for Windows failed. Make sure you have lld linker installed.')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
// os.rm(obj_name)
|
|
|
|
}
|
|
|
|
println('Done!')
|
|
|
|
}
|
|
|
|
|
2019-09-23 00:51:59 +03:00
|
|
|
fn (c &V) build_thirdparty_obj_files() {
|
2019-09-06 15:12:04 +03:00
|
|
|
for flag in c.get_os_cflags() {
|
2019-11-28 16:49:35 +03:00
|
|
|
if flag.value.ends_with('.o') {
|
2019-09-23 00:51:59 +03:00
|
|
|
rest_of_module_flags := c.get_rest_of_module_cflags( flag )
|
2019-10-14 08:41:46 +03:00
|
|
|
if c.pref.ccompiler == 'msvc' {
|
2019-09-23 00:51:59 +03:00
|
|
|
build_thirdparty_obj_file_with_msvc(flag.value, rest_of_module_flags)
|
2019-09-06 15:12:04 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-09-23 00:51:59 +03:00
|
|
|
build_thirdparty_obj_file(flag.value, rest_of_module_flags)
|
2019-09-06 15:12:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 12:05:16 +03:00
|
|
|
fn find_c_compiler() string {
|
|
|
|
args := env_vflags_and_os_args().join(' ')
|
|
|
|
defaultcc := find_c_compiler_default()
|
|
|
|
return get_arg( args, 'cc', defaultcc )
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_c_compiler_default() string {
|
|
|
|
//fast_clang := '/usr/local/Cellar/llvm/8.0.0/bin/clang'
|
|
|
|
//if os.file_exists(fast_clang) {
|
|
|
|
// return fast_clang
|
|
|
|
//}
|
|
|
|
// TODO fix $if after 'string'
|
|
|
|
$if windows { return 'gcc' }
|
|
|
|
return 'cc'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_c_compiler_thirdparty_options() string {
|
2019-09-22 10:56:02 +03:00
|
|
|
fullargs := env_vflags_and_os_args()
|
|
|
|
mut cflags := get_cmdline_cflags( fullargs )
|
|
|
|
$if !windows {
|
|
|
|
cflags += ' -fPIC'
|
|
|
|
}
|
|
|
|
if '-m32' in fullargs {
|
|
|
|
cflags += ' -m32'
|
|
|
|
}
|
|
|
|
return cflags
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_cmdline_cflags(args []string) string {
|
|
|
|
mut cflags := ''
|
|
|
|
for ci, cv in args {
|
|
|
|
if cv == '-cflags' {
|
|
|
|
cflags += args[ci+1] + ' '
|
2019-09-11 19:26:35 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-22 10:56:02 +03:00
|
|
|
return cflags
|
2019-08-23 12:05:16 +03:00
|
|
|
}
|
2019-11-28 16:49:35 +03:00
|
|
|
|
|
|
|
fn missing_compiler_info() string {
|
|
|
|
$if windows {
|
|
|
|
return 'https://github.com/vlang/v/wiki/Installing-a-C-compiler-on-Windows'
|
|
|
|
}
|
|
|
|
$if linux {
|
|
|
|
return 'On Debian/Ubuntu, run `sudo apt install build-essential`'
|
|
|
|
}
|
2019-12-03 16:29:24 +03:00
|
|
|
$if macos {
|
2019-11-28 16:49:35 +03:00
|
|
|
return 'Install command line XCode tools with `xcode-select --install`'
|
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|