2020-01-23 21:04:46 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-08-23 11:05:16 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-10-13 15:37:43 +02:00
|
|
|
module compiler
|
2019-08-23 11:05:16 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
os
|
|
|
|
time
|
2019-11-01 17:29:51 +01:00
|
|
|
filepath
|
2020-02-09 10:08:04 +01:00
|
|
|
v.pref
|
2020-03-03 12:01:23 +01:00
|
|
|
term
|
2019-08-23 11:05:16 +02:00
|
|
|
)
|
|
|
|
|
2019-10-25 17:53:45 +02:00
|
|
|
fn todo() {
|
2019-11-28 14:49:35 +01:00
|
|
|
}
|
2019-10-25 17:53:45 +02:00
|
|
|
|
2019-12-16 16:56:37 +01:00
|
|
|
fn (v &V) no_cc_installed() bool {
|
2019-12-18 18:07:32 +01:00
|
|
|
$if windows {
|
2019-12-19 21:36:18 +01:00
|
|
|
os.exec('$v.pref.ccompiler -v')or{
|
2019-12-16 16:56:37 +01:00
|
|
|
if v.pref.is_verbose {
|
|
|
|
println('C compiler not found, trying to build with msvc...')
|
|
|
|
}
|
|
|
|
return true
|
2019-12-12 12:27:47 +01:00
|
|
|
}
|
2019-11-08 19:15:14 +01:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-08-23 11:05:16 +02:00
|
|
|
fn (v mut V) cc() {
|
2019-12-18 18:07:32 +01:00
|
|
|
if os.executable().contains('vfmt') {
|
|
|
|
return
|
|
|
|
}
|
2019-09-06 14:12:04 +02:00
|
|
|
v.build_thirdparty_obj_files()
|
2020-02-20 11:33:01 +01:00
|
|
|
vexe := pref.vexe_path()
|
2019-12-23 11:09:22 +01:00
|
|
|
vdir := filepath.dir(vexe)
|
2019-09-14 22:48:30 +02:00
|
|
|
// Just create a C/JavaScript file and exit
|
2019-10-09 22:38:33 +02:00
|
|
|
// for example: `v -o v.c compiler`
|
2020-02-09 10:08:04 +01:00
|
|
|
ends_with_c := v.pref.out_name.ends_with('.c')
|
|
|
|
ends_with_js := v.pref.out_name.ends_with('.js')
|
2020-02-01 06:37:22 +01:00
|
|
|
|
|
|
|
if v.pref.is_pretty_c && !ends_with_js {
|
2020-02-02 02:51:27 +01:00
|
|
|
format_result := os.exec('clang-format -i -style=file "$v.out_name_c"') or {
|
2020-02-01 06:37:22 +01:00
|
|
|
eprintln('clang-format not found')
|
|
|
|
os.Result{exit_code:-1}
|
|
|
|
}
|
|
|
|
if format_result.exit_code > 0 {
|
|
|
|
eprintln('clang-format failed to format $v.out_name_c')
|
|
|
|
eprintln(format_result.output)
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 20:31:23 +01:00
|
|
|
|
2020-02-01 06:37:22 +01:00
|
|
|
if ends_with_c || ends_with_js {
|
2019-10-25 20:57:32 +02: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 22:48:30 +02:00
|
|
|
$if !js {
|
2020-02-01 06:37:22 +01:00
|
|
|
if ends_with_js {
|
2019-09-14 22:48:30 +02:00
|
|
|
vjs_path := vexe + 'js'
|
2019-12-04 21:03:12 +01:00
|
|
|
if !os.exists(vjs_path) {
|
2019-09-14 22:48:30 +02:00
|
|
|
println('V.js compiler not found, building...')
|
2019-10-25 20:57:32 +02:00
|
|
|
// Build V.js. Specifying `-os js` makes V include
|
|
|
|
// only _js.v files and ignore _c.v files.
|
2020-02-09 10:08:04 +01:00
|
|
|
ret := os.system('$vexe -o $vjs_path -os js $vdir/cmd/v')
|
2019-09-14 22:48:30 +02:00
|
|
|
if ret == 0 {
|
|
|
|
println('Done.')
|
2019-12-19 21:36:18 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-09-14 22:48:30 +02:00
|
|
|
println('Failed.')
|
|
|
|
exit(1)
|
2019-11-28 14:49:35 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-09 10:08:04 +01:00
|
|
|
ret := os.system('$vjs_path -o $v.pref.out_name $v.pref.path')
|
2019-09-14 22:48:30 +02:00
|
|
|
if ret == 0 {
|
2020-02-09 10:08:04 +01:00
|
|
|
println('Done. Run it with `node $v.pref.out_name`')
|
2019-09-16 18:02:10 +02:00
|
|
|
println('JS backend is at a very early stage.')
|
2019-11-28 14:49:35 +01:00
|
|
|
}
|
2019-09-14 22:48:30 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-19 10:55:03 +01:00
|
|
|
// v.out_name_c may be on a different partition than v.out_name
|
2020-02-09 10:08:04 +01:00
|
|
|
os.mv_by_cp(v.out_name_c, v.pref.out_name)or{
|
2019-12-19 21:36:18 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
2019-08-29 23:07:54 +02:00
|
|
|
exit(0)
|
|
|
|
}
|
2019-08-23 11:05:16 +02:00
|
|
|
// Cross compiling for Windows
|
2020-02-09 10:08:04 +01:00
|
|
|
if v.pref.os == .windows {
|
2019-08-23 11:05:16 +02:00
|
|
|
$if !windows {
|
|
|
|
v.cc_windows_cross()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$if windows {
|
2019-12-16 16:56:37 +01:00
|
|
|
if v.pref.ccompiler == 'msvc' || v.no_cc_installed() {
|
2019-08-23 11:05:16 +02:00
|
|
|
v.cc_msvc()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-11-16 23:14:05 +01:00
|
|
|
// arguments for the C compiler
|
2019-12-19 21:36:18 +01:00
|
|
|
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',
|
|
|
|
// '-Wno-unused-but-set-variable',
|
|
|
|
'-Wno-unused-parameter', '-Wno-unused-result', '-Wno-unused-function', '-Wno-missing-braces', '-Wno-unused-label']
|
2019-10-12 15:05:24 +02: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-10-31 22:37:24 +01:00
|
|
|
if v.pref.fast {
|
|
|
|
$if linux {
|
2019-12-19 21:36:18 +01:00
|
|
|
$if !android {
|
|
|
|
tcc_3rd := '$vdir/thirdparty/tcc/bin/tcc'
|
|
|
|
// println('tcc third "$tcc_3rd"')
|
|
|
|
tcc_path := '/var/tmp/tcc/bin/tcc'
|
|
|
|
if os.exists(tcc_3rd) && !os.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.exists(tcc_path) {
|
|
|
|
// TODO tcc bug, needs an empty libtcc1.a fila
|
|
|
|
// os.mkdir('/var/tmp/tcc/lib/tcc/') or { panic(err) }
|
|
|
|
// os.create('/var/tmp/tcc/lib/tcc/libtcc1.a')
|
|
|
|
v.pref.ccompiler = tcc_path
|
|
|
|
a << '-m64'
|
|
|
|
}
|
2019-10-31 22:37:24 +01:00
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
verror('-fast is only supported on Linux right now')
|
2019-11-28 14:49:35 +01:00
|
|
|
}
|
2019-10-31 18:32:34 +01:00
|
|
|
}
|
2020-01-07 15:42:14 +01:00
|
|
|
|
|
|
|
if !v.pref.is_so
|
2020-01-22 21:34:38 +01:00
|
|
|
&& v.pref.build_mode != .build_module
|
2020-01-07 15:42:14 +01:00
|
|
|
&& os.user_os() == 'windows'
|
2020-02-09 10:08:04 +01:00
|
|
|
&& !v.pref.out_name.ends_with('.exe')
|
2020-01-07 15:42:14 +01:00
|
|
|
{
|
2020-02-09 10:08:04 +01:00
|
|
|
v.pref.out_name += '.exe'
|
2020-01-07 15:42:14 +01:00
|
|
|
}
|
2020-01-22 21:34:38 +01:00
|
|
|
|
2019-12-19 21:36:18 +01:00
|
|
|
// linux_host := os.user_os() == 'linux'
|
2020-02-09 10:08:04 +01:00
|
|
|
v.log('cc() isprod=$v.pref.is_prod outname=$v.pref.out_name')
|
2019-08-23 11:05:16 +02:00
|
|
|
if v.pref.is_so {
|
2019-12-19 21:36:18 +01:00
|
|
|
a << '-shared -fPIC ' // -Wl,-z,defs'
|
2020-02-09 10:08:04 +01:00
|
|
|
v.pref.out_name += '.so'
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
2019-11-14 08:23:44 +01:00
|
|
|
if v.pref.is_bare {
|
2019-12-04 09:46:09 +01:00
|
|
|
a << '-fno-stack-protector -static -ffreestanding -nostdlib'
|
2019-11-28 14:49:35 +01:00
|
|
|
}
|
2019-09-10 16:36:14 +02:00
|
|
|
if v.pref.build_mode == .build_module {
|
2019-10-04 14:48:09 +02:00
|
|
|
// Create the modules & out directory if it's not there.
|
2020-02-17 20:31:23 +01:00
|
|
|
mut out_dir := if v.pref.path.starts_with('vlib') { '$v_modules_path${filepath.separator}cache${filepath.separator}$v.pref.path' } else { '$v_modules_path${filepath.separator}$v.pref.path' }
|
|
|
|
pdir := out_dir.all_before_last(filepath.separator)
|
2019-12-04 21:03:12 +01:00
|
|
|
if !os.is_dir(pdir) {
|
2019-10-12 07:41:41 +02:00
|
|
|
os.mkdir_all(pdir)
|
2019-09-13 19:47:17 +02:00
|
|
|
}
|
2020-02-09 10:08:04 +01:00
|
|
|
v.pref.out_name = '${out_dir}.o' // v.out_name
|
|
|
|
println('Building ${v.pref.out_name}...')
|
2019-09-03 11:10:51 +02:00
|
|
|
}
|
2019-10-12 12:54:01 +02:00
|
|
|
debug_mode := v.pref.is_debug
|
|
|
|
mut debug_options := '-g'
|
2019-09-26 20:58:08 +02:00
|
|
|
mut optimization_options := '-O2'
|
2019-12-03 11:58:24 +01:00
|
|
|
mut guessed_compiler := v.pref.ccompiler
|
|
|
|
if guessed_compiler == 'cc' && v.pref.is_prod {
|
|
|
|
// deliberately guessing only for -prod builds for performance reasons
|
2019-12-19 21:36:18 +01:00
|
|
|
if ccversion:=os.exec('cc --version'){
|
2019-12-03 11:58:24 +01:00
|
|
|
if ccversion.exit_code == 0 {
|
2019-12-19 21:36:18 +01:00
|
|
|
if ccversion.output.contains('This is free software;') && ccversion.output.contains('Free Software Foundation, Inc.') {
|
2019-12-03 11:58:24 +01:00
|
|
|
guessed_compiler = 'gcc'
|
|
|
|
}
|
2019-12-19 21:36:18 +01:00
|
|
|
if ccversion.output.contains('clang version ') {
|
2019-12-03 11:58:24 +01:00
|
|
|
guessed_compiler = 'clang'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v.pref.ccompiler.contains('clang') || guessed_compiler == 'clang' {
|
2019-10-12 12:54:01 +02:00
|
|
|
if debug_mode {
|
2019-11-01 11:20:26 +01:00
|
|
|
debug_options = '-g -O0 -no-pie'
|
2019-09-26 20:58:08 +02:00
|
|
|
}
|
2020-02-10 08:57:35 +01:00
|
|
|
optimization_options = '-O3'
|
|
|
|
mut have_flto := true
|
|
|
|
$if openbsd {
|
|
|
|
have_flto = false
|
|
|
|
}
|
|
|
|
if have_flto {
|
|
|
|
optimization_options += ' -flto'
|
|
|
|
}
|
2019-09-26 20:58:08 +02:00
|
|
|
}
|
2019-12-03 11:58:24 +01:00
|
|
|
if v.pref.ccompiler.contains('gcc') || guessed_compiler == 'gcc' {
|
2019-10-12 12:54:01 +02:00
|
|
|
if debug_mode {
|
2019-11-01 11:20:26 +01:00
|
|
|
debug_options = '-g3 -no-pie'
|
2019-09-26 20:58:08 +02:00
|
|
|
}
|
|
|
|
optimization_options = '-O3 -fno-strict-aliasing -flto'
|
|
|
|
}
|
2019-10-12 12:54:01 +02:00
|
|
|
if debug_mode {
|
|
|
|
a << debug_options
|
|
|
|
}
|
2019-08-23 11:05:16 +02:00
|
|
|
if v.pref.is_prod {
|
2019-09-26 20:58:08 +02:00
|
|
|
a << optimization_options
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
2019-12-19 21:36:18 +01:00
|
|
|
if debug_mode && os.user_os() != 'windows' {
|
2019-08-27 22:29:13 +02:00
|
|
|
a << ' -rdynamic ' // needed for nicer symbolic backtraces
|
|
|
|
}
|
2020-02-09 10:08:04 +01:00
|
|
|
if v.pref.ccompiler != 'msvc' && v.pref.os != .freebsd {
|
2019-11-01 12:16:58 +01:00
|
|
|
a << '-Werror=implicit-function-declaration'
|
2019-08-23 22:55:45 +02:00
|
|
|
}
|
2019-08-23 11:05:16 +02:00
|
|
|
for f in v.generate_hotcode_reloading_compiler_flags() {
|
|
|
|
a << f
|
|
|
|
}
|
2019-12-19 21:36:18 +01:00
|
|
|
mut libs := '' // builtin.o os.o http.o etc
|
2019-09-10 16:36:14 +02:00
|
|
|
if v.pref.build_mode == .build_module {
|
2019-08-23 11:05:16 +02:00
|
|
|
a << '-c'
|
|
|
|
}
|
2019-10-12 12:54:01 +02:00
|
|
|
else if v.pref.is_cache {
|
2019-12-19 21:36:18 +01:00
|
|
|
builtin_o_path := filepath.join(v_modules_path,'cache','vlib','builtin.o')
|
2019-10-31 22:47:11 +01:00
|
|
|
a << builtin_o_path.replace('builtin.o', 'strconv.o') // TODO hack no idea why this is needed
|
2019-12-04 21:03:12 +01:00
|
|
|
if os.exists(builtin_o_path) {
|
2019-10-10 14:00:18 +02:00
|
|
|
libs = builtin_o_path
|
2019-12-19 21:36:18 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-10-10 21:27:22 +02:00
|
|
|
println('$builtin_o_path not found... building module builtin')
|
2020-02-17 20:31:23 +01:00
|
|
|
os.system('$vexe build module vlib${filepath.separator}builtin')
|
2019-10-10 14:00:18 +02:00
|
|
|
}
|
2019-08-23 11:05:16 +02:00
|
|
|
for imp in v.table.imports {
|
2019-12-19 21:36:18 +01:00
|
|
|
if imp.contains('vweb') {
|
|
|
|
continue
|
|
|
|
} // not working
|
|
|
|
if imp == 'webview' {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-17 20:31:23 +01:00
|
|
|
imp_path := imp.replace('.', filepath.separator)
|
|
|
|
path := '$v_modules_path${filepath.separator}cache${filepath.separator}vlib${filepath.separator}${imp_path}.o'
|
2019-12-19 21:36:18 +01:00
|
|
|
// println('adding ${imp_path}.o')
|
2019-12-04 21:03:12 +01:00
|
|
|
if os.exists(path) {
|
2019-10-10 01:59:33 +02:00
|
|
|
libs += ' ' + path
|
2019-12-19 21:36:18 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-10-10 21:27:22 +02:00
|
|
|
println('$path not found... building module $imp')
|
2019-10-31 22:57:16 +01:00
|
|
|
if path.ends_with('vlib/ui.o') {
|
|
|
|
println('copying ui...')
|
2019-12-19 21:36:18 +01: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 + '/vlib/ui.vh')or{
|
|
|
|
panic('error copying ui files')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-02-17 20:31:23 +01:00
|
|
|
os.system('$vexe build module vlib${filepath.separator}$imp_path')
|
2019-10-31 22:57:16 +01:00
|
|
|
}
|
2019-11-28 14:49:35 +01:00
|
|
|
}
|
2019-10-31 22:57:16 +01:00
|
|
|
if path.ends_with('vlib/ui.o') {
|
|
|
|
a << '-framework Cocoa -framework Carbon'
|
|
|
|
}
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if v.pref.sanitize {
|
|
|
|
a << '-fsanitize=leak'
|
|
|
|
}
|
2019-09-03 11:10:51 +02:00
|
|
|
// Cross compiling linux TODO
|
|
|
|
/*
|
2019-09-29 03:54:12 +02:00
|
|
|
sysroot := '/tmp/lld/linuxroot/'
|
2019-08-23 11:05:16 +02: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 11:10:51 +02:00
|
|
|
*/
|
2019-08-23 11:05:16 +02:00
|
|
|
// Cross compiling windows
|
2019-09-03 11:10:51 +02:00
|
|
|
//
|
2019-08-23 11:05:16 +02:00
|
|
|
// Output executable name
|
2020-02-09 10:08:04 +01:00
|
|
|
a << '-o "$v.pref.out_name"'
|
|
|
|
if os.is_dir(v.pref.out_name) {
|
|
|
|
verror("\'$v.pref.out_name\' is a directory")
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
2019-09-07 18:19:17 +02:00
|
|
|
// macOS code can include objective C TODO remove once objective C is replaced with C
|
2020-02-09 10:08:04 +01:00
|
|
|
if v.pref.os == .mac {
|
2019-08-23 11:05:16 +02:00
|
|
|
a << '-x objective-c'
|
|
|
|
}
|
|
|
|
// The C file we are compiling
|
|
|
|
a << '"$v.out_name_c"'
|
2020-02-09 10:08:04 +01:00
|
|
|
if v.pref.os == .mac {
|
2019-08-23 11:05:16 +02:00
|
|
|
a << '-x none'
|
|
|
|
}
|
|
|
|
// Min macos version is mandatory I think?
|
2020-02-09 10:08:04 +01:00
|
|
|
if v.pref.os == .mac {
|
2019-08-23 11:05:16 +02:00
|
|
|
a << '-mmacosx-version-min=10.7'
|
|
|
|
}
|
2020-02-09 10:08:04 +01:00
|
|
|
if v.pref.os == .windows {
|
2019-12-27 09:27:19 +01:00
|
|
|
a << '-municode'
|
|
|
|
}
|
2019-09-08 11:02:03 +02:00
|
|
|
cflags := v.get_os_cflags()
|
2019-09-11 12:34:19 +02:00
|
|
|
// add .o files
|
2019-09-22 23:51:59 +02:00
|
|
|
a << cflags.c_options_only_object_files()
|
2019-09-11 12:34:19 +02:00
|
|
|
// add all flags (-I -l -L etc) not .o files
|
2019-09-22 23:51:59 +02:00
|
|
|
a << cflags.c_options_without_object_files()
|
2019-08-23 11:05:16 +02:00
|
|
|
a << libs
|
|
|
|
// Without these libs compilation will fail on Linux
|
|
|
|
// || os.user_os() == 'linux'
|
2020-02-09 10:08:04 +01:00
|
|
|
if !v.pref.is_bare && v.pref.build_mode != .build_module && v.pref.os in [.linux, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .haiku] {
|
2019-08-23 11:05:16 +02:00
|
|
|
a << '-lm -lpthread '
|
|
|
|
// -ldl is a Linux only thing. BSDs have it in libc.
|
2020-02-09 10:08:04 +01:00
|
|
|
if v.pref.os == .linux {
|
2019-08-23 11:05:16 +02:00
|
|
|
a << ' -ldl '
|
|
|
|
}
|
2020-02-09 10:08:04 +01:00
|
|
|
if v.pref.os == .freebsd {
|
2019-12-19 21:36:18 +01:00
|
|
|
// FreeBSD: backtrace needs execinfo library while linking
|
2019-12-18 11:21:21 +01:00
|
|
|
a << ' -lexecinfo '
|
|
|
|
}
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
2020-02-09 10:08:04 +01:00
|
|
|
if !v.pref.is_bare && v.pref.os == .js && os.user_os() == 'linux' {
|
2019-09-16 21:00:59 +02:00
|
|
|
a << '-lm'
|
|
|
|
}
|
2019-08-23 11:05:16 +02:00
|
|
|
args := a.join(' ')
|
2019-10-25 17:49:49 +02:00
|
|
|
start:
|
2019-10-25 17:53:45 +02:00
|
|
|
todo()
|
|
|
|
// TODO remove
|
2019-08-23 11:05:16 +02: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-12-19 21:36:18 +01:00
|
|
|
res := os.exec(cmd)or{
|
2019-11-08 19:15:14 +01:00
|
|
|
// 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-12-19 21:36:18 +01:00
|
|
|
|
2019-11-14 08:23:44 +01:00
|
|
|
verror(err)
|
2019-11-08 19:15:14 +01:00
|
|
|
return
|
|
|
|
}
|
2019-08-23 11:05:16 +02:00
|
|
|
if res.exit_code != 0 {
|
2019-10-25 17:53:45 +02:00
|
|
|
// the command could not be found by the system
|
2019-08-23 11:05:16 +02:00
|
|
|
if res.exit_code == 127 {
|
2019-10-25 17:49:49 +02:00
|
|
|
$if linux {
|
2019-10-25 17:53:45 +02:00
|
|
|
// TCC problems on linux? Try GCC.
|
2019-10-25 19:11:53 +02:00
|
|
|
if v.pref.ccompiler.contains('tcc') {
|
2019-10-25 17:49:49 +02:00
|
|
|
v.pref.ccompiler = 'cc'
|
|
|
|
goto start
|
2019-11-28 14:49:35 +01:00
|
|
|
}
|
2019-10-25 17:49:49 +02:00
|
|
|
}
|
2019-12-19 21:36:18 +01:00
|
|
|
verror('C compiler error, while attempting to run: \n' + '-----------------------------------------------------------\n' + '$cmd\n' + '-----------------------------------------------------------\n' + 'Probably your C compiler is missing. \n' + 'Please reinstall it, or make it available in your PATH.\n\n' + missing_compiler_info())
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
|
|
|
if v.pref.is_debug {
|
2020-03-03 12:01:23 +01:00
|
|
|
eword := 'error:'
|
|
|
|
khighlight := if term.can_show_color_on_stdout() { term.red(eword) } else { eword }
|
|
|
|
println(res.output.replace(eword, khighlight))
|
2020-02-09 10:08:04 +01:00
|
|
|
verror("
|
|
|
|
==================
|
|
|
|
C error. This should never happen.
|
|
|
|
|
|
|
|
V compiler version: V $Version $vhash()
|
|
|
|
Host OS: ${pref.get_host_os().str()}
|
|
|
|
Target OS: $v.pref.os.str()
|
|
|
|
|
|
|
|
If you were not working with C interop and are not sure about what's happening,
|
|
|
|
please put the whole output in a pastebin and contact us through the following ways with a link to the pastebin:
|
|
|
|
- Raise an issue on GitHub: https://github.com/vlang/v/issues/new/choose
|
|
|
|
- Ask a question in #help on Discord: https://discord.gg/vlang")
|
2019-12-19 21:36:18 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-01-17 19:30:45 +01:00
|
|
|
if res.output.len < 30 {
|
2020-01-17 19:44:03 +01:00
|
|
|
println(res.output)
|
2020-01-17 19:30:45 +01:00
|
|
|
} else {
|
2020-03-03 12:01:23 +01:00
|
|
|
elines := error_context_lines( res.output, 'error:', 1, 12 )
|
2020-01-22 21:34:38 +01:00
|
|
|
println('==================')
|
2020-03-03 12:01:23 +01:00
|
|
|
for eline in elines {
|
|
|
|
println(eline)
|
|
|
|
}
|
2020-02-09 10:08:04 +01:00
|
|
|
println('...')
|
2020-01-22 21:34:38 +01:00
|
|
|
println('==================')
|
2020-02-09 10:08:04 +01:00
|
|
|
println('(Use `v -cg` to print the entire error message)\n')
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
2020-02-09 10:08:04 +01:00
|
|
|
verror("C error.
|
|
|
|
|
|
|
|
Please make sure that:
|
|
|
|
- You have all V dependencies installed.
|
|
|
|
- You did not declare a C function that was not included. (Try commenting your code that involves C interop)
|
|
|
|
- You are running the latest version of V. (Try running `v up` and rerunning your command)
|
|
|
|
|
|
|
|
If you're confident that all of the above is true, please try running V with the `-cg` option which enables more debugging capabilities.\n")
|
2019-08-23 11:05:16 +02: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 11:10:51 +02:00
|
|
|
/*
|
2019-08-23 11:05:16 +02: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-23 23:40:34 +02:00
|
|
|
verror(err)
|
2019-08-29 02:30:17 +02:00
|
|
|
return
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
|
|
|
println(ress.output)
|
|
|
|
println('linux cross compilation done. resulting binary: "$v.out_name"')
|
|
|
|
}
|
2019-09-03 11:10:51 +02:00
|
|
|
*/
|
2019-12-19 21:36:18 +01:00
|
|
|
|
2019-10-12 12:54:01 +02:00
|
|
|
if !v.pref.is_keep_c && v.out_name_c != 'v.c' {
|
2019-10-09 06:02:45 +02:00
|
|
|
os.rm(v.out_name_c)
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
2019-09-19 14:52:38 +02:00
|
|
|
if v.pref.compress {
|
2019-09-19 15:05:17 +02:00
|
|
|
$if windows {
|
|
|
|
println('-compress does not work on Windows for now')
|
|
|
|
return
|
2019-11-28 14:49:35 +01:00
|
|
|
}
|
2020-02-09 10:08:04 +01:00
|
|
|
ret := os.system('strip $v.pref.out_name')
|
2019-09-19 14:52:38 +02:00
|
|
|
if ret != 0 {
|
|
|
|
println('strip failed')
|
|
|
|
return
|
|
|
|
}
|
2020-01-23 22:06:49 +01:00
|
|
|
// NB: upx --lzma can sometimes fail with NotCompressibleException
|
|
|
|
// See https://github.com/vlang/v/pull/3528
|
2020-02-09 10:08:04 +01:00
|
|
|
mut ret2 := os.system('upx --lzma -qqq $v.pref.out_name')
|
2020-01-23 22:06:49 +01:00
|
|
|
if ret2 != 0 {
|
2020-02-09 10:08:04 +01:00
|
|
|
ret2 = os.system('upx -qqq $v.pref.out_name')
|
2020-01-23 22:06:49 +01:00
|
|
|
}
|
2019-09-19 14:52:38 +02:00
|
|
|
if ret2 != 0 {
|
|
|
|
println('upx failed')
|
2019-12-03 14:29:24 +01:00
|
|
|
$if macos {
|
2019-09-19 14:52:38 +02:00
|
|
|
println('install upx with `brew install upx`')
|
2019-11-28 14:49:35 +01:00
|
|
|
}
|
2019-09-19 14:52:38 +02:00
|
|
|
$if linux {
|
2019-12-19 21:36:18 +01:00
|
|
|
println('install upx\n' + 'for example, on Debian/Ubuntu run `sudo apt install upx`')
|
2019-11-28 14:49:35 +01:00
|
|
|
}
|
2019-09-19 14:52:38 +02:00
|
|
|
$if windows {
|
|
|
|
// :)
|
2019-11-28 14:49:35 +01:00
|
|
|
}
|
2019-09-19 14:52:38 +02:00
|
|
|
}
|
2019-11-28 14:49:35 +01:00
|
|
|
}
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (c mut V) cc_windows_cross() {
|
2020-01-03 11:38:26 +01:00
|
|
|
println('Cross compiling for Windows...')
|
2020-02-09 10:08:04 +01:00
|
|
|
if !c.pref.out_name.ends_with('.exe') {
|
|
|
|
c.pref.out_name += '.exe'
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
2020-02-09 10:08:04 +01:00
|
|
|
mut args := '-o $c.pref.out_name -w -L. '
|
2019-09-08 11:02:03 +02:00
|
|
|
cflags := c.get_os_cflags()
|
2019-08-23 11:05:16 +02:00
|
|
|
// -I flags
|
2019-12-19 21:36:18 +01:00
|
|
|
args += if c.pref.ccompiler == 'msvc' { cflags.c_options_before_target_msvc() } else { cflags.c_options_before_target() }
|
2019-08-23 11:05:16 +02:00
|
|
|
mut libs := ''
|
2020-01-03 11:38:26 +01:00
|
|
|
if false && c.pref.build_mode == .default_mode {
|
2019-09-29 03:54:12 +02:00
|
|
|
libs = '"$v_modules_path/vlib/builtin.o"'
|
2019-12-04 21:03:12 +01:00
|
|
|
if !os.exists(libs) {
|
2019-12-19 21:36:18 +01:00
|
|
|
println('`$libs` not found')
|
|
|
|
exit(1)
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
|
|
|
for imp in c.table.imports {
|
2019-12-19 21:36:18 +01:00
|
|
|
libs += ' "$v_modules_path/vlib/${imp}.o"'
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
args += ' $c.out_name_c '
|
2020-01-03 11:38:26 +01:00
|
|
|
|
2019-12-19 21:36:18 +01:00
|
|
|
args += if c.pref.ccompiler == 'msvc' { cflags.c_options_after_target_msvc() } else { cflags.c_options_after_target() }
|
2020-01-03 11:38:26 +01:00
|
|
|
|
|
|
|
/*
|
2019-09-29 03:54:12 +02:00
|
|
|
winroot := '$v_modules_path/winroot'
|
2019-12-04 21:03:12 +01:00
|
|
|
if !os.is_dir(winroot) {
|
2019-08-23 11:05:16 +02:00
|
|
|
winroot_url := 'https://github.com/vlang/v/releases/download/v0.1.10/winroot.zip'
|
2019-08-28 13:35:48 +02:00
|
|
|
println('"$winroot" not found.')
|
2019-09-29 03:54:12 +02:00
|
|
|
println('Download it from $winroot_url and save it in $v_modules_path')
|
2019-08-28 13:35:48 +02:00
|
|
|
println('Unzip it afterwards.\n')
|
2019-12-19 21:36:18 +01:00
|
|
|
println('winroot.zip contains all library and header files needed ' + 'to cross-compile for Windows.')
|
2019-11-28 14:49:35 +01:00
|
|
|
exit(1)
|
2019-08-23 11:05:16 +02: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 '
|
2020-01-03 11:38:26 +01:00
|
|
|
*/
|
|
|
|
mut cmd := ''
|
2020-01-03 11:41:39 +01:00
|
|
|
cmd = ''
|
2020-01-03 11:38:26 +01:00
|
|
|
$if macos {
|
2020-01-04 00:06:01 +01:00
|
|
|
cmd = 'x86_64-w64-mingw32-gcc -std=gnu11 $args -municode'
|
2020-01-03 11:38:26 +01:00
|
|
|
}
|
|
|
|
$else {
|
|
|
|
panic('your platform is not supported yet')
|
|
|
|
}
|
|
|
|
|
2020-01-03 11:41:39 +01:00
|
|
|
println(cmd)
|
2020-01-03 11:38:26 +01:00
|
|
|
//cmd := 'clang -o $obj_name -w $include -m32 -c -target x86_64-win32 $v_modules_path/$c.out_name_c'
|
2019-08-23 11:05:16 +02:00
|
|
|
if c.pref.show_c_cmd {
|
2019-12-19 21:36:18 +01:00
|
|
|
println(cmd)
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
|
|
|
if os.system(cmd) != 0 {
|
|
|
|
println('Cross compilation for Windows failed. Make sure you have clang installed.')
|
|
|
|
exit(1)
|
|
|
|
}
|
2020-01-03 11:38:26 +01:00
|
|
|
/*
|
2019-09-10 17:19:29 +02:00
|
|
|
if c.pref.build_mode != .build_module {
|
2019-12-19 21:36:18 +01: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'
|
2019-08-23 11:05:16 +02:00
|
|
|
if c.pref.show_c_cmd {
|
|
|
|
println(link_cmd)
|
|
|
|
}
|
2019-12-19 21:36:18 +01:00
|
|
|
if os.system(link_cmd) != 0 {
|
2019-08-23 11:05:16 +02:00
|
|
|
println('Cross compilation for Windows failed. Make sure you have lld linker installed.')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
// os.rm(obj_name)
|
|
|
|
}
|
2020-01-03 11:38:26 +01:00
|
|
|
*/
|
2019-08-23 11:05:16 +02:00
|
|
|
println('Done!')
|
|
|
|
}
|
|
|
|
|
2019-09-22 23:51:59 +02:00
|
|
|
fn (c &V) build_thirdparty_obj_files() {
|
2019-09-06 14:12:04 +02:00
|
|
|
for flag in c.get_os_cflags() {
|
2019-11-28 14:49:35 +01:00
|
|
|
if flag.value.ends_with('.o') {
|
2019-12-19 21:36:18 +01:00
|
|
|
rest_of_module_flags := c.get_rest_of_module_cflags(flag)
|
2019-12-16 16:56:37 +01:00
|
|
|
if c.pref.ccompiler == 'msvc' || c.no_cc_installed() {
|
2019-09-22 23:51:59 +02:00
|
|
|
build_thirdparty_obj_file_with_msvc(flag.value, rest_of_module_flags)
|
2019-09-06 14:12:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-02-09 10:08:04 +01:00
|
|
|
c.build_thirdparty_obj_file(flag.value, rest_of_module_flags)
|
2019-12-23 11:09:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
2019-11-28 14:49:35 +01: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 14:29:24 +01:00
|
|
|
$if macos {
|
2019-11-28 14:49:35 +01:00
|
|
|
return 'Install command line XCode tools with `xcode-select --install`'
|
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|
2020-03-03 12:01:23 +01:00
|
|
|
|
|
|
|
fn error_context_lines(text, keyword string, before, after int) []string {
|
|
|
|
khighlight := if term.can_show_color_on_stdout() { term.red(keyword) } else { keyword }
|
|
|
|
mut eline_idx := 0
|
|
|
|
mut lines := text.split_into_lines()
|
|
|
|
for idx, eline in lines {
|
|
|
|
if eline.contains(keyword) {
|
|
|
|
lines[idx] = lines[idx].replace(keyword, khighlight)
|
|
|
|
if eline_idx == 0 {
|
|
|
|
eline_idx = idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
idx_s := if eline_idx - before >= 0 { eline_idx - before } else { 0 }
|
|
|
|
idx_e := if idx_s + after < lines.len { idx_s + after } else { lines.len }
|
|
|
|
return lines[idx_s..idx_e]
|
|
|
|
}
|