Skip to content

Commit a3ee521

Browse files
authored
std.Build: Add option to specify language of CSourceFiles
Closes: #20655
1 parent 3348478 commit a3ee521

File tree

2 files changed

+60
-19
lines changed

2 files changed

+60
-19
lines changed

lib/std/Build/Module.zig

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,51 @@ pub const SystemLib = struct {
7878
pub const SearchStrategy = enum { paths_first, mode_first, no_fallback };
7979
};
8080

81+
pub 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+
81105
pub 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

89115
pub 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.
384415
pub 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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,47 +1259,56 @@ 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);
12991303
for (c_source_files.files) |file| {
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

0 commit comments

Comments
 (0)