1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

make $if work with includes: skip the body if the target is different

This commit is contained in:
Alexander Medvednikov 2019-10-12 00:54:25 +03:00
parent 9faefe85e1
commit 6db7518189
6 changed files with 64 additions and 29 deletions

View File

@ -30,7 +30,31 @@ fn (p mut Parser) comp_time() {
p.genln('#ifdef $ifdef_name')
}
p.check(.lcbr)
p.statements_no_rcbr()
os := os_from_string(name)
if p.fileis('runtime.v') && os != p.os {
// `$if os {` for a different target, skip everything inside
// to avoid compilation errors (like including <windows.h>
// on non-Windows systems)
mut stack := 1
for {
if p.tok == .lcbr {
stack++
} else if p.tok == .rcbr {
stack--
}
if p.tok == .eof {
break
}
if stack <= 0 && p.tok == .rcbr {
//p.warn('exiting $stack')
p.next()
break
}
p.next()
}
} else {
p.statements_no_rcbr()
}
if ! (p.tok == .dollar && p.peek() == .key_else) {
p.genln('#endif')
}

View File

@ -529,18 +529,15 @@ fn (v mut V) gen_main_end(return_statement string){
}
fn final_target_out_name(out_name string) string {
mut cmd := if out_name.starts_with('/') {
$if windows {
return out_name.replace('/', '\\') + '.exe'
}
return if out_name.starts_with('/') {
out_name
}
else {
'./' + out_name
}
$if windows {
cmd = out_name
cmd = cmd.replace('/', '\\')
cmd += '.exe'
}
return cmd
}
fn (v V) run_compiled_executable_and_exit() {
@ -895,18 +892,7 @@ fn new_v(args[]string) &V {
}
}
else {
switch target_os {
case 'linux': _os = .linux
case 'windows': _os = .windows
case 'mac': _os = .mac
case 'freebsd': _os = .freebsd
case 'openbsd': _os = .openbsd
case 'netbsd': _os = .netbsd
case 'dragonfly': _os = .dragonfly
case 'msvc': _os = .msvc
case 'js': _os = .js
case 'solaris': _os = .solaris
}
_os = os_from_string(target_os)
}
// Location of all vlib files
vroot := os.dir(os.executable())
@ -1097,3 +1083,21 @@ fn vhash() string {
fn cescaped_path(s string) string {
return s.replace('\\','\\\\')
}
fn os_from_string(os string) OS {
switch os {
case 'linux': return .linux
case 'windows': return .windows
case 'mac': return .mac
case 'freebsd': return .freebsd
case 'openbsd': return .openbsd
case 'netbsd': return .netbsd
case 'dragonfly': return .dragonfly
case 'msvc': return .msvc
case 'js': return .js
case 'solaris': return .solaris
case 'android': return .android
}
println('bad os $os') // todo panic?
return .linux
}

View File

@ -1 +1,14 @@
module runtime
$if linux {
#include <sys/sysinfo.h>
fn C.get_nprocs() int
}
pub fn nr_cpus() int {
$if linux {
return C.get_nprocs()
}
return 0
}

View File

@ -1,5 +1,2 @@
module runtime
pub fn nr_cpus() int {
return 0
}

View File

@ -2,8 +2,8 @@ import runtime
fn test_nr_cpus() {
$if linux {
nr_cpus := runtime.nr_cpus()
println(nr_cpus)
assert nr_cpus > 0
nr_cpus := runtime.nr_cpus()
println(nr_cpus)
assert nr_cpus > 0
}
}

View File

@ -1,5 +1,2 @@
module runtime
pub fn nr_cpus() int {
return 0
}