authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-05-18 23:09:28+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-19 01:22:36-04:00
log1d6c804b29fed146aafda0f56cae7a4853b6abf9
tree14bd343538920798222de899203147826957d207
parent8861ee18f7fe9559a02389f07c4e5cdc5a8ffbde

stage2: only default to linking system libc if linking system libs

We do need to link the system libc if linking system libraries as they may potentially be compiled against e.g. a newer glibc version than zig can provide. However if not linking system libraries, using the zig provided libc is more reliable as it does not depend on any quirks of the host system or being able to invoke the system cc to find include dirs.

1 files changed, 15 insertions(+), 1 deletions(-)

src/Compilation.zig+15-1
......@@ -951,6 +951,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
951951 options.target,
952952 options.is_native_abi,
953953 link_libc,
954 options.system_libs.len != 0,
954955 options.libc_installation,
955956 );
956957
......@@ -3129,6 +3130,7 @@ fn detectLibCIncludeDirs(
31293130 target: Target,
31303131 is_native_abi: bool,
31313132 link_libc: bool,
3133 link_system_libs: bool,
31323134 libc_installation: ?*const LibCInstallation,
31333135) !LibCDirs {
31343136 if (!link_libc) {
......@@ -3142,12 +3144,16 @@ fn detectLibCIncludeDirs(
31423144 return detectLibCFromLibCInstallation(arena, target, lci);
31433145 }
31443146
3145 if (is_native_abi) {
3147 // If linking system libraries and targeting the native abi, default to
3148 // using the system libc installation.
3149 if (link_system_libs and is_native_abi) {
31463150 const libc = try arena.create(LibCInstallation);
31473151 libc.* = try LibCInstallation.findNative(.{ .allocator = arena, .verbose = true });
31483152 return detectLibCFromLibCInstallation(arena, target, libc);
31493153 }
31503154
3155 // If not linking system libraries, build and provide our own libc by
3156 // default if possible.
31513157 if (target_util.canBuildLibC(target)) {
31523158 const generic_name = target_util.libCGenericName(target);
31533159 // Some architectures are handled by the same set of headers.
......@@ -3198,6 +3204,14 @@ fn detectLibCIncludeDirs(
31983204 };
31993205 }
32003206
3207 // If zig can't build the libc for the target and we are targeting the
3208 // native abi, fall back to using the system libc installation.
3209 if (is_native_abi) {
3210 const libc = try arena.create(LibCInstallation);
3211 libc.* = try LibCInstallation.findNative(.{ .allocator = arena, .verbose = true });
3212 return detectLibCFromLibCInstallation(arena, target, libc);
3213 }
3214
32013215 return LibCDirs{
32023216 .libc_include_dir_list = &[0][]u8{},
32033217 .libc_installation = null,