| author | |
| committer | |
| log | e3575cdad44e63f598b557ba3142675197875906 |
| tree | 000783a9e8eb1feb1a77aebfa458a0d52e200723 |
| parent | 398672eb30dce08bd3370cde7adeb503c64a4892 |
Invoke `linkAsArchive` directly in MachO backend when LLVM is available
and we are asked to create a static lib.4 files changed, 29 insertions(+), 15 deletions(-)
src/Compilation.zig+9-7| ... | @@ -866,6 +866,10 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { | ... | @@ -866,6 +866,10 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { |
| 866 | 866 | ||
| 867 | // Make a decision on whether to use LLD or our own linker. | 867 | // Make a decision on whether to use LLD or our own linker. |
| 868 | const use_lld = options.use_lld orelse blk: { | 868 | const use_lld = options.use_lld orelse blk: { |
| 869 | if (options.target.isDarwin()) { | ||
| 870 | break :blk false; | ||
| 871 | } | ||
| 872 | |||
| 869 | if (!build_options.have_llvm) | 873 | if (!build_options.have_llvm) |
| 870 | break :blk false; | 874 | break :blk false; |
| 871 | 875 | ||
| ... | @@ -903,11 +907,9 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { | ... | @@ -903,11 +907,9 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { |
| 903 | break :blk false; | 907 | break :blk false; |
| 904 | }; | 908 | }; |
| 905 | 909 | ||
| 906 | const darwin_can_use_system_sdk = | 910 | const darwin_can_use_system_sdk = comptime std.Target.current.isDarwin() and |
| 907 | // comptime conditions | 911 | std.builtin.os.tag == .macos and |
| 908 | ((build_options.have_llvm and comptime std.Target.current.isDarwin()) and | 912 | options.target.isDarwin(); |
| 909 | // runtime conditions | ||
| 910 | (use_lld and std.builtin.os.tag == .macos and options.target.isDarwin())); | ||
| 911 | 913 | ||
| 912 | const sysroot = blk: { | 914 | const sysroot = blk: { |
| 913 | if (options.sysroot) |sysroot| { | 915 | if (options.sysroot) |sysroot| { |
| ... | @@ -924,10 +926,10 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { | ... | @@ -924,10 +926,10 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { |
| 924 | 926 | ||
| 925 | const lto = blk: { | 927 | const lto = blk: { |
| 926 | if (options.want_lto) |explicit| { | 928 | if (options.want_lto) |explicit| { |
| 927 | if (!use_lld) | 929 | if (!use_lld and !options.target.isDarwin()) |
| 928 | return error.LtoUnavailableWithoutLld; | 930 | return error.LtoUnavailableWithoutLld; |
| 929 | break :blk explicit; | 931 | break :blk explicit; |
| 930 | } else if (!use_lld) { | 932 | } else if (!use_lld and !options.target.isDarwin()) { |
| 931 | break :blk false; | 933 | break :blk false; |
| 932 | } else if (options.c_source_files.len == 0) { | 934 | } else if (options.c_source_files.len == 0) { |
| 933 | break :blk false; | 935 | break :blk false; |
src/link.zig+1-1| ... | @@ -515,7 +515,7 @@ pub const File = struct { | ... | @@ -515,7 +515,7 @@ pub const File = struct { |
| 515 | } | 515 | } |
| 516 | } | 516 | } |
| 517 | 517 | ||
| 518 | fn linkAsArchive(base: *File, comp: *Compilation) !void { | 518 | pub fn linkAsArchive(base: *File, comp: *Compilation) !void { |
| 519 | const tracy = trace(@src()); | 519 | const tracy = trace(@src()); |
| 520 | defer tracy.end(); | 520 | defer tracy.end(); |
| 521 | 521 |
src/link/MachO.zig+13-7| ... | @@ -341,7 +341,6 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio | ... | @@ -341,7 +341,6 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio |
| 341 | assert(options.object_format == .macho); | 341 | assert(options.object_format == .macho); |
| 342 | 342 | ||
| 343 | if (options.use_llvm) return error.LLVM_BackendIsTODO_ForMachO; // TODO | 343 | if (options.use_llvm) return error.LLVM_BackendIsTODO_ForMachO; // TODO |
| 344 | if (options.use_lld) return error.LLD_LinkingIsTODO_ForMachO; // TODO | ||
| 345 | 344 | ||
| 346 | const file = try options.emit.?.directory.handle.createFile(sub_path, .{ | 345 | const file = try options.emit.?.directory.handle.createFile(sub_path, .{ |
| 347 | .truncate = false, | 346 | .truncate = false, |
| ... | @@ -358,6 +357,10 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio | ... | @@ -358,6 +357,10 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio |
| 358 | 357 | ||
| 359 | self.base.file = file; | 358 | self.base.file = file; |
| 360 | 359 | ||
| 360 | if (options.output_mode == .Lib and options.link_mode == .Static) { | ||
| 361 | return self; | ||
| 362 | } | ||
| 363 | |||
| 361 | if (!options.strip and options.module != null) { | 364 | if (!options.strip and options.module != null) { |
| 362 | // Create dSYM bundle. | 365 | // Create dSYM bundle. |
| 363 | const dir = options.module.?.zig_cache_artifact_directory; | 366 | const dir = options.module.?.zig_cache_artifact_directory; |
| ... | @@ -393,12 +396,6 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio | ... | @@ -393,12 +396,6 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio |
| 393 | .n_value = 0, | 396 | .n_value = 0, |
| 394 | }); | 397 | }); |
| 395 | 398 | ||
| 396 | switch (options.output_mode) { | ||
| 397 | .Exe => {}, | ||
| 398 | .Obj => {}, | ||
| 399 | .Lib => return error.TODOImplementWritingLibFiles, | ||
| 400 | } | ||
| 401 | |||
| 402 | try self.populateMissingMetadata(); | 399 | try self.populateMissingMetadata(); |
| 403 | try self.writeLocalSymbol(0); | 400 | try self.writeLocalSymbol(0); |
| 404 | 401 | ||
| ... | @@ -428,6 +425,15 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO { | ... | @@ -428,6 +425,15 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO { |
| 428 | } | 425 | } |
| 429 | 426 | ||
| 430 | pub fn flush(self: *MachO, comp: *Compilation) !void { | 427 | pub fn flush(self: *MachO, comp: *Compilation) !void { |
| 428 | if (self.base.options.output_mode == .Lib and self.base.options.link_mode == .Static) { | ||
| 429 | if (build_options.have_llvm) { | ||
| 430 | return self.base.linkAsArchive(comp); | ||
| 431 | } else { | ||
| 432 | log.err("TODO: non-LLVM archiver for MachO object files", .{}); | ||
| 433 | return error.TODOImplementWritingStaticLibFiles; | ||
| 434 | } | ||
| 435 | } | ||
| 436 | |||
| 431 | if (build_options.have_llvm and self.base.options.use_lld) { | 437 | if (build_options.have_llvm and self.base.options.use_lld) { |
| 432 | return self.linkWithZld(comp); | 438 | return self.linkWithZld(comp); |
| 433 | } else { | 439 | } else { |
src/main.zig+6| ... | @@ -1646,6 +1646,12 @@ fn buildOutputType( | ... | @@ -1646,6 +1646,12 @@ fn buildOutputType( |
| 1646 | } | 1646 | } |
| 1647 | } | 1647 | } |
| 1648 | 1648 | ||
| 1649 | if (use_lld) |opt| { | ||
| 1650 | if (opt and cross_target.isDarwin()) { | ||
| 1651 | fatal("-fLLD requested with Mach-O object format. Only the self-hosted linker is supported for this target.", .{}); | ||
| 1652 | } | ||
| 1653 | } | ||
| 1654 | |||
| 1649 | if (comptime std.Target.current.isDarwin()) { | 1655 | if (comptime std.Target.current.isDarwin()) { |
| 1650 | // If we want to link against frameworks, we need system headers. | 1656 | // If we want to link against frameworks, we need system headers. |
| 1651 | if (framework_dirs.items.len > 0 or frameworks.items.len > 0) | 1657 | if (framework_dirs.items.len > 0 or frameworks.items.len > 0) |