authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-03-12 13:43:00+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-03-13 04:16:07+01:00
logab1a4b26ec2b90f514059b6e446b83c3881dbe4d
treeeddc41adfe2784c1903c1adc23c0ac4526834384
parent26f2749c1e843176c821bd9d5f527585ba43843f

zig cc: link compiler-rt when given -lcompiler_rt even with -nostdlib

closes https://codeberg.org/ziglang/zig/issues/31460

1 files changed, 19 insertions(+), 13 deletions(-)

src/main.zig+19-13
......@@ -871,6 +871,7 @@ fn buildOutputType(
871871 var emit_h: Emit = .no;
872872 var soname: SOName = undefined;
873873 var want_compiler_rt: ?bool = null;
874 var zig_cc_explicitly_link_compiler_rt = false;
874875 var want_ubsan_rt: ?bool = null;
875876 var linker_script: ?[]const u8 = null;
876877 var version_script: ?[]const u8 = null;
......@@ -2000,8 +2001,8 @@ fn buildOutputType(
20002001 try create_module.cli_link_inputs.append(arena, .{ .dso_exact = .{
20012002 .name = it.only_arg,
20022003 } });
2003 } else {
2004 try create_module.cli_link_inputs.append(arena, .{ .name_query = .{
2004 } else switch (target_util.classifyCompilerRtLibName(it.only_arg)) {
2005 .none => try create_module.cli_link_inputs.append(arena, .{ .name_query = .{
20052006 .name = it.only_arg,
20062007 .query = .{
20072008 .must_link = must_link,
......@@ -2011,7 +2012,21 @@ fn buildOutputType(
20112012 .search_strategy = lib_search_strategy,
20122013 .allow_so_scripts = allow_so_scripts,
20132014 },
2014 } });
2015 } }),
2016 .only_compiler_rt => {
2017 // We need this variable separately from `want_compiler_rt` because of
2018 // invocations such as `zig cc -lcompiler_rt -nostdlib`. If we just set
2019 // `want_compiler_rt = true` here, processing of the later `-nostdlib`
2020 // would undo that.
2021 zig_cc_explicitly_link_compiler_rt = true;
2022 },
2023 .only_libunwind => {
2024 create_module.opts.link_libunwind = true;
2025 },
2026 .both => {
2027 zig_cc_explicitly_link_compiler_rt = true;
2028 create_module.opts.link_libunwind = true;
2029 },
20152030 }
20162031 },
20172032 .ignore => {},
......@@ -3547,7 +3562,7 @@ fn buildOutputType(
35473562 .framework_dirs = create_module.framework_dirs.items,
35483563 .frameworks = resolved_frameworks.items,
35493564 .windows_lib_names = create_module.windows_libs.keys(),
3550 .want_compiler_rt = want_compiler_rt,
3565 .want_compiler_rt = if (zig_cc_explicitly_link_compiler_rt) true else want_compiler_rt,
35513566 .want_ubsan_rt = want_ubsan_rt,
35523567 .hash_style = hash_style,
35533568 .linker_script = linker_script,
......@@ -3979,15 +3994,6 @@ fn createModule(
39793994 continue;
39803995 }
39813996
3982 switch (target_util.classifyCompilerRtLibName(lib_name)) {
3983 .none => {},
3984 .only_libunwind, .both => {
3985 create_module.opts.link_libunwind = true;
3986 continue;
3987 },
3988 .only_compiler_rt => continue,
3989 }
3990
39913997 if (target.isMinGW()) {
39923998 const exists = mingw.libExists(arena, io, target, create_module.dirs.zig_lib, lib_name) catch |err|
39933999 fatal("failed to check zig installation for DLL import libs: {t}", .{err});