authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-12-01 06:21:23+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-01-05 16:50:46+01:00
log00e6895bde4b5871a944bccf4521b2f6f5f879f6
tree5ef6071b34f48ae481b6c0f8785121907403e2cf
parentd0ad76c03c11b6b033d13b28dac913ec11cb9c3a
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

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.

3 files changed, 3 insertions(+), 16 deletions(-)

src/Compilation/Config.zig+2-7
......@@ -137,7 +137,6 @@ pub const ResolveError = error{
137137 SanitizeThreadRequiresLibCpp,
138138 LibCRequiresLibUnwind,
139139 LibCppRequiresLibUnwind,
140 OsRequiresLibC,
141140 LibCppRequiresLibC,
142141 LibUnwindRequiresLibC,
143142 TargetCannotDynamicLink,
......@@ -226,10 +225,6 @@ pub fn resolve(options: Options) ResolveError!Config {
226225 };
227226
228227 const link_libc = b: {
229 if (target_util.osRequiresLibC(target)) {
230 if (options.link_libc == false) return error.OsRequiresLibC;
231 break :b true;
232 }
233228 if (link_libcpp) {
234229 if (options.link_libc == false) return error.LibCppRequiresLibC;
235230 break :b true;
......@@ -250,7 +245,7 @@ pub fn resolve(options: Options) ResolveError!Config {
250245 if (options.ensure_libc_on_non_freestanding and target.os.tag != .freestanding)
251246 break :b true;
252247
253 break :b false;
248 break :b target.requiresLibC();
254249 };
255250
256251 const link_mode = b: {
......@@ -269,7 +264,7 @@ pub fn resolve(options: Options) ResolveError!Config {
269264 break :b .dynamic;
270265 }
271266 if (explicitly_exe_or_dyn_lib and link_libc and
272 (target_util.osRequiresLibC(target) or
267 (target.requiresLibC() or
273268 // For these libcs, Zig can only provide dynamic libc when cross-compiling.
274269 ((target.isGnuLibC() or target.isFreeBSDLibC() or target.isNetBSDLibC()) and
275270 !options.resolved_target.is_native_abi)))
src/main.zig-1
......@@ -4078,7 +4078,6 @@ fn createModule(
40784078 error.SanitizeThreadRequiresLibCpp => fatal("thread sanitization is (for now) implemented in C++, so it requires linking libc++", .{}),
40794079 error.LibCRequiresLibUnwind => fatal("libc of the specified target requires linking libunwind", .{}),
40804080 error.LibCppRequiresLibUnwind => fatal("libc++ requires linking libunwind", .{}),
4081 error.OsRequiresLibC => fatal("the target OS requires using libc as the stable syscall interface", .{}),
40824081 error.LibCppRequiresLibC => fatal("libc++ requires linking libc", .{}),
40834082 error.LibUnwindRequiresLibC => fatal("libunwind requires linking libc", .{}),
40844083 error.TargetCannotDynamicLink => fatal("dynamic linking unavailable on the specified target", .{}),
src/target.zig+1-8
......@@ -17,13 +17,6 @@ pub fn cannotDynamicLink(target: *const std.Target) bool {
1717 };
1818}
1919
20/// On Darwin, we always link libSystem which contains libc.
21/// Similarly on FreeBSD and NetBSD we always link system libc
22/// since this is the stable syscall interface.
23pub fn osRequiresLibC(target: *const std.Target) bool {
24 return target.requiresLibC();
25}
26
2720pub fn libCNeedsLibUnwind(target: *const std.Target, link_mode: std.builtin.LinkMode) bool {
2821 return target.isGnuLibC() and link_mode == .static;
2922}
......@@ -49,7 +42,7 @@ pub fn libCxxNeedsLibUnwind(target: *const std.Target) bool {
4942pub fn requiresPIC(target: *const std.Target, linking_libc: bool) bool {
5043 return target.abi.isAndroid() or
5144 target.os.tag == .windows or target.os.tag == .uefi or
52 osRequiresLibC(target) or
45 target.requiresLibC() or
5346 (linking_libc and target.isGnuLibC());
5447}
5548