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(...@@ -167,21 +167,16 @@ pub fn detectFromBuilding(
167 }167 }
168168
169 const generic_name = libCGenericName(target);169 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.
171 const arch_name = if (target.abi.isMusl())171 const arch_name = if (target.abi.isMusl())
172 std.zig.target.muslArchNameHeaders(target.cpu.arch)172 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 }
180 else173 else
181 @tagName(target.cpu.arch);174 @tagName(target.cpu.arch);
182 const os_name = @tagName(target.os.tag);175 const os_name = @tagName(target.os.tag);
183 // Musl's headers are ABI-agnostic and so they all have the "musl" ABI name.176 const abi_name = if (target.abi.isMusl())
184 const abi_name = if (target.abi.isMusl()) "musl" else @tagName(target.abi);177 std.zig.target.muslAbiNameHeaders(target.abi)
178 else
179 @tagName(target.abi);
185 const arch_include_dir = try std.fmt.allocPrint(180 const arch_include_dir = try std.fmt.allocPrint(
186 arena,181 arena,
187 "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-{s}",182 "{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 {...@@ -97,31 +97,41 @@ pub fn canBuildLibC(target: std.Target) bool {
97 return false;97 return false;
98}98}
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
100pub fn muslArchNameHeaders(arch: std.Target.Cpu.Arch) [:0]const u8 {123pub fn muslArchNameHeaders(arch: std.Target.Cpu.Arch) [:0]const u8 {
101 return switch (arch) {124 return switch (arch) {
102 .x86 => return "x86",125 .x86 => "x86",
103 else => muslArchName(arch),126 else => muslArchName(arch, .musl),
104 };127 };
105}128}
106129
107pub fn muslArchName(arch: std.Target.Cpu.Arch) [:0]const u8 {130pub fn muslAbiNameHeaders(abi: std.Target.Abi) [:0]const u8 {
108 switch (arch) {131 return switch (abi) {
109 .aarch64, .aarch64_be => return "aarch64",132 .muslx32 => "muslx32",
110 .arm, .armeb, .thumb, .thumbeb => return "arm",133 else => "musl",
111 .x86 => return "i386",134 };
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 }
125}135}
126136
127pub fn isLibCLibName(target: std.Target, name: []const u8) bool {137pub fn isLibCLibName(target: std.Target, name: []const u8) bool {
src/musl.zig+9-8
...@@ -4,7 +4,6 @@ const mem = std.mem;...@@ -4,7 +4,6 @@ const mem = std.mem;
4const path = std.fs.path;4const path = std.fs.path;
5const assert = std.debug.assert;5const assert = std.debug.assert;
6const Module = @import("Package/Module.zig");6const Module = @import("Package/Module.zig");
7const archName = std.zig.target.muslArchName;
87
9const Compilation = @import("Compilation.zig");8const Compilation = @import("Compilation.zig");
10const build_options = @import("build_options");9const build_options = @import("build_options");
...@@ -113,7 +112,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro...@@ -113,7 +112,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
113 // When there is a src/<arch>/foo.* then it should substitute for src/foo.*112 // When there is a src/<arch>/foo.* then it should substitute for src/foo.*
114 // Even a .s file can substitute for a .c file.113 // Even a .s file can substitute for a .c file.
115 const target = comp.getTarget();114 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);
117 var source_table = std.StringArrayHashMap(Ext).init(comp.gpa);116 var source_table = std.StringArrayHashMap(Ext).init(comp.gpa);
118 defer source_table.deinit();117 defer source_table.deinit();
119118
...@@ -162,7 +161,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro...@@ -162,7 +161,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
162161
163 var is_arch_specific = false;162 var is_arch_specific = false;
164 // Architecture-specific implementations are under a <arch>/ folder.163 // Architecture-specific implementations are under a <arch>/ folder.
165 if (isMuslArchName(dirbasename)) {164 if (isArchName(dirbasename)) {
166 if (!mem.eql(u8, dirbasename, arch_name))165 if (!mem.eql(u8, dirbasename, arch_name))
167 continue; // Not the architecture we're compiling for.166 continue; // Not the architecture we're compiling for.
168 is_arch_specific = true;167 is_arch_specific = true;
...@@ -327,7 +326,7 @@ pub fn needsCrt0(output_mode: std.builtin.OutputMode, link_mode: std.builtin.Lin...@@ -327,7 +326,7 @@ pub fn needsCrt0(output_mode: std.builtin.OutputMode, link_mode: std.builtin.Lin
327 };326 };
328}327}
329328
330fn isMuslArchName(name: []const u8) bool {329fn isArchName(name: []const u8) bool {
331 const musl_arch_names = [_][]const u8{330 const musl_arch_names = [_][]const u8{
332 "aarch64",331 "aarch64",
333 "arm",332 "arm",
...@@ -401,10 +400,12 @@ fn addCcArgs(...@@ -401,10 +400,12 @@ fn addCcArgs(
401 want_O3: bool,400 want_O3: bool,
402) error{OutOfMemory}!void {401) error{OutOfMemory}!void {
403 const target = comp.getTarget();402 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);
405 const os_name = @tagName(target.os.tag);404 const os_name = @tagName(target.os.tag);
406 const triple = try std.fmt.allocPrint(arena, "{s}-{s}-musl", .{405 const triple = try std.fmt.allocPrint(arena, "{s}-{s}-{s}", .{
407 std.zig.target.muslArchNameHeaders(target.cpu.arch), os_name,406 std.zig.target.muslArchNameHeaders(target.cpu.arch),
407 os_name,
408 std.zig.target.muslAbiNameHeaders(target.abi),
408 });409 });
409 const o_arg = if (want_O3) "-O3" else "-Os";410 const o_arg = if (want_O3) "-O3" else "-Os";
410411
...@@ -460,7 +461,7 @@ fn addCcArgs(...@@ -460,7 +461,7 @@ fn addCcArgs(
460fn start_asm_path(comp: *Compilation, arena: Allocator, basename: []const u8) ![]const u8 {461fn start_asm_path(comp: *Compilation, arena: Allocator, basename: []const u8) ![]const u8 {
461 const target = comp.getTarget();462 const target = comp.getTarget();
462 return comp.zig_lib_directory.join(arena, &[_][]const u8{463 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,
464 });465 });
465}466}
466467