Skip to content

Commit 7abf9b3

Browse files
Step.Compile: add options struct for addCSourceFiles (#17420)
Closes #17410
1 parent 2ca7cc4 commit 7abf9b3

File tree

7 files changed

+43
-16
lines changed

7 files changed

+43
-16
lines changed

build.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,10 @@ fn addStaticLlvmOptionsToExe(exe: *std.Build.Step.Compile) !void {
693693
// in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is
694694
// unavailable when LLVM is compiled in Release mode.
695695
const zig_cpp_cflags = exe_cflags ++ [_][]const u8{"-DNDEBUG=1"};
696-
exe.addCSourceFiles(&zig_cpp_sources, &zig_cpp_cflags);
696+
exe.addCSourceFiles(.{
697+
.files = &zig_cpp_sources,
698+
.flags = &zig_cpp_cflags,
699+
});
697700

698701
for (clang_libs) |lib_name| {
699702
exe.linkSystemLibrary(lib_name);

lib/std/Build/Step/Compile.zig

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ generated_llvm_ir: ?*GeneratedFile,
216216
generated_h: ?*GeneratedFile,
217217

218218
pub const CSourceFiles = struct {
219-
/// Relative to the build root.
219+
dependency: ?*std.Build.Dependency,
220+
/// If `dependency` is not null relative to it,
221+
/// else relative to the build root.
220222
files: []const []const u8,
221223
flags: []const []const u8,
222224
};
@@ -924,15 +926,23 @@ pub fn linkSystemLibrary2(
924926
}) catch @panic("OOM");
925927
}
926928

929+
pub const AddCSourceFilesOptions = struct {
930+
/// When provided, `files` are relative to `dependency` rather than the package that owns the `Compile` step.
931+
dependency: ?*std.Build.Dependency = null,
932+
files: []const []const u8,
933+
flags: []const []const u8 = &.{},
934+
};
935+
927936
/// Handy when you have many C/C++ source files and want them all to have the same flags.
928-
pub fn addCSourceFiles(self: *Compile, files: []const []const u8, flags: []const []const u8) void {
937+
pub fn addCSourceFiles(self: *Compile, options: AddCSourceFilesOptions) void {
929938
const b = self.step.owner;
930939
const c_source_files = b.allocator.create(CSourceFiles) catch @panic("OOM");
931940

932-
const files_copy = b.dupeStrings(files);
933-
const flags_copy = b.dupeStrings(flags);
941+
const files_copy = b.dupeStrings(options.files);
942+
const flags_copy = b.dupeStrings(options.flags);
934943

935944
c_source_files.* = .{
945+
.dependency = options.dependency,
936946
.files = files_copy,
937947
.flags = flags_copy,
938948
};
@@ -1552,8 +1562,14 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
15521562
try zig_args.append("--");
15531563
prev_has_cflags = true;
15541564
}
1555-
for (c_source_files.files) |file| {
1556-
try zig_args.append(b.pathFromRoot(file));
1565+
if (c_source_files.dependency) |dep| {
1566+
for (c_source_files.files) |file| {
1567+
try zig_args.append(dep.builder.pathFromRoot(file));
1568+
}
1569+
} else {
1570+
for (c_source_files.files) |file| {
1571+
try zig_args.append(b.pathFromRoot(file));
1572+
}
15571573
}
15581574
},
15591575

test/link/common_symbols/build.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1616
.optimize = optimize,
1717
.target = .{},
1818
});
19-
lib_a.addCSourceFiles(&.{ "c.c", "a.c", "b.c" }, &.{"-fcommon"});
19+
lib_a.addCSourceFiles(.{
20+
.files = &.{ "c.c", "a.c", "b.c" },
21+
.flags = &.{"-fcommon"},
22+
});
2023

2124
const test_exe = b.addTest(.{
2225
.root_source_file = .{ .path = "main.zig" },

test/link/common_symbols_alignment/build.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
1616
.optimize = optimize,
1717
.target = .{},
1818
});
19-
lib_a.addCSourceFiles(&.{"a.c"}, &.{"-fcommon"});
19+
lib_a.addCSourceFiles(.{
20+
.files = &.{"a.c"},
21+
.flags = &.{"-fcommon"},
22+
});
2023

2124
const test_exe = b.addTest(.{
2225
.root_source_file = .{ .path = "main.zig" },

test/link/macho/unwind_info/build.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ fn createScenario(
7676
});
7777
b.default_step.dependOn(&exe.step);
7878
exe.addIncludePath(.{ .path = "." });
79-
exe.addCSourceFiles(&[_][]const u8{
80-
"main.cpp",
81-
"simple_string.cpp",
82-
"simple_string_owner.cpp",
83-
}, &[0][]const u8{});
79+
exe.addCSourceFiles(.{
80+
.files = &[_][]const u8{
81+
"main.cpp",
82+
"simple_string.cpp",
83+
"simple_string_owner.cpp",
84+
},
85+
});
8486
exe.linkLibCpp();
8587
return exe;
8688
}

test/standalone/issue_11595/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn build(b: *std.Build) void {
2525
"test.c",
2626
};
2727

28-
exe.addCSourceFiles(&c_sources, &.{});
28+
exe.addCSourceFiles(.{ .files = &c_sources });
2929
exe.linkLibC();
3030

3131
var i: i32 = 0;

test/standalone/issue_12706/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void {
1919
const c_sources = [_][]const u8{
2020
"test.c",
2121
};
22-
exe.addCSourceFiles(&c_sources, &.{});
22+
exe.addCSourceFiles(.{ .files = &c_sources });
2323
exe.linkLibC();
2424

2525
const run_cmd = b.addRunArtifact(exe);

0 commit comments

Comments
 (0)