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

Closes #17410
This commit is contained in:
Krzysztof Wolicki 2023-10-10 20:29:26 +02:00 committed by GitHub
parent 2ca7cc46c4
commit 7abf9b3a83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 16 deletions

View File

@ -693,7 +693,10 @@ fn addStaticLlvmOptionsToExe(exe: *std.Build.Step.Compile) !void {
// in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is // in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is
// unavailable when LLVM is compiled in Release mode. // unavailable when LLVM is compiled in Release mode.
const zig_cpp_cflags = exe_cflags ++ [_][]const u8{"-DNDEBUG=1"}; 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| { for (clang_libs) |lib_name| {
exe.linkSystemLibrary(lib_name); exe.linkSystemLibrary(lib_name);

View File

@ -216,7 +216,9 @@ generated_llvm_ir: ?*GeneratedFile,
generated_h: ?*GeneratedFile, generated_h: ?*GeneratedFile,
pub const CSourceFiles = struct { 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, files: []const []const u8,
flags: []const []const u8, flags: []const []const u8,
}; };
@ -924,15 +926,23 @@ pub fn linkSystemLibrary2(
}) catch @panic("OOM"); }) 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. /// 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 b = self.step.owner;
const c_source_files = b.allocator.create(CSourceFiles) catch @panic("OOM"); const c_source_files = b.allocator.create(CSourceFiles) catch @panic("OOM");
const files_copy = b.dupeStrings(files); const files_copy = b.dupeStrings(options.files);
const flags_copy = b.dupeStrings(flags); const flags_copy = b.dupeStrings(options.flags);
c_source_files.* = .{ c_source_files.* = .{
.dependency = options.dependency,
.files = files_copy, .files = files_copy,
.flags = flags_copy, .flags = flags_copy,
}; };
@ -1552,9 +1562,15 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
try zig_args.append("--"); try zig_args.append("--");
prev_has_cflags = true; prev_has_cflags = true;
} }
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| { for (c_source_files.files) |file| {
try zig_args.append(b.pathFromRoot(file)); try zig_args.append(b.pathFromRoot(file));
} }
}
}, },
.win32_resource_file => |rc_source_file| { .win32_resource_file => |rc_source_file| {

View File

@ -16,7 +16,10 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
.optimize = optimize, .optimize = optimize,
.target = .{}, .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(.{ const test_exe = b.addTest(.{
.root_source_file = .{ .path = "main.zig" }, .root_source_file = .{ .path = "main.zig" },

View File

@ -16,7 +16,10 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
.optimize = optimize, .optimize = optimize,
.target = .{}, .target = .{},
}); });
lib_a.addCSourceFiles(&.{"a.c"}, &.{"-fcommon"}); lib_a.addCSourceFiles(.{
.files = &.{"a.c"},
.flags = &.{"-fcommon"},
});
const test_exe = b.addTest(.{ const test_exe = b.addTest(.{
.root_source_file = .{ .path = "main.zig" }, .root_source_file = .{ .path = "main.zig" },

View File

@ -76,11 +76,13 @@ fn createScenario(
}); });
b.default_step.dependOn(&exe.step); b.default_step.dependOn(&exe.step);
exe.addIncludePath(.{ .path = "." }); exe.addIncludePath(.{ .path = "." });
exe.addCSourceFiles(&[_][]const u8{ exe.addCSourceFiles(.{
.files = &[_][]const u8{
"main.cpp", "main.cpp",
"simple_string.cpp", "simple_string.cpp",
"simple_string_owner.cpp", "simple_string_owner.cpp",
}, &[0][]const u8{}); },
});
exe.linkLibCpp(); exe.linkLibCpp();
return exe; return exe;
} }

View File

@ -25,7 +25,7 @@ pub fn build(b: *std.Build) void {
"test.c", "test.c",
}; };
exe.addCSourceFiles(&c_sources, &.{}); exe.addCSourceFiles(.{ .files = &c_sources });
exe.linkLibC(); exe.linkLibC();
var i: i32 = 0; var i: i32 = 0;

View File

@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void {
const c_sources = [_][]const u8{ const c_sources = [_][]const u8{
"test.c", "test.c",
}; };
exe.addCSourceFiles(&c_sources, &.{}); exe.addCSourceFiles(.{ .files = &c_sources });
exe.linkLibC(); exe.linkLibC();
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);