From e8992af7f0516c3c6dff5ada865c097788841f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Thu, 8 May 2025 21:12:49 +0200 Subject: [PATCH] Compilation.Config: Default to dynamic linking with FreeBSD libc. --- src/Compilation/Config.zig | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Compilation/Config.zig b/src/Compilation/Config.zig index 71df1d9311..82496845ca 100644 --- a/src/Compilation/Config.zig +++ b/src/Compilation/Config.zig @@ -353,7 +353,9 @@ pub fn resolve(options: Options) ResolveError!Config { break :b .static; } if (explicitly_exe_or_dyn_lib and link_libc and - (target_util.osRequiresLibC(target) or (target.isGnuLibC() and !options.resolved_target.is_native_abi))) + (target_util.osRequiresLibC(target) or + // For these libcs, Zig can only provide dynamic libc when cross-compiling. + ((target.isGnuLibC() or target.isFreeBSDLibC()) and !options.resolved_target.is_native_abi))) { if (options.link_mode == .static) return error.LibCRequiresDynamicLinking; break :b .dynamic; @@ -368,12 +370,18 @@ pub fn resolve(options: Options) ResolveError!Config { if (options.link_mode) |link_mode| break :b link_mode; - if (explicitly_exe_or_dyn_lib and link_libc and options.resolved_target.is_native_abi and - (target.isGnuLibC() or target.isMuslLibC())) - { - // If targeting the system's native ABI and the system's libc is - // glibc or musl, link dynamically by default. - break :b .dynamic; + if (explicitly_exe_or_dyn_lib and link_libc) { + // When using the native glibc/musl ABI, dynamic linking is usually what people want. + if (options.resolved_target.is_native_abi and (target.isGnuLibC() or target.isMuslLibC())) { + break :b .dynamic; + } + + // When targeting systems where the kernel and libc are developed alongside each other, + // dynamic linking is the better default; static libc may contain code that requires + // the very latest kernel version. + if (target.isFreeBSDLibC()) { + break :b .dynamic; + } } // Static is generally a better default. Fight me.