cc: format and fix linuxroot download

pull/5770/head
Alexander Medvednikov 2020-07-09 12:26:15 +02:00
parent dfa29b6e4d
commit 8c338a7c3b
1 changed files with 50 additions and 59 deletions

View File

@ -10,7 +10,7 @@ import v.pref
import term
const (
c_error_info = '
c_error_info = '
==================
C error. This should never happen.
@ -20,7 +20,7 @@ https://github.com/vlang/v/issues/new/choose
You can also use #help on Discord: https://discord.gg/vlang
'
no_compiler_error = '
no_compiler_error = '
==================
Error: no C compiler detected.
@ -39,13 +39,13 @@ const (
mingw_cc = 'x86_64-w64-mingw32-gcc'
)
fn todo() {
}
fn (v &Builder) find_win_cc() ?string {
$if !windows { return none }
$if !windows {
return none
}
os.exec('$v.pref.ccompiler -v') or {
if v.pref.is_verbose {
println('$v.pref.ccompiler not found, looking for msvc...')
@ -131,15 +131,13 @@ fn (mut v Builder) cc() {
}
}
// 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',
// '-Wno-unused-but-set-variable',
// 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 a := [v.pref.cflags, '-std=gnu11', '-Wall', '-Wextra', '-Wno-unused-variable',
'-Wno-unused-parameter', '-Wno-unused-result', '-Wno-unused-function', '-Wno-missing-braces',
'-Wno-unused-label'
'-Wno-unused-label'
]
mut linker_flags := []string{}
// TCC on Linux by default, unless -cc was provided
@ -171,8 +169,8 @@ fn (mut v Builder) cc() {
verror('-fast is only supported on Linux right now')
}
}
if !v.pref.is_shared && v.pref.build_mode != .build_module && os.user_os() == 'windows' &&
!v.pref.out_name.ends_with('.exe') {
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'
@ -194,7 +192,7 @@ fn (mut v Builder) cc() {
}
if v.pref.build_mode == .build_module {
// Create the modules & out directory if it's not there.
mut out_dir := if v.pref.path.starts_with('vlib') { '${pref.default_module_path}${os.path_separator}cache${os.path_separator}$v.pref.path' } else { '${pref.default_module_path}${os.path_separator}$v.pref.path' }
mut out_dir := if v.pref.path.starts_with('vlib') { '$pref.default_module_path${os.path_separator}cache$os.path_separator$v.pref.path' } else { '$pref.default_module_path$os.path_separator$v.pref.path' }
pdir := out_dir.all_before_last(os.path_separator)
if !os.is_dir(pdir) {
os.mkdir_all(pdir)
@ -210,7 +208,8 @@ fn (mut v Builder) cc() {
// 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.') {
if ccversion.output.contains('This is free software;') &&
ccversion.output.contains('Free Software Foundation, Inc.') {
guessed_compiler = 'gcc'
}
if ccversion.output.contains('clang version ') {
@ -222,7 +221,8 @@ fn (mut v Builder) cc() {
//
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_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 {
@ -350,10 +350,9 @@ fn (mut v Builder) cc() {
a << '-w'
}
if v.pref.use_cache {
//vexe := pref.vexe_path()
cached_modules:= [ 'builtin', 'os', 'math', 'strconv', 'strings']
for cfile in cached_modules{
// vexe := pref.vexe_path()
cached_modules := ['builtin', 'os', 'math', 'strconv', 'strings']
for cfile in cached_modules {
ofile := os.join_path(pref.default_module_path, 'cache', 'vlib', cfile + '.o')
if !os.exists(ofile) {
println('${cfile}.o is missing. Building...')
@ -374,8 +373,8 @@ fn (mut v Builder) cc() {
}
// 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] {
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.
@ -403,7 +402,7 @@ fn (mut v Builder) cc() {
start:
todo()
// TODO remove
cmd := '${ccompiler} @$response_file'
cmd := '$ccompiler @$response_file'
// Run
if v.pref.is_verbose || v.pref.show_cc {
println('\n==========')
@ -434,8 +433,9 @@ fn (mut v Builder) cc() {
goto start
}
}
verror('C compiler error, while attempting to run: \n' + '-----------------------------------------------------------\n' +
'$cmd\n' + '-----------------------------------------------------------\n' + 'Probably your C compiler is missing. \n' +
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())
}
if v.pref.is_debug {
@ -462,7 +462,7 @@ fn (mut v Builder) cc() {
diff := time.ticks() - ticks
// Print the C command
if v.pref.is_verbose {
println('${ccompiler} took $diff ms')
println('$ccompiler took $diff ms')
println('=========\n')
}
// Link it if we are cross compiling and need an executable
@ -520,57 +520,50 @@ fn (mut v Builder) cc() {
fn (mut b Builder) cc_linux_cross() {
parent_dir := os.home_dir() + '.vmodules'
if !os.exists(parent_dir) {
os.mkdir(parent_dir)
}
sysroot := os.home_dir() + '.vmodules/linuxroot/'
zip_url := 'https://github.com/vlang/v/releases/download/0.1.27/linuxroot.zip'
if !os.is_dir(sysroot) {
println('Downloading files for Linux cross compilation (~18 MB)...')
zip_file := sysroot[..sysroot.len-1] + '.zip'
os.system('curl -L -o $zip_file https://github.com/vlang/v/releases/download/0.1.27/linuxroot.zip ')
zip_file := sysroot[..sysroot.len - 1] + '.zip'
os.system('curl -L -o $zip_file $zip_url')
os.system('unzip -q $zip_file -d $parent_dir')
if !os.is_dir(sysroot) {
println('Failed to download.')
exit(1)
}
}
mut cc_args := '-fPIC -w -c -target x86_64-linux-gnu -c -o x.o $b.out_name_c -I $sysroot/include '
cflags := b.get_os_cflags()
cc_args += cflags.c_options_without_object_files()
if b.pref.show_cc {
println('cc $cc_args')
}
cc_res := os.exec('cc $cc_args') or { return }
cc_res := os.exec('cc $cc_args') or {
return
}
if cc_res.exit_code != 0 {
println('Cross compilation for Linux failed (first step, clang). Make sure you have clang installed.')
println(cc_res.output)
exit(1)
}
linker_args := [
'-L $sysroot/usr/lib/x86_64-linux-gnu/'
'--sysroot=$sysroot -v -o $b.pref.out_name -m elf_x86_64'
'-dynamic-linker /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2'
'$sysroot/crt1.o $sysroot/crti.o x.o'
//'SYSROOT/lib/x86_64-linux-gnu/libc.so.6'
'-lc'
//'-ldl'
//'SYSROOT/lib/x86_64-linux-gnu/libcrypto.so'
'-lcrypto'
'-lssl'
'-lpthread'
'-ldl'
//'-dynamic-linker /usr/lib/x86_64-linux-gnu/libcrypto.so'
//'SYSROOT/lib/x86_64-linux-gnu/libssl.a'
'$sysroot/crtn.o'
linker_args := ['-L $sysroot/usr/lib/x86_64-linux-gnu/', '--sysroot=$sysroot -v -o $b.pref.out_name -m elf_x86_64',
'-dynamic-linker /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2', '$sysroot/crt1.o $sysroot/crti.o x.o',
'-lc', '-lcrypto', '-lssl', '-lpthread', '-ldl', '$sysroot/crtn.o',
cflags.c_options_only_object_files()
]
linker_args_str := linker_args.join(' ')
cmd := '$sysroot/ld.lld ' + linker_args_str
//s = s.replace('SYSROOT', sysroot) // TODO $ inter bug
//s = s.replace('-o hi', '-o ' + c.pref.out_name)
// s = s.replace('SYSROOT', sysroot) // TODO $ inter bug
// s = s.replace('-o hi', '-o ' + c.pref.out_name)
if b.pref.show_cc {
println(cmd)
}
res := os.exec(cmd) or { return }
res := os.exec(cmd) or {
return
}
if res.exit_code != 0 {
println('Cross compilation for Linux failed (second step, lld):')
println(res.output)
@ -594,23 +587,21 @@ fn (mut c Builder) cc_windows_cross() {
optimization_options = if c.pref.ccompiler == 'msvc' { '' } else { ' -O3 -fno-strict-aliasing -flto ' }
}
if c.pref.is_debug {
debug_options = if c.pref.ccompiler == 'msvc' { '' } else { ' -g3 -no-pie ' }
debug_options = if c.pref.ccompiler == 'msvc' { '' } else { ' -g3 -no-pie ' }
}
mut libs := ''
if false && c.pref.build_mode == .default_mode {
libs = '"${pref.default_module_path}/vlib/builtin.o"'
libs = '"$pref.default_module_path/vlib/builtin.o"'
if !os.exists(libs) {
println('`$libs` not found')
exit(1)
}
for imp in c.table.imports {
libs += ' "${pref.default_module_path}/vlib/${imp}.o"'
libs += ' "$pref.default_module_path/vlib/${imp}.o"'
}
}
args += ' $c.out_name_c '
args += if c.pref.ccompiler == 'msvc' { cflags.c_options_after_target_msvc() } else { cflags.c_options_after_target() }
/*
winroot := '${pref.default_module_path}/winroot'
if !os.is_dir(winroot) {
@ -626,12 +617,12 @@ fn (mut c Builder) cc_windows_cross() {
obj_name = obj_name.replace('.o.o', '.o')
include := '-I $winroot/include '
*/
if os.user_os() !in ['mac', 'darwin','linux'] {
if os.user_os() !in ['mac', 'darwin', 'linux'] {
println(os.user_os())
panic('your platform is not supported yet')
}
mut cmd := '$mingw_cc $optimization_options $debug_options -std=gnu11 $args -municode'
//cmd := 'clang -o $obj_name -w $include -m32 -c -target x86_64-win32 ${pref.default_module_path}/$c.out_name_c'
// cmd := 'clang -o $obj_name -w $include -m32 -c -target x86_64-win32 ${pref.default_module_path}/$c.out_name_c'
if c.pref.is_verbose || c.pref.show_cc {
println(cmd)
}