authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-11 20:28:14-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-05-11 20:28:14-04:00
loge3dd4dc91d65702c085dac9cc4d85aebfa308dfa
treeee8aadf75cd14cd5bf9833b368b2553aae2365a8
parente6881d4373ce355f3f4565c22c125870ed8fcfc8
parentf8cf106fc971c9c61709443860b1f728ca4cfc9a
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #8737 from ifreund/link-system-libc

stage2: use system libc when targeting the native OS/ABI

4 files changed, 26 insertions(+), 19 deletions(-)

ci/azure/macos_arm64_script+4-4
......@@ -55,11 +55,11 @@ cmake .. \
5555 -DZIG_TARGET_MCPU="$HOST_MCPU" \
5656 -DZIG_STATIC=ON
5757
58make $JOBS install
59
6058unset CC
6159unset CXX
6260
61make $JOBS install
62
6363# Build zig compiler cross-compiled for arm64
6464cd $ZIGDIR
6565
......@@ -79,11 +79,11 @@ cmake .. \
7979 -DZIG_EXECUTABLE="$ZIG" \
8080 -DZIG_STATIC=ON
8181
82make $JOBS install
83
8482unset CC
8583unset CXX
8684
85make $JOBS install
86
8787if [ "${BUILD_REASON}" != "PullRequest" ]; then
8888 mv ../LICENSE release/
8989
src/Compilation.zig+8-8
......@@ -848,7 +848,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
848848 arena,
849849 options.zig_lib_directory.path.?,
850850 options.target,
851 options.is_native_os,
851 options.is_native_abi,
852852 link_libc,
853853 options.libc_installation,
854854 );
......@@ -2885,7 +2885,7 @@ fn detectLibCIncludeDirs(
28852885 arena: *Allocator,
28862886 zig_lib_dir: []const u8,
28872887 target: Target,
2888 is_native_os: bool,
2888 is_native_abi: bool,
28892889 link_libc: bool,
28902890 libc_installation: ?*const LibCInstallation,
28912891) !LibCDirs {
......@@ -2900,6 +2900,12 @@ fn detectLibCIncludeDirs(
29002900 return detectLibCFromLibCInstallation(arena, target, lci);
29012901 }
29022902
2903 if (is_native_abi) {
2904 const libc = try arena.create(LibCInstallation);
2905 libc.* = try LibCInstallation.findNative(.{ .allocator = arena });
2906 return detectLibCFromLibCInstallation(arena, target, libc);
2907 }
2908
29032909 if (target_util.canBuildLibC(target)) {
29042910 const generic_name = target_util.libCGenericName(target);
29052911 // Some architectures are handled by the same set of headers.
......@@ -2950,12 +2956,6 @@ fn detectLibCIncludeDirs(
29502956 };
29512957 }
29522958
2953 if (is_native_os) {
2954 const libc = try arena.create(LibCInstallation);
2955 libc.* = try LibCInstallation.findNative(.{ .allocator = arena });
2956 return detectLibCFromLibCInstallation(arena, target, libc);
2957 }
2958
29592959 return LibCDirs{
29602960 .libc_include_dir_list = &[0][]u8{},
29612961 .libc_installation = null,
src/glibc.zig+5-4
......@@ -764,16 +764,17 @@ pub fn buildSharedObjects(comp: *Compilation) !void {
764764 .lt => continue,
765765 .gt => {
766766 // TODO Expose via compile error mechanism instead of log.
767 std.log.warn("invalid target glibc version: {}", .{target_version});
767 std.log.err("invalid target glibc version: {}", .{target_version});
768768 return error.InvalidTargetGLibCVersion;
769769 },
770770 }
771 } else blk: {
771 } else {
772772 const latest_index = metadata.all_versions.len - 1;
773 std.log.warn("zig cannot build new glibc version {}; providing instead {}", .{
773 // TODO Expose via compile error mechanism instead of log.
774 std.log.err("zig does not yet provide glibc version {}, the max provided version is {}", .{
774775 target_version, metadata.all_versions[latest_index],
775776 });
776 break :blk latest_index;
777 return error.InvalidTargetGLibCVersion;
777778 };
778779 {
779780 var map_contents = std.ArrayList(u8).init(arena);
src/link/Elf.zig+9-3
......@@ -1650,9 +1650,15 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
16501650 if (self.base.options.libc_installation != null) {
16511651 const needs_grouping = self.base.options.link_mode == .Static;
16521652 if (needs_grouping) try argv.append("--start-group");
1653 try argv.append("-lm");
1654 try argv.append("-lpthread");
1655 try argv.append("-lc");
1653 // This matches the order of glibc.libs
1654 try argv.appendSlice(&[_][]const u8{
1655 "-lm",
1656 "-lpthread",
1657 "-lc",
1658 "-ldl",
1659 "-lrt",
1660 "-lutil",
1661 });
16561662 if (needs_grouping) try argv.append("--end-group");
16571663 } else if (target.isGnuLibC()) {
16581664 try argv.append(comp.libunwind_static_lib.?.full_object_path);