make "build module" generate an object file

pull/1873/head
Alexander Medvednikov 2019-09-03 12:10:51 +03:00
parent 7cf058feac
commit c12d4d1bd2
2 changed files with 47 additions and 21 deletions

View File

@ -29,7 +29,7 @@ fn (v mut V) cc() {
} }
} }
linux_host := os.user_os() == 'linux' //linux_host := os.user_os() == 'linux'
v.log('cc() isprod=$v.pref.is_prod outname=$v.out_name') v.log('cc() isprod=$v.pref.is_prod outname=$v.out_name')
mut a := [v.pref.cflags, '-std=gnu11', '-w'] // arguments for the C compiler mut a := [v.pref.cflags, '-std=gnu11', '-w'] // arguments for the C compiler
@ -41,12 +41,15 @@ fn (v mut V) cc() {
uniqueflags << f uniqueflags << f
} }
flags := uniqueflags.join(' ') flags := uniqueflags.join(' ')
// Set out name
//mut shared := ''
if v.pref.is_so { if v.pref.is_so {
a << '-shared -fPIC '// -Wl,-z,defs' a << '-shared -fPIC '// -Wl,-z,defs'
v.out_name = v.out_name + '.so' v.out_name = v.out_name + '.so'
} }
if v.pref.build_mode == .build {
v.out_name = ModPath + v.dir + '.o' //v.out_name
println('Building ${v.out_name}...')
}
if v.pref.is_prod { if v.pref.is_prod {
a << '-O2' a << '-O2'
} }
@ -74,9 +77,9 @@ fn (v mut V) cc() {
// //
} }
else if v.pref.build_mode == .default_mode { else if v.pref.build_mode == .default_mode {
libs = '"$ModPath/vlib/builtin.o"' libs = '$ModPath/vlib/builtin.o'
if !os.file_exists(libs) { if !os.file_exists(libs) {
println('`builtin.o` not found') println('object file `$libs` not found')
exit(1) exit(1)
} }
for imp in v.table.imports { for imp in v.table.imports {
@ -99,7 +102,8 @@ mut args := ''
if v.pref.sanitize { if v.pref.sanitize {
a << '-fsanitize=leak' a << '-fsanitize=leak'
} }
// Cross compiling linux // Cross compiling linux TODO
/*
sysroot := '/Users/alex/tmp/lld/linuxroot/' sysroot := '/Users/alex/tmp/lld/linuxroot/'
if v.os == .linux && !linux_host { if v.os == .linux && !linux_host {
// Build file.o // Build file.o
@ -109,10 +113,10 @@ mut args := ''
v.out_name = v.out_name + '.o' v.out_name = v.out_name + '.o'
} }
} }
*/
// Cross compiling windows // Cross compiling windows
// sysroot := '/Users/alex/tmp/lld/linuxroot/' //
// Output executable name // Output executable name
// else {
a << '-o $v.out_name' a << '-o $v.out_name'
if os.dir_exists(v.out_name) { if os.dir_exists(v.out_name) {
cerror('\'$v.out_name\' is a directory') cerror('\'$v.out_name\' is a directory')
@ -187,6 +191,7 @@ mut args := ''
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
/*
if v.os == .linux && !linux_host && v.pref.build_mode != .build { if v.os == .linux && !linux_host && v.pref.build_mode != .build {
v.out_name = v.out_name.replace('.o', '') v.out_name = v.out_name.replace('.o', '')
obj_file := v.out_name + '.o' obj_file := v.out_name + '.o'
@ -206,6 +211,7 @@ mut args := ''
println(ress.output) println(ress.output)
println('linux cross compilation done. resulting binary: "$v.out_name"') println('linux cross compilation done. resulting binary: "$v.out_name"')
} }
*/
if !v.pref.is_debug && v.out_name_c != 'v.c' && v.out_name_c != 'v_macos.c' { if !v.pref.is_debug && v.out_name_c != 'v.c' && v.out_name_c != 'v_macos.c' {
os.rm(v.out_name_c) os.rm(v.out_name_c)
} }
@ -228,7 +234,7 @@ fn (c mut V) cc_windows_cross() {
if c.pref.build_mode == .default_mode { if c.pref.build_mode == .default_mode {
libs = '"$ModPath/vlib/builtin.o"' libs = '"$ModPath/vlib/builtin.o"'
if !os.file_exists(libs) { if !os.file_exists(libs) {
println('`builtin.o` not found') println('`$libs` not found')
exit(1) exit(1)
} }
for imp in c.table.imports { for imp in c.table.imports {

View File

@ -237,6 +237,10 @@ fn (v mut V) compile() {
for i, file in v.files { for i, file in v.files {
// v.parsers << v.new_parser(file) // v.parsers << v.new_parser(file)
} }
if v.pref.is_verbose {
println('all .v files before:')
println(v.files)
}
v.add_v_files_to_compile() v.add_v_files_to_compile()
if v.pref.is_verbose { if v.pref.is_verbose {
println('all .v files:') println('all .v files:')
@ -512,7 +516,7 @@ fn (v mut V) add_v_files_to_compile() {
dir = dir.all_before('/') dir = dir.all_before('/')
} }
else { else {
// Add files from the dir user is compiling (only .v files) // Add .v files from the directory being compied
files := v.v_files_from_dir(dir) files := v.v_files_from_dir(dir)
for file in files { for file in files {
user_files << file user_files << file
@ -602,17 +606,30 @@ fn (v mut V) add_v_files_to_compile() {
*/ */
vfiles := v.v_files_from_dir(mod_path) vfiles := v.v_files_from_dir(mod_path)
for file in vfiles { for file in vfiles {
if !file in v.files { if !(file in v.files) {
v.files << file v.files << file
} }
} }
} }
// add remaining files (not modules) // Add remaining user files
for fit in v.table.file_imports { mut j := 0
//println('fit $fit.file_path') mut len := -1
if !fit.file_path in v.files { for i, fit in v.table.file_imports {
v.files << fit.file_path // Don't add a duplicate; builtin files are always there
if fit.file_path in v.files || fit.module_name == 'builtin' {
continue
} }
if len == -1 {
len = i
}
j++
// TODO remove this once imports work with .build
if v.pref.build_mode == .build && j >= len / 2{
break
}
//println(fit)
//println('fit $fit.file_path')
v.files << fit.file_path
} }
} }
@ -672,9 +689,11 @@ fn new_v(args[]string) &V {
if joined_args.contains('build module ') { if joined_args.contains('build module ') {
build_mode = .build build_mode = .build
// v -lib ~/v/os => os.o // v -lib ~/v/os => os.o
mod = os.dir(dir) //mod = os.dir(dir)
mod = mod.all_after('/') if dir.contains('/') {
println('Building module "${mod}" dir="$dir"...') mod = dir.all_after('/')
}
println('Building module "${mod}" (dir="$dir")...')
//out_name = '$TmpPath/vlib/${base}.o' //out_name = '$TmpPath/vlib/${base}.o'
out_name = mod + '.o' out_name = mod + '.o'
// Cross compiling? Use separate dirs for each os // Cross compiling? Use separate dirs for each os
@ -764,10 +783,11 @@ fn new_v(args[]string) &V {
println('Go to https://vlang.io to install V.') println('Go to https://vlang.io to install V.')
exit(1) exit(1)
} }
//println('out_name:$out_name')
mut out_name_c := os.realpath( out_name ) + '.tmp.c' mut out_name_c := os.realpath( out_name ) + '.tmp.c'
mut files := []string mut files := []string
// Add builtin files // Add builtin files
if !out_name.contains('builtin.o') { //if !out_name.contains('builtin.o') {
for builtin in builtins { for builtin in builtins {
mut f := '$vroot/vlib/builtin/$builtin' mut f := '$vroot/vlib/builtin/$builtin'
// In default mode we use precompiled vlib.o, point to .vh files with signatures // In default mode we use precompiled vlib.o, point to .vh files with signatures
@ -776,7 +796,7 @@ fn new_v(args[]string) &V {
} }
files << f files << f
} }
} //}
mut cflags := '' mut cflags := ''
for ci, cv in args { for ci, cv in args {