mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
Step.Compile: add options struct for addCSourceFiles (#17420)
Closes #17410
This commit is contained in:
parent
2ca7cc46c4
commit
7abf9b3a83
@ -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);
|
||||
|
||||
@ -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,9 +1562,15 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
|
||||
try zig_args.append("--");
|
||||
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| {
|
||||
try zig_args.append(b.pathFromRoot(file));
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.win32_resource_file => |rc_source_file| {
|
||||
|
||||
@ -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" },
|
||||
|
||||
@ -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" },
|
||||
|
||||
@ -76,11 +76,13 @@ fn createScenario(
|
||||
});
|
||||
b.default_step.dependOn(&exe.step);
|
||||
exe.addIncludePath(.{ .path = "." });
|
||||
exe.addCSourceFiles(&[_][]const u8{
|
||||
exe.addCSourceFiles(.{
|
||||
.files = &[_][]const u8{
|
||||
"main.cpp",
|
||||
"simple_string.cpp",
|
||||
"simple_string_owner.cpp",
|
||||
}, &[0][]const u8{});
|
||||
},
|
||||
});
|
||||
exe.linkLibCpp();
|
||||
return exe;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user