| author | |
| committer | |
| log | a3ee5215bba9119d8ab645b0cefee1d026816e13 |
| tree | 81a4fae5059f1df3e1bdcf032d2d9a7e79274297 |
| parent | 3348478fc3882fa3627147e54eeafa83ab7f4b09 |
| signature |
Closes: #206552 files changed, 60 insertions(+), 19 deletions(-)
lib/std/Build/Module.zig+33-1| ... | ... | @@ -78,22 +78,51 @@ pub const SystemLib = struct { |
| 78 | 78 | pub const SearchStrategy = enum { paths_first, mode_first, no_fallback }; |
| 79 | 79 | }; |
| 80 | 80 | |
| 81 | pub const CSourceLanguage = enum { | |
| 82 | c, | |
| 83 | cpp, | |
| 84 | ||
| 85 | objective_c, | |
| 86 | objective_cpp, | |
| 87 | ||
| 88 | /// Standard assembly | |
| 89 | assembly, | |
| 90 | /// Assembly with the C preprocessor | |
| 91 | assembly_with_preprocessor, | |
| 92 | ||
| 93 | pub fn internalIdentifier(self: CSourceLanguage) []const u8 { | |
| 94 | return switch (self) { | |
| 95 | .c => "c", | |
| 96 | .cpp => "c++", | |
| 97 | .objective_c => "objective-c", | |
| 98 | .objective_cpp => "objective-c++", | |
| 99 | .assembly => "assembler", | |
| 100 | .assembly_with_preprocessor => "assembler-with-cpp", | |
| 101 | }; | |
| 102 | } | |
| 103 | }; | |
| 104 | ||
| 81 | 105 | pub const CSourceFiles = struct { |
| 82 | 106 | root: LazyPath, |
| 83 | 107 | /// `files` is relative to `root`, which is |
| 84 | 108 | /// the build root by default |
| 85 | 109 | files: []const []const u8, |
| 86 | 110 | flags: []const []const u8, |
| 111 | /// By default, determines language of each file individually based on its file extension | |
| 112 | language: ?CSourceLanguage, | |
| 87 | 113 | }; |
| 88 | 114 | |
| 89 | 115 | pub const CSourceFile = struct { |
| 90 | 116 | file: LazyPath, |
| 91 | 117 | flags: []const []const u8 = &.{}, |
| 118 | /// By default, determines language of each file individually based on its file extension | |
| 119 | language: ?CSourceLanguage = null, | |
| 92 | 120 | |
| 93 | 121 | pub fn dupe(file: CSourceFile, b: *std.Build) CSourceFile { |
| 94 | 122 | return .{ |
| 95 | 123 | .file = file.file.dupe(b), |
| 96 | 124 | .flags = b.dupeStrings(file.flags), |
| 125 | .language = file.language, | |
| 97 | 126 | }; |
| 98 | 127 | } |
| 99 | 128 | }; |
| ... | ... | @@ -378,9 +407,11 @@ pub const AddCSourceFilesOptions = struct { |
| 378 | 407 | root: ?LazyPath = null, |
| 379 | 408 | files: []const []const u8, |
| 380 | 409 | flags: []const []const u8 = &.{}, |
| 410 | /// By default, determines language of each file individually based on its file extension | |
| 411 | language: ?CSourceLanguage = null, | |
| 381 | 412 | }; |
| 382 | 413 | |
| 383 | /// Handy when you have many C/C++ source files and want them all to have the same flags. | |
| 414 | /// Handy when you have many non-Zig source files and want them all to have the same flags. | |
| 384 | 415 | pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void { |
| 385 | 416 | const b = m.owner; |
| 386 | 417 | const allocator = b.allocator; |
| ... | ... | @@ -399,6 +430,7 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void { |
| 399 | 430 | .root = options.root orelse b.path(""), |
| 400 | 431 | .files = b.dupeStrings(options.files), |
| 401 | 432 | .flags = b.dupeStrings(options.flags), |
| 433 | .language = options.language, | |
| 402 | 434 | }; |
| 403 | 435 | m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM"); |
| 404 | 436 | } |
lib/std/Build/Step/Compile.zig+27-18| ... | ... | @@ -1259,40 +1259,44 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 { |
| 1259 | 1259 | .c_source_file => |c_source_file| l: { |
| 1260 | 1260 | if (!my_responsibility) break :l; |
| 1261 | 1261 | |
| 1262 | if (c_source_file.flags.len == 0) { | |
| 1263 | if (prev_has_cflags) { | |
| 1264 | try zig_args.append("-cflags"); | |
| 1265 | try zig_args.append("--"); | |
| 1266 | prev_has_cflags = false; | |
| 1267 | } | |
| 1268 | } else { | |
| 1262 | if (prev_has_cflags or c_source_file.flags.len != 0) { | |
| 1269 | 1263 | try zig_args.append("-cflags"); |
| 1270 | 1264 | for (c_source_file.flags) |arg| { |
| 1271 | 1265 | try zig_args.append(arg); |
| 1272 | 1266 | } |
| 1273 | 1267 | try zig_args.append("--"); |
| 1274 | prev_has_cflags = true; | |
| 1275 | 1268 | } |
| 1269 | prev_has_cflags = (c_source_file.flags.len != 0); | |
| 1270 | ||
| 1271 | if (c_source_file.language) |lang| { | |
| 1272 | try zig_args.append("-x"); | |
| 1273 | try zig_args.append(lang.internalIdentifier()); | |
| 1274 | } | |
| 1275 | ||
| 1276 | 1276 | try zig_args.append(c_source_file.file.getPath2(mod.owner, step)); |
| 1277 | ||
| 1278 | if (c_source_file.language != null) { | |
| 1279 | try zig_args.append("-x"); | |
| 1280 | try zig_args.append("none"); | |
| 1281 | } | |
| 1277 | 1282 | total_linker_objects += 1; |
| 1278 | 1283 | }, |
| 1279 | 1284 | |
| 1280 | 1285 | .c_source_files => |c_source_files| l: { |
| 1281 | 1286 | if (!my_responsibility) break :l; |
| 1282 | 1287 | |
| 1283 | if (c_source_files.flags.len == 0) { | |
| 1284 | if (prev_has_cflags) { | |
| 1285 | try zig_args.append("-cflags"); | |
| 1286 | try zig_args.append("--"); | |
| 1287 | prev_has_cflags = false; | |
| 1288 | } | |
| 1289 | } else { | |
| 1288 | if (prev_has_cflags or c_source_files.flags.len != 0) { | |
| 1290 | 1289 | try zig_args.append("-cflags"); |
| 1291 | for (c_source_files.flags) |flag| { | |
| 1292 | try zig_args.append(flag); | |
| 1290 | for (c_source_files.flags) |arg| { | |
| 1291 | try zig_args.append(arg); | |
| 1293 | 1292 | } |
| 1294 | 1293 | try zig_args.append("--"); |
| 1295 | prev_has_cflags = true; | |
| 1294 | } | |
| 1295 | prev_has_cflags = (c_source_files.flags.len != 0); | |
| 1296 | ||
| 1297 | if (c_source_files.language) |lang| { | |
| 1298 | try zig_args.append("-x"); | |
| 1299 | try zig_args.append(lang.internalIdentifier()); | |
| 1296 | 1300 | } |
| 1297 | 1301 | |
| 1298 | 1302 | const root_path = c_source_files.root.getPath2(mod.owner, step); |
| ... | ... | @@ -1300,6 +1304,11 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 { |
| 1300 | 1304 | try zig_args.append(b.pathJoin(&.{ root_path, file })); |
| 1301 | 1305 | } |
| 1302 | 1306 | |
| 1307 | if (c_source_files.language != null) { | |
| 1308 | try zig_args.append("-x"); | |
| 1309 | try zig_args.append("none"); | |
| 1310 | } | |
| 1311 | ||
| 1303 | 1312 | total_linker_objects += c_source_files.files.len; |
| 1304 | 1313 | }, |
| 1305 | 1314 |