Skip to content

Commit

Permalink
build.zig: Use std.Build.Module.addCMacro for config flags
Browse files Browse the repository at this point in the history
  • Loading branch information
sagehane committed Oct 21, 2024
1 parent fe81ed2 commit 7906361
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,43 @@ const config_h_flags = outer: {
var strs = std.mem.tokenizeScalar(u8, line[prefix.len..], ' ');
const flag = strs.next() orelse @compileError("src/config.h: Flag not found: " ++ line);

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

flags[i] = .{ "-D" ++ flag, value };
flags[i] = .{ flag, value };
i += 1;
}

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

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

const shared_flags = &[_][]const u8{
"-fPIC",
"-DBUILD_LIBTYPE_SHARED",
};
const raylib = if (options.shared)
b.addSharedLibrary(.{
.name = "raylib",
.target = target,
.optimize = optimize,
})
else
b.addStaticLibrary(.{
.name = "raylib",
.target = target,
.optimize = optimize,
});
raylib.linkLibC();

if (options.shared) {
const shared_flags = &[_][]const u8{
"-fPIC",
"-DBUILD_LIBTYPE_SHARED",
};

try raylib_flags_arr.appendSlice(b.allocator, shared_flags);
}

try raylib_flags_arr.appendSlice(b.allocator, &[_][]const u8{
"-std=gnu99",
"-D_GNU_SOURCE",
Expand Down Expand Up @@ -128,42 +146,25 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
// `n` corresponds to the number of user-specified flags
outer: for (config_h_flags) |flag_val| {
const flag = flag_val[0];
const value = flag_val[1];

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

// Otherwise, append default value from config.h to compile flags
try raylib_flags_arr.append(
b.allocator,
std.mem.concat(b.allocator, u8, &flag_val) catch @panic("OOM"),
);
raylib.root_module.addCMacro(flag, value);
}
}

if (options.shared) {
try raylib_flags_arr.appendSlice(b.allocator, shared_flags);
}

const raylib = if (options.shared)
b.addSharedLibrary(.{
.name = "raylib",
.target = target,
.optimize = optimize,
})
else
b.addStaticLibrary(.{
.name = "raylib",
.target = target,
.optimize = optimize,
});
raylib.linkLibC();

// No GLFW required on PLATFORM_DRM
if (options.platform != .drm) {
raylib.addIncludePath(b.path("src/external/glfw/include"));
Expand Down

0 comments on commit 7906361

Please sign in to comment.