authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-06-25 22:37:46+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-06-25 22:37:46+02:00
log7a85dc6935ffaf1828cdc75c2602fb232217f7b3
tree4b18151af1320b2801054a5dc0e1765a541ddc62
parent73f77f30804f3a512fbb35caad3d87ff2778ecc0
parentf9171bf54295df0bd8f4dbe1ab9698e5940ba445
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #9202 from ifreund/sysroot

stage2: add --sysroot link option

7 files changed, 60 insertions(+), 26 deletions(-)

lib/std/build.zig+5
......@@ -57,6 +57,7 @@ pub const Builder = struct {
5757 exe_dir: []const u8,
5858 h_dir: []const u8,
5959 install_path: []const u8,
60 sysroot: ?[]const u8 = null,
6061 search_prefixes: ArrayList([]const u8),
6162 libc_file: ?[]const u8 = null,
6263 installed_files: ArrayList(InstalledFile),
......@@ -2701,6 +2702,10 @@ pub const LibExeObjStep = struct {
27012702 }
27022703 }
27032704
2705 if (builder.sysroot) |sysroot| {
2706 try zig_args.appendSlice(&[_][]const u8{ "--sysroot", sysroot });
2707 }
2708
27042709 for (builder.search_prefixes.items) |search_prefix| {
27052710 try zig_args.append("-L");
27062711 try zig_args.append(try fs.path.join(builder.allocator, &[_][]const u8{
lib/std/special/build_runner.zig+7
......@@ -104,6 +104,12 @@ pub fn main() !void {
104104 warn("Expected argument after {s}\n\n", .{arg});
105105 return usageAndErr(builder, false, stderr_stream);
106106 };
107 } else if (mem.eql(u8, arg, "--sysroot")) {
108 const sysroot = nextArg(args, &arg_idx) orelse {
109 warn("Expected argument after --sysroot\n\n", .{});
110 return usageAndErr(builder, false, stderr_stream);
111 };
112 builder.sysroot = sysroot;
107113 } else if (mem.eql(u8, arg, "--search-prefix")) {
108114 const search_prefix = nextArg(args, &arg_idx) orelse {
109115 warn("Expected argument after --search-prefix\n\n", .{});
......@@ -214,6 +220,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void
214220 \\ --prefix-exe-dir [path] Override default executable directory path
215221 \\ --prefix-include-dir [path] Override default include directory path
216222 \\
223 \\ --sysroot [path] Set the system root directory (usually /)
217224 \\ --search-prefix [path] Add a path to look for binaries, libraries, headers
218225 \\ --libc [file] Provide a file which specifies libc paths
219226 \\
src/Compilation.zig+25-17
......@@ -612,6 +612,7 @@ pub const InitOptions = struct {
612612 output_mode: std.builtin.OutputMode,
613613 thread_pool: *ThreadPool,
614614 dynamic_linker: ?[]const u8 = null,
615 sysroot: ?[]const u8 = null,
615616 /// `null` means to not emit a binary file.
616617 emit_bin: ?EmitLoc,
617618 /// `null` means to not emit a C header file.
......@@ -879,25 +880,32 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
879880 break :blk false;
880881 };
881882
882 const DarwinOptions = struct {
883 syslibroot: ?[]const u8 = null,
884 system_linker_hack: bool = false,
883 const darwin_can_use_system_linker_and_sdk =
884 // comptime conditions
885 ((build_options.have_llvm and comptime std.Target.current.isDarwin()) and
886 // runtime conditions
887 (use_lld and std.builtin.os.tag == .macos and options.target.isDarwin()));
888
889 const darwin_system_linker_hack = blk: {
890 if (darwin_can_use_system_linker_and_sdk) {
891 break :blk std.os.getenv("ZIG_SYSTEM_LINKER_HACK") != null;
892 } else {
893 break :blk false;
894 }
885895 };
886896
887 const darwin_options: DarwinOptions = if (build_options.have_llvm and comptime std.Target.current.isDarwin()) outer: {
888 const opts: DarwinOptions = if (use_lld and std.builtin.os.tag == .macos and options.target.isDarwin()) inner: {
897 const sysroot = blk: {
898 if (options.sysroot) |sysroot| {
899 break :blk sysroot;
900 } else if (darwin_can_use_system_linker_and_sdk) {
889901 // TODO Revisit this targeting versions lower than macOS 11 when LLVM 12 is out.
890902 // See https://github.com/ziglang/zig/issues/6996
891903 const at_least_big_sur = options.target.os.getVersionRange().semver.min.major >= 11;
892 const syslibroot = if (at_least_big_sur) try std.zig.system.getSDKPath(arena) else null;
893 const system_linker_hack = std.os.getenv("ZIG_SYSTEM_LINKER_HACK") != null;
894 break :inner .{
895 .syslibroot = syslibroot,
896 .system_linker_hack = system_linker_hack,
897 };
898 } else .{};
899 break :outer opts;
900 } else .{};
904 break :blk if (at_least_big_sur) try std.zig.system.getSDKPath(arena) else null;
905 } else {
906 break :blk null;
907 }
908 };
901909
902910 const lto = blk: {
903911 if (options.want_lto) |explicit| {
......@@ -908,7 +916,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
908916 break :blk false;
909917 } else if (options.c_source_files.len == 0) {
910918 break :blk false;
911 } else if (darwin_options.system_linker_hack) {
919 } else if (darwin_system_linker_hack) {
912920 break :blk false;
913921 } else switch (options.output_mode) {
914922 .Lib, .Obj => break :blk false,
......@@ -1281,13 +1289,14 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
12811289 .module = module,
12821290 .target = options.target,
12831291 .dynamic_linker = options.dynamic_linker,
1292 .sysroot = sysroot,
12841293 .output_mode = options.output_mode,
12851294 .link_mode = link_mode,
12861295 .object_format = ofmt,
12871296 .optimize_mode = options.optimize_mode,
12881297 .use_lld = use_lld,
12891298 .use_llvm = use_llvm,
1290 .system_linker_hack = darwin_options.system_linker_hack,
1299 .system_linker_hack = darwin_system_linker_hack,
12911300 .link_libc = link_libc,
12921301 .link_libcpp = link_libcpp,
12931302 .link_libunwind = link_libunwind,
......@@ -1295,7 +1304,6 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
12951304 .frameworks = options.frameworks,
12961305 .framework_dirs = options.framework_dirs,
12971306 .system_libs = system_libs,
1298 .syslibroot = darwin_options.syslibroot,
12991307 .wasi_emulated_libs = options.wasi_emulated_libs,
13001308 .lib_dirs = options.lib_dirs,
13011309 .rpath_list = options.rpath_list,
src/link.zig+2-2
......@@ -38,6 +38,8 @@ pub const Options = struct {
3838 /// Not every Compilation compiles .zig code! For example you could do `zig build-exe foo.o`.
3939 module: ?*Module,
4040 dynamic_linker: ?[]const u8,
41 /// The root path for the dynamic linker and system libraries (as well as frameworks on Darwin)
42 sysroot: ?[]const u8,
4143 /// Used for calculating how much space to reserve for symbols in case the binary file
4244 /// does not already have a symbol table.
4345 symbol_count_hint: u64 = 32,
......@@ -104,8 +106,6 @@ pub const Options = struct {
104106 llvm_cpu_features: ?[*:0]const u8,
105107 /// Extra args passed directly to LLD. Ignored when not linking with LLD.
106108 extra_lld_args: []const []const u8,
107 /// Darwin-only. Set the root path to the system libraries and frameworks.
108 syslibroot: ?[]const u8,
109109
110110 objects: []const []const u8,
111111 framework_dirs: []const []const u8,
src/link/Elf.zig+5
......@@ -1354,6 +1354,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13541354 man.hash.add(allow_shlib_undefined);
13551355 man.hash.add(self.base.options.bind_global_refs_locally);
13561356 man.hash.add(self.base.options.tsan);
1357 man.hash.addOptionalBytes(self.base.options.sysroot);
13571358
13581359 // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock.
13591360 _ = try man.hit();
......@@ -1423,6 +1424,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
14231424
14241425 try argv.append("-error-limit=0");
14251426
1427 if (self.base.options.sysroot) |sysroot| {
1428 try argv.append(try std.fmt.allocPrint(arena, "--sysroot={s}", .{sysroot}));
1429 }
1430
14261431 if (self.base.options.lto) {
14271432 switch (self.base.options.optimize_mode) {
14281433 .Debug => {},
src/link/MachO.zig+6-6
......@@ -704,7 +704,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
704704 man.hash.add(allow_shlib_undefined);
705705 man.hash.add(self.base.options.bind_global_refs_locally);
706706 man.hash.add(self.base.options.system_linker_hack);
707 man.hash.addOptionalBytes(self.base.options.syslibroot);
707 man.hash.addOptionalBytes(self.base.options.sysroot);
708708
709709 // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock.
710710 _ = try man.hit();
......@@ -789,7 +789,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
789789 zld.deinit();
790790 }
791791 zld.arch = target.cpu.arch;
792 zld.syslibroot = self.base.options.syslibroot;
792 zld.syslibroot = self.base.options.sysroot;
793793 zld.stack_size = stack_size;
794794
795795 // Positional arguments to the linker such as object files and static archives.
......@@ -833,7 +833,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
833833 try resolvePaths(
834834 arena,
835835 &libs,
836 self.base.options.syslibroot,
836 self.base.options.sysroot,
837837 self.base.options.lib_dirs,
838838 search_lib_names.items,
839839 .lib,
......@@ -856,7 +856,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
856856 try resolvePaths(
857857 arena,
858858 &libs,
859 self.base.options.syslibroot,
859 self.base.options.sysroot,
860860 self.base.options.framework_dirs,
861861 self.base.options.frameworks,
862862 .framework,
......@@ -868,7 +868,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
868868 try argv.append("zig");
869869 try argv.append("ld");
870870
871 if (self.base.options.syslibroot) |syslibroot| {
871 if (self.base.options.sysroot) |syslibroot| {
872872 try argv.append("-syslibroot");
873873 try argv.append(syslibroot);
874874 }
......@@ -1017,7 +1017,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void {
10171017 }
10181018 }
10191019
1020 if (self.base.options.syslibroot) |dir| {
1020 if (self.base.options.sysroot) |dir| {
10211021 try argv.append("-syslibroot");
10221022 try argv.append(dir);
10231023 }
src/main.zig+10-1
......@@ -379,6 +379,7 @@ const usage_build_generic =
379379 \\ -T[script], --script [script] Use a custom linker script
380380 \\ --version-script [path] Provide a version .map file
381381 \\ --dynamic-linker [path] Set the dynamic interpreter path (usually ld.so)
382 \\ --sysroot [path] Set the system root directory (usually /)
382383 \\ --version [ver] Dynamic library semver
383384 \\ -fsoname[=name] (Linux) Override the default SONAME value
384385 \\ -fno-soname (Linux) Disable emitting a SONAME
......@@ -604,6 +605,7 @@ fn buildOutputType(
604605 var link_eh_frame_hdr = false;
605606 var link_emit_relocs = false;
606607 var each_lib_rpath: ?bool = null;
608 var sysroot: ?[]const u8 = null;
607609 var libc_paths_file: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIBC");
608610 var machine_code_model: std.builtin.CodeModel = .default;
609611 var runtime_args_start: ?usize = null;
......@@ -865,6 +867,10 @@ fn buildOutputType(
865867 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
866868 i += 1;
867869 target_dynamic_linker = args[i];
870 } else if (mem.eql(u8, arg, "--sysroot")) {
871 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
872 i += 1;
873 sysroot = args[i];
868874 } else if (mem.eql(u8, arg, "--libc")) {
869875 if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg});
870876 i += 1;
......@@ -1641,7 +1647,9 @@ fn buildOutputType(
16411647 want_native_include_dirs = true;
16421648 }
16431649
1644 if (cross_target.isNativeOs() and (system_libs.items.len != 0 or want_native_include_dirs)) {
1650 if (sysroot == null and cross_target.isNativeOs() and
1651 (system_libs.items.len != 0 or want_native_include_dirs))
1652 {
16451653 const paths = std.zig.system.NativePaths.detect(arena, target_info) catch |err| {
16461654 fatal("unable to detect native system paths: {s}", .{@errorName(err)});
16471655 };
......@@ -1918,6 +1926,7 @@ fn buildOutputType(
19181926 .is_native_os = cross_target.isNativeOs(),
19191927 .is_native_abi = cross_target.isNativeAbi(),
19201928 .dynamic_linker = target_info.dynamic_linker.get(),
1929 .sysroot = sysroot,
19211930 .output_mode = output_mode,
19221931 .root_pkg = root_pkg,
19231932 .emit_bin = emit_bin_loc,