Skip to content

Commit e607064

Browse files
committed
Add option to specify language of source files
1 parent d83a3f1 commit e607064

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
@@ -800,7 +800,7 @@ pub fn linkFrameworkWeak(c: *Compile, name: []const u8) void {
800800
c.root_module.linkFramework(name, .{ .weak = true });
801801
}
802802

803-
/// Handy when you have many C/C++ source files and want them all to have the same flags.
803+
/// Handy when you have many non-Zig source files and want them all to have the same flags.
804804
pub fn addCSourceFiles(compile: *Compile, options: Module.AddCSourceFilesOptions) void {
805805
compile.root_module.addCSourceFiles(options);
806806
}
@@ -1250,47 +1250,72 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
12501250
.c_source_file => |c_source_file| l: {
12511251
if (!my_responsibility) break :l;
12521252

1253-
if (c_source_file.flags.len == 0) {
1254-
if (prev_has_cflags) {
1255-
try zig_args.append("-cflags");
1256-
try zig_args.append("--");
1257-
prev_has_cflags = false;
1258-
}
1259-
} else {
1253+
if (prev_has_cflags or c_source_file.flags.len != 0) {
12601254
try zig_args.append("-cflags");
12611255
for (c_source_file.flags) |arg| {
12621256
try zig_args.append(arg);
12631257
}
12641258
try zig_args.append("--");
1265-
prev_has_cflags = true;
12661259
}
1260+
prev_has_cflags = (c_source_file.flags.len != 0);
1261+
1262+
if (c_source_file.language != .find_by_file_extension) {
1263+
try zig_args.append("-x");
1264+
try zig_args.append(switch (c_source_file.language) {
1265+
.find_by_file_extension => unreachable,
1266+
.c => "c",
1267+
.cpp => "c++",
1268+
.assembly => "assembler",
1269+
.assembly_with_cpp => "assembler-with-cpp",
1270+
.objc => "objective-c",
1271+
.objcpp => "objective-c++",
1272+
});
1273+
}
1274+
12671275
try zig_args.append(c_source_file.file.getPath2(dep.module.owner, step));
1276+
1277+
if (c_source_file.language != .find_by_file_extension) {
1278+
try zig_args.append("-x");
1279+
try zig_args.append("none");
1280+
}
12681281
total_linker_objects += 1;
12691282
},
12701283

12711284
.c_source_files => |c_source_files| l: {
12721285
if (!my_responsibility) break :l;
12731286

1274-
if (c_source_files.flags.len == 0) {
1275-
if (prev_has_cflags) {
1276-
try zig_args.append("-cflags");
1277-
try zig_args.append("--");
1278-
prev_has_cflags = false;
1279-
}
1280-
} else {
1287+
if (prev_has_cflags or c_source_files.flags.len != 0) {
12811288
try zig_args.append("-cflags");
1282-
for (c_source_files.flags) |flag| {
1283-
try zig_args.append(flag);
1289+
for (c_source_files.flags) |arg| {
1290+
try zig_args.append(arg);
12841291
}
12851292
try zig_args.append("--");
1286-
prev_has_cflags = true;
1293+
}
1294+
prev_has_cflags = (c_source_files.flags.len != 0);
1295+
1296+
if (c_source_files.language != .find_by_file_extension) {
1297+
try zig_args.append("-x");
1298+
try zig_args.append(switch (c_source_files.language) {
1299+
.find_by_file_extension => unreachable,
1300+
.c => "c",
1301+
.cpp => "c++",
1302+
.assembly => "assembler",
1303+
.assembly_with_cpp => "assembler-with-cpp",
1304+
.objc => "objective-c",
1305+
.objcpp => "objective-c++",
1306+
});
12871307
}
12881308

12891309
const root_path = c_source_files.root.getPath2(dep.module.owner, step);
12901310
for (c_source_files.files) |file| {
12911311
try zig_args.append(b.pathJoin(&.{ root_path, file }));
12921312
}
12931313

1314+
if (c_source_files.language != .find_by_file_extension) {
1315+
try zig_args.append("-x");
1316+
try zig_args.append("none");
1317+
}
1318+
12941319
total_linker_objects += c_source_files.files.len;
12951320
},
12961321

0 commit comments

Comments
 (0)