authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-06 10:58:49+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-06 11:34:51+01:00
logb7c3ebcb9e3aefbdd82a43c7ab122931787c7805
tree4136470a9950065a63e77f5ef61d93e2b3b0f93f
parente0e3ceac19c0de0f8b3e408f9d3728496bc051f7

Rely on ZIG_SYSTEM_LINKER_HACK instead of input flags


9 files changed, 74 insertions(+), 122 deletions(-)

CMakeLists.txt+7-18
......@@ -505,24 +505,13 @@ add_dependencies(zig zig_build_zig1)
505505
506506install(TARGETS zig DESTINATION bin)
507507
508if(NOT SYSTEM_LINKER_HACK)
509 set(ZIG_INSTALL_ARGS "build"
510 --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"
511 "-Dlib-files-only"
512 --prefix "${CMAKE_INSTALL_PREFIX}"
513 "-Dconfig_h=${ZIG_CONFIG_H_OUT}"
514 install
515 )
516else()
517 set(ZIG_INSTALL_ARGS "build"
518 --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"
519 "-Dlib-files-only"
520 --prefix "${CMAKE_INSTALL_PREFIX}"
521 "-Dconfig_h=${ZIG_CONFIG_H_OUT}"
522 --system-linker-hack
523 install
524 )
525endif()
508set(ZIG_INSTALL_ARGS "build"
509 --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"
510 "-Dlib-files-only"
511 --prefix "${CMAKE_INSTALL_PREFIX}"
512 "-Dconfig_h=${ZIG_CONFIG_H_OUT}"
513 install
514)
526515
527516# CODE has no effect with Visual Studio build system generator, therefore
528517# when using Visual Studio build system generator we resort to running
build.zig+1-1
......@@ -396,8 +396,8 @@ fn configureStage2(b: *Builder, exe: anytype, ctx: Context, need_cpp_includes: b
396396 try addCxxKnownPath(b, ctx, exe, "libstdc++.a", null, need_cpp_includes);
397397 exe.linkSystemLibrary("pthread");
398398 // TODO LLD cannot perform this link.
399 // Set ZIG_SYSTEM_LINKER_HACK env var to use system linker ld instead.
399400 // See https://github.com/ziglang/zig/issues/1535
400 exe.enableSystemLinkerHack();
401401 } else |err| switch (err) {
402402 error.RequiredLibraryNotFound => {
403403 // System compiler, not gcc.
lib/std/build.zig-8
......@@ -67,7 +67,6 @@ pub const Builder = struct {
6767 vcpkg_root: VcpkgRoot,
6868 pkg_config_pkg_list: ?(PkgConfigError![]const PkgConfigPkg) = null,
6969 args: ?[][]const u8 = null,
70 system_linker_hack: bool,
7170
7271 const PkgConfigError = error{
7372 PkgConfigCrashed,
......@@ -174,7 +173,6 @@ pub const Builder = struct {
174173 .install_path = undefined,
175174 .vcpkg_root = VcpkgRoot{ .Unattempted = {} },
176175 .args = null,
177 .system_linker_hack = false,
178176 };
179177 try self.top_level_steps.append(&self.install_tls);
180178 try self.top_level_steps.append(&self.uninstall_tls);
......@@ -1239,7 +1237,6 @@ pub const LibExeObjStep = struct {
12391237 packages: ArrayList(Pkg),
12401238 build_options_contents: std.ArrayList(u8),
12411239 build_options_artifact_args: std.ArrayList(BuildOptionArtifactArg),
1242 system_linker_hack: bool = false,
12431240
12441241 object_src: []const u8,
12451242
......@@ -1900,10 +1897,6 @@ pub const LibExeObjStep = struct {
19001897 self.exec_cmd_args = args;
19011898 }
19021899
1903 pub fn enableSystemLinkerHack(self: *LibExeObjStep) void {
1904 self.system_linker_hack = true;
1905 }
1906
19071900 fn linkLibraryOrObject(self: *LibExeObjStep, other: *LibExeObjStep) void {
19081901 self.step.dependOn(&other.step);
19091902 self.link_objects.append(LinkObject{ .OtherStep = other }) catch unreachable;
......@@ -2076,7 +2069,6 @@ pub const LibExeObjStep = struct {
20762069 if (builder.verbose_link or self.verbose_link) zig_args.append("--verbose-link") catch unreachable;
20772070 if (builder.verbose_cc or self.verbose_cc) zig_args.append("--verbose-cc") catch unreachable;
20782071 if (builder.verbose_llvm_cpu_features) zig_args.append("--verbose-llvm-cpu-features") catch unreachable;
2079 if (builder.system_linker_hack or self.system_linker_hack) zig_args.append("--system-linker-hack") catch unreachable;
20802072
20812073 if (self.emit_llvm_ir) try zig_args.append("-femit-llvm-ir");
20822074 if (self.emit_asm) try zig_args.append("-femit-asm");
lib/std/special/build_runner.zig-3
......@@ -112,8 +112,6 @@ pub fn main() !void {
112112 builder.verbose_cc = true;
113113 } else if (mem.eql(u8, arg, "--verbose-llvm-cpu-features")) {
114114 builder.verbose_llvm_cpu_features = true;
115 } else if (mem.eql(u8, arg, "--system-linker-hack")) {
116 builder.system_linker_hack = true;
117115 } else if (mem.eql(u8, arg, "--")) {
118116 builder.args = argsRest(args, arg_idx);
119117 break;
......@@ -215,7 +213,6 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void
215213 \\ --verbose-cimport Enable compiler debug output for C imports
216214 \\ --verbose-cc Enable compiler debug output for C compilation
217215 \\ --verbose-llvm-cpu-features Enable compiler debug output for LLVM CPU features
218 \\ --system-linker-hack Use system linker LD instead of LLD (requires LLVM enabled)
219216 \\
220217 );
221218}
src/Compilation.zig+18-11
......@@ -348,8 +348,6 @@ pub const InitOptions = struct {
348348 want_valgrind: ?bool = null,
349349 use_llvm: ?bool = null,
350350 use_lld: ?bool = null,
351 /// When set, this option will overwrite LLD with the system linker LD.
352 system_linker_hack: bool = false,
353351 use_clang: ?bool = null,
354352 rdynamic: bool = false,
355353 strip: bool = false,
......@@ -474,13 +472,22 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
474472 break :blk false;
475473 };
476474
477 const syslibroot = if (build_options.have_llvm and comptime std.Target.current.isDarwin()) outer: {
478 const path = if (use_lld and options.is_native_os and options.target.isDarwin()) inner: {
479 const syslibroot_path = try std.zig.system.getSDKPath(arena);
480 break :inner syslibroot_path;
481 } else null;
482 break :outer path;
483 } else null;
475 const DarwinOptions = struct {
476 syslibroot: ?[]const u8 = null,
477 system_linker_hack: bool = false,
478 };
479
480 const darwin_options: DarwinOptions = if (build_options.have_llvm and comptime std.Target.current.isDarwin()) outer: {
481 const opts: DarwinOptions = if (use_lld and options.is_native_os and options.target.isDarwin()) inner: {
482 const syslibroot = try std.zig.system.getSDKPath(arena);
483 const system_linker_hack = if (std.os.getenv("ZIG_SYSTEM_LINKER_HACK")) |_| true else false;
484 break :inner .{
485 .syslibroot = syslibroot,
486 .system_linker_hack = system_linker_hack,
487 };
488 } else .{};
489 break :outer opts;
490 } else .{};
484491
485492 const link_libc = options.link_libc or target_util.osRequiresLibC(options.target);
486493
......@@ -777,14 +784,14 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
777784 .optimize_mode = options.optimize_mode,
778785 .use_lld = use_lld,
779786 .use_llvm = use_llvm,
780 .system_linker_hack = options.system_linker_hack,
787 .system_linker_hack = darwin_options.system_linker_hack,
781788 .link_libc = link_libc,
782789 .link_libcpp = options.link_libcpp,
783790 .objects = options.link_objects,
784791 .frameworks = options.frameworks,
785792 .framework_dirs = options.framework_dirs,
786793 .system_libs = system_libs,
787 .syslibroot = syslibroot,
794 .syslibroot = darwin_options.syslibroot,
788795 .lib_dirs = options.lib_dirs,
789796 .rpath_list = options.rpath_list,
790797 .strip = strip,
src/link.zig+2-2
......@@ -57,8 +57,8 @@ pub const Options = struct {
5757 /// other objects.
5858 /// Otherwise (depending on `use_lld`) this link code directly outputs and updates the final binary.
5959 use_llvm: bool,
60 /// If this is true and `use_llvm` is true, this link code will use system linker `ld` instead of
61 /// the LLD.
60 /// Darwin-only. If this is true, `use_llvm` is true, and `is_native_os` is true, this link code will
61 /// use system linker `ld` instead of the LLD.
6262 system_linker_hack: bool,
6363 link_libc: bool,
6464 link_libcpp: bool,
src/link/Elf.zig+40-66
......@@ -1353,20 +1353,14 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13531353 // Create an LLD command line and invoke it.
13541354 var argv = std.ArrayList([]const u8).init(self.base.allocator);
13551355 defer argv.deinit();
1356
1357 if (self.base.options.is_native_os and self.base.options.system_linker_hack) {
1358 try argv.append("ld");
1359 } else {
1360 // Even though we're calling LLD as a library it thinks the first argument is its own exe name.
1361 try argv.append("lld");
1362
1363 try argv.append("-error-limit=0");
1364 }
1365
1356 // Even though we're calling LLD as a library it thinks the first argument is its own exe name.
1357 try argv.append("lld");
13661358 if (is_obj) {
13671359 try argv.append("-r");
13681360 }
13691361
1362 try argv.append("-error-limit=0");
1363
13701364 if (self.base.options.output_mode == .Exe) {
13711365 try argv.append("-z");
13721366 try argv.append(try std.fmt.allocPrint(arena, "stack-size={}", .{stack_size}));
......@@ -1622,63 +1616,43 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
16221616 Compilation.dump_argv(argv.items);
16231617 }
16241618
1625 if (self.base.options.is_native_os and self.base.options.system_linker_hack) {
1626 const result = try std.ChildProcess.exec(.{ .allocator = self.base.allocator, .argv = argv.items });
1627 defer {
1628 self.base.allocator.free(result.stdout);
1629 self.base.allocator.free(result.stderr);
1630 }
1631 if (result.stdout.len != 0) {
1632 std.log.warn("unexpected LD stdout: {}", .{result.stdout});
1633 }
1634 if (result.stderr.len != 0) {
1635 std.log.warn("unexpected LD stderr: {}", .{result.stderr});
1636 }
1637 if (result.term.Exited != 0) {
1638 // TODO parse this output and surface with the Compilation API rather than
1639 // directly outputting to stderr here.
1640 std.debug.print("{}", .{result.stderr});
1641 return error.LDReportedFailure;
1642 }
1643 } else {
1644 // Oh, snapplesauce! We need null terminated argv.
1645 const new_argv = try arena.allocSentinel(?[*:0]const u8, argv.items.len, null);
1646 for (argv.items) |arg, i| {
1647 new_argv[i] = try arena.dupeZ(u8, arg);
1648 }
1619 // Oh, snapplesauce! We need null terminated argv.
1620 const new_argv = try arena.allocSentinel(?[*:0]const u8, argv.items.len, null);
1621 for (argv.items) |arg, i| {
1622 new_argv[i] = try arena.dupeZ(u8, arg);
1623 }
16491624
1650 var stderr_context: LLDContext = .{
1651 .elf = self,
1652 .data = std.ArrayList(u8).init(self.base.allocator),
1653 };
1654 defer stderr_context.data.deinit();
1655 var stdout_context: LLDContext = .{
1656 .elf = self,
1657 .data = std.ArrayList(u8).init(self.base.allocator),
1658 };
1659 defer stdout_context.data.deinit();
1660 const llvm = @import("../llvm.zig");
1661 const ok = llvm.Link(
1662 .ELF,
1663 new_argv.ptr,
1664 new_argv.len,
1665 append_diagnostic,
1666 @ptrToInt(&stdout_context),
1667 @ptrToInt(&stderr_context),
1668 );
1669 if (stderr_context.oom or stdout_context.oom) return error.OutOfMemory;
1670 if (stdout_context.data.items.len != 0) {
1671 std.log.warn("unexpected LLD stdout: {}", .{stdout_context.data.items});
1672 }
1673 if (!ok) {
1674 // TODO parse this output and surface with the Compilation API rather than
1675 // directly outputting to stderr here.
1676 std.debug.print("{}", .{stderr_context.data.items});
1677 return error.LLDReportedFailure;
1678 }
1679 if (stderr_context.data.items.len != 0) {
1680 std.log.warn("unexpected LLD stderr: {}", .{stderr_context.data.items});
1681 }
1625 var stderr_context: LLDContext = .{
1626 .elf = self,
1627 .data = std.ArrayList(u8).init(self.base.allocator),
1628 };
1629 defer stderr_context.data.deinit();
1630 var stdout_context: LLDContext = .{
1631 .elf = self,
1632 .data = std.ArrayList(u8).init(self.base.allocator),
1633 };
1634 defer stdout_context.data.deinit();
1635 const llvm = @import("../llvm.zig");
1636 const ok = llvm.Link(
1637 .ELF,
1638 new_argv.ptr,
1639 new_argv.len,
1640 append_diagnostic,
1641 @ptrToInt(&stdout_context),
1642 @ptrToInt(&stderr_context),
1643 );
1644 if (stderr_context.oom or stdout_context.oom) return error.OutOfMemory;
1645 if (stdout_context.data.items.len != 0) {
1646 std.log.warn("unexpected LLD stdout: {}", .{stdout_context.data.items});
1647 }
1648 if (!ok) {
1649 // TODO parse this output and surface with the Compilation API rather than
1650 // directly outputting to stderr here.
1651 std.debug.print("{}", .{stderr_context.data.items});
1652 return error.LLDReportedFailure;
1653 }
1654 if (stderr_context.data.items.len != 0) {
1655 std.log.warn("unexpected LLD stderr: {}", .{stderr_context.data.items});
16821656 }
16831657
16841658 if (!self.base.options.disable_lld_caching) {
src/link/MachO.zig+6-2
......@@ -534,7 +534,9 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
534534 defer argv.deinit();
535535
536536 // TODO https://github.com/ziglang/zig/issues/6971
537 if (self.base.options.is_native_os and self.base.options.system_linker_hack) {
537 // Note that there is no need to check if running natively since we do that already
538 // when setting `system_linker_hack` in Compilation struct.
539 if (self.base.options.system_linker_hack) {
538540 try argv.append("ld");
539541 } else {
540542 // 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 {
710712 }
711713
712714 // TODO https://github.com/ziglang/zig/issues/6971
713 if (self.base.options.is_native_os and self.base.options.system_linker_hack) {
715 // Note that there is no need to check if running natively since we do that already
716 // when setting `system_linker_hack` in Compilation struct.
717 if (self.base.options.system_linker_hack) {
714718 const result = try std.ChildProcess.exec(.{ .allocator = self.base.allocator, .argv = argv.items });
715719 defer {
716720 self.base.allocator.free(result.stdout);
src/main.zig-11
......@@ -317,7 +317,6 @@ const usage_build_generic =
317317 \\ --image-base [addr] Set base address for executable image
318318 \\ -framework [name] (darwin) link against framework
319319 \\ -F[dir] (darwin) add search path for frameworks
320 \\ --system-linker-hack Use system linker LD instead of LLD (requires LLVM enabled)
321320 \\
322321 \\Test Options:
323322 \\ --test-filter [text] Skip tests that do not match filter
......@@ -475,7 +474,6 @@ fn buildOutputType(
475474 var image_base_override: ?u64 = null;
476475 var use_llvm: ?bool = null;
477476 var use_lld: ?bool = null;
478 var system_linker_hack = false;
479477 var use_clang: ?bool = null;
480478 var link_eh_frame_hdr = false;
481479 var link_emit_relocs = false;
......@@ -917,8 +915,6 @@ fn buildOutputType(
917915 mem.startsWith(u8, arg, "-I"))
918916 {
919917 try clang_argv.append(arg);
920 } else if (mem.startsWith(u8, arg, "--system-linker-hack")) {
921 system_linker_hack = true;
922918 } else {
923919 fatal("unrecognized parameter: '{}'", .{arg});
924920 }
......@@ -1643,7 +1639,6 @@ fn buildOutputType(
16431639 .want_valgrind = want_valgrind,
16441640 .use_llvm = use_llvm,
16451641 .use_lld = use_lld,
1646 .system_linker_hack = system_linker_hack,
16471642 .use_clang = use_clang,
16481643 .rdynamic = rdynamic,
16491644 .linker_script = linker_script,
......@@ -2165,7 +2160,6 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
21652160 var override_global_cache_dir: ?[]const u8 = null;
21662161 var override_local_cache_dir: ?[]const u8 = null;
21672162 var child_argv = std.ArrayList([]const u8).init(arena);
2168 var system_linker_hack = false;
21692163
21702164 const argv_index_exe = child_argv.items.len;
21712165 _ = try child_argv.addOne();
......@@ -2206,10 +2200,6 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
22062200 override_global_cache_dir = args[i];
22072201 try child_argv.appendSlice(&[_][]const u8{ arg, args[i] });
22082202 continue;
2209 } else if (mem.eql(u8, arg, "--system-linker-hack")) {
2210 system_linker_hack = true;
2211 try child_argv.append(arg);
2212 continue;
22132203 }
22142204 }
22152205 try child_argv.append(arg);
......@@ -2345,7 +2335,6 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
23452335 .optimize_mode = .Debug,
23462336 .self_exe_path = self_exe_path,
23472337 .rand = &default_prng.random,
2348 .system_linker_hack = system_linker_hack,
23492338 }) catch |err| {
23502339 fatal("unable to create compilation: {}", .{@errorName(err)});
23512340 };