authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-10-16 04:12:41+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-11-02 10:38:06+01:00
log8045268698bb46e954c886b913fe216c59f5824e
tree257035a6382ec86acba573125d9e00b0cfdf485a
parent533d8ea771a307a33d1e6f93d819d808f9c27597
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.zig.target: Update musl path helpers to support alternative ABIs.

This is needed for muslx32 and muslabin32 support.

3 files changed, 44 insertions(+), 38 deletions(-)

lib/std/zig/LibCDirs.zig+5-10
......@@ -167,21 +167,16 @@ pub fn detectFromBuilding(
167167 }
168168
169169 const generic_name = libCGenericName(target);
170 // Some architectures are handled by the same set of headers.
170 // Some architecture families are handled by the same set of headers.
171171 const arch_name = if (target.abi.isMusl())
172172 std.zig.target.muslArchNameHeaders(target.cpu.arch)
173 else if (target.cpu.arch.isThumb())
174 // ARM headers are valid for Thumb too.
175 switch (target.cpu.arch) {
176 .thumb => "arm",
177 .thumbeb => "armeb",
178 else => unreachable,
179 }
180173 else
181174 @tagName(target.cpu.arch);
182175 const os_name = @tagName(target.os.tag);
183 // Musl's headers are ABI-agnostic and so they all have the "musl" ABI name.
184 const abi_name = if (target.abi.isMusl()) "musl" else @tagName(target.abi);
176 const abi_name = if (target.abi.isMusl())
177 std.zig.target.muslAbiNameHeaders(target.abi)
178 else
179 @tagName(target.abi);
185180 const arch_include_dir = try std.fmt.allocPrint(
186181 arena,
187182 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-{s}",
lib/std/zig/target.zig+30-20
......@@ -97,31 +97,41 @@ pub fn canBuildLibC(target: std.Target) bool {
9797 return false;
9898}
9999
100pub fn muslArchName(arch: std.Target.Cpu.Arch, abi: std.Target.Abi) [:0]const u8 {
101 return switch (abi) {
102 .muslx32 => "x32",
103 else => switch (arch) {
104 .arm, .armeb, .thumb, .thumbeb => "arm",
105 .aarch64, .aarch64_be => "aarch64",
106 .loongarch64 => "loongarch64",
107 .m68k => "m68k",
108 .mips, .mipsel => "mips",
109 .mips64el, .mips64 => "mips64",
110 .powerpc => "powerpc",
111 .powerpc64, .powerpc64le => "powerpc64",
112 .riscv32 => "riscv32",
113 .riscv64 => "riscv64",
114 .s390x => "s390x",
115 .wasm32, .wasm64 => "wasm",
116 .x86 => "i386",
117 .x86_64 => "x86_64",
118 else => unreachable,
119 },
120 };
121}
122
100123pub fn muslArchNameHeaders(arch: std.Target.Cpu.Arch) [:0]const u8 {
101124 return switch (arch) {
102 .x86 => return "x86",
103 else => muslArchName(arch),
125 .x86 => "x86",
126 else => muslArchName(arch, .musl),
104127 };
105128}
106129
107pub fn muslArchName(arch: std.Target.Cpu.Arch) [:0]const u8 {
108 switch (arch) {
109 .aarch64, .aarch64_be => return "aarch64",
110 .arm, .armeb, .thumb, .thumbeb => return "arm",
111 .x86 => return "i386",
112 .loongarch64 => return "loongarch64",
113 .m68k => return "m68k",
114 .mips, .mipsel => return "mips",
115 .mips64el, .mips64 => return "mips64",
116 .powerpc => return "powerpc",
117 .powerpc64, .powerpc64le => return "powerpc64",
118 .riscv32 => return "riscv32",
119 .riscv64 => return "riscv64",
120 .s390x => return "s390x",
121 .wasm32, .wasm64 => return "wasm",
122 .x86_64 => return "x86_64",
123 else => unreachable,
124 }
130pub fn muslAbiNameHeaders(abi: std.Target.Abi) [:0]const u8 {
131 return switch (abi) {
132 .muslx32 => "muslx32",
133 else => "musl",
134 };
125135}
126136
127137pub fn isLibCLibName(target: std.Target, name: []const u8) bool {
src/musl.zig+9-8
......@@ -4,7 +4,6 @@ const mem = std.mem;
44const path = std.fs.path;
55const assert = std.debug.assert;
66const Module = @import("Package/Module.zig");
7const archName = std.zig.target.muslArchName;
87
98const Compilation = @import("Compilation.zig");
109const build_options = @import("build_options");
......@@ -113,7 +112,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
113112 // When there is a src/<arch>/foo.* then it should substitute for src/foo.*
114113 // Even a .s file can substitute for a .c file.
115114 const target = comp.getTarget();
116 const arch_name = archName(target.cpu.arch);
115 const arch_name = std.zig.target.muslArchName(target.cpu.arch, target.abi);
117116 var source_table = std.StringArrayHashMap(Ext).init(comp.gpa);
118117 defer source_table.deinit();
119118
......@@ -162,7 +161,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
162161
163162 var is_arch_specific = false;
164163 // Architecture-specific implementations are under a <arch>/ folder.
165 if (isMuslArchName(dirbasename)) {
164 if (isArchName(dirbasename)) {
166165 if (!mem.eql(u8, dirbasename, arch_name))
167166 continue; // Not the architecture we're compiling for.
168167 is_arch_specific = true;
......@@ -327,7 +326,7 @@ pub fn needsCrt0(output_mode: std.builtin.OutputMode, link_mode: std.builtin.Lin
327326 };
328327}
329328
330fn isMuslArchName(name: []const u8) bool {
329fn isArchName(name: []const u8) bool {
331330 const musl_arch_names = [_][]const u8{
332331 "aarch64",
333332 "arm",
......@@ -401,10 +400,12 @@ fn addCcArgs(
401400 want_O3: bool,
402401) error{OutOfMemory}!void {
403402 const target = comp.getTarget();
404 const arch_name = archName(target.cpu.arch);
403 const arch_name = std.zig.target.muslArchName(target.cpu.arch, target.abi);
405404 const os_name = @tagName(target.os.tag);
406 const triple = try std.fmt.allocPrint(arena, "{s}-{s}-musl", .{
407 std.zig.target.muslArchNameHeaders(target.cpu.arch), os_name,
405 const triple = try std.fmt.allocPrint(arena, "{s}-{s}-{s}", .{
406 std.zig.target.muslArchNameHeaders(target.cpu.arch),
407 os_name,
408 std.zig.target.muslAbiNameHeaders(target.abi),
408409 });
409410 const o_arg = if (want_O3) "-O3" else "-Os";
410411
......@@ -460,7 +461,7 @@ fn addCcArgs(
460461fn start_asm_path(comp: *Compilation, arena: Allocator, basename: []const u8) ![]const u8 {
461462 const target = comp.getTarget();
462463 return comp.zig_lib_directory.join(arena, &[_][]const u8{
463 "libc", "musl", "crt", archName(target.cpu.arch), basename,
464 "libc", "musl", "crt", std.zig.target.muslArchName(target.cpu.arch, target.abi), basename,
464465 });
465466}
466467