aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMichael Dusan <michael.dusan@gmail.com>2024-03-13 02:17:28 -0400
committerMichael Dusan <michael.dusan@gmail.com>2024-03-13 02:17:28 -0400
commit22fd1851bd56dae5b61e5f170671f3af5ef54330 (patch)
tree631a1e010de32051879bab6898e5a4b7bede339b /lib
parent11bc3fb1906e10ed38fb913559a6c36d4c027e22 (diff)
downloadzig-22fd1851bd56dae5b61e5f170671f3af5ef54330.tar.gz
zig-22fd1851bd56dae5b61e5f170671f3af5ef54330.zip
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 ```
Diffstat (limited to 'lib')
-rw-r--r--lib/compiler/libc.zig2
-rw-r--r--lib/std/Target/Query.zig9
-rw-r--r--lib/std/zig/LibCInstallation.zig11
-rw-r--r--lib/std/zig/system/darwin.zig6
4 files changed, 22 insertions, 6 deletions
diff --git a/lib/compiler/libc.zig b/lib/compiler/libc.zig
index e828cd5528..866dd1535e 100644
--- a/lib/compiler/libc.zig
+++ b/lib/compiler/libc.zig
@@ -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(.{
diff --git a/lib/std/Target/Query.zig b/lib/std/Target/Query.zig
index e54ac0c3c6..d290e2a8d2 100644
--- a/lib/std/Target/Query.zig
+++ b/lib/std/Target/Query.zig
@@ -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 {
diff --git a/lib/std/zig/LibCInstallation.zig b/lib/std/zig/LibCInstallation.zig
index 3305d9484d..817947989d 100644
--- a/lib/std/zig/LibCInstallation.zig
+++ b/lib/std/zig/LibCInstallation.zig
@@ -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",
diff --git a/lib/std/zig/system/darwin.zig b/lib/std/zig/system/darwin.zig
index 59262beadc..8917bf5d3a 100644
--- a/lib/std/zig/system/darwin.zig
+++ b/lib/std/zig/system/darwin.zig
@@ -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,