zig libc: allow non-native targets

On macos, allow targets supported by the SDK. This then spawns `xcrun`
and correct paths are emitted for:

- x86_64-macos
- x86_64-ios
- x86_64-tvos
- x86_64-watchos
- x86_64-ios-macbi
- aarch64-macos
- aarch64-ios
- aarch64-tvos
- aarch64-watchos
- aarch64-ios-macbi

On platforms with android NDK, allow android targets. Example usage:

```
CC=/NDK/.../bin/aarch64-linux-android34-clang zig libc -target aarch64-linux-android
```
This commit is contained in:
Michael Dusan 2024-03-13 02:17:28 -04:00
parent 11bc3fb190
commit 22fd1851bd
No known key found for this signature in database
GPG Key ID: ED4C5BA849FA1B74
4 changed files with 22 additions and 6 deletions

View File

@ -113,7 +113,7 @@ pub fn main() !void {
};
defer libc.deinit(gpa);
} else {
if (!target_query.isNative()) {
if (!target_query.canDetectLibC()) {
fatal("unable to detect libc for non-native target", .{});
}
var libc = LibCInstallation.findNative(.{

View File

@ -415,6 +415,15 @@ pub fn isNative(self: Query) bool {
return self.isNativeCpu() and self.isNativeOs() and self.isNativeAbi();
}
pub fn canDetectLibC(self: Query) bool {
if (self.isNative()) return true;
if (self.os_tag) |os| {
if (builtin.os.tag == .macos and os.isDarwin()) return true;
if (os == .linux and self.abi == .android) return true;
}
return false;
}
/// Formats a version with the patch component omitted if it is zero,
/// unlike SemanticVersion.format which formats all its version components regardless.
fn formatVersion(version: SemanticVersion, writer: anytype) !void {

View File

@ -167,7 +167,7 @@ pub const FindNativeOptions = struct {
pub fn findNative(args: FindNativeOptions) FindError!LibCInstallation {
var self: LibCInstallation = .{};
if (is_darwin) {
if (is_darwin and args.target.isDarwin()) {
if (!std.zig.system.darwin.isSdkInstalled(args.allocator))
return error.DarwinSdkNotFound;
const sdk = std.zig.system.darwin.getSdk(args.allocator, args.target) orelse
@ -196,7 +196,7 @@ pub fn findNative(args: FindNativeOptions) FindError!LibCInstallation {
try self.findNativeCrtDirWindows(args, &sdk);
} else if (is_haiku) {
try self.findNativeIncludeDirPosix(args);
try self.findNativeCrtBeginDirHaiku(args);
try self.findNativeGccDirHaiku(args);
self.crt_dir = try args.allocator.dupeZ(u8, "/system/develop/lib");
} else if (builtin.target.os.tag.isSolarish()) {
// There is only one libc, and its headers/libraries are always in the same spot.
@ -443,13 +443,16 @@ fn findNativeCrtDirWindows(
fn findNativeCrtDirPosix(self: *LibCInstallation, args: FindNativeOptions) FindError!void {
self.crt_dir = try ccPrintFileName(.{
.allocator = args.allocator,
.search_basename = "crt1.o",
.search_basename = switch (args.target.os.tag) {
.linux => if (args.target.isAndroid()) "crtbegin_dynamic.o" else "crt1.o",
else => "crt1.o",
},
.want_dirname = .only_dir,
.verbose = args.verbose,
});
}
fn findNativeCrtBeginDirHaiku(self: *LibCInstallation, args: FindNativeOptions) FindError!void {
fn findNativeGccDirHaiku(self: *LibCInstallation, args: FindNativeOptions) FindError!void {
self.gcc_dir = try ccPrintFileName(.{
.allocator = args.allocator,
.search_basename = "crtbeginS.o",

View File

@ -38,7 +38,11 @@ pub fn getSdk(allocator: Allocator, target: Target) ?[]const u8 {
const is_simulator_abi = target.abi == .simulator;
const sdk = switch (target.os.tag) {
.macos => "macosx",
.ios => if (is_simulator_abi) "iphonesimulator" else "iphoneos",
.ios => switch (target.abi) {
.simulator => "iphonesimulator",
.macabi => "macosx",
else => "iphoneos",
},
.watchos => if (is_simulator_abi) "watchsimulator" else "watchos",
.tvos => if (is_simulator_abi) "appletvsimulator" else "appletvos",
else => return null,