authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2024-03-13 02:17:28-04:00
committergravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2024-03-13 02:17:28-04:00
log22fd1851bd56dae5b61e5f170671f3af5ef54330
tree631a1e010de32051879bab6898e5a4b7bede339b
parent11bc3fb1906e10ed38fb913559a6c36d4c027e22
signaturelock-open Commit is signed but in an unrecognized format.

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 ```

4 files changed, 22 insertions(+), 6 deletions(-)

lib/compiler/libc.zig+1-1
......@@ -113,7 +113,7 @@ pub fn main() !void {
113113 };
114114 defer libc.deinit(gpa);
115115 } else {
116 if (!target_query.isNative()) {
116 if (!target_query.canDetectLibC()) {
117117 fatal("unable to detect libc for non-native target", .{});
118118 }
119119 var libc = LibCInstallation.findNative(.{
lib/std/Target/Query.zig+9
......@@ -415,6 +415,15 @@ pub fn isNative(self: Query) bool {
415415 return self.isNativeCpu() and self.isNativeOs() and self.isNativeAbi();
416416}
417417
418pub fn canDetectLibC(self: Query) bool {
419 if (self.isNative()) return true;
420 if (self.os_tag) |os| {
421 if (builtin.os.tag == .macos and os.isDarwin()) return true;
422 if (os == .linux and self.abi == .android) return true;
423 }
424 return false;
425}
426
418427/// Formats a version with the patch component omitted if it is zero,
419428/// unlike SemanticVersion.format which formats all its version components regardless.
420429fn formatVersion(version: SemanticVersion, writer: anytype) !void {
lib/std/zig/LibCInstallation.zig+7-4
......@@ -167,7 +167,7 @@ pub const FindNativeOptions = struct {
167167pub fn findNative(args: FindNativeOptions) FindError!LibCInstallation {
168168 var self: LibCInstallation = .{};
169169
170 if (is_darwin) {
170 if (is_darwin and args.target.isDarwin()) {
171171 if (!std.zig.system.darwin.isSdkInstalled(args.allocator))
172172 return error.DarwinSdkNotFound;
173173 const sdk = std.zig.system.darwin.getSdk(args.allocator, args.target) orelse
......@@ -196,7 +196,7 @@ pub fn findNative(args: FindNativeOptions) FindError!LibCInstallation {
196196 try self.findNativeCrtDirWindows(args, &sdk);
197197 } else if (is_haiku) {
198198 try self.findNativeIncludeDirPosix(args);
199 try self.findNativeCrtBeginDirHaiku(args);
199 try self.findNativeGccDirHaiku(args);
200200 self.crt_dir = try args.allocator.dupeZ(u8, "/system/develop/lib");
201201 } else if (builtin.target.os.tag.isSolarish()) {
202202 // There is only one libc, and its headers/libraries are always in the same spot.
......@@ -443,13 +443,16 @@ fn findNativeCrtDirWindows(
443443fn findNativeCrtDirPosix(self: *LibCInstallation, args: FindNativeOptions) FindError!void {
444444 self.crt_dir = try ccPrintFileName(.{
445445 .allocator = args.allocator,
446 .search_basename = "crt1.o",
446 .search_basename = switch (args.target.os.tag) {
447 .linux => if (args.target.isAndroid()) "crtbegin_dynamic.o" else "crt1.o",
448 else => "crt1.o",
449 },
447450 .want_dirname = .only_dir,
448451 .verbose = args.verbose,
449452 });
450453}
451454
452fn findNativeCrtBeginDirHaiku(self: *LibCInstallation, args: FindNativeOptions) FindError!void {
455fn findNativeGccDirHaiku(self: *LibCInstallation, args: FindNativeOptions) FindError!void {
453456 self.gcc_dir = try ccPrintFileName(.{
454457 .allocator = args.allocator,
455458 .search_basename = "crtbeginS.o",
lib/std/zig/system/darwin.zig+5-1
......@@ -38,7 +38,11 @@ pub fn getSdk(allocator: Allocator, target: Target) ?[]const u8 {
3838 const is_simulator_abi = target.abi == .simulator;
3939 const sdk = switch (target.os.tag) {
4040 .macos => "macosx",
41 .ios => if (is_simulator_abi) "iphonesimulator" else "iphoneos",
41 .ios => switch (target.abi) {
42 .simulator => "iphonesimulator",
43 .macabi => "macosx",
44 else => "iphoneos",
45 },
4246 .watchos => if (is_simulator_abi) "watchsimulator" else "watchos",
4347 .tvos => if (is_simulator_abi) "appletvsimulator" else "appletvos",
4448 else => return null,