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

This is needed for muslx32 and muslabin32 support.
This commit is contained in:
Alex Rønne Petersen 2024-10-16 04:12:41 +02:00
parent 533d8ea771
commit 8045268698
No known key found for this signature in database
3 changed files with 46 additions and 40 deletions

View file

@ -167,21 +167,16 @@ pub fn detectFromBuilding(
} }
const generic_name = libCGenericName(target); const generic_name = libCGenericName(target);
// Some architectures are handled by the same set of headers. // Some architecture families are handled by the same set of headers.
const arch_name = if (target.abi.isMusl()) const arch_name = if (target.abi.isMusl())
std.zig.target.muslArchNameHeaders(target.cpu.arch) std.zig.target.muslArchNameHeaders(target.cpu.arch)
else if (target.cpu.arch.isThumb())
// ARM headers are valid for Thumb too.
switch (target.cpu.arch) {
.thumb => "arm",
.thumbeb => "armeb",
else => unreachable,
}
else else
@tagName(target.cpu.arch); @tagName(target.cpu.arch);
const os_name = @tagName(target.os.tag); const os_name = @tagName(target.os.tag);
// Musl's headers are ABI-agnostic and so they all have the "musl" ABI name. const abi_name = if (target.abi.isMusl())
const abi_name = if (target.abi.isMusl()) "musl" else @tagName(target.abi); std.zig.target.muslAbiNameHeaders(target.abi)
else
@tagName(target.abi);
const arch_include_dir = try std.fmt.allocPrint( const arch_include_dir = try std.fmt.allocPrint(
arena, arena,
"{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-{s}", "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-{s}",

View file

@ -97,31 +97,41 @@ pub fn canBuildLibC(target: std.Target) bool {
return false; return false;
} }
pub fn muslArchNameHeaders(arch: std.Target.Cpu.Arch) [:0]const u8 { pub fn muslArchName(arch: std.Target.Cpu.Arch, abi: std.Target.Abi) [:0]const u8 {
return switch (arch) { return switch (abi) {
.x86 => return "x86", .muslx32 => "x32",
else => muslArchName(arch), else => switch (arch) {
.arm, .armeb, .thumb, .thumbeb => "arm",
.aarch64, .aarch64_be => "aarch64",
.loongarch64 => "loongarch64",
.m68k => "m68k",
.mips, .mipsel => "mips",
.mips64el, .mips64 => "mips64",
.powerpc => "powerpc",
.powerpc64, .powerpc64le => "powerpc64",
.riscv32 => "riscv32",
.riscv64 => "riscv64",
.s390x => "s390x",
.wasm32, .wasm64 => "wasm",
.x86 => "i386",
.x86_64 => "x86_64",
else => unreachable,
},
}; };
} }
pub fn muslArchName(arch: std.Target.Cpu.Arch) [:0]const u8 { pub fn muslArchNameHeaders(arch: std.Target.Cpu.Arch) [:0]const u8 {
switch (arch) { return switch (arch) {
.aarch64, .aarch64_be => return "aarch64", .x86 => "x86",
.arm, .armeb, .thumb, .thumbeb => return "arm", else => muslArchName(arch, .musl),
.x86 => return "i386", };
.loongarch64 => return "loongarch64", }
.m68k => return "m68k",
.mips, .mipsel => return "mips", pub fn muslAbiNameHeaders(abi: std.Target.Abi) [:0]const u8 {
.mips64el, .mips64 => return "mips64", return switch (abi) {
.powerpc => return "powerpc", .muslx32 => "muslx32",
.powerpc64, .powerpc64le => return "powerpc64", else => "musl",
.riscv32 => return "riscv32", };
.riscv64 => return "riscv64",
.s390x => return "s390x",
.wasm32, .wasm64 => return "wasm",
.x86_64 => return "x86_64",
else => unreachable,
}
} }
pub fn isLibCLibName(target: std.Target, name: []const u8) bool { pub fn isLibCLibName(target: std.Target, name: []const u8) bool {

View file

@ -4,7 +4,6 @@ const mem = std.mem;
const path = std.fs.path; const path = std.fs.path;
const assert = std.debug.assert; const assert = std.debug.assert;
const Module = @import("Package/Module.zig"); const Module = @import("Package/Module.zig");
const archName = std.zig.target.muslArchName;
const Compilation = @import("Compilation.zig"); const Compilation = @import("Compilation.zig");
const build_options = @import("build_options"); const build_options = @import("build_options");
@ -113,7 +112,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
// When there is a src/<arch>/foo.* then it should substitute for src/foo.* // When there is a src/<arch>/foo.* then it should substitute for src/foo.*
// Even a .s file can substitute for a .c file. // Even a .s file can substitute for a .c file.
const target = comp.getTarget(); const target = comp.getTarget();
const arch_name = archName(target.cpu.arch); const arch_name = std.zig.target.muslArchName(target.cpu.arch, target.abi);
var source_table = std.StringArrayHashMap(Ext).init(comp.gpa); var source_table = std.StringArrayHashMap(Ext).init(comp.gpa);
defer source_table.deinit(); defer source_table.deinit();
@ -162,7 +161,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
var is_arch_specific = false; var is_arch_specific = false;
// Architecture-specific implementations are under a <arch>/ folder. // Architecture-specific implementations are under a <arch>/ folder.
if (isMuslArchName(dirbasename)) { if (isArchName(dirbasename)) {
if (!mem.eql(u8, dirbasename, arch_name)) if (!mem.eql(u8, dirbasename, arch_name))
continue; // Not the architecture we're compiling for. continue; // Not the architecture we're compiling for.
is_arch_specific = true; is_arch_specific = true;
@ -327,7 +326,7 @@ pub fn needsCrt0(output_mode: std.builtin.OutputMode, link_mode: std.builtin.Lin
}; };
} }
fn isMuslArchName(name: []const u8) bool { fn isArchName(name: []const u8) bool {
const musl_arch_names = [_][]const u8{ const musl_arch_names = [_][]const u8{
"aarch64", "aarch64",
"arm", "arm",
@ -401,10 +400,12 @@ fn addCcArgs(
want_O3: bool, want_O3: bool,
) error{OutOfMemory}!void { ) error{OutOfMemory}!void {
const target = comp.getTarget(); const target = comp.getTarget();
const arch_name = archName(target.cpu.arch); const arch_name = std.zig.target.muslArchName(target.cpu.arch, target.abi);
const os_name = @tagName(target.os.tag); const os_name = @tagName(target.os.tag);
const triple = try std.fmt.allocPrint(arena, "{s}-{s}-musl", .{ const triple = try std.fmt.allocPrint(arena, "{s}-{s}-{s}", .{
std.zig.target.muslArchNameHeaders(target.cpu.arch), os_name, std.zig.target.muslArchNameHeaders(target.cpu.arch),
os_name,
std.zig.target.muslAbiNameHeaders(target.abi),
}); });
const o_arg = if (want_O3) "-O3" else "-Os"; const o_arg = if (want_O3) "-O3" else "-Os";
@ -460,7 +461,7 @@ fn addCcArgs(
fn start_asm_path(comp: *Compilation, arena: Allocator, basename: []const u8) ![]const u8 { fn start_asm_path(comp: *Compilation, arena: Allocator, basename: []const u8) ![]const u8 {
const target = comp.getTarget(); const target = comp.getTarget();
return comp.zig_lib_directory.join(arena, &[_][]const u8{ return comp.zig_lib_directory.join(arena, &[_][]const u8{
"libc", "musl", "crt", archName(target.cpu.arch), basename, "libc", "musl", "crt", std.zig.target.muslArchName(target.cpu.arch, target.abi), basename,
}); });
} }