Skip to content

Commit e16d5eb

Browse files
committed
build.zig: Make more flags from src/config.h configurable
1 parent d8feef5 commit e16d5eb

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

build.zig

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,33 +76,48 @@ fn emSdkSetupStep(b: *std.Build, emsdk: *std.Build.Dependency) !?*std.Build.Step
7676
}
7777
}
7878

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
8080
const config_h_flags = outer: {
8181
// Set this value higher if compile errors happen as `src/config.h` gets larger
8282
@setEvalBranchQuota(1 << 20);
8383

8484
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`?");
8689

8790
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');
8992
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+
}
93103

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+
}
99115

100-
flags[i] = flag;
101116
i += 1;
102117
}
103118

104119
// 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] }));
106121
break :outer flags[0..i].*;
107122
};
108123

@@ -135,21 +150,32 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
135150

136151
// Apply all relevant configs from `src/config.h` *except* the user-specified ones
137152
//
153+
// Note: This entire loop might become unnecessary depending on https://github.com/raysan5/raylib/issues/4411
154+
//
138155
// Note: Currently using a suboptimal `O(m*n)` time algorithm where:
139156
// `m` corresponds roughly to the number of lines in `src/config.h`
140157
// `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+
142162
// If a user already specified the flag, skip it
143163
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+
145168
// For a user-specified flag to match, it must share the same prefix and have the
146169
// 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;
149172
}
150173

151174
// 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+
);
153179
}
154180
}
155181

0 commit comments

Comments
 (0)