Commit 7abf9b3a83

Krzysztof Wolicki <der.teufel.mail@gmail.com>
2023-10-10 20:29:26
Step.Compile: add options struct for `addCSourceFiles` (#17420)
Closes #17410
1 parent 2ca7cc4
Changed files (7)
lib
std
Build
test
link
common_symbols
common_symbols_alignment
macho
unwind_info
standalone
issue_11595
issue_12706
lib/std/Build/Step/Compile.zig
@@ -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,
 };
@@ -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,
     };
@@ -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));
+                    }
                 }
             },
 
test/link/common_symbols/build.zig
@@ -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" },
test/link/common_symbols_alignment/build.zig
@@ -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" },
test/link/macho/unwind_info/build.zig
@@ -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;
 }
test/standalone/issue_11595/build.zig
@@ -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;
test/standalone/issue_12706/build.zig
@@ -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);
build.zig
@@ -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);