diff --git a/compiler/comptime.v b/compiler/comptime.v index c2d4bed885..b0286e1763 100644 --- a/compiler/comptime.v +++ b/compiler/comptime.v @@ -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 + // 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') } diff --git a/compiler/main.v b/compiler/main.v index 605fded22c..93bb63002e 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -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 +} diff --git a/vlib/runtime/runtime.v b/vlib/runtime/runtime.v index 7777c00615..2d11a30730 100644 --- a/vlib/runtime/runtime.v +++ b/vlib/runtime/runtime.v @@ -1 +1,14 @@ module runtime + +$if linux { + #include + fn C.get_nprocs() int +} + + +pub fn nr_cpus() int { + $if linux { + return C.get_nprocs() + } + return 0 +} diff --git a/vlib/runtime/runtime_mac.v b/vlib/runtime/runtime_mac.v index 7345c63217..9df2fff173 100644 --- a/vlib/runtime/runtime_mac.v +++ b/vlib/runtime/runtime_mac.v @@ -1,5 +1,2 @@ module runtime -pub fn nr_cpus() int { - return 0 -} diff --git a/vlib/runtime/runtime_test.v b/vlib/runtime/runtime_test.v index 639a86eb59..cd0bb6e58a 100644 --- a/vlib/runtime/runtime_test.v +++ b/vlib/runtime/runtime_test.v @@ -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 } } diff --git a/vlib/runtime/runtime_win.v b/vlib/runtime/runtime_win.v index 7345c63217..9df2fff173 100644 --- a/vlib/runtime/runtime_win.v +++ b/vlib/runtime/runtime_win.v @@ -1,5 +1,2 @@ module runtime -pub fn nr_cpus() int { - return 0 -}