authorgravatar for der.teufel.mail@gmail.comKrzysztof Wolicki <der.teufel.mail@gmail.com> 2023-10-10 20:29:26+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-10-10 14:29:26-04:00
log7abf9b3a83b3d37bbeeac7dc2df238c3b94aa148
treed48ec6e73ae9b09b08b737f4deecf6deed68773e
parent2ca7cc46c41dc5fe91fe385df4f3330634bd88f3
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Step.Compile: add options struct for `addCSourceFiles` (#17420)

Closes #17410

7 files changed, 43 insertions(+), 16 deletions(-)

build.zig+4-1
......@@ -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+22-6
......@@ -216,7 +216,9 @@ generated_llvm_ir: ?*GeneratedFile,
216216generated_h: ?*GeneratedFile,
217217
218218pub 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
929pub 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.
928pub fn addCSourceFiles(self: *Compile, files: []const []const u8, flags: []const []const u8) void {
937pub 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+4-1
......@@ -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+4-1
......@@ -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+7-5
......@@ -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+1-1
......@@ -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+1-1
......@@ -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);