builder: linux cross compilation

pull/5145/head
Alexander Medvednikov 2020-05-31 15:09:07 +02:00
parent f1f6fb1a9f
commit a5ddb613fb
1 changed files with 32 additions and 9 deletions

View File

@ -275,10 +275,11 @@ fn (mut v Builder) cc() {
if v.pref.sanitize { if v.pref.sanitize {
a << '-fsanitize=leak' a << '-fsanitize=leak'
} }
// Cross compiling linux // Cross compiling for linux
if v.pref.os == .linux { if v.pref.os == .linux {
$if !linux { $if !linux {
v.cc_linux_cross() v.cc_linux_cross()
return
} }
} }
// Cross compiling windows // Cross compiling windows
@ -476,15 +477,37 @@ fn (mut v Builder) cc() {
} }
fn (mut c Builder) cc_linux_cross() { fn (mut c Builder) cc_linux_cross() {
/* parent_dir := os.home_dir() + '.vmodules'
sysroot := '/tmp/lld/linuxroot/' sysroot := os.home_dir() + '.vmodules/linuxroot/'
// Build file.o if !os.is_dir(sysroot) {
a << '-c --sysroot=$sysroot -target x86_64-linux-gnu' println('Downloading files for Linux cross compilation (~18 MB)...')
// Right now `out_name` can be `file`, not `file.o` zip_file := sysroot[..sysroot.len-1] + '.zip'
if !v.out_name.ends_with('.o') { os.system('curl -L -o $zip_file https://github.com/vlang/v/releases/download/0.1.27/linuxroot.zip ')
v.out_name = v.out_name + '.o' 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 $c.out_name_c -I $sysroot/include'
if os.system('cc $cc_args') != 0 {
println('Cross compilation for Linux failed. Make sure you have clang installed.')
}
mut args := [
'-L SYSROOT/usr/lib/x86_64-linux-gnu/'
'--sysroot=SYSROOT -v -o hi -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'
'SYSROOT/crtn.o'
]
mut s := args.join(' ')
s = s.replace('SYSROOT', sysroot)
if os.system('$sysroot/ld.lld ' + s) != 0 {
println('Cross compilation for Linux failed. Make sure you have clang installed.')
}
println(c.pref.out_name + ' has been successfully compiled')
} }
fn (mut c Builder) cc_windows_cross() { fn (mut c Builder) cc_windows_cross() {