Skip to content

Commit 4b6eb4d

Browse files
committed
Add option to specify language of source files
1 parent c157550 commit 4b6eb4d

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

lib/std/Build/Module.zig

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

81+
pub const ForeignSourceLanguage = enum {
82+
c,
83+
cpp,
84+
assembly,
85+
assembly_with_cpp,
86+
objc,
87+
objcpp,
88+
89+
find_by_file_extension,
90+
};
91+
8192
pub const CSourceFiles = struct {
8293
root: LazyPath,
8394
/// `files` is relative to `root`, which is
8495
/// the build root by default
8596
files: []const []const u8,
8697
flags: []const []const u8,
98+
language: ForeignSourceLanguage,
8799
};
88100

89101
pub const CSourceFile = struct {
90102
file: LazyPath,
91103
flags: []const []const u8 = &.{},
104+
language: ForeignSourceLanguage = .find_by_file_extension,
92105

93106
pub fn dupe(file: CSourceFile, b: *std.Build) CSourceFile {
94107
return .{
95108
.file = file.file.dupe(b),
96109
.flags = b.dupeStrings(file.flags),
110+
.language = file.language,
97111
};
98112
}
99113
};
@@ -486,9 +500,10 @@ pub const AddCSourceFilesOptions = struct {
486500
root: ?LazyPath = null,
487501
files: []const []const u8,
488502
flags: []const []const u8 = &.{},
503+
language: ForeignSourceLanguage = .find_by_file_extension,
489504
};
490505

491-
/// Handy when you have many C/C++ source files and want them all to have the same flags.
506+
/// Handy when you have many non-Zig source files and want them all to have the same flags.
492507
pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
493508
const b = m.owner;
494509
const allocator = b.allocator;
@@ -507,6 +522,7 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
507522
.root = options.root orelse b.path(""),
508523
.files = b.dupeStrings(options.files),
509524
.flags = b.dupeStrings(options.flags),
525+
.language = options.language,
510526
};
511527
m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM");
512528
addLazyPathDependenciesOnly(m, c_source_files.root);

lib/std/Build/Step/Compile.zig

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ pub fn linkFrameworkWeak(c: *Compile, name: []const u8) void {
793793
c.root_module.linkFramework(name, .{ .weak = true });
794794
}
795795

796-
/// Handy when you have many C/C++ source files and want them all to have the same flags.
796+
/// Handy when you have many non-Zig source files and want them all to have the same flags.
797797
pub fn addCSourceFiles(compile: *Compile, options: Module.AddCSourceFilesOptions) void {
798798
compile.root_module.addCSourceFiles(options);
799799
}
@@ -1243,47 +1243,72 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
12431243
.c_source_file => |c_source_file| l: {
12441244
if (!my_responsibility) break :l;
12451245

1246-
if (c_source_file.flags.len == 0) {
1247-
if (prev_has_cflags) {
1248-
try zig_args.append("-cflags");
1249-
try zig_args.append("--");
1250-
prev_has_cflags = false;
1251-
}
1252-
} else {
1246+
if (prev_has_cflags or c_source_file.flags.len != 0) {
12531247
try zig_args.append("-cflags");
12541248
for (c_source_file.flags) |arg| {
12551249
try zig_args.append(arg);
12561250
}
12571251
try zig_args.append("--");
1258-
prev_has_cflags = true;
12591252
}
1253+
prev_has_cflags = (c_source_file.flags.len != 0);
1254+
1255+
if (c_source_file.language != .find_by_file_extension) {
1256+
try zig_args.append("-x");
1257+
try zig_args.append(switch (c_source_file.language) {
1258+
.find_by_file_extension => unreachable,
1259+
.c => "c",
1260+
.cpp => "c++",
1261+
.assembly => "assembler",
1262+
.assembly_with_cpp => "assembler-with-cpp",
1263+
.objc => "objective-c",
1264+
.objcpp => "objective-c++",
1265+
});
1266+
}
1267+
12601268
try zig_args.append(c_source_file.file.getPath2(dep.module.owner, step));
1269+
1270+
if (c_source_file.language != .find_by_file_extension) {
1271+
try zig_args.append("-x");
1272+
try zig_args.append("none");
1273+
}
12611274
total_linker_objects += 1;
12621275
},
12631276

12641277
.c_source_files => |c_source_files| l: {
12651278
if (!my_responsibility) break :l;
12661279

1267-
if (c_source_files.flags.len == 0) {
1268-
if (prev_has_cflags) {
1269-
try zig_args.append("-cflags");
1270-
try zig_args.append("--");
1271-
prev_has_cflags = false;
1272-
}
1273-
} else {
1280+
if (prev_has_cflags or c_source_files.flags.len != 0) {
12741281
try zig_args.append("-cflags");
1275-
for (c_source_files.flags) |flag| {
1276-
try zig_args.append(flag);
1282+
for (c_source_files.flags) |arg| {
1283+
try zig_args.append(arg);
12771284
}
12781285
try zig_args.append("--");
1279-
prev_has_cflags = true;
1286+
}
1287+
prev_has_cflags = (c_source_files.flags.len != 0);
1288+
1289+
if (c_source_files.language != .find_by_file_extension) {
1290+
try zig_args.append("-x");
1291+
try zig_args.append(switch (c_source_files.language) {
1292+
.find_by_file_extension => unreachable,
1293+
.c => "c",
1294+
.cpp => "c++",
1295+
.assembly => "assembler",
1296+
.assembly_with_cpp => "assembler-with-cpp",
1297+
.objc => "objective-c",
1298+
.objcpp => "objective-c++",
1299+
});
12801300
}
12811301

12821302
const root_path = c_source_files.root.getPath2(dep.module.owner, step);
12831303
for (c_source_files.files) |file| {
12841304
try zig_args.append(b.pathJoin(&.{ root_path, file }));
12851305
}
12861306

1307+
if (c_source_files.language != .find_by_file_extension) {
1308+
try zig_args.append("-x");
1309+
try zig_args.append("none");
1310+
}
1311+
12871312
total_linker_objects += c_source_files.files.len;
12881313
},
12891314

0 commit comments

Comments
 (0)