diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index 635e6cc33431..9250d4e0c0e2 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -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, }; } }; @@ -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; @@ -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); diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 0f0b5d32017c..b9044b297da3 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -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); } @@ -1250,40 +1250,60 @@ 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); @@ -1291,6 +1311,11 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 { 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; },