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

View File

@ -237,6 +237,10 @@ fn (v mut V) compile() {
for i, file in v.files {
// 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()
if v.pref.is_verbose {
println('all .v files:')
@ -512,7 +516,7 @@ fn (v mut V) add_v_files_to_compile() {
dir = dir.all_before('/')
}
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)
for file in files {
user_files << file
@ -602,17 +606,30 @@ fn (v mut V) add_v_files_to_compile() {
*/
vfiles := v.v_files_from_dir(mod_path)
for file in vfiles {
if !file in v.files {
if !(file in v.files) {
v.files << file
}
}
}
// add remaining files (not modules)
for fit in v.table.file_imports {
//println('fit $fit.file_path')
if !fit.file_path in v.files {
v.files << fit.file_path
// Add remaining user files
mut j := 0
mut len := -1
for i, fit in v.table.file_imports {
// 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 ') {
build_mode = .build
// v -lib ~/v/os => os.o
mod = os.dir(dir)
mod = mod.all_after('/')
println('Building module "${mod}" dir="$dir"...')
//mod = os.dir(dir)
if dir.contains('/') {
mod = dir.all_after('/')
}
println('Building module "${mod}" (dir="$dir")...')
//out_name = '$TmpPath/vlib/${base}.o'
out_name = mod + '.o'
// 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.')
exit(1)
}
//println('out_name:$out_name')
mut out_name_c := os.realpath( out_name ) + '.tmp.c'
mut files := []string
// Add builtin files
if !out_name.contains('builtin.o') {
//if !out_name.contains('builtin.o') {
for builtin in builtins {
mut f := '$vroot/vlib/builtin/$builtin'
// 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
}
}
//}
mut cflags := ''
for ci, cv in args {