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.
|
2020-04-07 00:44:19 +02:00
|
|
|
module builder
|
2019-08-23 11:05:16 +02:00
|
|
|
|
2020-04-25 17:49:16 +02:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import v.cflag
|
|
|
|
import v.pref
|
2020-12-15 16:07:06 +01:00
|
|
|
import v.util
|
2020-04-25 17:49:16 +02:00
|
|
|
import term
|
2019-08-23 11:05:16 +02:00
|
|
|
|
2020-05-14 18:14:35 +02:00
|
|
|
const (
|
2020-10-17 17:27:06 +02:00
|
|
|
c_verror_message_marker = 'VERROR_MESSAGE '
|
|
|
|
c_error_info = '
|
2020-05-14 18:14:35 +02:00
|
|
|
==================
|
|
|
|
C error. This should never happen.
|
|
|
|
|
|
|
|
If you were not working with C interop, please raise an issue on GitHub:
|
|
|
|
|
|
|
|
https://github.com/vlang/v/issues/new/choose
|
|
|
|
|
|
|
|
You can also use #help on Discord: https://discord.gg/vlang
|
2020-06-19 12:54:56 +02:00
|
|
|
'
|
2020-10-17 17:27:06 +02:00
|
|
|
no_compiler_error = '
|
2020-06-19 12:54:56 +02:00
|
|
|
==================
|
|
|
|
Error: no C compiler detected.
|
|
|
|
|
|
|
|
You can find instructions on how to install one in the V wiki:
|
|
|
|
https://github.com/vlang/v/wiki/Installing-a-C-compiler-on-Windows
|
|
|
|
|
|
|
|
If you think you have one installed, make sure it is in your PATH.
|
|
|
|
If you do have one in your PATH, please raise an issue on GitHub:
|
|
|
|
https://github.com/vlang/v/issues/new/choose
|
|
|
|
|
2020-09-21 17:29:52 +02:00
|
|
|
You can also use `v doctor`, to see what V knows about your current environment.
|
|
|
|
|
|
|
|
You can also seek #help on Discord: https://discord.gg/vlang
|
2020-06-19 12:54:56 +02:00
|
|
|
'
|
|
|
|
)
|
2020-05-14 18:14:35 +02:00
|
|
|
|
2020-06-10 17:15:24 +02:00
|
|
|
const (
|
|
|
|
mingw_cc = 'x86_64-w64-mingw32-gcc'
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2020-09-18 00:58:54 +02:00
|
|
|
fn (mut v Builder) find_win_cc() ? {
|
2020-07-09 12:26:15 +02:00
|
|
|
$if !windows {
|
|
|
|
return none
|
|
|
|
}
|
2020-06-19 12:54:56 +02:00
|
|
|
os.exec('$v.pref.ccompiler -v') or {
|
|
|
|
if v.pref.is_verbose {
|
|
|
|
println('$v.pref.ccompiler not found, looking for msvc...')
|
|
|
|
}
|
|
|
|
find_msvc() or {
|
2020-04-07 00:44:19 +02:00
|
|
|
if v.pref.is_verbose {
|
2020-06-19 12:54:56 +02:00
|
|
|
println('msvc not found, looking for thirdparty/tcc...')
|
|
|
|
}
|
2020-11-30 20:48:16 +01:00
|
|
|
vpath := os.dir(pref.vexe_path())
|
2020-06-19 12:54:56 +02:00
|
|
|
thirdparty_tcc := os.join_path(vpath, 'thirdparty', 'tcc', 'tcc.exe')
|
|
|
|
os.exec('$thirdparty_tcc -v') or {
|
|
|
|
if v.pref.is_verbose {
|
2020-09-18 00:58:54 +02:00
|
|
|
println('tcc not found')
|
2020-06-19 12:54:56 +02:00
|
|
|
}
|
|
|
|
return none
|
2019-12-16 16:56:37 +01:00
|
|
|
}
|
2020-09-18 00:58:54 +02:00
|
|
|
v.pref.ccompiler = thirdparty_tcc
|
|
|
|
v.pref.ccompiler_type = .tinyc
|
|
|
|
return
|
2019-12-12 12:27:47 +01:00
|
|
|
}
|
2020-09-18 00:58:54 +02:00
|
|
|
v.pref.ccompiler = 'msvc'
|
|
|
|
v.pref.ccompiler_type = .msvc
|
|
|
|
return
|
2019-11-08 19:15:14 +01:00
|
|
|
}
|
2020-09-18 00:58:54 +02:00
|
|
|
v.pref.ccompiler_type = pref.cc_from_string(v.pref.ccompiler)
|
2019-11-08 19:15:14 +01:00
|
|
|
}
|
|
|
|
|
2020-11-06 15:26:59 +01:00
|
|
|
fn (mut v Builder) show_c_compiler_output(res os.Result) {
|
|
|
|
println('======== C Compiler output ========')
|
|
|
|
println(res.output)
|
|
|
|
println('=================================')
|
|
|
|
}
|
|
|
|
|
2020-10-17 17:27:06 +02:00
|
|
|
fn (mut v Builder) post_process_c_compiler_output(res os.Result) {
|
|
|
|
if res.exit_code == 0 {
|
2020-10-24 19:29:24 +02:00
|
|
|
if v.pref.reuse_tmpc {
|
|
|
|
return
|
|
|
|
}
|
2020-10-24 14:08:45 +02:00
|
|
|
for tmpfile in v.pref.cleanup_files {
|
|
|
|
if os.is_file(tmpfile) {
|
|
|
|
if v.pref.is_verbose {
|
|
|
|
eprintln('>> remove tmp file: $tmpfile')
|
|
|
|
}
|
|
|
|
os.rm(tmpfile)
|
|
|
|
}
|
|
|
|
}
|
2020-10-17 17:27:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for emsg_marker in [c_verror_message_marker, 'error: include file '] {
|
|
|
|
if res.output.contains(emsg_marker) {
|
|
|
|
emessage := res.output.all_after(emsg_marker).all_before('\n').all_before('\r').trim_right('\r\n')
|
|
|
|
verror(emessage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v.pref.is_debug {
|
|
|
|
eword := 'error:'
|
|
|
|
khighlight := if term.can_show_color_on_stdout() { term.red(eword) } else { eword }
|
|
|
|
println(res.output.trim_right('\r\n').replace(eword, khighlight))
|
|
|
|
} else {
|
|
|
|
if res.output.len < 30 {
|
|
|
|
println(res.output)
|
|
|
|
} else {
|
|
|
|
elines := error_context_lines(res.output, 'error:', 1, 12)
|
|
|
|
println('==================')
|
|
|
|
for eline in elines {
|
|
|
|
println(eline)
|
|
|
|
}
|
|
|
|
println('...')
|
|
|
|
println('==================')
|
|
|
|
println('(Use `v -cg` to print the entire error message)\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
verror(c_error_info)
|
|
|
|
}
|
|
|
|
|
2020-10-25 02:09:07 +02:00
|
|
|
fn (mut v Builder) rebuild_cached_module(vexe string, imp_path string) string {
|
|
|
|
res := v.pref.cache_manager.exists('.o', imp_path) or {
|
|
|
|
println('Cached $imp_path .o file not found... Building .o file for $imp_path')
|
2020-11-08 08:18:05 +01:00
|
|
|
// do run `v build-module x` always in main vfolder; x can be a relative path
|
|
|
|
pwd := os.getwd()
|
|
|
|
vroot := os.dir(vexe)
|
|
|
|
os.chdir(vroot)
|
2020-10-25 02:09:07 +02:00
|
|
|
boptions := v.pref.build_options.join(' ')
|
|
|
|
rebuild_cmd := '$vexe $boptions build-module $imp_path'
|
|
|
|
// eprintln('>> rebuild_cmd: $rebuild_cmd')
|
|
|
|
os.system(rebuild_cmd)
|
|
|
|
rebuilded_o := v.pref.cache_manager.exists('.o', imp_path) or {
|
|
|
|
panic('could not rebuild cache module for $imp_path, error: $err')
|
|
|
|
}
|
2020-11-08 08:18:05 +01:00
|
|
|
os.chdir(pwd)
|
2020-10-25 02:09:07 +02:00
|
|
|
return rebuilded_o
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-11-03 11:37:04 +01:00
|
|
|
fn (mut v Builder) show_cc(cmd string, response_file string, response_file_content string) {
|
|
|
|
if v.pref.is_verbose || v.pref.show_cc {
|
|
|
|
println('')
|
|
|
|
println('=====================')
|
|
|
|
println('> C compiler cmd: $cmd')
|
|
|
|
if v.pref.show_cc {
|
|
|
|
println('> C compiler response file $response_file:')
|
|
|
|
println(response_file_content)
|
|
|
|
}
|
|
|
|
println('=====================')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 17:49:16 +02:00
|
|
|
fn (mut v Builder) cc() {
|
2019-12-18 18:07:32 +01:00
|
|
|
if os.executable().contains('vfmt') {
|
|
|
|
return
|
|
|
|
}
|
2020-04-07 00:44:19 +02:00
|
|
|
if v.pref.is_verbose {
|
|
|
|
println('builder.cc() pref.out_name="$v.pref.out_name"')
|
|
|
|
}
|
2020-07-12 21:44:38 +02:00
|
|
|
if v.pref.only_check_syntax {
|
|
|
|
if v.pref.is_verbose {
|
|
|
|
println('builder.cc returning early, since pref.only_check_syntax is true')
|
|
|
|
}
|
|
|
|
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()
|
2020-03-07 22:26:26 +01:00
|
|
|
vdir := os.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 ends_with_c || ends_with_js {
|
2020-05-08 14:31:03 +02:00
|
|
|
v.pref.skip_running = true
|
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.')
|
2020-04-25 17:49:16 +02: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-12-04 12:25:23 +01:00
|
|
|
os.mv_by_cp(v.out_name_c, v.pref.out_name) or { panic(err) }
|
2020-05-08 14:31:03 +02:00
|
|
|
return
|
2019-08-29 23:07:54 +02:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
2020-11-29 16:18:49 +01:00
|
|
|
//
|
|
|
|
mut tried_compilation_commands := []string{}
|
|
|
|
original_pwd := os.getwd()
|
2020-12-01 21:10:02 +01:00
|
|
|
for {
|
|
|
|
// try to compile with the choosen compiler
|
|
|
|
// if compilation fails, retry again with another
|
|
|
|
mut ccompiler := v.pref.ccompiler
|
|
|
|
$if windows {
|
|
|
|
if ccompiler == 'msvc' {
|
|
|
|
v.cc_msvc()
|
|
|
|
return
|
|
|
|
}
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
if v.pref.os == .ios {
|
|
|
|
ios_sdk := if v.pref.is_ios_simulator { 'iphonesimulator' } else { 'iphoneos' }
|
|
|
|
ios_sdk_path_res := os.exec('xcrun --sdk $ios_sdk --show-sdk-path') or {
|
|
|
|
panic("Couldn\'t find iphonesimulator")
|
|
|
|
}
|
|
|
|
mut isysroot := ios_sdk_path_res.output.replace('\n', '')
|
|
|
|
ccompiler = 'xcrun --sdk iphoneos clang -isysroot $isysroot'
|
2020-09-05 01:35:35 +02:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
// arguments for the C compiler
|
|
|
|
// TODO : activate -Werror once no warnings remain
|
|
|
|
// '-Werror',
|
|
|
|
// TODO : try and remove the below workaround options when the corresponding
|
|
|
|
// warnings are totally fixed/removed
|
|
|
|
mut args := [v.pref.cflags, '-std=gnu99', '-Wall', '-Wextra', '-Wno-unused-variable', '-Wno-unused-parameter',
|
2020-12-12 23:06:02 +01:00
|
|
|
'-Wno-unused-result', '-Wno-unused-function', '-Wno-missing-braces', '-Wno-unused-label', '-Wshadow',
|
|
|
|
'-Wno-unused',
|
|
|
|
]
|
2020-12-01 21:10:02 +01:00
|
|
|
if v.pref.os == .ios {
|
|
|
|
args << '-framework Foundation'
|
|
|
|
args << '-framework UIKit'
|
|
|
|
args << '-framework Metal'
|
|
|
|
args << '-framework MetalKit'
|
|
|
|
args << '-DSOKOL_METAL'
|
|
|
|
args << '-fobjc-arc'
|
2020-05-29 04:52:19 +02:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
mut linker_flags := []string{}
|
|
|
|
if !v.pref.is_shared && v.pref.build_mode != .build_module && os.user_os() == 'windows' &&
|
|
|
|
!v.pref.out_name.ends_with('.exe') {
|
|
|
|
v.pref.out_name += '.exe'
|
|
|
|
}
|
|
|
|
// linux_host := os.user_os() == 'linux'
|
|
|
|
v.log('cc() isprod=$v.pref.is_prod outname=$v.pref.out_name')
|
|
|
|
if v.pref.is_shared {
|
2020-12-15 17:22:07 +01:00
|
|
|
mut shared_postfix := '.so'
|
2020-12-01 21:10:02 +01:00
|
|
|
$if macos {
|
2020-12-15 17:22:07 +01:00
|
|
|
shared_postfix = '.dylib'
|
2020-12-01 21:10:02 +01:00
|
|
|
} $else $if windows {
|
2020-12-15 17:22:07 +01:00
|
|
|
shared_postfix = '.dll'
|
|
|
|
}
|
|
|
|
if !v.pref.out_name.ends_with(shared_postfix) {
|
|
|
|
v.pref.out_name += shared_postfix
|
2020-12-01 21:10:02 +01:00
|
|
|
}
|
2020-12-15 17:22:07 +01:00
|
|
|
linker_flags << '-shared'
|
|
|
|
args << '-fPIC' // -Wl,-z,defs'
|
2020-12-01 21:10:02 +01:00
|
|
|
}
|
|
|
|
if v.pref.is_bare {
|
|
|
|
args << '-fno-stack-protector'
|
|
|
|
args << '-ffreestanding'
|
|
|
|
linker_flags << '-static'
|
|
|
|
linker_flags << '-nostdlib'
|
|
|
|
}
|
|
|
|
if v.pref.build_mode == .build_module {
|
|
|
|
v.pref.out_name = v.pref.cache_manager.postfix_with_key2cpath('.o', v.pref.path) // v.out_name
|
|
|
|
println('Building $v.pref.path to $v.pref.out_name ...')
|
|
|
|
v.pref.cache_manager.save('.description.txt', v.pref.path, '${v.pref.path:-30} @ $v.pref.cache_manager.vopts\n')
|
|
|
|
// println('v.table.imports:')
|
|
|
|
// println(v.table.imports)
|
|
|
|
}
|
|
|
|
debug_mode := v.pref.is_debug
|
|
|
|
mut debug_options := '-g3'
|
|
|
|
mut optimization_options := '-O2'
|
|
|
|
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'
|
|
|
|
}
|
2019-12-03 11:58:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
//
|
|
|
|
is_cc_tcc := ccompiler.contains('tcc') || guessed_compiler == 'tcc'
|
|
|
|
is_cc_clang := !is_cc_tcc && (ccompiler.contains('clang') || guessed_compiler == 'clang')
|
|
|
|
is_cc_gcc := !is_cc_tcc && !is_cc_clang &&
|
|
|
|
(ccompiler.contains('gcc') || guessed_compiler == 'gcc')
|
|
|
|
// is_cc_msvc := v.pref.ccompiler.contains('msvc') || guessed_compiler == 'msvc'
|
|
|
|
//
|
|
|
|
if is_cc_clang {
|
|
|
|
if debug_mode {
|
|
|
|
debug_options = '-g3 -O0'
|
|
|
|
}
|
|
|
|
optimization_options = '-O3'
|
|
|
|
mut have_flto := true
|
|
|
|
$if openbsd {
|
|
|
|
have_flto = false
|
|
|
|
}
|
|
|
|
if have_flto {
|
|
|
|
optimization_options += ' -flto'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_cc_gcc {
|
|
|
|
if debug_mode {
|
|
|
|
debug_options = '-g3 -no-pie'
|
|
|
|
}
|
|
|
|
optimization_options = '-O3 -fno-strict-aliasing -flto'
|
|
|
|
}
|
2019-10-12 12:54:01 +02:00
|
|
|
if debug_mode {
|
2020-12-01 21:10:02 +01:00
|
|
|
args << debug_options
|
|
|
|
// $if macos {
|
|
|
|
// args << ' -ferror-limit=5000 '
|
|
|
|
// }
|
2019-09-26 20:58:08 +02:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
if v.pref.is_prod {
|
|
|
|
args << optimization_options
|
2020-02-10 08:57:35 +01:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
if v.pref.is_prod && !debug_mode {
|
|
|
|
// sokol and other C libraries that use asserts
|
|
|
|
// have much better performance when NDEBUG is defined
|
|
|
|
// See also http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
|
|
|
|
args << '-DNDEBUG'
|
2020-02-10 08:57:35 +01:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
if debug_mode && os.user_os() != 'windows' {
|
|
|
|
linker_flags << ' -rdynamic ' // needed for nicer symbolic backtraces
|
2019-09-26 20:58:08 +02:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
if ccompiler != 'msvc' && v.pref.os != .freebsd {
|
|
|
|
args << '-Werror=implicit-function-declaration'
|
2020-04-18 17:46:23 +02:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
if v.pref.is_liveshared || v.pref.is_livemain {
|
|
|
|
if v.pref.os == .linux || os.user_os() == 'linux' {
|
|
|
|
linker_flags << '-rdynamic'
|
|
|
|
}
|
|
|
|
if v.pref.os == .macos || os.user_os() == 'macos' {
|
|
|
|
args << '-flat_namespace'
|
|
|
|
}
|
2020-04-18 17:46:23 +02:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
mut libs := '' // builtin.o os.o http.o etc
|
|
|
|
if v.pref.build_mode == .build_module {
|
|
|
|
args << '-c'
|
|
|
|
} else if v.pref.use_cache {
|
|
|
|
mut built_modules := []string{}
|
|
|
|
builtin_obj_path := v.rebuild_cached_module(vexe, 'vlib/builtin')
|
|
|
|
libs += ' ' + builtin_obj_path
|
|
|
|
for ast_file in v.parsed_files {
|
|
|
|
for imp_stmt in ast_file.imports {
|
|
|
|
imp := imp_stmt.mod
|
2020-12-18 01:21:40 +01:00
|
|
|
// strconv is already imported inside builtin, so skip generating its object file
|
|
|
|
// TODO: incase we have other modules with the same name, make sure they are vlib
|
|
|
|
// is this even doign anything?
|
|
|
|
if imp in ['strconv', 'strings'] {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
if imp in built_modules {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-15 16:07:06 +01:00
|
|
|
if util.should_bundle_module(imp) {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
// not working
|
|
|
|
if imp == 'webview' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// The problem is cmd/v is in module main and imports
|
|
|
|
// the relative module named help, which is built as cmd.v.help not help
|
|
|
|
// currently this got this workign by building into main, see ast.FnDecl in cgen
|
|
|
|
if imp == 'help' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// we are skipping help manually above, this code will skip all relative imports
|
|
|
|
// if os.is_dir(af_base_dir + os.path_separator + mod_path) {
|
|
|
|
// continue
|
|
|
|
// }
|
2020-12-18 01:21:40 +01:00
|
|
|
// mod_path := imp.replace('.', os.path_separator)
|
|
|
|
// imp_path := os.join_path('vlib', mod_path)
|
|
|
|
imp_path := v.find_module_path(imp, ast_file.path) or {
|
|
|
|
verror('cannot import module "$imp" (not found)')
|
|
|
|
break
|
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
obj_path := v.rebuild_cached_module(vexe, imp_path)
|
|
|
|
libs += ' ' + obj_path
|
|
|
|
if obj_path.ends_with('vlib/ui.o') {
|
|
|
|
args << '-framework Cocoa -framework Carbon'
|
|
|
|
}
|
|
|
|
built_modules << imp
|
2020-07-31 12:10:19 +02:00
|
|
|
}
|
2019-10-31 22:57:16 +01:00
|
|
|
}
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
if v.pref.sanitize {
|
|
|
|
args << '-fsanitize=leak'
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
// Cross compiling for linux
|
|
|
|
if v.pref.os == .linux {
|
|
|
|
$if !linux {
|
|
|
|
v.cc_linux_cross()
|
|
|
|
return
|
|
|
|
}
|
2020-11-29 16:18:49 +01:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
// Cross compiling windows
|
|
|
|
//
|
|
|
|
// Output executable name
|
|
|
|
if v.pref.os == .ios {
|
|
|
|
bundle_name := v.pref.out_name.split('/').last()
|
|
|
|
args << '-o "${v.pref.out_name}.app/$bundle_name"'
|
|
|
|
} else {
|
|
|
|
args << '-o "$v.pref.out_name"'
|
|
|
|
}
|
|
|
|
if os.is_dir(v.pref.out_name) {
|
|
|
|
verror("'$v.pref.out_name' is a directory")
|
|
|
|
}
|
|
|
|
// macOS code can include objective C TODO remove once objective C is replaced with C
|
|
|
|
if v.pref.os == .macos || v.pref.os == .ios {
|
|
|
|
if !is_cc_tcc {
|
|
|
|
args << '-x objective-c'
|
2020-04-11 14:24:00 +02:00
|
|
|
}
|
2020-04-11 08:57:31 +02:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
// The C file we are compiling
|
|
|
|
args << '"$v.out_name_c"'
|
|
|
|
if v.pref.os == .macos {
|
|
|
|
args << '-x none'
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
// Min macos version is mandatory I think?
|
|
|
|
if v.pref.os == .macos {
|
|
|
|
args << '-mmacosx-version-min=10.7'
|
2019-12-18 11:21:21 +01:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
if v.pref.os == .ios {
|
|
|
|
args << '-miphoneos-version-min=10.0'
|
|
|
|
}
|
|
|
|
if v.pref.os == .windows {
|
|
|
|
args << '-municode'
|
|
|
|
}
|
|
|
|
cflags := v.get_os_cflags()
|
|
|
|
// add .o files
|
|
|
|
args << cflags.c_options_only_object_files()
|
|
|
|
// add all flags (-I -l -L etc) not .o files
|
|
|
|
args << cflags.c_options_without_object_files()
|
|
|
|
args << libs
|
|
|
|
// For C++ we must be very tolerant
|
|
|
|
if guessed_compiler.contains('++') {
|
|
|
|
args << '-fpermissive'
|
|
|
|
args << '-w'
|
|
|
|
}
|
|
|
|
// TODO: why is this duplicated from above?
|
|
|
|
if v.pref.use_cache {
|
|
|
|
// vexe := pref.vexe_path()
|
|
|
|
// cached_modules := ['builtin', 'os', 'math', 'strconv', 'strings', 'hash'], // , 'strconv.ftoa']
|
|
|
|
// for cfile in cached_modules {
|
|
|
|
// ofile := os.join_path(pref.default_module_path, 'cache', 'vlib', cfile.replace('.', '/') +
|
|
|
|
// '.o')
|
|
|
|
// if !os.exists(ofile) {
|
|
|
|
// println('${cfile}.o is missing. Building...')
|
|
|
|
// println('$vexe build-module vlib/$cfile')
|
|
|
|
// os.system('$vexe build-module vlib/$cfile')
|
|
|
|
// }
|
|
|
|
// args << ofile
|
|
|
|
// }
|
|
|
|
if !is_cc_tcc {
|
|
|
|
$if linux {
|
|
|
|
linker_flags << '-Xlinker -z'
|
|
|
|
linker_flags << '-Xlinker muldefs'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_cc_tcc && 'no_backtrace' !in v.pref.compile_defines {
|
|
|
|
args << '-bt25'
|
|
|
|
}
|
|
|
|
// Without these libs compilation will fail on Linux
|
|
|
|
// || os.user_os() == 'linux'
|
|
|
|
if !v.pref.is_bare && v.pref.build_mode != .build_module && v.pref.os in
|
|
|
|
[.linux, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .haiku] {
|
|
|
|
linker_flags << '-lm'
|
|
|
|
linker_flags << '-lpthread'
|
|
|
|
// -ldl is a Linux only thing. BSDs have it in libc.
|
|
|
|
if v.pref.os == .linux {
|
|
|
|
linker_flags << '-ldl'
|
|
|
|
}
|
|
|
|
if v.pref.os == .freebsd {
|
|
|
|
// FreeBSD: backtrace needs execinfo library while linking
|
|
|
|
linker_flags << '-lexecinfo'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !v.pref.is_bare && v.pref.os == .js && os.user_os() == 'linux' {
|
|
|
|
linker_flags << '-lm'
|
|
|
|
}
|
|
|
|
env_cflags := os.getenv('CFLAGS')
|
|
|
|
env_ldflags := os.getenv('LDFLAGS')
|
|
|
|
str_args := env_cflags + ' ' + args.join(' ') + ' ' + linker_flags.join(' ') + ' ' + env_ldflags
|
|
|
|
if v.pref.is_verbose {
|
|
|
|
println('cc args=$str_args')
|
|
|
|
println(args)
|
|
|
|
}
|
|
|
|
// write args to response file
|
|
|
|
response_file := '${v.out_name_c}.rsp'
|
|
|
|
response_file_content := str_args.replace('\\', '\\\\')
|
|
|
|
os.write_file(response_file, response_file_content) or {
|
|
|
|
verror('Unable to write response file "$response_file"')
|
|
|
|
}
|
|
|
|
if !debug_mode {
|
|
|
|
v.pref.cleanup_files << v.out_name_c
|
|
|
|
v.pref.cleanup_files << response_file
|
|
|
|
}
|
|
|
|
//
|
|
|
|
todo()
|
|
|
|
os.chdir(vdir)
|
|
|
|
cmd := '$ccompiler @$response_file'
|
|
|
|
tried_compilation_commands << cmd
|
|
|
|
v.show_cc(cmd, response_file, response_file_content)
|
|
|
|
// Run
|
|
|
|
ticks := time.ticks()
|
|
|
|
res := os.exec(cmd) or {
|
|
|
|
// C compilation failed.
|
|
|
|
// If we are on Windows, try msvc
|
|
|
|
println('C compilation failed.')
|
|
|
|
os.chdir(original_pwd)
|
|
|
|
/*
|
|
|
|
if os.user_os() == 'windows' && v.pref.ccompiler != 'msvc' {
|
2019-11-08 19:15:14 +01:00
|
|
|
println('Trying to build with MSVC')
|
|
|
|
v.cc_msvc()
|
|
|
|
return
|
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
*/
|
|
|
|
verror(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
diff := time.ticks() - ticks
|
|
|
|
v.timing_message('C ${ccompiler:3}', diff)
|
|
|
|
if v.pref.show_c_output {
|
|
|
|
v.show_c_compiler_output(res)
|
|
|
|
}
|
|
|
|
os.chdir(original_pwd)
|
|
|
|
if res.exit_code != 0 {
|
|
|
|
if ccompiler.contains('tcc.exe') {
|
|
|
|
// a TCC problem? Retry with the system cc:
|
|
|
|
if tried_compilation_commands.len > 1 {
|
|
|
|
eprintln('Recompilation loop detected (ccompiler: $ccompiler):')
|
|
|
|
for recompile_command in tried_compilation_commands {
|
|
|
|
eprintln(' $recompile_command')
|
|
|
|
}
|
|
|
|
exit(101)
|
2020-11-29 16:18:49 +01:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
v.pref.ccompiler = pref.default_c_compiler()
|
2020-12-15 09:47:41 +01:00
|
|
|
eprintln('Compilation with tcc failed. Retrying with $v.pref.ccompiler ...')
|
2020-12-01 21:10:02 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if res.exit_code == 127 {
|
|
|
|
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
|
|
|
}
|
2020-11-29 16:18:49 +01:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
if !v.pref.show_c_output {
|
|
|
|
v.post_process_c_compiler_output(res)
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
2020-12-01 21:10:02 +01:00
|
|
|
// Print the C command
|
|
|
|
if v.pref.is_verbose {
|
|
|
|
println('$ccompiler took $diff ms')
|
|
|
|
println('=========\n')
|
|
|
|
}
|
|
|
|
break
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
|
|
|
// 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-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 {
|
2020-07-25 01:37:52 +02: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
|
|
|
}
|
2020-07-28 22:27:38 +02:00
|
|
|
// if v.pref.os == .ios {
|
2020-09-05 01:35:35 +02:00
|
|
|
// ret := os.system('ldid2 -S $v.pref.out_name')
|
|
|
|
// if ret != 0 {
|
|
|
|
// eprintln('failed to run ldid2, try: brew install ldid')
|
|
|
|
// }
|
2020-07-28 22:27:38 +02:00
|
|
|
// }
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
|
|
|
|
2020-06-21 23:09:17 +02:00
|
|
|
fn (mut b Builder) cc_linux_cross() {
|
2020-11-08 07:07:17 +01:00
|
|
|
parent_dir := os.vmodules_dir()
|
2020-07-09 12:26:15 +02:00
|
|
|
if !os.exists(parent_dir) {
|
|
|
|
os.mkdir(parent_dir)
|
|
|
|
}
|
2020-11-08 07:07:17 +01:00
|
|
|
sysroot := os.join_path(os.vmodules_dir(), 'linuxroot')
|
2020-05-31 15:09:07 +02:00
|
|
|
if !os.is_dir(sysroot) {
|
|
|
|
println('Downloading files for Linux cross compilation (~18 MB)...')
|
2020-09-05 01:35:35 +02:00
|
|
|
zip_url := 'https://github.com/vlang/v/releases/download/0.1.27/linuxroot.zip'
|
|
|
|
zip_file := sysroot + '.zip'
|
2020-07-09 12:26:15 +02:00
|
|
|
os.system('curl -L -o $zip_file $zip_url')
|
2020-09-05 01:35:35 +02:00
|
|
|
if !os.exists(zip_file) {
|
|
|
|
verror('Failed to download `$zip_url` as $zip_file')
|
|
|
|
}
|
2020-09-02 10:09:06 +02:00
|
|
|
os.system('tar -C $parent_dir -xf $zip_file')
|
2020-05-31 15:09:07 +02:00
|
|
|
if !os.is_dir(sysroot) {
|
2020-09-05 01:35:35 +02:00
|
|
|
verror('Failed to unzip $zip_file to $parent_dir')
|
2020-05-31 15:09:07 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-05 01:35:35 +02:00
|
|
|
obj_file := b.out_name_c + '.o'
|
|
|
|
mut cc_args := '-fPIC -w -c -target x86_64-linux-gnu -c -o $obj_file $b.out_name_c -I $sysroot/include '
|
2020-06-21 23:09:17 +02:00
|
|
|
cflags := b.get_os_cflags()
|
|
|
|
cc_args += cflags.c_options_without_object_files()
|
2020-09-05 01:35:35 +02:00
|
|
|
cc_cmd := 'cc $cc_args'
|
2020-06-21 23:09:17 +02:00
|
|
|
if b.pref.show_cc {
|
2020-09-05 01:35:35 +02:00
|
|
|
println(cc_cmd)
|
2020-06-13 16:19:17 +02:00
|
|
|
}
|
2020-09-05 01:35:35 +02:00
|
|
|
cc_res := os.exec(cc_cmd) or {
|
|
|
|
println('Cross compilation for Linux failed (first step, cc). Make sure you have clang installed.')
|
|
|
|
verror(err)
|
2020-07-09 12:26:15 +02:00
|
|
|
return
|
|
|
|
}
|
2020-06-21 23:09:17 +02:00
|
|
|
if cc_res.exit_code != 0 {
|
2020-09-05 01:35:35 +02:00
|
|
|
println('Cross compilation for Linux failed (first step, cc). Make sure you have clang installed.')
|
|
|
|
verror(cc_res.output)
|
2020-05-31 15:09:07 +02:00
|
|
|
}
|
2020-07-09 12:26:15 +02:00
|
|
|
linker_args := ['-L $sysroot/usr/lib/x86_64-linux-gnu/', '--sysroot=$sysroot -v -o $b.pref.out_name -m elf_x86_64',
|
2020-09-05 01:35:35 +02:00
|
|
|
'-dynamic-linker /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2', '$sysroot/crt1.o $sysroot/crti.o $obj_file',
|
2020-07-16 19:01:22 +02:00
|
|
|
'-lc', '-lcrypto', '-lssl', '-lpthread', '$sysroot/crtn.o', cflags.c_options_only_object_files()]
|
2020-07-09 12:29:49 +02:00
|
|
|
// -ldl
|
2020-06-13 22:54:20 +02:00
|
|
|
linker_args_str := linker_args.join(' ')
|
2020-09-05 01:35:35 +02:00
|
|
|
linker_cmd := '$sysroot/ld.lld $linker_args_str'
|
2020-07-09 12:26:15 +02:00
|
|
|
// s = s.replace('SYSROOT', sysroot) // TODO $ inter bug
|
|
|
|
// s = s.replace('-o hi', '-o ' + c.pref.out_name)
|
2020-06-21 23:09:17 +02:00
|
|
|
if b.pref.show_cc {
|
2020-09-05 01:35:35 +02:00
|
|
|
println(linker_cmd)
|
2020-06-13 16:19:17 +02:00
|
|
|
}
|
2020-09-05 01:35:35 +02:00
|
|
|
res := os.exec(linker_cmd) or {
|
|
|
|
println('Cross compilation for Linux failed (second step, lld).')
|
|
|
|
verror(err)
|
2020-07-09 12:26:15 +02:00
|
|
|
return
|
|
|
|
}
|
2020-06-13 16:19:17 +02:00
|
|
|
if res.exit_code != 0 {
|
2020-09-05 01:35:35 +02:00
|
|
|
println('Cross compilation for Linux failed (second step, lld).')
|
|
|
|
verror(res.output)
|
2020-05-31 15:09:07 +02:00
|
|
|
}
|
2020-06-21 23:09:17 +02:00
|
|
|
println(b.pref.out_name + ' has been successfully compiled')
|
2020-05-14 04:15:01 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 17:49:16 +02:00
|
|
|
fn (mut c Builder) 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-12-07 22:38:02 +01:00
|
|
|
mut args := ''
|
|
|
|
args += ' $c.pref.cflags '
|
|
|
|
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() }
|
2020-05-18 11:11:26 +02:00
|
|
|
mut optimization_options := ''
|
|
|
|
mut debug_options := ''
|
|
|
|
if c.pref.is_prod {
|
2020-12-07 22:38:02 +01:00
|
|
|
if c.pref.ccompiler != 'msvc' {
|
|
|
|
optimization_options = ' -O3 -fno-strict-aliasing -flto '
|
|
|
|
}
|
2020-05-18 11:11:26 +02:00
|
|
|
}
|
|
|
|
if c.pref.is_debug {
|
2020-12-07 22:38:02 +01:00
|
|
|
if c.pref.ccompiler != 'msvc' {
|
|
|
|
debug_options = ' -O0 -g -gdwarf-2 '
|
|
|
|
}
|
2020-05-18 11:11:26 +02:00
|
|
|
}
|
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 {
|
2020-07-09 12:26:15 +02:00
|
|
|
libs = '"$pref.default_module_path/vlib/builtin.o"'
|
2019-12-04 21:03:12 +01:00
|
|
|
if !os.exists(libs) {
|
2020-09-05 01:35:35 +02:00
|
|
|
verror('`$libs` not found')
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
|
|
|
for imp in c.table.imports {
|
2020-07-09 12:26:15 +02:00
|
|
|
libs += ' "$pref.default_module_path/vlib/${imp}.o"'
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-07 19:35:04 +01:00
|
|
|
// add the thirdparty .o files, produced by all the #flag directives:
|
2020-12-07 18:57:06 +01:00
|
|
|
args += ' ' + cflags.c_options_only_object_files() + ' '
|
2019-08-23 11:05:16 +02:00
|
|
|
args += ' $c.out_name_c '
|
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
|
|
|
/*
|
2020-04-03 17:38:41 +02:00
|
|
|
winroot := '${pref.default_module_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.')
|
2020-04-03 17:38:41 +02:00
|
|
|
println('Download it from $winroot_url and save it in ${pref.default_module_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
|
|
|
*/
|
2020-09-29 21:10:33 +02:00
|
|
|
if os.user_os() !in ['macos', 'linux'] {
|
2020-05-13 23:02:31 +02:00
|
|
|
println(os.user_os())
|
2020-01-03 11:38:26 +01:00
|
|
|
panic('your platform is not supported yet')
|
|
|
|
}
|
2020-06-10 17:15:24 +02:00
|
|
|
mut cmd := '$mingw_cc $optimization_options $debug_options -std=gnu11 $args -municode'
|
2020-07-09 12:26:15 +02:00
|
|
|
// cmd := 'clang -o $obj_name -w $include -m32 -c -target x86_64-win32 ${pref.default_module_path}/$c.out_name_c'
|
2020-05-18 11:11:26 +02:00
|
|
|
if c.pref.is_verbose || c.pref.show_cc {
|
2019-12-19 21:36:18 +01:00
|
|
|
println(cmd)
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
|
|
|
if os.system(cmd) != 0 {
|
2020-05-13 23:02:31 +02:00
|
|
|
println('Cross compilation for Windows failed. Make sure you have mingw-w64 installed.')
|
|
|
|
$if macos {
|
|
|
|
println('brew install mingw-w64')
|
|
|
|
}
|
|
|
|
$if linux {
|
2020-05-30 12:49:03 +02:00
|
|
|
println('Try `sudo apt install -y mingw-w64` on Debian based distros, or `sudo pacman -S mingw-w64-gcc` on Arch, etc...')
|
2020-05-13 23:02:31 +02:00
|
|
|
}
|
2019-08-23 11:05:16 +02:00
|
|
|
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'
|
2020-05-02 18:01:43 +02:00
|
|
|
if c.pref.show_cc {
|
2019-08-23 11:05:16 +02:00
|
|
|
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
|
|
|
*/
|
2020-05-13 23:02:31 +02:00
|
|
|
println(c.pref.out_name + ' has been successfully compiled')
|
2019-08-23 11:05:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-28 08:38:52 +02:00
|
|
|
fn (mut v Builder) build_thirdparty_obj_files() {
|
|
|
|
v.log('build_thirdparty_obj_files: v.table.cflags: $v.table.cflags')
|
|
|
|
for flag in v.get_os_cflags() {
|
2019-11-28 14:49:35 +01:00
|
|
|
if flag.value.ends_with('.o') {
|
2020-08-28 08:38:52 +02:00
|
|
|
rest_of_module_flags := v.get_rest_of_module_cflags(flag)
|
|
|
|
if v.pref.ccompiler == 'msvc' {
|
|
|
|
v.build_thirdparty_obj_file_with_msvc(flag.value, rest_of_module_flags)
|
2020-04-25 17:49:16 +02:00
|
|
|
} else {
|
2020-08-28 08:38:52 +02:00
|
|
|
v.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
|
|
|
|
2020-06-10 17:15:24 +02:00
|
|
|
fn (mut v Builder) build_thirdparty_obj_file(path string, moduleflags []cflag.CFlag) {
|
2020-04-01 21:24:58 +02:00
|
|
|
obj_path := os.real_path(path)
|
2020-06-10 17:15:24 +02:00
|
|
|
if v.pref.os == .windows {
|
|
|
|
// Cross compiling for Windows
|
|
|
|
$if !windows {
|
|
|
|
v.pref.ccompiler = mingw_cc
|
|
|
|
}
|
|
|
|
}
|
2020-10-25 02:09:07 +02:00
|
|
|
opath := v.pref.cache_manager.postfix_with_key2cpath('.o', obj_path)
|
|
|
|
if os.exists(opath) {
|
2020-04-01 21:24:58 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-25 02:35:28 +02:00
|
|
|
if os.exists(obj_path) {
|
|
|
|
// Some .o files are distributed with no source
|
|
|
|
// for example thirdparty\tcc\lib\openlibm.o
|
|
|
|
// the best we can do for them is just copy them,
|
|
|
|
// and hope that they work with any compiler...
|
|
|
|
os.cp(obj_path, opath)
|
|
|
|
return
|
|
|
|
}
|
2020-10-25 02:09:07 +02:00
|
|
|
println('$obj_path not found, building it in $opath ...')
|
2020-11-13 19:35:15 +01:00
|
|
|
cfile := '${obj_path[..obj_path.len - 2]}.c'
|
2020-04-01 21:24:58 +02:00
|
|
|
btarget := moduleflags.c_options_before_target()
|
|
|
|
atarget := moduleflags.c_options_after_target()
|
2020-05-30 10:06:16 +02:00
|
|
|
cppoptions := if v.pref.ccompiler.contains('++') { ' -fpermissive -w ' } else { '' }
|
2020-11-30 20:48:16 +01:00
|
|
|
//
|
|
|
|
// prepare for tcc, it needs relative paths to thirdparty/tcc to work:
|
|
|
|
current_folder := os.getwd()
|
|
|
|
os.chdir(os.dir(pref.vexe_path()))
|
|
|
|
//
|
2020-11-13 19:42:26 +01:00
|
|
|
cmd := '$v.pref.ccompiler $cppoptions $v.pref.third_party_option $btarget -o "$opath" -c "$cfile" $atarget'
|
2020-11-30 20:48:16 +01:00
|
|
|
// eprintln('>>> cmd: $cmd')
|
2020-04-25 17:49:16 +02:00
|
|
|
res := os.exec(cmd) or {
|
2020-08-16 09:18:14 +02:00
|
|
|
eprintln('exec failed for thirdparty object build cmd:\n$cmd')
|
2020-04-01 21:24:58 +02:00
|
|
|
verror(err)
|
|
|
|
return
|
|
|
|
}
|
2020-11-30 20:48:16 +01:00
|
|
|
os.chdir(current_folder)
|
2020-04-01 21:24:58 +02:00
|
|
|
if res.exit_code != 0 {
|
2020-08-16 09:18:14 +02:00
|
|
|
eprintln('failed thirdparty object build cmd:\n$cmd')
|
2020-04-01 21:24:58 +02:00
|
|
|
verror(res.output)
|
|
|
|
return
|
|
|
|
}
|
2020-10-25 02:09:07 +02:00
|
|
|
v.pref.cache_manager.save('.description.txt', obj_path, '${obj_path:-30} @ $cmd\n')
|
2020-04-01 21:24:58 +02:00
|
|
|
println(res.output)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-10-15 12:32:28 +02:00
|
|
|
fn error_context_lines(text string, keyword string, before int, after int) []string {
|
2020-03-03 12:01:23 +01:00
|
|
|
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]
|
|
|
|
}
|