@@ -22,28 +22,45 @@ pub fn (c &CFlag) str() string {
2222}
2323
2424const fexisting_literal = r '$first_existing'
25+ const wexisting_literal = r '$when_first_existing'
26+
27+ fn find_first_existing_path (remainder string , literal string ) (bool , string , int , []string ) {
28+ sparams := remainder[literal.len + 1 ..].all_before (')' )
29+ delta_i := sparams.len + literal.len + 1
30+ svalues := sparams.replace (',' , '\n ' ).split_into_lines ().map (it .trim ('\t \' "' ))
31+ for spath in svalues {
32+ if os.exists (spath) {
33+ return true , spath, delta_i, []string {}
34+ }
35+ }
36+ return false , '' , delta_i, svalues
37+ }
2538
2639// expand the flag value
27- pub fn (cf &CFlag) eval () string {
40+ pub fn (cf &CFlag) eval () ? string {
2841 mut value_builder := strings.new_builder (10 * cf.value.len)
2942 cflag_eval_outer_loop: for i := 0 ; i < cf.value.len; i++ {
3043 x := cf.value[i]
3144 if x == `$` {
3245 remainder := cf.value[i..]
3346 if remainder.starts_with (fexisting_literal) {
34- sparams := remainder[fexisting_literal.len + 1 ..].all_before (')' )
35- i + = sparams.len + fexisting_literal.len + 1
36- svalues := sparams.replace (',' , '\n ' ).split_into_lines ().map (it .trim ('\t \' "' ))
37- // mut found_spath := ''
38- for spath in svalues {
39- if os.exists (spath) {
40- // found_spath = spath
41- value_builder.write_string (spath)
42- continue cflag_eval_outer_loop
43- }
47+ found , spath , delta_i , svalues := find_first_existing_path (remainder,
48+ fexisting_literal)
49+ if found {
50+ value_builder.write_string (spath)
51+ i + = delta_i
52+ continue
4453 }
4554 panic ('>> error: none of the paths ${svalues } exist' )
46- continue
55+ }
56+ if remainder.starts_with (wexisting_literal) {
57+ found , spath , delta_i , _ := find_first_existing_path (remainder, wexisting_literal)
58+ if found {
59+ value_builder.write_string (spath)
60+ i + = delta_i
61+ continue
62+ }
63+ return none
4764 }
4865 }
4966 value_builder.write_string (x.ascii_str ())
@@ -52,12 +69,12 @@ pub fn (cf &CFlag) eval() string {
5269}
5370
5471// format flag
55- pub fn (cf &CFlag) format () string {
72+ pub fn (cf &CFlag) format () ? string {
5673 mut value := ''
5774 if cf.cached != '' {
5875 value = cf.cached
5976 } else {
60- value = cf.eval ()
77+ value = cf.eval ()?
6178 }
6279 if cf.name in ['-l' , '-Wa' , '-Wl' , '-Wp' ] && value != '' {
6380 return '${cf .name }${value }' .trim_space ()
@@ -97,7 +114,7 @@ pub fn (cflags []CFlag) c_options_without_object_files() []string {
97114 if flag.value.ends_with ('.o' ) || flag.value.ends_with ('.obj' ) {
98115 continue
99116 }
100- args << flag.format ()
117+ args << flag.format () or { continue }
101118 }
102119 return args
103120}
@@ -108,10 +125,10 @@ pub fn (cflags []CFlag) c_options_only_object_files() []string {
108125 // TODO figure out a better way to copy cross compiling flags to the linker
109126 if flag.value.ends_with ('.o' ) || flag.value.ends_with ('.obj' )
110127 || (flag.name == '-l' && flag.value == 'pq' ) {
111- args << flag.format ()
128+ args << flag.format () or { continue }
112129 }
113130 }
114- return args
131+ return args. filter ( it != '' )
115132}
116133
117134pub fn (cflags []CFlag) defines_others_libs () ([]string , []string , []string ) {
0 commit comments