Skip to content

Commit

Permalink
std.Build: Add option to specify language of CSourceFiles
Browse files Browse the repository at this point in the history
Closes: #20655
  • Loading branch information
GalaxyShard authored Jan 30, 2025
1 parent 3348478 commit a3ee521
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 19 deletions.
34 changes: 33 additions & 1 deletion lib/std/Build/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,51 @@ pub const SystemLib = struct {
pub const SearchStrategy = enum { paths_first, mode_first, no_fallback };
};

pub const CSourceLanguage = enum {
c,
cpp,

objective_c,
objective_cpp,

/// Standard assembly
assembly,
/// Assembly with the C preprocessor
assembly_with_preprocessor,

pub fn internalIdentifier(self: CSourceLanguage) []const u8 {
return switch (self) {
.c => "c",
.cpp => "c++",
.objective_c => "objective-c",
.objective_cpp => "objective-c++",
.assembly => "assembler",
.assembly_with_preprocessor => "assembler-with-cpp",
};
}
};

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,
/// By default, determines language of each file individually based on its file extension
language: ?CSourceLanguage,
};

pub const CSourceFile = struct {
file: LazyPath,
flags: []const []const u8 = &.{},
/// By default, determines language of each file individually based on its file extension
language: ?CSourceLanguage = null,

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 @@ -378,9 +407,11 @@ pub const AddCSourceFilesOptions = struct {
root: ?LazyPath = null,
files: []const []const u8,
flags: []const []const u8 = &.{},
/// By default, determines language of each file individually based on its file extension
language: ?CSourceLanguage = null,
};

/// 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 @@ -399,6 +430,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");
}
Expand Down
45 changes: 27 additions & 18 deletions lib/std/Build/Step/Compile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1259,47 +1259,56 @@ 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) |lang| {
try zig_args.append("-x");
try zig_args.append(lang.internalIdentifier());
}

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

if (c_source_file.language != null) {
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) |lang| {
try zig_args.append("-x");
try zig_args.append(lang.internalIdentifier());
}

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

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

total_linker_objects += c_source_files.files.len;
},

Expand Down

0 comments on commit a3ee521

Please sign in to comment.