mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parser: skip comp_if when building for a different compiler
This commit is contained in:
parent
5ddb129bc2
commit
68967e833d
@ -45,7 +45,8 @@ fn test_fmt() {
|
|||||||
}
|
}
|
||||||
table := table.new_table()
|
table := table.new_table()
|
||||||
file_ast := parser.parse_file(ipath, table, .parse_comments, &pref.Preferences{
|
file_ast := parser.parse_file(ipath, table, .parse_comments, &pref.Preferences{
|
||||||
is_fmt: true
|
is_fmt: true,
|
||||||
|
ccompiler: 'gcc'
|
||||||
}, &ast.Scope{
|
}, &ast.Scope{
|
||||||
parent: 0
|
parent: 0
|
||||||
})
|
})
|
||||||
|
@ -13,6 +13,7 @@ import vweb.tmpl
|
|||||||
const (
|
const (
|
||||||
supported_platforms = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd',
|
supported_platforms = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd',
|
||||||
'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos']
|
'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos']
|
||||||
|
supported_ccompilers = ['tinyc', 'clang', 'mingw', 'msvc', 'gcc']
|
||||||
)
|
)
|
||||||
|
|
||||||
fn (mut p Parser) resolve_vroot(flag string) string {
|
fn (mut p Parser) resolve_vroot(flag string) string {
|
||||||
@ -154,15 +155,25 @@ fn (mut p Parser) comp_if() ast.Stmt {
|
|||||||
}
|
}
|
||||||
val := p.check_name()
|
val := p.check_name()
|
||||||
mut stmts := []ast.Stmt{}
|
mut stmts := []ast.Stmt{}
|
||||||
mut skip_os := false
|
mut skip := false
|
||||||
|
|
||||||
if val in supported_platforms {
|
if val in supported_platforms {
|
||||||
os := os_from_string(val)
|
os := os_from_string(val)
|
||||||
// `$if os {` for a different target, skip everything inside
|
if (!is_not && os != p.pref.os) || (is_not && os == p.pref.os) {
|
||||||
|
skip = true
|
||||||
|
}
|
||||||
|
} else if val in supported_ccompilers {
|
||||||
|
cc := cc_from_string(val)
|
||||||
|
user_cc := cc_from_string(p.pref.ccompiler)
|
||||||
|
if (!is_not && cc != user_cc) || (is_not && cc == user_cc) {
|
||||||
|
skip = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// `$if os {` or `$if compiler {` for a different target, skip everything inside
|
||||||
// to avoid compilation errors (like including <windows.h> or calling WinAPI fns
|
// to avoid compilation errors (like including <windows.h> or calling WinAPI fns
|
||||||
// on non-Windows systems)
|
// on non-Windows systems)
|
||||||
if !p.pref.is_fmt && ((!is_not && os != p.pref.os) || (is_not && os == p.pref.os)) &&
|
if !p.pref.is_fmt && !p.pref.output_cross_c && skip {
|
||||||
!p.pref.output_cross_c {
|
|
||||||
skip_os = true
|
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
// p.warn('skipping $if $val os=$os p.pref.os=$p.pref.os')
|
// p.warn('skipping $if $val os=$os p.pref.os=$p.pref.os')
|
||||||
mut stack := 1
|
mut stack := 1
|
||||||
@ -185,14 +196,14 @@ fn (mut p Parser) comp_if() ast.Stmt {
|
|||||||
}
|
}
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
}
|
} else { skip = false }
|
||||||
}
|
|
||||||
mut is_opt := false
|
mut is_opt := false
|
||||||
if p.tok.kind == .question {
|
if p.tok.kind == .question {
|
||||||
p.next()
|
p.next()
|
||||||
is_opt = true
|
is_opt = true
|
||||||
}
|
}
|
||||||
if !skip_os {
|
if !skip {
|
||||||
stmts = p.parse_block()
|
stmts = p.parse_block()
|
||||||
}
|
}
|
||||||
mut node := ast.CompIf{
|
mut node := ast.CompIf{
|
||||||
@ -269,6 +280,17 @@ fn os_from_string(os string) pref.OS {
|
|||||||
return .linux
|
return .linux
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to convert string names to CC enum
|
||||||
|
pub fn cc_from_string(cc_str string) pref.CompilerType {
|
||||||
|
cc := cc_str.replace('\\', '/').split('/').last().all_before('.')
|
||||||
|
if 'tcc' in cc { return .tinyc }
|
||||||
|
if 'tinyc' in cc { return .tinyc }
|
||||||
|
if 'clang' in cc { return .clang }
|
||||||
|
if 'mingw' in cc { return .mingw }
|
||||||
|
if 'msvc' in cc { return .msvc }
|
||||||
|
return .gcc
|
||||||
|
}
|
||||||
|
|
||||||
// `app.$action()` (`action` is a string)
|
// `app.$action()` (`action` is a string)
|
||||||
// `typ` is `App` in this example
|
// `typ` is `App` in this example
|
||||||
// fn (mut p Parser) comptime_method_call(typ table.Type) ast.ComptimeCall {
|
// fn (mut p Parser) comptime_method_call(typ table.Type) ast.ComptimeCall {
|
||||||
|
@ -32,6 +32,14 @@ pub enum Backend {
|
|||||||
x64 // The x64 backend
|
x64 // The x64 backend
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum CompilerType {
|
||||||
|
tinyc
|
||||||
|
clang
|
||||||
|
mingw
|
||||||
|
msvc
|
||||||
|
gcc
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
list_of_flags_with_param = [
|
list_of_flags_with_param = [
|
||||||
'o'
|
'o'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user