diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2020-11-06 10:58:49 +0100 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2020-11-06 11:34:51 +0100 |
| commit | b7c3ebcb9e3aefbdd82a43c7ab122931787c7805 (patch) | |
| tree | 4136470a9950065a63e77f5ef61d93e2b3b0f93f /src | |
| parent | e0e3ceac19c0de0f8b3e408f9d3728496bc051f7 (diff) | |
| download | zig-b7c3ebcb9e3aefbdd82a43c7ab122931787c7805.tar.gz zig-b7c3ebcb9e3aefbdd82a43c7ab122931787c7805.zip | |
Rely on ZIG_SYSTEM_LINKER_HACK instead of input flags
Diffstat (limited to 'src')
| -rw-r--r-- | src/Compilation.zig | 29 | ||||
| -rw-r--r-- | src/link.zig | 4 | ||||
| -rw-r--r-- | src/link/Elf.zig | 106 | ||||
| -rw-r--r-- | src/link/MachO.zig | 8 | ||||
| -rw-r--r-- | src/main.zig | 11 |
5 files changed, 66 insertions, 92 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig index 961f8c08d5..b6f380ae60 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -348,8 +348,6 @@ pub const InitOptions = struct { want_valgrind: ?bool = null, use_llvm: ?bool = null, use_lld: ?bool = null, - /// When set, this option will overwrite LLD with the system linker LD. - system_linker_hack: bool = false, use_clang: ?bool = null, rdynamic: bool = false, strip: bool = false, @@ -474,13 +472,22 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { break :blk false; }; - const syslibroot = if (build_options.have_llvm and comptime std.Target.current.isDarwin()) outer: { - const path = if (use_lld and options.is_native_os and options.target.isDarwin()) inner: { - const syslibroot_path = try std.zig.system.getSDKPath(arena); - break :inner syslibroot_path; - } else null; - break :outer path; - } else null; + const DarwinOptions = struct { + syslibroot: ?[]const u8 = null, + system_linker_hack: bool = false, + }; + + const darwin_options: DarwinOptions = if (build_options.have_llvm and comptime std.Target.current.isDarwin()) outer: { + const opts: DarwinOptions = if (use_lld and options.is_native_os and options.target.isDarwin()) inner: { + const syslibroot = try std.zig.system.getSDKPath(arena); + const system_linker_hack = if (std.os.getenv("ZIG_SYSTEM_LINKER_HACK")) |_| true else false; + break :inner .{ + .syslibroot = syslibroot, + .system_linker_hack = system_linker_hack, + }; + } else .{}; + break :outer opts; + } else .{}; const link_libc = options.link_libc or target_util.osRequiresLibC(options.target); @@ -777,14 +784,14 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { .optimize_mode = options.optimize_mode, .use_lld = use_lld, .use_llvm = use_llvm, - .system_linker_hack = options.system_linker_hack, + .system_linker_hack = darwin_options.system_linker_hack, .link_libc = link_libc, .link_libcpp = options.link_libcpp, .objects = options.link_objects, .frameworks = options.frameworks, .framework_dirs = options.framework_dirs, .system_libs = system_libs, - .syslibroot = syslibroot, + .syslibroot = darwin_options.syslibroot, .lib_dirs = options.lib_dirs, .rpath_list = options.rpath_list, .strip = strip, diff --git a/src/link.zig b/src/link.zig index 65074924d1..7b0271f978 100644 --- a/src/link.zig +++ b/src/link.zig @@ -57,8 +57,8 @@ pub const Options = struct { /// other objects. /// Otherwise (depending on `use_lld`) this link code directly outputs and updates the final binary. use_llvm: bool, - /// If this is true and `use_llvm` is true, this link code will use system linker `ld` instead of - /// the LLD. + /// Darwin-only. If this is true, `use_llvm` is true, and `is_native_os` is true, this link code will + /// use system linker `ld` instead of the LLD. system_linker_hack: bool, link_libc: bool, link_libcpp: bool, diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 886d74cd95..7d4662c68d 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -1353,20 +1353,14 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { // Create an LLD command line and invoke it. var argv = std.ArrayList([]const u8).init(self.base.allocator); defer argv.deinit(); - - if (self.base.options.is_native_os and self.base.options.system_linker_hack) { - try argv.append("ld"); - } else { - // Even though we're calling LLD as a library it thinks the first argument is its own exe name. - try argv.append("lld"); - - try argv.append("-error-limit=0"); - } - + // Even though we're calling LLD as a library it thinks the first argument is its own exe name. + try argv.append("lld"); if (is_obj) { try argv.append("-r"); } + try argv.append("-error-limit=0"); + if (self.base.options.output_mode == .Exe) { try argv.append("-z"); try argv.append(try std.fmt.allocPrint(arena, "stack-size={}", .{stack_size})); @@ -1622,63 +1616,43 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { Compilation.dump_argv(argv.items); } - if (self.base.options.is_native_os and self.base.options.system_linker_hack) { - const result = try std.ChildProcess.exec(.{ .allocator = self.base.allocator, .argv = argv.items }); - defer { - self.base.allocator.free(result.stdout); - self.base.allocator.free(result.stderr); - } - if (result.stdout.len != 0) { - std.log.warn("unexpected LD stdout: {}", .{result.stdout}); - } - if (result.stderr.len != 0) { - std.log.warn("unexpected LD stderr: {}", .{result.stderr}); - } - if (result.term.Exited != 0) { - // TODO parse this output and surface with the Compilation API rather than - // directly outputting to stderr here. - std.debug.print("{}", .{result.stderr}); - return error.LDReportedFailure; - } - } else { - // Oh, snapplesauce! We need null terminated argv. - const new_argv = try arena.allocSentinel(?[*:0]const u8, argv.items.len, null); - for (argv.items) |arg, i| { - new_argv[i] = try arena.dupeZ(u8, arg); - } + // Oh, snapplesauce! We need null terminated argv. + const new_argv = try arena.allocSentinel(?[*:0]const u8, argv.items.len, null); + for (argv.items) |arg, i| { + new_argv[i] = try arena.dupeZ(u8, arg); + } - var stderr_context: LLDContext = .{ - .elf = self, - .data = std.ArrayList(u8).init(self.base.allocator), - }; - defer stderr_context.data.deinit(); - var stdout_context: LLDContext = .{ - .elf = self, - .data = std.ArrayList(u8).init(self.base.allocator), - }; - defer stdout_context.data.deinit(); - const llvm = @import("../llvm.zig"); - const ok = llvm.Link( - .ELF, - new_argv.ptr, - new_argv.len, - append_diagnostic, - @ptrToInt(&stdout_context), - @ptrToInt(&stderr_context), - ); - if (stderr_context.oom or stdout_context.oom) return error.OutOfMemory; - if (stdout_context.data.items.len != 0) { - std.log.warn("unexpected LLD stdout: {}", .{stdout_context.data.items}); - } - if (!ok) { - // TODO parse this output and surface with the Compilation API rather than - // directly outputting to stderr here. - std.debug.print("{}", .{stderr_context.data.items}); - return error.LLDReportedFailure; - } - if (stderr_context.data.items.len != 0) { - std.log.warn("unexpected LLD stderr: {}", .{stderr_context.data.items}); - } + var stderr_context: LLDContext = .{ + .elf = self, + .data = std.ArrayList(u8).init(self.base.allocator), + }; + defer stderr_context.data.deinit(); + var stdout_context: LLDContext = .{ + .elf = self, + .data = std.ArrayList(u8).init(self.base.allocator), + }; + defer stdout_context.data.deinit(); + const llvm = @import("../llvm.zig"); + const ok = llvm.Link( + .ELF, + new_argv.ptr, + new_argv.len, + append_diagnostic, + @ptrToInt(&stdout_context), + @ptrToInt(&stderr_context), + ); + if (stderr_context.oom or stdout_context.oom) return error.OutOfMemory; + if (stdout_context.data.items.len != 0) { + std.log.warn("unexpected LLD stdout: {}", .{stdout_context.data.items}); + } + if (!ok) { + // TODO parse this output and surface with the Compilation API rather than + // directly outputting to stderr here. + std.debug.print("{}", .{stderr_context.data.items}); + return error.LLDReportedFailure; + } + if (stderr_context.data.items.len != 0) { + std.log.warn("unexpected LLD stderr: {}", .{stderr_context.data.items}); } if (!self.base.options.disable_lld_caching) { diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 9352669eba..391f490c1f 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -534,7 +534,9 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { defer argv.deinit(); // TODO https://github.com/ziglang/zig/issues/6971 - if (self.base.options.is_native_os and self.base.options.system_linker_hack) { + // Note that there is no need to check if running natively since we do that already + // when setting `system_linker_hack` in Compilation struct. + if (self.base.options.system_linker_hack) { try argv.append("ld"); } else { // Even though we're calling LLD as a library it thinks the first argument is its own exe name. @@ -710,7 +712,9 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { } // TODO https://github.com/ziglang/zig/issues/6971 - if (self.base.options.is_native_os and self.base.options.system_linker_hack) { + // Note that there is no need to check if running natively since we do that already + // when setting `system_linker_hack` in Compilation struct. + if (self.base.options.system_linker_hack) { const result = try std.ChildProcess.exec(.{ .allocator = self.base.allocator, .argv = argv.items }); defer { self.base.allocator.free(result.stdout); diff --git a/src/main.zig b/src/main.zig index 5bb9a7afdd..7df8cb1eda 100644 --- a/src/main.zig +++ b/src/main.zig @@ -317,7 +317,6 @@ const usage_build_generic = \\ --image-base [addr] Set base address for executable image \\ -framework [name] (darwin) link against framework \\ -F[dir] (darwin) add search path for frameworks - \\ --system-linker-hack Use system linker LD instead of LLD (requires LLVM enabled) \\ \\Test Options: \\ --test-filter [text] Skip tests that do not match filter @@ -475,7 +474,6 @@ fn buildOutputType( var image_base_override: ?u64 = null; var use_llvm: ?bool = null; var use_lld: ?bool = null; - var system_linker_hack = false; var use_clang: ?bool = null; var link_eh_frame_hdr = false; var link_emit_relocs = false; @@ -917,8 +915,6 @@ fn buildOutputType( mem.startsWith(u8, arg, "-I")) { try clang_argv.append(arg); - } else if (mem.startsWith(u8, arg, "--system-linker-hack")) { - system_linker_hack = true; } else { fatal("unrecognized parameter: '{}'", .{arg}); } @@ -1643,7 +1639,6 @@ fn buildOutputType( .want_valgrind = want_valgrind, .use_llvm = use_llvm, .use_lld = use_lld, - .system_linker_hack = system_linker_hack, .use_clang = use_clang, .rdynamic = rdynamic, .linker_script = linker_script, @@ -2165,7 +2160,6 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v var override_global_cache_dir: ?[]const u8 = null; var override_local_cache_dir: ?[]const u8 = null; var child_argv = std.ArrayList([]const u8).init(arena); - var system_linker_hack = false; const argv_index_exe = child_argv.items.len; _ = try child_argv.addOne(); @@ -2206,10 +2200,6 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v override_global_cache_dir = args[i]; try child_argv.appendSlice(&[_][]const u8{ arg, args[i] }); continue; - } else if (mem.eql(u8, arg, "--system-linker-hack")) { - system_linker_hack = true; - try child_argv.append(arg); - continue; } } try child_argv.append(arg); @@ -2345,7 +2335,6 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v .optimize_mode = .Debug, .self_exe_path = self_exe_path, .rand = &default_prng.random, - .system_linker_hack = system_linker_hack, }) catch |err| { fatal("unable to create compilation: {}", .{@errorName(err)}); }; |
