compiler: support passing -cc to specify the desired C compiler

pull/1722/head
Delyan Angelov 2019-08-23 11:42:48 +03:00 committed by Alexander Medvednikov
parent df593870ae
commit d362f73af8
4 changed files with 35 additions and 20 deletions

View File

@ -2,10 +2,11 @@ CC ?= cc
PREFIX ?= /usr/local PREFIX ?= /usr/local
all: all:
rm -rf vc/
git clone --depth 1 --quiet https://github.com/vlang/vc git clone --depth 1 --quiet https://github.com/vlang/vc
${CC} -std=gnu11 -w -o v vc/v.c -lm ${CC} -std=gnu11 -w -o v vc/v.c -lm
./v -o v compiler ./v -o v compiler
rm -rf vc rm -rf vc/
@echo "V has been successfully built" @echo "V has been successfully built"
symlink: v symlink: v

View File

@ -39,7 +39,7 @@ mut:
} }
fn new_cgen(out_name_c string) *CGen { fn new_cgen(out_name_c string) *CGen {
path:='.$out_name_c' path := out_name_c
out := os.create(path) or { out := os.create(path) or {
println('failed to create $path') println('failed to create $path')
return &CGen{} return &CGen{}
@ -257,8 +257,9 @@ fn build_thirdparty_obj_file(flag string) {
cfiles += parent + '/' + file + ' ' cfiles += parent + '/' + file + ' '
} }
} }
cc := if os.user_os() == 'windows' { 'gcc' } else { 'cc' } // TODO clang support on Windows cc := find_c_compiler()
res := os.exec('$cc -fPIC -c -o $obj_path $cfiles') or { cc_thirdparty_options := find_c_compiler_thirdparty_options()
res := os.exec('$cc $cc_thirdparty_options -c -o $obj_path $cfiles') or {
panic(err) panic(err)
} }
println(res.output) println(res.output)

View File

@ -94,6 +94,7 @@ mut:
// For example, passing -cflags -Os will cause the C compiler to optimize the generated binaries for size. // For example, passing -cflags -Os will cause the C compiler to optimize the generated binaries for size.
// You could pass several -cflags XXX arguments. They will be merged with each other. // You could pass several -cflags XXX arguments. They will be merged with each other.
// You can also quote several options at the same time: -cflags '-Os -fno-inline-small-functions'. // You can also quote several options at the same time: -cflags '-Os -fno-inline-small-functions'.
ccompiler string // the name of the used C compiler
} }
@ -604,7 +605,7 @@ mut args := ''
a << '-x objective-c' a << '-x objective-c'
} }
// The C file we are compiling // The C file we are compiling
a << '".$v.out_name_c"' a << '"$v.out_name_c"'
if v.os == .mac { if v.os == .mac {
a << '-x none' a << '-x none'
} }
@ -628,20 +629,10 @@ mut args := ''
if v.os == .windows { if v.os == .windows {
a << '-DUNICODE -D_UNICODE' a << '-DUNICODE -D_UNICODE'
} }
// Find clang executable
//fast_clang := '/usr/local/Cellar/llvm/8.0.0/bin/clang'
args := a.join(' ') args := a.join(' ')
//mut cmd := if os.file_exists(fast_clang) { cmd := '${v.pref.ccompiler} $args'
//'$fast_clang $args'
//}
//else {
mut cmd := ('cc $args') // TODO fix $if after 'string'
//}
$if windows {
cmd = 'gcc $args'
}
if v.out_name.ends_with('.c') { if v.out_name.ends_with('.c') {
os.mv( '.$v.out_name_c', v.out_name ) os.mv( v.out_name_c, v.out_name )
exit(0) exit(0)
} }
// Run // Run
@ -677,7 +668,7 @@ mut args := ''
diff := time.ticks() - ticks diff := time.ticks() - ticks
// Print the C command // Print the C command
if v.pref.show_c_cmd || v.pref.is_verbose { if v.pref.show_c_cmd || v.pref.is_verbose {
println('cc took $diff ms') println('${v.pref.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
@ -700,7 +691,7 @@ mut args := ''
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)
} }
} }
@ -1009,7 +1000,7 @@ 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)
} }
mut out_name_c := out_name.all_after('/') + '.c' mut out_name_c := out_name.all_after('/') + '.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') {
@ -1051,6 +1042,7 @@ fn new_v(args[]string) *V {
is_repl: args.contains('-repl') is_repl: args.contains('-repl')
build_mode: build_mode build_mode: build_mode
cflags: cflags cflags: cflags
ccompiler: find_c_compiler()
} }
if pref.is_play { if pref.is_play {
println('Playground') println('Playground')
@ -1182,3 +1174,23 @@ fn test_v() {
} }
} }
fn find_c_compiler() string {
args := env_vflags_and_os_args().join(' ')
defaultcc := find_c_compiler_default()
return get_arg( args, 'cc', defaultcc )
}
fn find_c_compiler_default() string {
//fast_clang := '/usr/local/Cellar/llvm/8.0.0/bin/clang'
//if os.file_exists(fast_clang) {
// return fast_clang
//}
// TODO fix $if after 'string'
$if windows { return 'gcc' }
return 'cc'
}
fn find_c_compiler_thirdparty_options() string {
$if windows { return '' }
return '-fPIC'
}

View File

@ -1,3 +1,4 @@
rd /s /q vc
git clone --depth 1 --quiet https://github.com/vlang/vc git clone --depth 1 --quiet https://github.com/vlang/vc
gcc -std=gnu11 -DUNICODE -D_UNICODE -w -o v2.exe vc/v_win.c gcc -std=gnu11 -DUNICODE -D_UNICODE -w -o v2.exe vc/v_win.c
v2.exe -o v.exe compiler v2.exe -o v.exe compiler