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 {...@@ -113,7 +113,7 @@ pub fn main() !void {
113 };113 };
114 defer libc.deinit(gpa);114 defer libc.deinit(gpa);
115 } else {115 } else {
116 if (!target_query.isNative()) {116 if (!target_query.canDetectLibC()) {
117 fatal("unable to detect libc for non-native target", .{});117 fatal("unable to detect libc for non-native target", .{});
118 }118 }
119 var libc = LibCInstallation.findNative(.{119 var libc = LibCInstallation.findNative(.{
lib/std/Target/Query.zig+9
...@@ -415,6 +415,15 @@ pub fn isNative(self: Query) bool {...@@ -415,6 +415,15 @@ pub fn isNative(self: Query) bool {
415 return self.isNativeCpu() and self.isNativeOs() and self.isNativeAbi();415 return self.isNativeCpu() and self.isNativeOs() and self.isNativeAbi();
416}416}
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
418/// Formats a version with the patch component omitted if it is zero,427/// Formats a version with the patch component omitted if it is zero,
419/// unlike SemanticVersion.format which formats all its version components regardless.428/// unlike SemanticVersion.format which formats all its version components regardless.
420fn formatVersion(version: SemanticVersion, writer: anytype) !void {429fn formatVersion(version: SemanticVersion, writer: anytype) !void {
lib/std/zig/LibCInstallation.zig+7-4
...@@ -167,7 +167,7 @@ pub const FindNativeOptions = struct {...@@ -167,7 +167,7 @@ pub const FindNativeOptions = struct {
167pub fn findNative(args: FindNativeOptions) FindError!LibCInstallation {167pub fn findNative(args: FindNativeOptions) FindError!LibCInstallation {
168 var self: LibCInstallation = .{};168 var self: LibCInstallation = .{};
169169
170 if (is_darwin) {170 if (is_darwin and args.target.isDarwin()) {
171 if (!std.zig.system.darwin.isSdkInstalled(args.allocator))171 if (!std.zig.system.darwin.isSdkInstalled(args.allocator))
172 return error.DarwinSdkNotFound;172 return error.DarwinSdkNotFound;
173 const sdk = std.zig.system.darwin.getSdk(args.allocator, args.target) orelse173 const sdk = std.zig.system.darwin.getSdk(args.allocator, args.target) orelse
...@@ -196,7 +196,7 @@ pub fn findNative(args: FindNativeOptions) FindError!LibCInstallation {...@@ -196,7 +196,7 @@ pub fn findNative(args: FindNativeOptions) FindError!LibCInstallation {
196 try self.findNativeCrtDirWindows(args, &sdk);196 try self.findNativeCrtDirWindows(args, &sdk);
197 } else if (is_haiku) {197 } else if (is_haiku) {
198 try self.findNativeIncludeDirPosix(args);198 try self.findNativeIncludeDirPosix(args);
199 try self.findNativeCrtBeginDirHaiku(args);199 try self.findNativeGccDirHaiku(args);
200 self.crt_dir = try args.allocator.dupeZ(u8, "/system/develop/lib");200 self.crt_dir = try args.allocator.dupeZ(u8, "/system/develop/lib");
201 } else if (builtin.target.os.tag.isSolarish()) {201 } else if (builtin.target.os.tag.isSolarish()) {
202 // There is only one libc, and its headers/libraries are always in the same spot.202 // There is only one libc, and its headers/libraries are always in the same spot.
...@@ -443,13 +443,16 @@ fn findNativeCrtDirWindows(...@@ -443,13 +443,16 @@ fn findNativeCrtDirWindows(
443fn findNativeCrtDirPosix(self: *LibCInstallation, args: FindNativeOptions) FindError!void {443fn findNativeCrtDirPosix(self: *LibCInstallation, args: FindNativeOptions) FindError!void {
444 self.crt_dir = try ccPrintFileName(.{444 self.crt_dir = try ccPrintFileName(.{
445 .allocator = args.allocator,445 .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 },
447 .want_dirname = .only_dir,450 .want_dirname = .only_dir,
448 .verbose = args.verbose,451 .verbose = args.verbose,
449 });452 });
450}453}
451454
452fn findNativeCrtBeginDirHaiku(self: *LibCInstallation, args: FindNativeOptions) FindError!void {455fn findNativeGccDirHaiku(self: *LibCInstallation, args: FindNativeOptions) FindError!void {
453 self.gcc_dir = try ccPrintFileName(.{456 self.gcc_dir = try ccPrintFileName(.{
454 .allocator = args.allocator,457 .allocator = args.allocator,
455 .search_basename = "crtbeginS.o",458 .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 {...@@ -38,7 +38,11 @@ pub fn getSdk(allocator: Allocator, target: Target) ?[]const u8 {
38 const is_simulator_abi = target.abi == .simulator;38 const is_simulator_abi = target.abi == .simulator;
39 const sdk = switch (target.os.tag) {39 const sdk = switch (target.os.tag) {
40 .macos => "macosx",40 .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 },
42 .watchos => if (is_simulator_abi) "watchsimulator" else "watchos",46 .watchos => if (is_simulator_abi) "watchsimulator" else "watchos",
43 .tvos => if (is_simulator_abi) "appletvsimulator" else "appletvos",47 .tvos => if (is_simulator_abi) "appletvsimulator" else "appletvos",
44 else => return null,48 else => return null,