Skip to content

Commit

Permalink
Support CFLAGS_EXTRA option is zig script
Browse files Browse the repository at this point in the history
PR #607 (bdwgc).

This passes through extra options (flags) to the C compiler.
No flags validation is performed; the values given in CFLAGS_EXTRA are
split on a space and append to compiler flags one by one.

* build.zig (cflags_extra): New option (an empty string by default).
* build.zig (flags): Append space-separated flags from cflags_extra
(after all other flags are appended); remove relevant TODO item.
  • Loading branch information
plajjan authored and ivmai committed Jan 11, 2024
1 parent 4071e87 commit 70c9e87
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub fn build(b: *std.Build) void {
// TODO: support build_cord
const build_tests = b.option(bool, "build_tests",
"Build tests") orelse false;
const cflags_extra = b.option([]const u8, "CFLAGS_EXTRA",
"Extra user-defined cflags") orelse "";
// TODO: support enable_docs
const enable_threads = b.option(bool, "enable_threads",
"Support threads") orelse default_enable_threads;
Expand Down Expand Up @@ -383,8 +385,6 @@ pub fn build(b: *std.Build) void {
}
}

// TODO: support CFLAGS_EXTRA (extra user-defined flags to pass to clang)

// Note: Zig uses clang which ships with these so, unless another
// sysroot/libc, etc. headers location is pointed out, it is fine to
// hard-code enable this.
Expand Down Expand Up @@ -423,6 +423,15 @@ pub fn build(b: *std.Build) void {
// Define to use 'dladdr' function (used for debugging).
flags.append("-D HAVE_DLADDR") catch unreachable;

// Extra user-defined flags (if any) to pass to the compiler.
if (cflags_extra.len > 0) {
// Split it up on a space and append each part to flags separately.
var tokenizer = std.mem.tokenizeScalar(u8, cflags_extra, ' ');
while (tokenizer.next()) |token| {
flags.append(token) catch unreachable;
}
}

lib.addCSourceFiles(.{
.files = source_files.items,
.flags = flags.items,
Expand Down

0 comments on commit 70c9e87

Please sign in to comment.