diff options
| author | Alex Rønne Petersen <alex@alexrp.com> | 2024-10-30 21:57:44 +0100 |
|---|---|---|
| committer | Alex Rønne Petersen <alex@alexrp.com> | 2024-11-01 06:23:59 +0100 |
| commit | 3c1ccbdf3990b6d802cdc8a0db7c11eaac0960e8 (patch) | |
| tree | 4c68b538c30b3df1362fd03fa3947958cce6d744 /lib/std/Target/Query.zig | |
| parent | c5395f7cd948f02ada095515b9d6824cbbac4f22 (diff) | |
| download | zig-3c1ccbdf3990b6d802cdc8a0db7c11eaac0960e8.tar.gz zig-3c1ccbdf3990b6d802cdc8a0db7c11eaac0960e8.zip | |
std.Target: Add support for specifying Android API level.
Diffstat (limited to 'lib/std/Target/Query.zig')
| -rw-r--r-- | lib/std/Target/Query.zig | 59 |
1 files changed, 52 insertions, 7 deletions
diff --git a/lib/std/Target/Query.zig b/lib/std/Target/Query.zig index 26001f1408..e50759a347 100644 --- a/lib/std/Target/Query.zig +++ b/lib/std/Target/Query.zig @@ -25,10 +25,14 @@ os_version_min: ?OsVersion = null, /// When `os_tag` is native, `null` means equal to the native OS version. os_version_max: ?OsVersion = null, -/// `null` means default when cross compiling, or native when os_tag is native. +/// `null` means default when cross compiling, or native when `os_tag` is native. /// If `isGnuLibC()` is `false`, this must be `null` and is ignored. glibc_version: ?SemanticVersion = null, +/// `null` means default when cross compiling, or native when `os_tag` is native. +/// If `isAndroid()` is `false`, this must be `null` and is ignored. +android_api_level: ?u32 = null, + /// `null` means the native C ABI, if `os_tag` is native, otherwise it means the default C ABI. abi: ?Target.Abi = null, @@ -98,10 +102,8 @@ pub fn fromTarget(target: Target) Query { .os_version_min = undefined, .os_version_max = undefined, .abi = target.abi, - .glibc_version = if (target.isGnuLibC()) - target.os.version_range.linux.glibc - else - null, + .glibc_version = if (target.isGnuLibC()) target.os.version_range.linux.glibc else null, + .android_api_level = if (target.abi.isAndroid()) target.os.version_range.linux.android else null, }; result.updateOsVersionRange(target.os); @@ -147,7 +149,7 @@ pub const ParseOptions = struct { /// The fields are, respectively: /// * CPU Architecture /// * Operating System (and optional version range) - /// * C ABI (optional, with optional glibc version) + /// * C ABI (optional, with optional glibc version or Android API level) /// The string "native" can be used for CPU architecture as well as Operating System. /// If the CPU Architecture is specified as "native", then the Operating System and C ABI may be omitted. arch_os_abi: []const u8 = "native", @@ -234,6 +236,11 @@ pub fn parse(args: ParseOptions) !Query { error.Overflow => return error.InvalidAbiVersion, error.InvalidVersion => return error.InvalidAbiVersion, }; + } else if (abi.isAndroid()) { + result.android_api_level = std.fmt.parseUnsigned(u32, abi_ver_text, 10) catch |err| switch (err) { + error.InvalidCharacter => return error.InvalidVersion, + error.Overflow => return error.Overflow, + }; } else { return error.InvalidAbiVersion; } @@ -355,7 +362,7 @@ pub fn isNativeCpu(self: Query) bool { pub fn isNativeOs(self: Query) bool { return self.os_tag == null and self.os_version_min == null and self.os_version_max == null and - self.dynamic_linker.get() == null and self.glibc_version == null; + self.dynamic_linker.get() == null and self.glibc_version == null and self.android_api_level == null; } pub fn isNativeAbi(self: Query) bool { @@ -439,6 +446,13 @@ pub fn zigTriple(self: Query, allocator: Allocator) Allocator.Error![]u8 { result.appendSliceAssumeCapacity(name); result.appendAssumeCapacity('.'); try formatVersion(v, result.writer()); + } else if (self.android_api_level) |lvl| { + const name = if (self.abi) |abi| @tagName(abi) else "android"; + try result.ensureUnusedCapacity(name.len + 2); + result.appendAssumeCapacity('-'); + result.appendSliceAssumeCapacity(name); + result.appendAssumeCapacity('.'); + try result.writer().print("{d}", .{lvl}); } else if (self.abi) |abi| { const name = @tagName(abi); try result.ensureUnusedCapacity(name.len + 1); @@ -561,6 +575,7 @@ pub fn eql(a: Query, b: Query) bool { if (!OsVersion.eqlOpt(a.os_version_min, b.os_version_min)) return false; if (!OsVersion.eqlOpt(a.os_version_max, b.os_version_max)) return false; if (!versionEqualOpt(a.glibc_version, b.glibc_version)) return false; + if (a.android_api_level != b.android_api_level) return false; if (a.abi != b.abi) return false; if (!a.dynamic_linker.eql(b.dynamic_linker)) return false; if (a.ofmt != b.ofmt) return false; @@ -592,6 +607,15 @@ test parse { try std.testing.expectEqualSlices(u8, "native-native-gnu.2.1.1", text); } + if (builtin.target.abi.isAndroid()) { + var query = try Query.parse(.{}); + query.android_api_level = 30; + + const text = try query.zigTriple(std.testing.allocator); + defer std.testing.allocator.free(text); + + try std.testing.expectEqualSlices(u8, "native-native-android.30", text); + } { const query = try Query.parse(.{ .arch_os_abi = "aarch64-linux", @@ -677,4 +701,25 @@ test parse { defer std.testing.allocator.free(text); try std.testing.expectEqualSlices(u8, "aarch64-linux.3.10...4.4.1-gnu.2.27", text); } + { + const query = try Query.parse(.{ + .arch_os_abi = "aarch64-linux.3.10...4.4.1-android.30", + }); + const target = try std.zig.system.resolveTargetQuery(query); + + try std.testing.expect(target.cpu.arch == .aarch64); + try std.testing.expect(target.os.tag == .linux); + try std.testing.expect(target.os.version_range.linux.range.min.major == 3); + try std.testing.expect(target.os.version_range.linux.range.min.minor == 10); + try std.testing.expect(target.os.version_range.linux.range.min.patch == 0); + try std.testing.expect(target.os.version_range.linux.range.max.major == 4); + try std.testing.expect(target.os.version_range.linux.range.max.minor == 4); + try std.testing.expect(target.os.version_range.linux.range.max.patch == 1); + try std.testing.expect(target.os.version_range.linux.android == 30); + try std.testing.expect(target.abi == .android); + + const text = try query.zigTriple(std.testing.allocator); + defer std.testing.allocator.free(text); + try std.testing.expectEqualSlices(u8, "aarch64-linux.3.10...4.4.1-android.30", text); + } } |
