| author | |
| committer | |
| log | 7a85dc6935ffaf1828cdc75c2602fb232217f7b3 |
| tree | 4b18151af1320b2801054a5dc0e1765a541ddc62 |
| parent | 73f77f30804f3a512fbb35caad3d87ff2778ecc0 |
| parent | f9171bf54295df0bd8f4dbe1ab9698e5940ba445 |
| signature | Signed by PGP key 4AEE18F83AFDEB23 |
stage2: add --sysroot link option7 files changed, 60 insertions(+), 26 deletions(-)
lib/std/build.zig+5| ... | ... | @@ -57,6 +57,7 @@ pub const Builder = struct { |
| 57 | 57 | exe_dir: []const u8, |
| 58 | 58 | h_dir: []const u8, |
| 59 | 59 | install_path: []const u8, |
| 60 | sysroot: ?[]const u8 = null, | |
| 60 | 61 | search_prefixes: ArrayList([]const u8), |
| 61 | 62 | libc_file: ?[]const u8 = null, |
| 62 | 63 | installed_files: ArrayList(InstalledFile), |
| ... | ... | @@ -2701,6 +2702,10 @@ pub const LibExeObjStep = struct { |
| 2701 | 2702 | } |
| 2702 | 2703 | } |
| 2703 | 2704 | |
| 2705 | if (builder.sysroot) |sysroot| { | |
| 2706 | try zig_args.appendSlice(&[_][]const u8{ "--sysroot", sysroot }); | |
| 2707 | } | |
| 2708 | ||
| 2704 | 2709 | for (builder.search_prefixes.items) |search_prefix| { |
| 2705 | 2710 | try zig_args.append("-L"); |
| 2706 | 2711 | 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 { |
| 104 | 104 | warn("Expected argument after {s}\n\n", .{arg}); |
| 105 | 105 | return usageAndErr(builder, false, stderr_stream); |
| 106 | 106 | }; |
| 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; | |
| 107 | 113 | } else if (mem.eql(u8, arg, "--search-prefix")) { |
| 108 | 114 | const search_prefix = nextArg(args, &arg_idx) orelse { |
| 109 | 115 | warn("Expected argument after --search-prefix\n\n", .{}); |
| ... | ... | @@ -214,6 +220,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void |
| 214 | 220 | \\ --prefix-exe-dir [path] Override default executable directory path |
| 215 | 221 | \\ --prefix-include-dir [path] Override default include directory path |
| 216 | 222 | \\ |
| 223 | \\ --sysroot [path] Set the system root directory (usually /) | |
| 217 | 224 | \\ --search-prefix [path] Add a path to look for binaries, libraries, headers |
| 218 | 225 | \\ --libc [file] Provide a file which specifies libc paths |
| 219 | 226 | \\ |
src/Compilation.zig+25-17| ... | ... | @@ -612,6 +612,7 @@ pub const InitOptions = struct { |
| 612 | 612 | output_mode: std.builtin.OutputMode, |
| 613 | 613 | thread_pool: *ThreadPool, |
| 614 | 614 | dynamic_linker: ?[]const u8 = null, |
| 615 | sysroot: ?[]const u8 = null, | |
| 615 | 616 | /// `null` means to not emit a binary file. |
| 616 | 617 | emit_bin: ?EmitLoc, |
| 617 | 618 | /// `null` means to not emit a C header file. |
| ... | ... | @@ -879,25 +880,32 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { |
| 879 | 880 | break :blk false; |
| 880 | 881 | }; |
| 881 | 882 | |
| 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 | } | |
| 885 | 895 | }; |
| 886 | 896 | |
| 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) { | |
| 889 | 901 | // TODO Revisit this targeting versions lower than macOS 11 when LLVM 12 is out. |
| 890 | 902 | // See https://github.com/ziglang/zig/issues/6996 |
| 891 | 903 | 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 | }; | |
| 901 | 909 | |
| 902 | 910 | const lto = blk: { |
| 903 | 911 | if (options.want_lto) |explicit| { |
| ... | ... | @@ -908,7 +916,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { |
| 908 | 916 | break :blk false; |
| 909 | 917 | } else if (options.c_source_files.len == 0) { |
| 910 | 918 | break :blk false; |
| 911 | } else if (darwin_options.system_linker_hack) { | |
| 919 | } else if (darwin_system_linker_hack) { | |
| 912 | 920 | break :blk false; |
| 913 | 921 | } else switch (options.output_mode) { |
| 914 | 922 | .Lib, .Obj => break :blk false, |
| ... | ... | @@ -1281,13 +1289,14 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { |
| 1281 | 1289 | .module = module, |
| 1282 | 1290 | .target = options.target, |
| 1283 | 1291 | .dynamic_linker = options.dynamic_linker, |
| 1292 | .sysroot = sysroot, | |
| 1284 | 1293 | .output_mode = options.output_mode, |
| 1285 | 1294 | .link_mode = link_mode, |
| 1286 | 1295 | .object_format = ofmt, |
| 1287 | 1296 | .optimize_mode = options.optimize_mode, |
| 1288 | 1297 | .use_lld = use_lld, |
| 1289 | 1298 | .use_llvm = use_llvm, |
| 1290 | .system_linker_hack = darwin_options.system_linker_hack, | |
| 1299 | .system_linker_hack = darwin_system_linker_hack, | |
| 1291 | 1300 | .link_libc = link_libc, |
| 1292 | 1301 | .link_libcpp = link_libcpp, |
| 1293 | 1302 | .link_libunwind = link_libunwind, |
| ... | ... | @@ -1295,7 +1304,6 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { |
| 1295 | 1304 | .frameworks = options.frameworks, |
| 1296 | 1305 | .framework_dirs = options.framework_dirs, |
| 1297 | 1306 | .system_libs = system_libs, |
| 1298 | .syslibroot = darwin_options.syslibroot, | |
| 1299 | 1307 | .wasi_emulated_libs = options.wasi_emulated_libs, |
| 1300 | 1308 | .lib_dirs = options.lib_dirs, |
| 1301 | 1309 | .rpath_list = options.rpath_list, |
src/link.zig+2-2| ... | ... | @@ -38,6 +38,8 @@ pub const Options = struct { |
| 38 | 38 | /// Not every Compilation compiles .zig code! For example you could do `zig build-exe foo.o`. |
| 39 | 39 | module: ?*Module, |
| 40 | 40 | 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, | |
| 41 | 43 | /// Used for calculating how much space to reserve for symbols in case the binary file |
| 42 | 44 | /// does not already have a symbol table. |
| 43 | 45 | symbol_count_hint: u64 = 32, |
| ... | ... | @@ -104,8 +106,6 @@ pub const Options = struct { |
| 104 | 106 | llvm_cpu_features: ?[*:0]const u8, |
| 105 | 107 | /// Extra args passed directly to LLD. Ignored when not linking with LLD. |
| 106 | 108 | extra_lld_args: []const []const u8, |
| 107 | /// Darwin-only. Set the root path to the system libraries and frameworks. | |
| 108 | syslibroot: ?[]const u8, | |
| 109 | 109 | |
| 110 | 110 | objects: []const []const u8, |
| 111 | 111 | framework_dirs: []const []const u8, |
src/link/Elf.zig+5| ... | ... | @@ -1354,6 +1354,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { |
| 1354 | 1354 | man.hash.add(allow_shlib_undefined); |
| 1355 | 1355 | man.hash.add(self.base.options.bind_global_refs_locally); |
| 1356 | 1356 | man.hash.add(self.base.options.tsan); |
| 1357 | man.hash.addOptionalBytes(self.base.options.sysroot); | |
| 1357 | 1358 | |
| 1358 | 1359 | // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock. |
| 1359 | 1360 | _ = try man.hit(); |
| ... | ... | @@ -1423,6 +1424,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { |
| 1423 | 1424 | |
| 1424 | 1425 | try argv.append("-error-limit=0"); |
| 1425 | 1426 | |
| 1427 | if (self.base.options.sysroot) |sysroot| { | |
| 1428 | try argv.append(try std.fmt.allocPrint(arena, "--sysroot={s}", .{sysroot})); | |
| 1429 | } | |
| 1430 | ||
| 1426 | 1431 | if (self.base.options.lto) { |
| 1427 | 1432 | switch (self.base.options.optimize_mode) { |
| 1428 | 1433 | .Debug => {}, |
src/link/MachO.zig+6-6| ... | ... | @@ -704,7 +704,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { |
| 704 | 704 | man.hash.add(allow_shlib_undefined); |
| 705 | 705 | man.hash.add(self.base.options.bind_global_refs_locally); |
| 706 | 706 | 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); | |
| 708 | 708 | |
| 709 | 709 | // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock. |
| 710 | 710 | _ = try man.hit(); |
| ... | ... | @@ -789,7 +789,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { |
| 789 | 789 | zld.deinit(); |
| 790 | 790 | } |
| 791 | 791 | zld.arch = target.cpu.arch; |
| 792 | zld.syslibroot = self.base.options.syslibroot; | |
| 792 | zld.syslibroot = self.base.options.sysroot; | |
| 793 | 793 | zld.stack_size = stack_size; |
| 794 | 794 | |
| 795 | 795 | // Positional arguments to the linker such as object files and static archives. |
| ... | ... | @@ -833,7 +833,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { |
| 833 | 833 | try resolvePaths( |
| 834 | 834 | arena, |
| 835 | 835 | &libs, |
| 836 | self.base.options.syslibroot, | |
| 836 | self.base.options.sysroot, | |
| 837 | 837 | self.base.options.lib_dirs, |
| 838 | 838 | search_lib_names.items, |
| 839 | 839 | .lib, |
| ... | ... | @@ -856,7 +856,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { |
| 856 | 856 | try resolvePaths( |
| 857 | 857 | arena, |
| 858 | 858 | &libs, |
| 859 | self.base.options.syslibroot, | |
| 859 | self.base.options.sysroot, | |
| 860 | 860 | self.base.options.framework_dirs, |
| 861 | 861 | self.base.options.frameworks, |
| 862 | 862 | .framework, |
| ... | ... | @@ -868,7 +868,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { |
| 868 | 868 | try argv.append("zig"); |
| 869 | 869 | try argv.append("ld"); |
| 870 | 870 | |
| 871 | if (self.base.options.syslibroot) |syslibroot| { | |
| 871 | if (self.base.options.sysroot) |syslibroot| { | |
| 872 | 872 | try argv.append("-syslibroot"); |
| 873 | 873 | try argv.append(syslibroot); |
| 874 | 874 | } |
| ... | ... | @@ -1017,7 +1017,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { |
| 1017 | 1017 | } |
| 1018 | 1018 | } |
| 1019 | 1019 | |
| 1020 | if (self.base.options.syslibroot) |dir| { | |
| 1020 | if (self.base.options.sysroot) |dir| { | |
| 1021 | 1021 | try argv.append("-syslibroot"); |
| 1022 | 1022 | try argv.append(dir); |
| 1023 | 1023 | } |
src/main.zig+10-1| ... | ... | @@ -379,6 +379,7 @@ const usage_build_generic = |
| 379 | 379 | \\ -T[script], --script [script] Use a custom linker script |
| 380 | 380 | \\ --version-script [path] Provide a version .map file |
| 381 | 381 | \\ --dynamic-linker [path] Set the dynamic interpreter path (usually ld.so) |
| 382 | \\ --sysroot [path] Set the system root directory (usually /) | |
| 382 | 383 | \\ --version [ver] Dynamic library semver |
| 383 | 384 | \\ -fsoname[=name] (Linux) Override the default SONAME value |
| 384 | 385 | \\ -fno-soname (Linux) Disable emitting a SONAME |
| ... | ... | @@ -604,6 +605,7 @@ fn buildOutputType( |
| 604 | 605 | var link_eh_frame_hdr = false; |
| 605 | 606 | var link_emit_relocs = false; |
| 606 | 607 | var each_lib_rpath: ?bool = null; |
| 608 | var sysroot: ?[]const u8 = null; | |
| 607 | 609 | var libc_paths_file: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIBC"); |
| 608 | 610 | var machine_code_model: std.builtin.CodeModel = .default; |
| 609 | 611 | var runtime_args_start: ?usize = null; |
| ... | ... | @@ -865,6 +867,10 @@ fn buildOutputType( |
| 865 | 867 | if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg}); |
| 866 | 868 | i += 1; |
| 867 | 869 | 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]; | |
| 868 | 874 | } else if (mem.eql(u8, arg, "--libc")) { |
| 869 | 875 | if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg}); |
| 870 | 876 | i += 1; |
| ... | ... | @@ -1641,7 +1647,9 @@ fn buildOutputType( |
| 1641 | 1647 | want_native_include_dirs = true; |
| 1642 | 1648 | } |
| 1643 | 1649 | |
| 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 | { | |
| 1645 | 1653 | const paths = std.zig.system.NativePaths.detect(arena, target_info) catch |err| { |
| 1646 | 1654 | fatal("unable to detect native system paths: {s}", .{@errorName(err)}); |
| 1647 | 1655 | }; |
| ... | ... | @@ -1918,6 +1926,7 @@ fn buildOutputType( |
| 1918 | 1926 | .is_native_os = cross_target.isNativeOs(), |
| 1919 | 1927 | .is_native_abi = cross_target.isNativeAbi(), |
| 1920 | 1928 | .dynamic_linker = target_info.dynamic_linker.get(), |
| 1929 | .sysroot = sysroot, | |
| 1921 | 1930 | .output_mode = output_mode, |
| 1922 | 1931 | .root_pkg = root_pkg, |
| 1923 | 1932 | .emit_bin = emit_bin_loc, |