Skip to content

Commit 1bc8dd6

Browse files
committed
Add option to specify language of source files
1 parent 5bf9dc3 commit 1bc8dd6

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)