Skip to content

Commit

Permalink
std.Build: add an option to CSourceFile to override the language dete…
Browse files Browse the repository at this point in the history
…ction.

It is normally based on the file extension, but it can be ambiguous.
Notably, ".h" is often used for c headers or c++ headers.
  • Loading branch information
xxxbxxx committed Apr 8, 2024
1 parent 304b8f8 commit ab5d1b4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/std/Build/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,66 @@ pub const SystemLib = struct {
pub const SearchStrategy = enum { paths_first, mode_first, no_fallback };
};

/// Supported languages for "zig clang -x <lang>".
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),
};
}
Expand Down Expand Up @@ -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 = &.{},
};

Expand All @@ -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");
Expand Down
11 changes: 11 additions & 0 deletions lib/std/Build/Step/Compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand All @@ -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 }));
Expand Down

0 comments on commit ab5d1b4

Please sign in to comment.