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 {...@@ -693,7 +693,10 @@ fn addStaticLlvmOptionsToExe(exe: *std.Build.Step.Compile) !void {
693 // in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is693 // in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is
694 // unavailable when LLVM is compiled in Release mode.694 // unavailable when LLVM is compiled in Release mode.
695 const zig_cpp_cflags = exe_cflags ++ [_][]const u8{"-DNDEBUG=1"};695 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
698 for (clang_libs) |lib_name| {701 for (clang_libs) |lib_name| {
699 exe.linkSystemLibrary(lib_name);702 exe.linkSystemLibrary(lib_name);
lib/std/Build/Step/Compile.zig+22-6
...@@ -216,7 +216,9 @@ generated_llvm_ir: ?*GeneratedFile,...@@ -216,7 +216,9 @@ generated_llvm_ir: ?*GeneratedFile,
216generated_h: ?*GeneratedFile,216generated_h: ?*GeneratedFile,
217217
218pub const CSourceFiles = struct {218pub 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.
220 files: []const []const u8,222 files: []const []const u8,
221 flags: []const []const u8,223 flags: []const []const u8,
222};224};
...@@ -924,15 +926,23 @@ pub fn linkSystemLibrary2(...@@ -924,15 +926,23 @@ pub fn linkSystemLibrary2(
924 }) catch @panic("OOM");926 }) catch @panic("OOM");
925}927}
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
927/// Handy when you have many C/C++ source files and want them all to have the same flags.936/// 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 {
929 const b = self.step.owner;938 const b = self.step.owner;
930 const c_source_files = b.allocator.create(CSourceFiles) catch @panic("OOM");939 const c_source_files = b.allocator.create(CSourceFiles) catch @panic("OOM");
931940
932 const files_copy = b.dupeStrings(files);941 const files_copy = b.dupeStrings(options.files);
933 const flags_copy = b.dupeStrings(flags);942 const flags_copy = b.dupeStrings(options.flags);
934943
935 c_source_files.* = .{944 c_source_files.* = .{
945 .dependency = options.dependency,
936 .files = files_copy,946 .files = files_copy,
937 .flags = flags_copy,947 .flags = flags_copy,
938 };948 };
...@@ -1552,8 +1562,14 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -1552,8 +1562,14 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
1552 try zig_args.append("--");1562 try zig_args.append("--");
1553 prev_has_cflags = true;1563 prev_has_cflags = true;
1554 }1564 }
1555 for (c_source_files.files) |file| {1565 if (c_source_files.dependency) |dep| {
1556 try zig_args.append(b.pathFromRoot(file));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 }
1557 }1573 }
1558 },1574 },
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...@@ -16,7 +16,10 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
16 .optimize = optimize,16 .optimize = optimize,
17 .target = .{},17 .target = .{},
18 });18 });
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
21 const test_exe = b.addTest(.{24 const test_exe = b.addTest(.{
22 .root_source_file = .{ .path = "main.zig" },25 .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...@@ -16,7 +16,10 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
16 .optimize = optimize,16 .optimize = optimize,
17 .target = .{},17 .target = .{},
18 });18 });
19 lib_a.addCSourceFiles(&.{"a.c"}, &.{"-fcommon"});19 lib_a.addCSourceFiles(.{
20 .files = &.{"a.c"},
21 .flags = &.{"-fcommon"},
22 });
2023
21 const test_exe = b.addTest(.{24 const test_exe = b.addTest(.{
22 .root_source_file = .{ .path = "main.zig" },25 .root_source_file = .{ .path = "main.zig" },
test/link/macho/unwind_info/build.zig+7-5
...@@ -76,11 +76,13 @@ fn createScenario(...@@ -76,11 +76,13 @@ fn createScenario(
76 });76 });
77 b.default_step.dependOn(&exe.step);77 b.default_step.dependOn(&exe.step);
78 exe.addIncludePath(.{ .path = "." });78 exe.addIncludePath(.{ .path = "." });
79 exe.addCSourceFiles(&[_][]const u8{79 exe.addCSourceFiles(.{
80 "main.cpp",80 .files = &[_][]const u8{
81 "simple_string.cpp",81 "main.cpp",
82 "simple_string_owner.cpp",82 "simple_string.cpp",
83 }, &[0][]const u8{});83 "simple_string_owner.cpp",
84 },
85 });
84 exe.linkLibCpp();86 exe.linkLibCpp();
85 return exe;87 return exe;
86}88}
test/standalone/issue_11595/build.zig+1-1
...@@ -25,7 +25,7 @@ pub fn build(b: *std.Build) void {...@@ -25,7 +25,7 @@ pub fn build(b: *std.Build) void {
25 "test.c",25 "test.c",
26 };26 };
2727
28 exe.addCSourceFiles(&c_sources, &.{});28 exe.addCSourceFiles(.{ .files = &c_sources });
29 exe.linkLibC();29 exe.linkLibC();
3030
31 var i: i32 = 0;31 var i: i32 = 0;
test/standalone/issue_12706/build.zig+1-1
...@@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void {...@@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void {
19 const c_sources = [_][]const u8{19 const c_sources = [_][]const u8{
20 "test.c",20 "test.c",
21 };21 };
22 exe.addCSourceFiles(&c_sources, &.{});22 exe.addCSourceFiles(.{ .files = &c_sources });
23 exe.linkLibC();23 exe.linkLibC();
2424
25 const run_cmd = b.addRunArtifact(exe);25 const run_cmd = b.addRunArtifact(exe);