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 {...@@ -951,6 +951,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
951 options.target,951 options.target,
952 options.is_native_abi,952 options.is_native_abi,
953 link_libc,953 link_libc,
954 options.system_libs.len != 0,
954 options.libc_installation,955 options.libc_installation,
955 );956 );
956957
...@@ -3129,6 +3130,7 @@ fn detectLibCIncludeDirs(...@@ -3129,6 +3130,7 @@ fn detectLibCIncludeDirs(
3129 target: Target,3130 target: Target,
3130 is_native_abi: bool,3131 is_native_abi: bool,
3131 link_libc: bool,3132 link_libc: bool,
3133 link_system_libs: bool,
3132 libc_installation: ?*const LibCInstallation,3134 libc_installation: ?*const LibCInstallation,
3133) !LibCDirs {3135) !LibCDirs {
3134 if (!link_libc) {3136 if (!link_libc) {
...@@ -3142,12 +3144,16 @@ fn detectLibCIncludeDirs(...@@ -3142,12 +3144,16 @@ fn detectLibCIncludeDirs(
3142 return detectLibCFromLibCInstallation(arena, target, lci);3144 return detectLibCFromLibCInstallation(arena, target, lci);
3143 }3145 }
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) {
3146 const libc = try arena.create(LibCInstallation);3150 const libc = try arena.create(LibCInstallation);
3147 libc.* = try LibCInstallation.findNative(.{ .allocator = arena, .verbose = true });3151 libc.* = try LibCInstallation.findNative(.{ .allocator = arena, .verbose = true });
3148 return detectLibCFromLibCInstallation(arena, target, libc);3152 return detectLibCFromLibCInstallation(arena, target, libc);
3149 }3153 }
31503154
3155 // If not linking system libraries, build and provide our own libc by
3156 // default if possible.
3151 if (target_util.canBuildLibC(target)) {3157 if (target_util.canBuildLibC(target)) {
3152 const generic_name = target_util.libCGenericName(target);3158 const generic_name = target_util.libCGenericName(target);
3153 // Some architectures are handled by the same set of headers.3159 // Some architectures are handled by the same set of headers.
...@@ -3198,6 +3204,14 @@ fn detectLibCIncludeDirs(...@@ -3198,6 +3204,14 @@ fn detectLibCIncludeDirs(
3198 };3204 };
3199 }3205 }
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
3201 return LibCDirs{3215 return LibCDirs{
3202 .libc_include_dir_list = &[0][]u8{},3216 .libc_include_dir_list = &[0][]u8{},
3203 .libc_installation = null,3217 .libc_installation = null,