Commit d1ecb742ec

Andrew Kelley <andrew@ziglang.org>
2024-10-19 08:57:26
don't create unused musl crt objects
1 parent de0f7fc
Changed files (2)
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;
 
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 {