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 {...@@ -78,22 +78,51 @@ pub const SystemLib = struct {
78 pub const SearchStrategy = enum { paths_first, mode_first, no_fallback };78 pub const SearchStrategy = enum { paths_first, mode_first, no_fallback };
79};79};
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
81pub const CSourceFiles = struct {105pub const CSourceFiles = struct {
82 root: LazyPath,106 root: LazyPath,
83 /// `files` is relative to `root`, which is107 /// `files` is relative to `root`, which is
84 /// the build root by default108 /// the build root by default
85 files: []const []const u8,109 files: []const []const u8,
86 flags: []const []const u8,110 flags: []const []const u8,
111 /// By default, determines language of each file individually based on its file extension
112 language: ?CSourceLanguage,
87};113};
88114
89pub const CSourceFile = struct {115pub const CSourceFile = struct {
90 file: LazyPath,116 file: LazyPath,
91 flags: []const []const u8 = &.{},117 flags: []const []const u8 = &.{},
118 /// By default, determines language of each file individually based on its file extension
119 language: ?CSourceLanguage = null,
92120
93 pub fn dupe(file: CSourceFile, b: *std.Build) CSourceFile {121 pub fn dupe(file: CSourceFile, b: *std.Build) CSourceFile {
94 return .{122 return .{
95 .file = file.file.dupe(b),123 .file = file.file.dupe(b),
96 .flags = b.dupeStrings(file.flags),124 .flags = b.dupeStrings(file.flags),
125 .language = file.language,
97 };126 };
98 }127 }
99};128};
...@@ -378,9 +407,11 @@ pub const AddCSourceFilesOptions = struct {...@@ -378,9 +407,11 @@ pub const AddCSourceFilesOptions = struct {
378 root: ?LazyPath = null,407 root: ?LazyPath = null,
379 files: []const []const u8,408 files: []const []const u8,
380 flags: []const []const u8 = &.{},409 flags: []const []const u8 = &.{},
410 /// By default, determines language of each file individually based on its file extension
411 language: ?CSourceLanguage = null,
381};412};
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.
384pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {415pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
385 const b = m.owner;416 const b = m.owner;
386 const allocator = b.allocator;417 const allocator = b.allocator;
...@@ -399,6 +430,7 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {...@@ -399,6 +430,7 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
399 .root = options.root orelse b.path(""),430 .root = options.root orelse b.path(""),
400 .files = b.dupeStrings(options.files),431 .files = b.dupeStrings(options.files),
401 .flags = b.dupeStrings(options.flags),432 .flags = b.dupeStrings(options.flags),
433 .language = options.language,
402 };434 };
403 m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM");435 m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM");
404}436}
lib/std/Build/Step/Compile.zig+27-18
...@@ -1259,40 +1259,44 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {...@@ -1259,40 +1259,44 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
1259 .c_source_file => |c_source_file| l: {1259 .c_source_file => |c_source_file| l: {
1260 if (!my_responsibility) break :l;1260 if (!my_responsibility) break :l;
12611261
1262 if (c_source_file.flags.len == 0) {1262 if (prev_has_cflags or 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 {
1269 try zig_args.append("-cflags");1263 try zig_args.append("-cflags");
1270 for (c_source_file.flags) |arg| {1264 for (c_source_file.flags) |arg| {
1271 try zig_args.append(arg);1265 try zig_args.append(arg);
1272 }1266 }
1273 try zig_args.append("--");1267 try zig_args.append("--");
1274 prev_has_cflags = true;
1275 }1268 }
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
1276 try zig_args.append(c_source_file.file.getPath2(mod.owner, step));1276 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 }
1277 total_linker_objects += 1;1282 total_linker_objects += 1;
1278 },1283 },
12791284
1280 .c_source_files => |c_source_files| l: {1285 .c_source_files => |c_source_files| l: {
1281 if (!my_responsibility) break :l;1286 if (!my_responsibility) break :l;
12821287
1283 if (c_source_files.flags.len == 0) {1288 if (prev_has_cflags or 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 {
1290 try zig_args.append("-cflags");1289 try zig_args.append("-cflags");
1291 for (c_source_files.flags) |flag| {1290 for (c_source_files.flags) |arg| {
1292 try zig_args.append(flag);1291 try zig_args.append(arg);
1293 }1292 }
1294 try zig_args.append("--");1293 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());
1296 }1300 }
12971301
1298 const root_path = c_source_files.root.getPath2(mod.owner, step);1302 const root_path = c_source_files.root.getPath2(mod.owner, step);
...@@ -1300,6 +1304,11 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {...@@ -1300,6 +1304,11 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
1300 try zig_args.append(b.pathJoin(&.{ root_path, file }));1304 try zig_args.append(b.pathJoin(&.{ root_path, file }));
1301 }1305 }
13021306
1307 if (c_source_files.language != null) {
1308 try zig_args.append("-x");
1309 try zig_args.append("none");
1310 }
1311
1303 total_linker_objects += c_source_files.files.len;1312 total_linker_objects += c_source_files.files.len;
1304 },1313 },
13051314