@@ -22,28 +22,46 @@ pub fn (c &CFlag) str() string {
22
22
}
23
23
24
24
const 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
+ }
25
38
26
39
// expand the flag value
27
- pub fn (cf &CFlag) eval () string {
40
+ pub fn (cf &CFlag) eval () ? string {
28
41
mut value_builder := strings.new_builder (10 * cf.value.len)
29
42
cflag_eval_outer_loop: for i := 0 ; i < cf.value.len; i++ {
30
43
x := cf.value[i]
31
44
if x == `$` {
32
45
remainder := cf.value[i..]
33
46
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
44
53
}
45
54
panic ('>> error: none of the paths ${svalues} exist' )
46
- continue
55
+ }
56
+ if remainder.starts_with (wexisting_literal) {
57
+ found , spath , delta_i , svalues := find_first_existing_path (remainder,
58
+ wexisting_literal)
59
+ if found {
60
+ value_builder.write_string (spath)
61
+ i + = delta_i
62
+ continue
63
+ }
64
+ return none
47
65
}
48
66
}
49
67
value_builder.write_string (x.ascii_str ())
@@ -52,12 +70,12 @@ pub fn (cf &CFlag) eval() string {
52
70
}
53
71
54
72
// format flag
55
- pub fn (cf &CFlag) format () string {
73
+ pub fn (cf &CFlag) format () ? string {
56
74
mut value := ''
57
75
if cf.cached != '' {
58
76
value = cf.cached
59
77
} else {
60
- value = cf.eval ()
78
+ value = cf.eval ()?
61
79
}
62
80
if cf.name in ['-l' , '-Wa' , '-Wl' , '-Wp' ] && value != '' {
63
81
return '${cf.name}${value} ' .trim_space ()
@@ -97,7 +115,7 @@ pub fn (cflags []CFlag) c_options_without_object_files() []string {
97
115
if flag.value.ends_with ('.o' ) || flag.value.ends_with ('.obj' ) {
98
116
continue
99
117
}
100
- args << flag.format ()
118
+ args << flag.format () or { continue }
101
119
}
102
120
return args
103
121
}
@@ -108,10 +126,10 @@ pub fn (cflags []CFlag) c_options_only_object_files() []string {
108
126
// TODO figure out a better way to copy cross compiling flags to the linker
109
127
if flag.value.ends_with ('.o' ) || flag.value.ends_with ('.obj' )
110
128
|| (flag.name == '-l' && flag.value == 'pq' ) {
111
- args << flag.format ()
129
+ args << flag.format () or { continue }
112
130
}
113
131
}
114
- return args
132
+ return args. filter ( it != '' )
115
133
}
116
134
117
135
pub fn (cflags []CFlag) defines_others_libs () ([]string , []string , []string ) {
0 commit comments