compiler: don't error on explicit link_libc=false on requiresLibC() targets

There are various reasons why one might want to still create libc-less
compilations on these targets. Case in point: Compiling our bundled crt0 for
OpenBSD.

We will still default to linking libc on these targets, though.
This commit is contained in:
Alex Rønne Petersen 2025-12-01 06:21:23 +01:00
parent e4f3eedb8a
commit 7ce50db17d
No known key found for this signature in database
3 changed files with 3 additions and 16 deletions

View file

@ -137,7 +137,6 @@ pub const ResolveError = error{
SanitizeThreadRequiresLibCpp,
LibCRequiresLibUnwind,
LibCppRequiresLibUnwind,
OsRequiresLibC,
LibCppRequiresLibC,
LibUnwindRequiresLibC,
TargetCannotDynamicLink,
@ -226,10 +225,6 @@ pub fn resolve(options: Options) ResolveError!Config {
};
const link_libc = b: {
if (target_util.osRequiresLibC(target)) {
if (options.link_libc == false) return error.OsRequiresLibC;
break :b true;
}
if (link_libcpp) {
if (options.link_libc == false) return error.LibCppRequiresLibC;
break :b true;
@ -250,7 +245,7 @@ pub fn resolve(options: Options) ResolveError!Config {
if (options.ensure_libc_on_non_freestanding and target.os.tag != .freestanding)
break :b true;
break :b false;
break :b target.requiresLibC();
};
const link_mode = b: {
@ -269,7 +264,7 @@ pub fn resolve(options: Options) ResolveError!Config {
break :b .dynamic;
}
if (explicitly_exe_or_dyn_lib and link_libc and
(target_util.osRequiresLibC(target) or
(target.requiresLibC() or
// For these libcs, Zig can only provide dynamic libc when cross-compiling.
((target.isGnuLibC() or target.isFreeBSDLibC() or target.isNetBSDLibC()) and
!options.resolved_target.is_native_abi)))

View file

@ -3993,7 +3993,6 @@ fn createModule(
error.SanitizeThreadRequiresLibCpp => fatal("thread sanitization is (for now) implemented in C++, so it requires linking libc++", .{}),
error.LibCRequiresLibUnwind => fatal("libc of the specified target requires linking libunwind", .{}),
error.LibCppRequiresLibUnwind => fatal("libc++ requires linking libunwind", .{}),
error.OsRequiresLibC => fatal("the target OS requires using libc as the stable syscall interface", .{}),
error.LibCppRequiresLibC => fatal("libc++ requires linking libc", .{}),
error.LibUnwindRequiresLibC => fatal("libunwind requires linking libc", .{}),
error.TargetCannotDynamicLink => fatal("dynamic linking unavailable on the specified target", .{}),

View file

@ -17,13 +17,6 @@ pub fn cannotDynamicLink(target: *const std.Target) bool {
};
}
/// On Darwin, we always link libSystem which contains libc.
/// Similarly on FreeBSD and NetBSD we always link system libc
/// since this is the stable syscall interface.
pub fn osRequiresLibC(target: *const std.Target) bool {
return target.requiresLibC();
}
pub fn libCNeedsLibUnwind(target: *const std.Target, link_mode: std.builtin.LinkMode) bool {
return target.isGnuLibC() and link_mode == .static;
}
@ -49,7 +42,7 @@ pub fn libCxxNeedsLibUnwind(target: *const std.Target) bool {
pub fn requiresPIC(target: *const std.Target, linking_libc: bool) bool {
return target.abi.isAndroid() or
target.os.tag == .windows or target.os.tag == .uefi or
osRequiresLibC(target) or
target.requiresLibC() or
(linking_libc and target.isGnuLibC());
}