Skip to content

Commit

Permalink
Step.Compile: add options struct for addCSourceFiles (#17420)
Browse files Browse the repository at this point in the history
Closes #17410
  • Loading branch information
der-teufel-programming authored Oct 10, 2023
1 parent 2ca7cc4 commit 7abf9b3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 16 deletions.
5 changes: 4 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,10 @@ fn addStaticLlvmOptionsToExe(exe: *std.Build.Step.Compile) !void {
// in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is
// unavailable when LLVM is compiled in Release mode.
const zig_cpp_cflags = exe_cflags ++ [_][]const u8{"-DNDEBUG=1"};
exe.addCSourceFiles(&zig_cpp_sources, &zig_cpp_cflags);
exe.addCSourceFiles(.{
.files = &zig_cpp_sources,
.flags = &zig_cpp_cflags,
});

for (clang_libs) |lib_name| {
exe.linkSystemLibrary(lib_name);
Expand Down
28 changes: 22 additions & 6 deletions lib/std/Build/Step/Compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ generated_llvm_ir: ?*GeneratedFile,
generated_h: ?*GeneratedFile,

pub const CSourceFiles = struct {
/// Relative to the build root.
dependency: ?*std.Build.Dependency,
/// If `dependency` is not null relative to it,
/// else relative to the build root.
files: []const []const u8,
flags: []const []const u8,
};
Expand Down Expand Up @@ -924,15 +926,23 @@ pub fn linkSystemLibrary2(
}) catch @panic("OOM");
}

pub const AddCSourceFilesOptions = struct {
/// When provided, `files` are relative to `dependency` rather than the package that owns the `Compile` step.
dependency: ?*std.Build.Dependency = null,
files: []const []const u8,
flags: []const []const u8 = &.{},
};

/// Handy when you have many C/C++ source files and want them all to have the same flags.
pub fn addCSourceFiles(self: *Compile, files: []const []const u8, flags: []const []const u8) void {
pub fn addCSourceFiles(self: *Compile, options: AddCSourceFilesOptions) void {
const b = self.step.owner;
const c_source_files = b.allocator.create(CSourceFiles) catch @panic("OOM");

const files_copy = b.dupeStrings(files);
const flags_copy = b.dupeStrings(flags);
const files_copy = b.dupeStrings(options.files);
const flags_copy = b.dupeStrings(options.flags);

c_source_files.* = .{
.dependency = options.dependency,
.files = files_copy,
.flags = flags_copy,
};
Expand Down Expand Up @@ -1552,8 +1562,14 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
try zig_args.append("--");
prev_has_cflags = true;
}
for (c_source_files.files) |file| {
try zig_args.append(b.pathFromRoot(file));
if (c_source_files.dependency) |dep| {
for (c_source_files.files) |file| {
try zig_args.append(dep.builder.pathFromRoot(file));
}
} else {
for (c_source_files.files) |file| {
try zig_args.append(b.pathFromRoot(file));
}
}
},

Expand Down
5 changes: 4 additions & 1 deletion test/link/common_symbols/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
.optimize = optimize,
.target = .{},
});
lib_a.addCSourceFiles(&.{ "c.c", "a.c", "b.c" }, &.{"-fcommon"});
lib_a.addCSourceFiles(.{
.files = &.{ "c.c", "a.c", "b.c" },
.flags = &.{"-fcommon"},
});

const test_exe = b.addTest(.{
.root_source_file = .{ .path = "main.zig" },
Expand Down
5 changes: 4 additions & 1 deletion test/link/common_symbols_alignment/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
.optimize = optimize,
.target = .{},
});
lib_a.addCSourceFiles(&.{"a.c"}, &.{"-fcommon"});
lib_a.addCSourceFiles(.{
.files = &.{"a.c"},
.flags = &.{"-fcommon"},
});

const test_exe = b.addTest(.{
.root_source_file = .{ .path = "main.zig" },
Expand Down
12 changes: 7 additions & 5 deletions test/link/macho/unwind_info/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ fn createScenario(
});
b.default_step.dependOn(&exe.step);
exe.addIncludePath(.{ .path = "." });
exe.addCSourceFiles(&[_][]const u8{
"main.cpp",
"simple_string.cpp",
"simple_string_owner.cpp",
}, &[0][]const u8{});
exe.addCSourceFiles(.{
.files = &[_][]const u8{
"main.cpp",
"simple_string.cpp",
"simple_string_owner.cpp",
},
});
exe.linkLibCpp();
return exe;
}
2 changes: 1 addition & 1 deletion test/standalone/issue_11595/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn build(b: *std.Build) void {
"test.c",
};

exe.addCSourceFiles(&c_sources, &.{});
exe.addCSourceFiles(.{ .files = &c_sources });
exe.linkLibC();

var i: i32 = 0;
Expand Down
2 changes: 1 addition & 1 deletion test/standalone/issue_12706/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void {
const c_sources = [_][]const u8{
"test.c",
};
exe.addCSourceFiles(&c_sources, &.{});
exe.addCSourceFiles(.{ .files = &c_sources });
exe.linkLibC();

const run_cmd = b.addRunArtifact(exe);
Expand Down

0 comments on commit 7abf9b3

Please sign in to comment.