@@ -76,33 +76,48 @@ fn emSdkSetupStep(b: *std.Build, emsdk: *std.Build.Dependency) !?*std.Build.Step
76
76
}
77
77
}
78
78
79
- /// A list of all flags from `src/config.h` that one may override
79
+ /// A list of all flags and their corresponding values from `src/config.h` that one may override
80
80
const config_h_flags = outer : {
81
81
// Set this value higher if compile errors happen as `src/config.h` gets larger
82
82
@setEvalBranchQuota (1 << 20 );
83
83
84
84
const config_h = @embedFile ("src/config.h" );
85
- var flags : [std .mem .count (u8 , config_h , "\n " ) + 1 ][]const u8 = undefined ;
85
+ var flags = [1 ][2 ][]const u8 {.{ undefined , "1" }} ** (std .mem .count (u8 , config_h , "\n " ) + 1 );
86
+
87
+ const first_def = "#define CONFIG_H\n " ;
88
+ const first_line_idx = std .mem .indexOf (u8 , config_h , first_def ) orelse @compileError ("Invalid `src/config.h`?" );
86
89
87
90
var i = 0 ;
88
- var lines = std .mem .tokenizeScalar (u8 , config_h , '\n ' );
91
+ var lines = std .mem .tokenizeScalar (u8 , config_h [ first_line_idx + first_def . len .. ] , '\n ' );
89
92
while (lines .next ()) | line | {
90
- if (! std .mem .containsAtLeast (u8 , line , 1 , "SUPPORT" )) continue ;
91
- if (std .mem .startsWith (u8 , line , "//" )) continue ;
92
- if (std .mem .startsWith (u8 , line , "#if" )) continue ;
93
+ // Jump past `#if` lines until `#endif` is reached
94
+ if (std .mem .startsWith (u8 , line , "#if" )) {
95
+ // Count of `#if`s found without a delimiting `#endif`
96
+ var unpaired_if : u32 = 1 ;
97
+ while (unpaired_if != 0 ) {
98
+ const next_line = lines .next () orelse @compileError ("src/config.h: `#endif` not found" );
99
+ if (std .mem .startsWith (u8 , next_line , "#if" )) unpaired_if += 1 ;
100
+ if (std .mem .startsWith (u8 , next_line , "#endif" )) unpaired_if -= 1 ;
101
+ }
102
+ }
93
103
94
- var flag = std .mem .trimLeft (u8 , line , " \t " ); // Trim whitespace
95
- flag = flag ["#define " .len - 1 .. ]; // Remove #define
96
- flag = std .mem .trimLeft (u8 , flag , " \t " ); // Trim whitespace
97
- flag = flag [0 .. std .mem .indexOf (u8 , flag , " " ) orelse continue ]; // Flag is only one word, so capture till space
98
- flag = "-D" ++ flag ; // Prepend with -D
104
+ // Ignore everything but `#define` lines
105
+ const prefix = "#define " ;
106
+ if (! std .mem .startsWith (u8 , line , prefix )) continue ;
107
+
108
+ // Get space-separated strings
109
+ var strs = std .mem .tokenizeScalar (u8 , line [prefix .len .. ], ' ' );
110
+
111
+ flags [i ][0 ] = strs .next () orelse @compileError ("src/config.h: Flag not found: " ++ line );
112
+ if (strs .next ()) | value | {
113
+ if (! std .mem .startsWith (u8 , value , "//" )) flags [i ][1 ] = value ;
114
+ }
99
115
100
- flags [i ] = flag ;
101
116
i += 1 ;
102
117
}
103
118
104
119
// Uncomment this to check what flags normally get passed
105
- //@compileLog (flags[0..i].* );
120
+ //for (flags[0..i]) |flag| @compileLog(std.fmt.comptimePrint("{s}={s}", .{ flag[0], flag[1] }) );
106
121
break :outer flags [0.. i ].* ;
107
122
};
108
123
@@ -135,21 +150,32 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
135
150
136
151
// Apply all relevant configs from `src/config.h` *except* the user-specified ones
137
152
//
153
+ // Note: This entire loop might become unnecessary depending on https://github.com/raysan5/raylib/issues/4411
154
+ //
138
155
// Note: Currently using a suboptimal `O(m*n)` time algorithm where:
139
156
// `m` corresponds roughly to the number of lines in `src/config.h`
140
157
// `n` corresponds to the number of user-specified flags
141
- outer : for (config_h_flags ) | flag | {
158
+ outer : for (config_h_flags ) | flag_val | {
159
+ const flag = flag_val [0 ];
160
+ const value = flag_val [1 ];
161
+
142
162
// If a user already specified the flag, skip it
143
163
config_iter .reset ();
144
- while (config_iter .next ()) | config_flag | {
164
+ while (config_iter .next ()) | user_flag | {
165
+ if (! std .mem .startsWith (u8 , user_flag , "-D" )) continue ;
166
+ const u_flag_stripped = user_flag ["-D" .len .. ];
167
+
145
168
// For a user-specified flag to match, it must share the same prefix and have the
146
169
// same length or be followed by an equals sign
147
- if (! std .mem .startsWith (u8 , config_flag , flag )) continue ;
148
- if (config_flag .len == flag .len or config_flag [flag .len ] == '=' ) continue :outer ;
170
+ if (! std .mem .startsWith (u8 , u_flag_stripped , flag )) continue ;
171
+ if (u_flag_stripped .len == flag .len or u_flag_stripped [flag .len ] == '=' ) continue :outer ;
149
172
}
150
173
151
174
// Otherwise, append default value from config.h to compile flags
152
- try raylib_flags_arr .append (b .allocator , flag );
175
+ try raylib_flags_arr .append (
176
+ b .allocator ,
177
+ b .fmt ("-D{s}={s}" , .{ flag , value }),
178
+ );
153
179
}
154
180
}
155
181
0 commit comments