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 import term
const ( const (
c_error_info = ' c_error_info = '
================== ==================
C error. This should never happen. 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 You can also use #help on Discord: https://discord.gg/vlang
' '
no_compiler_error = ' no_compiler_error = '
================== ==================
Error: no C compiler detected. Error: no C compiler detected.
@ -39,13 +39,13 @@ const (
mingw_cc = 'x86_64-w64-mingw32-gcc' mingw_cc = 'x86_64-w64-mingw32-gcc'
) )
fn todo() { fn todo() {
} }
fn (v &Builder) find_win_cc() ?string { fn (v &Builder) find_win_cc() ?string {
$if !windows { return none } $if !windows {
return none
}
os.exec('$v.pref.ccompiler -v') or { os.exec('$v.pref.ccompiler -v') or {
if v.pref.is_verbose { if v.pref.is_verbose {
println('$v.pref.ccompiler not found, looking for msvc...') println('$v.pref.ccompiler not found, looking for msvc...')
@ -131,13 +131,11 @@ fn (mut v Builder) cc() {
} }
} }
// arguments for the C compiler // arguments for the C compiler
mut a := [ v.pref.cflags, '-std=gnu11', '-Wall', '-Wextra',
// TODO : activate -Werror once no warnings remain // TODO : activate -Werror once no warnings remain
// '-Werror', // '-Werror',
// TODO : try and remove the below workaround options when the corresponding // TODO : try and remove the below workaround options when the corresponding
// warnings are totally fixed/removed // warnings are totally fixed/removed
'-Wno-unused-variable', mut a := [v.pref.cflags, '-std=gnu11', '-Wall', '-Wextra', '-Wno-unused-variable',
// '-Wno-unused-but-set-variable',
'-Wno-unused-parameter', '-Wno-unused-result', '-Wno-unused-function', '-Wno-missing-braces', '-Wno-unused-parameter', '-Wno-unused-result', '-Wno-unused-function', '-Wno-missing-braces',
'-Wno-unused-label' '-Wno-unused-label'
] ]
@ -171,8 +169,8 @@ fn (mut v Builder) cc() {
verror('-fast is only supported on Linux right now') verror('-fast is only supported on Linux right now')
} }
} }
if !v.pref.is_shared && v.pref.build_mode != .build_module && os.user_os() == 'windows' && if !v.pref.is_shared && v.pref.build_mode != .build_module &&
!v.pref.out_name.ends_with('.exe') { os.user_os() == 'windows' && !v.pref.out_name.ends_with('.exe') {
v.pref.out_name += '.exe' v.pref.out_name += '.exe'
} }
// linux_host := os.user_os() == 'linux' // linux_host := os.user_os() == 'linux'
@ -194,7 +192,7 @@ fn (mut v Builder) cc() {
} }
if v.pref.build_mode == .build_module { if v.pref.build_mode == .build_module {
// Create the modules & out directory if it's not there. // 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) pdir := out_dir.all_before_last(os.path_separator)
if !os.is_dir(pdir) { if !os.is_dir(pdir) {
os.mkdir_all(pdir) os.mkdir_all(pdir)
@ -210,7 +208,8 @@ fn (mut v Builder) cc() {
// deliberately guessing only for -prod builds for performance reasons // deliberately guessing only for -prod builds for performance reasons
if ccversion := os.exec('cc --version') { if ccversion := os.exec('cc --version') {
if ccversion.exit_code == 0 { 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' guessed_compiler = 'gcc'
} }
if ccversion.output.contains('clang version ') { 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_tcc := ccompiler.contains('tcc') || guessed_compiler == 'tcc'
is_cc_clang := !is_cc_tcc && (ccompiler.contains('clang') || guessed_compiler == 'clang') 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' // is_cc_msvc := v.pref.ccompiler.contains('msvc') || guessed_compiler == 'msvc'
// //
if is_cc_clang { if is_cc_clang {
@ -350,10 +350,9 @@ fn (mut v Builder) cc() {
a << '-w' a << '-w'
} }
if v.pref.use_cache { if v.pref.use_cache {
//vexe := pref.vexe_path() // vexe := pref.vexe_path()
cached_modules := ['builtin', 'os', 'math', 'strconv', 'strings']
cached_modules:= [ 'builtin', 'os', 'math', 'strconv', 'strings'] for cfile in cached_modules {
for cfile in cached_modules{
ofile := os.join_path(pref.default_module_path, 'cache', 'vlib', cfile + '.o') ofile := os.join_path(pref.default_module_path, 'cache', 'vlib', cfile + '.o')
if !os.exists(ofile) { if !os.exists(ofile) {
println('${cfile}.o is missing. Building...') println('${cfile}.o is missing. Building...')
@ -374,8 +373,8 @@ fn (mut v Builder) cc() {
} }
// Without these libs compilation will fail on Linux // Without these libs compilation will fail on Linux
// || os.user_os() == 'linux' // || os.user_os() == 'linux'
if !v.pref.is_bare && v.pref.build_mode != .build_module && v.pref.os in [ .linux, .freebsd, if !v.pref.is_bare && v.pref.build_mode != .build_module &&
.openbsd, .netbsd, .dragonfly, .solaris, .haiku] { v.pref.os in [.linux, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .haiku] {
linker_flags << '-lm' linker_flags << '-lm'
linker_flags << '-lpthread' linker_flags << '-lpthread'
// -ldl is a Linux only thing. BSDs have it in libc. // -ldl is a Linux only thing. BSDs have it in libc.
@ -403,7 +402,7 @@ fn (mut v Builder) cc() {
start: start:
todo() todo()
// TODO remove // TODO remove
cmd := '${ccompiler} @$response_file' cmd := '$ccompiler @$response_file'
// Run // Run
if v.pref.is_verbose || v.pref.show_cc { if v.pref.is_verbose || v.pref.show_cc {
println('\n==========') println('\n==========')
@ -434,8 +433,9 @@ fn (mut v Builder) cc() {
goto start goto start
} }
} }
verror('C compiler error, while attempting to run: \n' + '-----------------------------------------------------------\n' + verror('C compiler error, while attempting to run: \n' +
'$cmd\n' + '-----------------------------------------------------------\n' + 'Probably your C compiler is missing. \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()) 'Please reinstall it, or make it available in your PATH.\n\n' + missing_compiler_info())
} }
if v.pref.is_debug { if v.pref.is_debug {
@ -462,7 +462,7 @@ fn (mut v Builder) cc() {
diff := time.ticks() - ticks diff := time.ticks() - ticks
// Print the C command // Print the C command
if v.pref.is_verbose { if v.pref.is_verbose {
println('${ccompiler} took $diff ms') println('$ccompiler took $diff ms')
println('=========\n') println('=========\n')
} }
// Link it if we are cross compiling and need an executable // 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() { fn (mut b Builder) cc_linux_cross() {
parent_dir := os.home_dir() + '.vmodules' parent_dir := os.home_dir() + '.vmodules'
if !os.exists(parent_dir) {
os.mkdir(parent_dir)
}
sysroot := os.home_dir() + '.vmodules/linuxroot/' 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) { if !os.is_dir(sysroot) {
println('Downloading files for Linux cross compilation (~18 MB)...') println('Downloading files for Linux cross compilation (~18 MB)...')
zip_file := sysroot[..sysroot.len-1] + '.zip' 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 ') os.system('curl -L -o $zip_file $zip_url')
os.system('unzip -q $zip_file -d $parent_dir') os.system('unzip -q $zip_file -d $parent_dir')
if !os.is_dir(sysroot) { if !os.is_dir(sysroot) {
println('Failed to download.') println('Failed to download.')
exit(1) exit(1)
} }
} }
mut cc_args := '-fPIC -w -c -target x86_64-linux-gnu -c -o x.o $b.out_name_c -I $sysroot/include ' 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() cflags := b.get_os_cflags()
cc_args += cflags.c_options_without_object_files() cc_args += cflags.c_options_without_object_files()
if b.pref.show_cc { if b.pref.show_cc {
println('cc $cc_args') 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 { if cc_res.exit_code != 0 {
println('Cross compilation for Linux failed (first step, clang). Make sure you have clang installed.') println('Cross compilation for Linux failed (first step, clang). Make sure you have clang installed.')
println(cc_res.output) println(cc_res.output)
exit(1) exit(1)
} }
linker_args := ['-L $sysroot/usr/lib/x86_64-linux-gnu/', '--sysroot=$sysroot -v -o $b.pref.out_name -m elf_x86_64',
linker_args := [ '-dynamic-linker /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2', '$sysroot/crt1.o $sysroot/crti.o x.o',
'-L $sysroot/usr/lib/x86_64-linux-gnu/' '-lc', '-lcrypto', '-lssl', '-lpthread', '-ldl', '$sysroot/crtn.o',
'--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'
cflags.c_options_only_object_files() cflags.c_options_only_object_files()
] ]
linker_args_str := linker_args.join(' ') linker_args_str := linker_args.join(' ')
cmd := '$sysroot/ld.lld ' + linker_args_str cmd := '$sysroot/ld.lld ' + linker_args_str
//s = s.replace('SYSROOT', sysroot) // TODO $ inter bug // s = s.replace('SYSROOT', sysroot) // TODO $ inter bug
//s = s.replace('-o hi', '-o ' + c.pref.out_name) // s = s.replace('-o hi', '-o ' + c.pref.out_name)
if b.pref.show_cc { if b.pref.show_cc {
println(cmd) println(cmd)
} }
res := os.exec(cmd) or { return } res := os.exec(cmd) or {
return
}
if res.exit_code != 0 { if res.exit_code != 0 {
println('Cross compilation for Linux failed (second step, lld):') println('Cross compilation for Linux failed (second step, lld):')
println(res.output) println(res.output)
@ -598,19 +591,17 @@ fn (mut c Builder) cc_windows_cross() {
} }
mut libs := '' mut libs := ''
if false && c.pref.build_mode == .default_mode { 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) { if !os.exists(libs) {
println('`$libs` not found') println('`$libs` not found')
exit(1) exit(1)
} }
for imp in c.table.imports { 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 += ' $c.out_name_c '
args += if c.pref.ccompiler == 'msvc' { cflags.c_options_after_target_msvc() } else { cflags.c_options_after_target() } args += if c.pref.ccompiler == 'msvc' { cflags.c_options_after_target_msvc() } else { cflags.c_options_after_target() }
/* /*
winroot := '${pref.default_module_path}/winroot' winroot := '${pref.default_module_path}/winroot'
if !os.is_dir(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') obj_name = obj_name.replace('.o.o', '.o')
include := '-I $winroot/include ' include := '-I $winroot/include '
*/ */
if os.user_os() !in ['mac', 'darwin','linux'] { if os.user_os() !in ['mac', 'darwin', 'linux'] {
println(os.user_os()) println(os.user_os())
panic('your platform is not supported yet') panic('your platform is not supported yet')
} }
mut cmd := '$mingw_cc $optimization_options $debug_options -std=gnu11 $args -municode' 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 { if c.pref.is_verbose || c.pref.show_cc {
println(cmd) println(cmd)
} }