Skip to content

Commit 7906361

Browse files
committed
build.zig: Use std.Build.Module.addCMacro for config flags
1 parent fe81ed2 commit 7906361

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

build.zig

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,43 @@ const config_h_flags = outer: {
8282
var strs = std.mem.tokenizeScalar(u8, line[prefix.len..], ' ');
8383
const flag = strs.next() orelse @compileError("src/config.h: Flag not found: " ++ line);
8484

85-
var value = strs.next() orelse "";
86-
value = if (std.mem.startsWith(u8, value, "//")) "" else "=" ++ value;
85+
// Set to 1 if no value is provided
86+
var value = strs.next() orelse "1";
87+
value = if (std.mem.startsWith(u8, value, "//")) "1" else value;
8788

88-
flags[i] = .{ "-D" ++ flag, value };
89+
flags[i] = .{ flag, value };
8990
i += 1;
9091
}
9192

92-
// Uncomment this to check what flags normally get passed
93-
//@compileLog(flags[0..i].*);
9493
break :outer flags[0..i].*;
9594
};
9695

9796
fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, options: Options) !*std.Build.Step.Compile {
9897
raylib_flags_arr.clearRetainingCapacity();
9998

100-
const shared_flags = &[_][]const u8{
101-
"-fPIC",
102-
"-DBUILD_LIBTYPE_SHARED",
103-
};
99+
const raylib = if (options.shared)
100+
b.addSharedLibrary(.{
101+
.name = "raylib",
102+
.target = target,
103+
.optimize = optimize,
104+
})
105+
else
106+
b.addStaticLibrary(.{
107+
.name = "raylib",
108+
.target = target,
109+
.optimize = optimize,
110+
});
111+
raylib.linkLibC();
112+
113+
if (options.shared) {
114+
const shared_flags = &[_][]const u8{
115+
"-fPIC",
116+
"-DBUILD_LIBTYPE_SHARED",
117+
};
118+
119+
try raylib_flags_arr.appendSlice(b.allocator, shared_flags);
120+
}
121+
104122
try raylib_flags_arr.appendSlice(b.allocator, &[_][]const u8{
105123
"-std=gnu99",
106124
"-D_GNU_SOURCE",
@@ -128,42 +146,25 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
128146
// `n` corresponds to the number of user-specified flags
129147
outer: for (config_h_flags) |flag_val| {
130148
const flag = flag_val[0];
149+
const value = flag_val[1];
131150

132151
// If a user already specified the flag, skip it
133152
config_iter.reset();
134-
while (config_iter.next()) |config_flag| {
153+
while (config_iter.next()) |config_flag_str| {
135154
// For a user-specified flag to match, it must share the same prefix and have the
136155
// same length or be followed by an equals sign
156+
const prefix = "-D";
157+
if (!std.mem.startsWith(u8, config_flag_str, prefix)) continue;
158+
const config_flag = config_flag_str[prefix.len..];
137159
if (!std.mem.startsWith(u8, config_flag, flag)) continue;
138160
if (config_flag.len == flag.len or config_flag[flag.len] == '=') continue :outer;
139161
}
140162

141163
// Otherwise, append default value from config.h to compile flags
142-
try raylib_flags_arr.append(
143-
b.allocator,
144-
std.mem.concat(b.allocator, u8, &flag_val) catch @panic("OOM"),
145-
);
164+
raylib.root_module.addCMacro(flag, value);
146165
}
147166
}
148167

149-
if (options.shared) {
150-
try raylib_flags_arr.appendSlice(b.allocator, shared_flags);
151-
}
152-
153-
const raylib = if (options.shared)
154-
b.addSharedLibrary(.{
155-
.name = "raylib",
156-
.target = target,
157-
.optimize = optimize,
158-
})
159-
else
160-
b.addStaticLibrary(.{
161-
.name = "raylib",
162-
.target = target,
163-
.optimize = optimize,
164-
});
165-
raylib.linkLibC();
166-
167168
// No GLFW required on PLATFORM_DRM
168169
if (options.platform != .drm) {
169170
raylib.addIncludePath(b.path("src/external/glfw/include"));

0 commit comments

Comments
 (0)