Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to specify language of CSourceFiles #20687

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -800,7 +800,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 @@ -1250,47 +1250,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