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 .. \...@@ -55,11 +55,11 @@ cmake .. \
55 -DZIG_TARGET_MCPU="$HOST_MCPU" \55 -DZIG_TARGET_MCPU="$HOST_MCPU" \
56 -DZIG_STATIC=ON56 -DZIG_STATIC=ON
5757
58make $JOBS install
59
60unset CC58unset CC
61unset CXX59unset CXX
6260
61make $JOBS install
62
63# Build zig compiler cross-compiled for arm6463# Build zig compiler cross-compiled for arm64
64cd $ZIGDIR64cd $ZIGDIR
6565
...@@ -79,11 +79,11 @@ cmake .. \...@@ -79,11 +79,11 @@ cmake .. \
79 -DZIG_EXECUTABLE="$ZIG" \79 -DZIG_EXECUTABLE="$ZIG" \
80 -DZIG_STATIC=ON80 -DZIG_STATIC=ON
8181
82make $JOBS install
83
84unset CC82unset CC
85unset CXX83unset CXX
8684
85make $JOBS install
86
87if [ "${BUILD_REASON}" != "PullRequest" ]; then87if [ "${BUILD_REASON}" != "PullRequest" ]; then
88 mv ../LICENSE release/88 mv ../LICENSE release/
8989
src/Compilation.zig+8-8
...@@ -848,7 +848,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -848,7 +848,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
848 arena,848 arena,
849 options.zig_lib_directory.path.?,849 options.zig_lib_directory.path.?,
850 options.target,850 options.target,
851 options.is_native_os,851 options.is_native_abi,
852 link_libc,852 link_libc,
853 options.libc_installation,853 options.libc_installation,
854 );854 );
...@@ -2885,7 +2885,7 @@ fn detectLibCIncludeDirs(...@@ -2885,7 +2885,7 @@ fn detectLibCIncludeDirs(
2885 arena: *Allocator,2885 arena: *Allocator,
2886 zig_lib_dir: []const u8,2886 zig_lib_dir: []const u8,
2887 target: Target,2887 target: Target,
2888 is_native_os: bool,2888 is_native_abi: bool,
2889 link_libc: bool,2889 link_libc: bool,
2890 libc_installation: ?*const LibCInstallation,2890 libc_installation: ?*const LibCInstallation,
2891) !LibCDirs {2891) !LibCDirs {
...@@ -2900,6 +2900,12 @@ fn detectLibCIncludeDirs(...@@ -2900,6 +2900,12 @@ fn detectLibCIncludeDirs(
2900 return detectLibCFromLibCInstallation(arena, target, lci);2900 return detectLibCFromLibCInstallation(arena, target, lci);
2901 }2901 }
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
2903 if (target_util.canBuildLibC(target)) {2909 if (target_util.canBuildLibC(target)) {
2904 const generic_name = target_util.libCGenericName(target);2910 const generic_name = target_util.libCGenericName(target);
2905 // Some architectures are handled by the same set of headers.2911 // Some architectures are handled by the same set of headers.
...@@ -2950,12 +2956,6 @@ fn detectLibCIncludeDirs(...@@ -2950,12 +2956,6 @@ fn detectLibCIncludeDirs(
2950 };2956 };
2951 }2957 }
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
2959 return LibCDirs{2959 return LibCDirs{
2960 .libc_include_dir_list = &[0][]u8{},2960 .libc_include_dir_list = &[0][]u8{},
2961 .libc_installation = null,2961 .libc_installation = null,
src/glibc.zig+5-4
...@@ -764,16 +764,17 @@ pub fn buildSharedObjects(comp: *Compilation) !void {...@@ -764,16 +764,17 @@ pub fn buildSharedObjects(comp: *Compilation) !void {
764 .lt => continue,764 .lt => continue,
765 .gt => {765 .gt => {
766 // TODO Expose via compile error mechanism instead of log.766 // 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});
768 return error.InvalidTargetGLibCVersion;768 return error.InvalidTargetGLibCVersion;
769 },769 },
770 }770 }
771 } else blk: {771 } else {
772 const latest_index = metadata.all_versions.len - 1;772 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 {}", .{
774 target_version, metadata.all_versions[latest_index],775 target_version, metadata.all_versions[latest_index],
775 });776 });
776 break :blk latest_index;777 return error.InvalidTargetGLibCVersion;
777 };778 };
778 {779 {
779 var map_contents = std.ArrayList(u8).init(arena);780 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 {...@@ -1650,9 +1650,15 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
1650 if (self.base.options.libc_installation != null) {1650 if (self.base.options.libc_installation != null) {
1651 const needs_grouping = self.base.options.link_mode == .Static;1651 const needs_grouping = self.base.options.link_mode == .Static;
1652 if (needs_grouping) try argv.append("--start-group");1652 if (needs_grouping) try argv.append("--start-group");
1653 try argv.append("-lm");1653 // This matches the order of glibc.libs
1654 try argv.append("-lpthread");1654 try argv.appendSlice(&[_][]const u8{
1655 try argv.append("-lc");1655 "-lm",
1656 "-lpthread",
1657 "-lc",
1658 "-ldl",
1659 "-lrt",
1660 "-lutil",
1661 });
1656 if (needs_grouping) try argv.append("--end-group");1662 if (needs_grouping) try argv.append("--end-group");
1657 } else if (target.isGnuLibC()) {1663 } else if (target.isGnuLibC()) {
1658 try argv.append(comp.libunwind_static_lib.?.full_object_path);1664 try argv.append(comp.libunwind_static_lib.?.full_object_path);