diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index 02386de4308f..348385f7ff76 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -77,21 +77,66 @@ pub const SystemLib = struct { pub const SearchStrategy = enum { paths_first, mode_first, no_fallback }; }; +/// Supported languages for "zig clang -x ". +pub const CSourceLang = enum { + /// "c" + c, + /// "c-header" + h, + /// "c++" + cpp, + /// "c++-header" + hpp, + /// "objective-c" + m, + /// "objective-c-header" + hm, + /// "objective-c++" + mm, + /// "objective-c++-header" + hmm, + /// "assembler" + assembly, + /// "assembler-with-cpp" + assembly_with_cpp, + /// "cuda" + cu, + + pub fn getLangName(lang: @This()) []const u8 { + return switch (lang) { + .assembly => "assembler", + .assembly_with_cpp => "assembler-with-cpp", + .c => "c", + .cpp => "c++", + .h => "c-header", + .hpp => "c++-header", + .hm => "objective-c-header", + .hmm => "objective-c++-header", + .cu => "cuda", + .m => "objective-c", + .mm => "objective-c++", + }; + } +}; + pub const CSourceFiles = struct { root: LazyPath, /// `files` is relative to `root`, which is /// the build root by default files: []const []const u8, + lang: ?CSourceLang = null, flags: []const []const u8, }; pub const CSourceFile = struct { file: LazyPath, + lang: ?CSourceLang = null, flags: []const []const u8 = &.{}, pub fn dupe(self: CSourceFile, b: *std.Build) CSourceFile { return .{ .file = self.file.dupe(b), + .lang = self.lang, .flags = b.dupeStrings(self.flags), }; } @@ -453,6 +498,7 @@ pub const AddCSourceFilesOptions = struct { /// package that owns the `Compile` step. root: LazyPath = .{ .path = "" }, files: []const []const u8, + lang: ?CSourceLang = null, flags: []const []const u8 = &.{}, }; @@ -474,6 +520,7 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void { c_source_files.* = .{ .root = options.root, .files = b.dupeStrings(options.files), + .lang = options.lang, .flags = b.dupeStrings(options.flags), }; m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM"); diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 4a5364176aab..e451b09ac450 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -1273,6 +1273,12 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { try zig_args.append("--"); prev_has_cflags = true; } + + if (c_source_file.lang) |lang| { + try zig_args.append("-x"); + try zig_args.append(lang.getLangName()); + } + try zig_args.append(c_source_file.file.getPath2(module.owner, step)); total_linker_objects += 1; }, @@ -1295,6 +1301,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { prev_has_cflags = true; } + if (c_source_files.lang) |lang| { + try zig_args.append("-x"); + try zig_args.append(lang.getLangName()); + } + const root_path = c_source_files.root.getPath2(module.owner, step); for (c_source_files.files) |file| { try zig_args.append(b.pathJoin(&.{ root_path, file }));