Skip to content

Commit a3e4718

Browse files
committed
std.Build: add an option to CSourceFile to override the language detection.
It is normally based on the file extension, but it can be ambiguous. Notably, ".h" is often used for c or c++.
1 parent d639b79 commit a3e4718

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

lib/std/Build/Module.zig

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

81+
/// Supported languages for "zig clang -x <lang>".
82+
pub const CSourceLang = enum {
83+
/// "c"
84+
c,
85+
/// "c-header"
86+
h,
87+
/// "c++"
88+
cpp,
89+
/// "c++-header"
90+
hpp,
91+
/// "objective-c"
92+
m,
93+
/// "objective-c-header"
94+
hm,
95+
/// "objective-c++"
96+
mm,
97+
/// "objective-c++-header"
98+
hmm,
99+
/// "assembler"
100+
assembly,
101+
/// "assembler-with-cpp"
102+
assembly_with_cpp,
103+
/// "cuda"
104+
cu,
105+
106+
pub fn getLangName(lang: @This()) []const u8 {
107+
return switch (lang) {
108+
.assembly => "assembler",
109+
.assembly_with_cpp => "assembler-with-cpp",
110+
.c => "c",
111+
.cpp => "c++",
112+
.h => "c-header",
113+
.hpp => "c++-header",
114+
.hm => "objective-c-header",
115+
.hmm => "objective-c++-header",
116+
.cu => "cuda",
117+
.m => "objective-c",
118+
.mm => "objective-c++",
119+
};
120+
}
121+
};
122+
81123
pub const CSourceFiles = struct {
82124
dependency: ?*std.Build.Dependency,
83125
/// If `dependency` is not null relative to it,
84126
/// else relative to the build root.
85127
files: []const []const u8,
128+
lang: ?CSourceLang = null,
86129
flags: []const []const u8,
87130
};
88131

89132
pub const CSourceFile = struct {
90133
file: LazyPath,
134+
/// `lang` optionally overrides the language detection.
135+
lang: ?CSourceLang = null,
91136
flags: []const []const u8 = &.{},
92137

93138
pub fn dupe(self: CSourceFile, b: *std.Build) CSourceFile {
94139
return .{
95140
.file = self.file.dupe(b),
141+
.lang = self.lang,
96142
.flags = b.dupeStrings(self.flags),
97143
};
98144
}
@@ -457,6 +503,7 @@ pub const AddCSourceFilesOptions = struct {
457503
/// package that owns the `Compile` step.
458504
dependency: ?*std.Build.Dependency = null,
459505
files: []const []const u8,
506+
lang: ?CSourceLang = null,
460507
flags: []const []const u8 = &.{},
461508
};
462509

@@ -468,6 +515,7 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
468515
c_source_files.* = .{
469516
.dependency = options.dependency,
470517
.files = b.dupeStrings(options.files),
518+
.lang = options.lang,
471519
.flags = b.dupeStrings(options.flags),
472520
};
473521
m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM");

lib/std/Build/Step/Compile.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,12 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
11431143
try zig_args.append("--");
11441144
prev_has_cflags = true;
11451145
}
1146+
1147+
if (c_source_file.lang) |lang| {
1148+
try zig_args.append("-x");
1149+
try zig_args.append(lang.getLangName());
1150+
}
1151+
11461152
try zig_args.append(c_source_file.file.getPath2(module.owner, step));
11471153
total_linker_objects += 1;
11481154
},
@@ -1165,6 +1171,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
11651171
prev_has_cflags = true;
11661172
}
11671173

1174+
if (c_source_files.lang) |lang| {
1175+
try zig_args.append("-x");
1176+
try zig_args.append(lang.getLangName());
1177+
}
1178+
11681179
if (c_source_files.dependency) |dep| {
11691180
for (c_source_files.files) |file| {
11701181
try zig_args.append(dep.builder.pathFromRoot(file));

0 commit comments

Comments
 (0)