Skip to content

Commit

Permalink
Add option to specify language of source files
Browse files Browse the repository at this point in the history
  • Loading branch information
GalaxyShard committed Jul 29, 2024
1 parent c157550 commit 4b6eb4d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
18 changes: 17 additions & 1 deletion lib/std/Build/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,36 @@ pub const SystemLib = struct {
pub const SearchStrategy = enum { paths_first, mode_first, no_fallback };
};

pub const ForeignSourceLanguage = enum {
c,
cpp,
assembly,
assembly_with_cpp,
objc,
objcpp,

find_by_file_extension,
};

pub const CSourceFiles = struct {
root: LazyPath,
/// `files` is relative to `root`, which is
/// the build root by default
files: []const []const u8,
flags: []const []const u8,
language: ForeignSourceLanguage,
};

pub const CSourceFile = struct {
file: LazyPath,
flags: []const []const u8 = &.{},
language: ForeignSourceLanguage = .find_by_file_extension,

pub fn dupe(file: CSourceFile, b: *std.Build) CSourceFile {
return .{
.file = file.file.dupe(b),
.flags = b.dupeStrings(file.flags),
.language = file.language,
};
}
};
Expand Down Expand Up @@ -486,9 +500,10 @@ pub const AddCSourceFilesOptions = struct {
root: ?LazyPath = null,
files: []const []const u8,
flags: []const []const u8 = &.{},
language: ForeignSourceLanguage = .find_by_file_extension,
};

/// Handy when you have many C/C++ source files and want them all to have the same flags.
/// Handy when you have many non-Zig source files and want them all to have the same flags.
pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
const b = m.owner;
const allocator = b.allocator;
Expand All @@ -507,6 +522,7 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
.root = options.root orelse b.path(""),
.files = b.dupeStrings(options.files),
.flags = b.dupeStrings(options.flags),
.language = options.language,
};
m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM");
addLazyPathDependenciesOnly(m, c_source_files.root);
Expand Down
63 changes: 44 additions & 19 deletions lib/std/Build/Step/Compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ pub fn linkFrameworkWeak(c: *Compile, name: []const u8) void {
c.root_module.linkFramework(name, .{ .weak = true });
}

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

if (c_source_file.flags.len == 0) {
if (prev_has_cflags) {
try zig_args.append("-cflags");
try zig_args.append("--");
prev_has_cflags = false;
}
} else {
if (prev_has_cflags or c_source_file.flags.len != 0) {
try zig_args.append("-cflags");
for (c_source_file.flags) |arg| {
try zig_args.append(arg);
}
try zig_args.append("--");
prev_has_cflags = true;
}
prev_has_cflags = (c_source_file.flags.len != 0);

if (c_source_file.language != .find_by_file_extension) {
try zig_args.append("-x");
try zig_args.append(switch (c_source_file.language) {
.find_by_file_extension => unreachable,
.c => "c",
.cpp => "c++",
.assembly => "assembler",
.assembly_with_cpp => "assembler-with-cpp",
.objc => "objective-c",
.objcpp => "objective-c++",
});
}

try zig_args.append(c_source_file.file.getPath2(dep.module.owner, step));

if (c_source_file.language != .find_by_file_extension) {
try zig_args.append("-x");
try zig_args.append("none");
}
total_linker_objects += 1;
},

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

if (c_source_files.flags.len == 0) {
if (prev_has_cflags) {
try zig_args.append("-cflags");
try zig_args.append("--");
prev_has_cflags = false;
}
} else {
if (prev_has_cflags or c_source_files.flags.len != 0) {
try zig_args.append("-cflags");
for (c_source_files.flags) |flag| {
try zig_args.append(flag);
for (c_source_files.flags) |arg| {
try zig_args.append(arg);
}
try zig_args.append("--");
prev_has_cflags = true;
}
prev_has_cflags = (c_source_files.flags.len != 0);

if (c_source_files.language != .find_by_file_extension) {
try zig_args.append("-x");
try zig_args.append(switch (c_source_files.language) {
.find_by_file_extension => unreachable,
.c => "c",
.cpp => "c++",
.assembly => "assembler",
.assembly_with_cpp => "assembler-with-cpp",
.objc => "objective-c",
.objcpp => "objective-c++",
});
}

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

if (c_source_files.language != .find_by_file_extension) {
try zig_args.append("-x");
try zig_args.append("none");
}

total_linker_objects += c_source_files.files.len;
},

Expand Down

0 comments on commit 4b6eb4d

Please sign in to comment.