Skip to content

Commit 7aca101

Browse files
committed
better type safety for addCSourceFile(s)
1 parent 94b7ffb commit 7aca101

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

lib/std/Build/Module.zig

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,28 @@ pub const SystemLib = struct {
7878
pub const SearchStrategy = enum { paths_first, mode_first, no_fallback };
7979
};
8080

81-
pub const ForeignSourceLanguage = enum {
81+
pub const CSourceLanguage = enum {
8282
c,
8383
cpp,
84-
assembly,
85-
assembly_with_cpp,
86-
objc,
87-
objcpp,
8884

89-
find_by_file_extension,
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+
}
90103
};
91104

92105
pub const CSourceFiles = struct {
@@ -95,13 +108,15 @@ pub const CSourceFiles = struct {
95108
/// the build root by default
96109
files: []const []const u8,
97110
flags: []const []const u8,
98-
language: ForeignSourceLanguage,
111+
/// By default, determines language of each file individually based on its file extension
112+
language: ?CSourceLanguage,
99113
};
100114

101115
pub const CSourceFile = struct {
102116
file: LazyPath,
103117
flags: []const []const u8 = &.{},
104-
language: ForeignSourceLanguage = .find_by_file_extension,
118+
/// By default, determines language of each file individually based on its file extension
119+
language: ?CSourceLanguage = null,
105120

106121
pub fn dupe(file: CSourceFile, b: *std.Build) CSourceFile {
107122
return .{
@@ -392,7 +407,8 @@ pub const AddCSourceFilesOptions = struct {
392407
root: ?LazyPath = null,
393408
files: []const []const u8,
394409
flags: []const []const u8 = &.{},
395-
language: ForeignSourceLanguage = .find_by_file_extension,
410+
/// By default, determines language of each file individually based on its file extension
411+
language: ?CSourceLanguage = null,
396412
};
397413

398414
/// Handy when you have many non-Zig source files and want them all to have the same flags.

lib/std/Build/Step/Compile.zig

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,22 +1254,14 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
12541254
}
12551255
prev_has_cflags = (c_source_file.flags.len != 0);
12561256

1257-
if (c_source_file.language != .find_by_file_extension) {
1257+
if (c_source_file.language) |lang| {
12581258
try zig_args.append("-x");
1259-
try zig_args.append(switch (c_source_file.language) {
1260-
.find_by_file_extension => unreachable,
1261-
.c => "c",
1262-
.cpp => "c++",
1263-
.assembly => "assembler",
1264-
.assembly_with_cpp => "assembler-with-cpp",
1265-
.objc => "objective-c",
1266-
.objcpp => "objective-c++",
1267-
});
1259+
try zig_args.append(lang.internalIdentifier());
12681260
}
12691261

12701262
try zig_args.append(c_source_file.file.getPath2(mod.owner, step));
12711263

1272-
if (c_source_file.language != .find_by_file_extension) {
1264+
if (c_source_file.language != null) {
12731265
try zig_args.append("-x");
12741266
try zig_args.append("none");
12751267
}
@@ -1288,25 +1280,17 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
12881280
}
12891281
prev_has_cflags = (c_source_files.flags.len != 0);
12901282

1291-
if (c_source_files.language != .find_by_file_extension) {
1283+
if (c_source_files.language) |lang| {
12921284
try zig_args.append("-x");
1293-
try zig_args.append(switch (c_source_files.language) {
1294-
.find_by_file_extension => unreachable,
1295-
.c => "c",
1296-
.cpp => "c++",
1297-
.assembly => "assembler",
1298-
.assembly_with_cpp => "assembler-with-cpp",
1299-
.objc => "objective-c",
1300-
.objcpp => "objective-c++",
1301-
});
1285+
try zig_args.append(lang.internalIdentifier());
13021286
}
13031287

13041288
const root_path = c_source_files.root.getPath2(mod.owner, step);
13051289
for (c_source_files.files) |file| {
13061290
try zig_args.append(b.pathJoin(&.{ root_path, file }));
13071291
}
13081292

1309-
if (c_source_files.language != .find_by_file_extension) {
1293+
if (c_source_files.language != null) {
13101294
try zig_args.append("-x");
13111295
try zig_args.append("none");
13121296
}

0 commit comments

Comments
 (0)