v.cflag: support `#flag $first_existing("/opt/homebrew/lib/libgc.a", "/usr/local/lib/libgc.a")`

pull/9663/head
Delyan Angelov 2021-04-10 10:46:43 +03:00
parent bf6a2f80ef
commit d54becf52e
2 changed files with 35 additions and 3 deletions

View File

@ -4,8 +4,8 @@ module builtin
$if static_boehm ? {
$if macos {
#flag -I/opt/homebrew/include
#flag /opt/homebrew/lib/libgc.a
#flag -I$first_existing("/opt/homebrew/include", "/usr/local/include")
#flag $first_existing("/opt/homebrew/lib/libgc.a", "/usr/local/lib/libgc.a")
} $else $if linux {
#flag -l:libgc.a
} $else {

View File

@ -20,11 +20,43 @@ pub fn (c &CFlag) str() string {
return 'CFlag{ name: "$c.name" value: "$c.value" mod: "$c.mod" os: "$c.os" cached: "$c.cached" }'
}
const fexisting_literal = r'$first_existing'
// expand the flag value
pub fn (cf &CFlag) eval() string {
mut value := ''
cflag_eval_outer_loop: for i := 0; i < cf.value.len; i++ {
x := cf.value[i]
if x == `$` {
remainder := cf.value[i..]
if remainder.starts_with(cflag.fexisting_literal) {
sparams := remainder[cflag.fexisting_literal.len + 1..].all_before(')')
i += sparams.len + cflag.fexisting_literal.len + 1
svalues := sparams.replace(',', '\n').split_into_lines().map(it.trim(' \'"'))
mut found_spath := ''
for spath in svalues {
if os.exists(spath) {
found_spath = spath
value += spath
continue cflag_eval_outer_loop
}
}
panic('>> error: none of the paths $svalues exist')
continue
}
}
value += x.ascii_str()
}
return value
}
// format flag
pub fn (cf &CFlag) format() string {
mut value := cf.value
mut value := ''
if cf.cached != '' {
value = cf.cached
} else {
value = cf.eval()
}
if cf.name in ['-l', '-Wa', '-Wl', '-Wp'] && value.len > 0 {
return '$cf.name$value'.trim_space()