authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-07-13 22:58:25+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-07-15 18:49:47+02:00
loge3575cdad44e63f598b557ba3142675197875906
tree000783a9e8eb1feb1a77aebfa458a0d52e200723
parent398672eb30dce08bd3370cde7adeb503c64a4892

zld: decommision use_lld for MachO

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 {
866866
867867 // Make a decision on whether to use LLD or our own linker.
868868 const use_lld = options.use_lld orelse blk: {
869 if (options.target.isDarwin()) {
870 break :blk false;
871 }
872
869873 if (!build_options.have_llvm)
870874 break :blk false;
871875
......@@ -903,11 +907,9 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
903907 break :blk false;
904908 };
905909
906 const darwin_can_use_system_sdk =
907 // comptime conditions
908 ((build_options.have_llvm and comptime std.Target.current.isDarwin()) and
909 // runtime conditions
910 (use_lld and std.builtin.os.tag == .macos and options.target.isDarwin()));
910 const darwin_can_use_system_sdk = comptime std.Target.current.isDarwin() and
911 std.builtin.os.tag == .macos and
912 options.target.isDarwin();
911913
912914 const sysroot = blk: {
913915 if (options.sysroot) |sysroot| {
......@@ -924,10 +926,10 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
924926
925927 const lto = blk: {
926928 if (options.want_lto) |explicit| {
927 if (!use_lld)
929 if (!use_lld and !options.target.isDarwin())
928930 return error.LtoUnavailableWithoutLld;
929931 break :blk explicit;
930 } else if (!use_lld) {
932 } else if (!use_lld and !options.target.isDarwin()) {
931933 break :blk false;
932934 } else if (options.c_source_files.len == 0) {
933935 break :blk false;
src/link.zig+1-1
......@@ -515,7 +515,7 @@ pub const File = struct {
515515 }
516516 }
517517
518 fn linkAsArchive(base: *File, comp: *Compilation) !void {
518 pub fn linkAsArchive(base: *File, comp: *Compilation) !void {
519519 const tracy = trace(@src());
520520 defer tracy.end();
521521
src/link/MachO.zig+13-7
......@@ -341,7 +341,6 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
341341 assert(options.object_format == .macho);
342342
343343 if (options.use_llvm) return error.LLVM_BackendIsTODO_ForMachO; // TODO
344 if (options.use_lld) return error.LLD_LinkingIsTODO_ForMachO; // TODO
345344
346345 const file = try options.emit.?.directory.handle.createFile(sub_path, .{
347346 .truncate = false,
......@@ -358,6 +357,10 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
358357
359358 self.base.file = file;
360359
360 if (options.output_mode == .Lib and options.link_mode == .Static) {
361 return self;
362 }
363
361364 if (!options.strip and options.module != null) {
362365 // Create dSYM bundle.
363366 const dir = options.module.?.zig_cache_artifact_directory;
......@@ -393,12 +396,6 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
393396 .n_value = 0,
394397 });
395398
396 switch (options.output_mode) {
397 .Exe => {},
398 .Obj => {},
399 .Lib => return error.TODOImplementWritingLibFiles,
400 }
401
402399 try self.populateMissingMetadata();
403400 try self.writeLocalSymbol(0);
404401
......@@ -428,6 +425,15 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO {
428425}
429426
430427pub 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
431437 if (build_options.have_llvm and self.base.options.use_lld) {
432438 return self.linkWithZld(comp);
433439 } else {
src/main.zig+6
......@@ -1646,6 +1646,12 @@ fn buildOutputType(
16461646 }
16471647 }
16481648
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
16491655 if (comptime std.Target.current.isDarwin()) {
16501656 // If we want to link against frameworks, we need system headers.
16511657 if (framework_dirs.items.len > 0 or frameworks.items.len > 0)