From d1ecb742ec5cb80e29178fddefe6663bed40daa8 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 18 Oct 2024 23:57:26 -0700 Subject: don't create unused musl crt objects --- src/Compilation.zig | 16 +++++++--------- src/musl.zig | 20 +++++++++++++------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 0b058b99a4..870d5faf8f 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1815,15 +1815,13 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil .{ .musl_crt_file = .crtn_o }, }); } - try comp.queueJobs(&[_]Job{ - .{ .musl_crt_file = .crt1_o }, - .{ .musl_crt_file = .scrt1_o }, - .{ .musl_crt_file = .rcrt1_o }, - switch (comp.config.link_mode) { - .static => .{ .musl_crt_file = .libc_a }, - .dynamic => .{ .musl_crt_file = .libc_so }, - }, - }); + if (musl.needsCrt0(comp.config.output_mode, comp.config.link_mode, comp.config.pie)) |f| { + try comp.queueJobs(&.{.{ .musl_crt_file = f }}); + } + try comp.queueJobs(&.{.{ .musl_crt_file = switch (comp.config.link_mode) { + .static => .libc_a, + .dynamic => .libc_so, + } }}); } else if (target.isGnuLibC()) { if (!std.zig.target.canBuildLibC(target)) return error.LibCUnavailable; diff --git a/src/musl.zig b/src/musl.zig index 41a9c89143..975929a59d 100644 --- a/src/musl.zig +++ b/src/musl.zig @@ -292,18 +292,24 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro } } -// Return true if musl has arch-specific crti/crtn sources. -// See lib/libc/musl/crt/ARCH/crt?.s . +/// Return true if musl has arch-specific crti/crtn sources. +/// See lib/libc/musl/crt/ARCH/crt?.s . pub fn needsCrtiCrtn(target: std.Target) bool { - // zig fmt: off return switch (target.cpu.arch) { - .riscv32, - .riscv64, - .wasm32, .wasm64 => false, + .riscv32, .riscv64, .wasm32, .wasm64 => false, .loongarch64 => false, else => true, }; - // zig fmt: on +} + +pub fn needsCrt0(output_mode: std.builtin.OutputMode, link_mode: std.builtin.LinkMode, pie: bool) ?CrtFile { + return switch (output_mode) { + .Obj, .Lib => null, + .Exe => switch (link_mode) { + .dynamic => if (pie) .scrt1_o else .crt1_o, + .static => if (pie) .rcrt1_o else .crt1_o, + }, + }; } fn isMuslArchName(name: []const u8) bool { -- cgit v1.2.3