authorgravatar for dominic.adragna@byteroach.comGalaxyShard <dominic.adragna@byteroach.com> 2025-01-30 13:04:47+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-01-30 14:04:47+01:00
loga3ee5215bba9119d8ab645b0cefee1d026816e13
tree81a4fae5059f1df3e1bdcf032d2d9a7e79274297
parent3348478fc3882fa3627147e54eeafa83ab7f4b09
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.Build: Add option to specify language of CSourceFiles

Closes: #20655

2 files changed, 60 insertions(+), 19 deletions(-)

lib/std/Build/Module.zig+33-1
......@@ -78,22 +78,51 @@ pub const SystemLib = struct {
7878 pub const SearchStrategy = enum { paths_first, mode_first, no_fallback };
7979};
8080
81pub const CSourceLanguage = enum {
82 c,
83 cpp,
84
85 objective_c,
86 objective_cpp,
87
88 /// Standard assembly
89 assembly,
90 /// Assembly with the C preprocessor
91 assembly_with_preprocessor,
92
93 pub fn internalIdentifier(self: CSourceLanguage) []const u8 {
94 return switch (self) {
95 .c => "c",
96 .cpp => "c++",
97 .objective_c => "objective-c",
98 .objective_cpp => "objective-c++",
99 .assembly => "assembler",
100 .assembly_with_preprocessor => "assembler-with-cpp",
101 };
102 }
103};
104
81105pub const CSourceFiles = struct {
82106 root: LazyPath,
83107 /// `files` is relative to `root`, which is
84108 /// the build root by default
85109 files: []const []const u8,
86110 flags: []const []const u8,
111 /// By default, determines language of each file individually based on its file extension
112 language: ?CSourceLanguage,
87113};
88114
89115pub const CSourceFile = struct {
90116 file: LazyPath,
91117 flags: []const []const u8 = &.{},
118 /// By default, determines language of each file individually based on its file extension
119 language: ?CSourceLanguage = null,
92120
93121 pub fn dupe(file: CSourceFile, b: *std.Build) CSourceFile {
94122 return .{
95123 .file = file.file.dupe(b),
96124 .flags = b.dupeStrings(file.flags),
125 .language = file.language,
97126 };
98127 }
99128};
......@@ -378,9 +407,11 @@ pub const AddCSourceFilesOptions = struct {
378407 root: ?LazyPath = null,
379408 files: []const []const u8,
380409 flags: []const []const u8 = &.{},
410 /// By default, determines language of each file individually based on its file extension
411 language: ?CSourceLanguage = null,
381412};
382413
383/// Handy when you have many C/C++ source files and want them all to have the same flags.
414/// Handy when you have many non-Zig source files and want them all to have the same flags.
384415pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
385416 const b = m.owner;
386417 const allocator = b.allocator;
......@@ -399,6 +430,7 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
399430 .root = options.root orelse b.path(""),
400431 .files = b.dupeStrings(options.files),
401432 .flags = b.dupeStrings(options.flags),
433 .language = options.language,
402434 };
403435 m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM");
404436}
lib/std/Build/Step/Compile.zig+27-18
......@@ -1259,40 +1259,44 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
12591259 .c_source_file => |c_source_file| l: {
12601260 if (!my_responsibility) break :l;
12611261
1262 if (c_source_file.flags.len == 0) {
1263 if (prev_has_cflags) {
1264 try zig_args.append("-cflags");
1265 try zig_args.append("--");
1266 prev_has_cflags = false;
1267 }
1268 } else {
1262 if (prev_has_cflags or c_source_file.flags.len != 0) {
12691263 try zig_args.append("-cflags");
12701264 for (c_source_file.flags) |arg| {
12711265 try zig_args.append(arg);
12721266 }
12731267 try zig_args.append("--");
1274 prev_has_cflags = true;
12751268 }
1269 prev_has_cflags = (c_source_file.flags.len != 0);
1270
1271 if (c_source_file.language) |lang| {
1272 try zig_args.append("-x");
1273 try zig_args.append(lang.internalIdentifier());
1274 }
1275
12761276 try zig_args.append(c_source_file.file.getPath2(mod.owner, step));
1277
1278 if (c_source_file.language != null) {
1279 try zig_args.append("-x");
1280 try zig_args.append("none");
1281 }
12771282 total_linker_objects += 1;
12781283 },
12791284
12801285 .c_source_files => |c_source_files| l: {
12811286 if (!my_responsibility) break :l;
12821287
1283 if (c_source_files.flags.len == 0) {
1284 if (prev_has_cflags) {
1285 try zig_args.append("-cflags");
1286 try zig_args.append("--");
1287 prev_has_cflags = false;
1288 }
1289 } else {
1288 if (prev_has_cflags or c_source_files.flags.len != 0) {
12901289 try zig_args.append("-cflags");
1291 for (c_source_files.flags) |flag| {
1292 try zig_args.append(flag);
1290 for (c_source_files.flags) |arg| {
1291 try zig_args.append(arg);
12931292 }
12941293 try zig_args.append("--");
1295 prev_has_cflags = true;
1294 }
1295 prev_has_cflags = (c_source_files.flags.len != 0);
1296
1297 if (c_source_files.language) |lang| {
1298 try zig_args.append("-x");
1299 try zig_args.append(lang.internalIdentifier());
12961300 }
12971301
12981302 const root_path = c_source_files.root.getPath2(mod.owner, step);
......@@ -1300,6 +1304,11 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
13001304 try zig_args.append(b.pathJoin(&.{ root_path, file }));
13011305 }
13021306
1307 if (c_source_files.language != null) {
1308 try zig_args.append("-x");
1309 try zig_args.append("none");
1310 }
1311
13031312 total_linker_objects += c_source_files.files.len;
13041313 },
13051314