From a876bf8afe0d706f291516b36ec6724cf7cb0654 Mon Sep 17 00:00:00 2001 From: Jomy10 Date: Tue, 12 Sep 2023 19:38:37 +0200 Subject: [PATCH] initial Zig 12 support --- AndroidManifest.xml | 0 Sdk.zig | 63 +++++++++++++++++++++-------------------- android-21-arm64.conf | 0 android-21-armeabi.conf | 0 android-21-x86.conf | 0 android-21-x86_64.conf | 6 ++++ build.zig | 2 +- build/auto-detect.zig | 34 +++++++++++----------- strings.xml | 0 9 files changed, 57 insertions(+), 48 deletions(-) create mode 100644 AndroidManifest.xml create mode 100644 android-21-arm64.conf create mode 100644 android-21-armeabi.conf create mode 100644 android-21-x86.conf create mode 100644 android-21-x86_64.conf create mode 100644 strings.xml diff --git a/AndroidManifest.xml b/AndroidManifest.xml new file mode 100644 index 0000000..e69de29 diff --git a/Sdk.zig b/Sdk.zig index 29f59f9..a54bcfa 100644 --- a/Sdk.zig +++ b/Sdk.zig @@ -18,11 +18,9 @@ fn sdkRoot() *const [sdkRootIntern().len]u8 { // linux-x86_64 pub fn toolchainHostTag() []const u8 { - comptime { - const os = builtin.os.tag; - const arch = builtin.cpu.arch; - return @tagName(os) ++ "-" ++ @tagName(arch); - } + const os = builtin.os.tag; + const arch = builtin.cpu.arch; + return (comptime if (std.mem.eql(u8, @tagName(os), "macos")) "darwin" else @tagName(os)) ++ "-" ++ @tagName(arch); } /// This file encodes a instance of an Android SDK interface. @@ -92,12 +90,12 @@ pub fn init(b: *Builder, user_config: ?UserConfig, toolchains: ToolchainVersions .name = "zip_add", .root_source_file = .{ .path = sdkRoot() ++ "/tools/zip_add.zig" }, }); - zip_add.addCSourceFile(sdkRoot() ++ "/vendor/kuba-zip/zip.c", &[_][]const u8{ + zip_add.addCSourceFile(.{ .file = .{ .path = sdkRoot() ++ "/vendor/kuba-zip/zip.c" }, .flags = &[_][]const u8{ "-std=c99", "-fno-sanitize=undefined", "-D_POSIX_C_SOURCE=200112L", - }); - zip_add.addIncludePath(sdkRoot() ++ "/vendor/kuba-zip"); + } }); + zip_add.addIncludePath(.{ .path = sdkRoot() ++ "/vendor/kuba-zip" }); zip_add.linkLibC(); break :blk HostTools{ @@ -503,10 +501,11 @@ pub fn createApp( } resource_dir_step.add(Resource{ .path = "values/strings.xml", - .content = write_xml_step.getFileSource("strings.xml").?, + // .content = write_xml_step.getFileSource("strings.xml").?, + .content = write_xml_step.addCopyFile(.{ .path = "strings.xml" }, ""), }); - const sdk_version_int = @enumToInt(app_config.target_version); + const sdk_version_int = @intFromEnum(app_config.target_version); if (sdk_version_int < 16) @panic("Minimum supported sdk version is 16."); @@ -549,7 +548,8 @@ pub fn createApp( const unaligned_apk_file = make_unsigned_apk.addOutputFileArg(unaligned_apk_name); make_unsigned_apk.addArg("-M"); // specify full path to AndroidManifest.xml to include in zip - make_unsigned_apk.addFileSourceArg(manifest_step.getFileSource("AndroidManifest.xml").?); + // make_unsigned_apk.addFileSourceArg(manifest_step.getFileSource("AndroidManifest.xml").?); + make_unsigned_apk.addFileSourceArg(manifest_step.addCopyFile(.{ .path = "AndroidManifest.xml" }, "")); make_unsigned_apk.addArg("-S"); // directory in which to find resources. Multiple directories will be scanned and the first match found (left to right) will take precedence make_unsigned_apk.addDirectorySourceArg(resource_dir_step.getOutputDirectory()); @@ -848,7 +848,7 @@ pub fn compileAppLibrary( ndk_root, toolchainHostTag(), config.lib_dir, - @enumToInt(app_config.target_version), + @intFromEnum(app_config.target_version), }); const include_dir = std.fs.path.resolve(sdk.b.allocator, &[_][]const u8{ @@ -887,7 +887,7 @@ pub fn compileAppLibrary( // exe.addIncludePath(include_dir); - exe.addLibraryPath(lib_dir); + exe.addLibraryPath(.{ .path = lib_dir }); // exe.addIncludePath(include_dir); // exe.addIncludePath(system_include_dir); @@ -904,7 +904,7 @@ pub fn compileAppLibrary( } fn createLibCFile(sdk: *const Sdk, version: AndroidVersion, folder_name: []const u8, include_dir: []const u8, sys_include_dir: []const u8, crt_dir: []const u8) !std.build.FileSource { - const fname = sdk.b.fmt("android-{d}-{s}.conf", .{ @enumToInt(version), folder_name }); + const fname = sdk.b.fmt("android-{d}-{s}.conf", .{ @intFromEnum(version), folder_name }); var contents = std.ArrayList(u8).init(sdk.b.allocator); errdefer contents.deinit(); @@ -926,7 +926,8 @@ fn createLibCFile(sdk: *const Sdk, version: AndroidVersion, folder_name: []const try writer.writeAll("gcc_dir=\n"); const step = sdk.b.addWriteFile(fname, contents.items); - return step.getFileSource(fname) orelse unreachable; + // return step.getFileSource(fname) orelse unreachable; + return step.addCopyFile(.{ .path = fname }, ""); } pub fn compressApk(sdk: Sdk, input_apk_file: []const u8, output_apk_file: []const u8) *Step { @@ -1151,22 +1152,22 @@ const BuildOptionStep = struct { } return; }, - std.builtin.Version => { - out.print( - \\pub const {}: @import("std").builtin.Version = .{{ - \\ .major = {d}, - \\ .minor = {d}, - \\ .patch = {d}, - \\}}; - \\ - , .{ - std.zig.fmtId(name), - - value.major, - value.minor, - value.patch, - }) catch unreachable; - }, + // std.builtin.Version => { + // out.print( + // \\pub const {}: @import("std").builtin.Version = .{{ + // \\ .major = {d}, + // \\ .minor = {d}, + // \\ .patch = {d}, + // \\}}; + // \\ + // , .{ + // std.zig.fmtId(name), + + // value.major, + // value.minor, + // value.patch, + // }) catch unreachable; + // }, std.SemanticVersion => { out.print( \\pub const {}: @import("std").SemanticVersion = .{{ diff --git a/android-21-arm64.conf b/android-21-arm64.conf new file mode 100644 index 0000000..e69de29 diff --git a/android-21-armeabi.conf b/android-21-armeabi.conf new file mode 100644 index 0000000..e69de29 diff --git a/android-21-x86.conf b/android-21-x86.conf new file mode 100644 index 0000000..e69de29 diff --git a/android-21-x86_64.conf b/android-21-x86_64.conf new file mode 100644 index 0000000..6720c42 --- /dev/null +++ b/android-21-x86_64.conf @@ -0,0 +1,6 @@ +include_dir=/Users/jonaseveraert/Library/Android/sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include +sys_include_dir=/Users/jonaseveraert/Library/Android/sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/x86_64-linux-android +crt_dir=/Users/jonaseveraert/Library/Android/sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/x86_64-linux-android/21/ +msvc_lib_dir= +kernel32_lib_dir= +gcc_dir= diff --git a/build.zig b/build.zig index ea7a0e4..c5e14f6 100644 --- a/build.zig +++ b/build.zig @@ -6,7 +6,7 @@ const std = @import("std"); const Sdk = @import("Sdk.zig"); pub fn build(b: *std.build.Builder) !void { - // Default-initialize SDK + // // Default-initialize SDK const sdk = Sdk.init(b, null, .{}); const mode = b.standardOptimizeOption(.{}); const android_version = b.option(Sdk.AndroidVersion, "android", "Select the android version, default is 'android5'") orelse .android5; diff --git a/build/auto-detect.zig b/build/auto-detect.zig index dfe0351..6b83097 100644 --- a/build/auto-detect.zig +++ b/build/auto-detect.zig @@ -24,17 +24,19 @@ pub fn findUserConfig(b: *Builder, versions: Sdk.ToolchainVersions) !UserConfig // Check for a user config file. if (std.fs.cwd().openFile(config_path, .{})) |file| { defer file.close(); - const bytes = file.readToEndAlloc(b.allocator, 1 * 1000 * 1000) catch |err| { - print("Unexpected error reading {s}: {s}\n", .{ config_path, @errorName(err) }); - return err; - }; - var stream = std.json.TokenStream.init(bytes); - if (std.json.parse(UserConfig, &stream, .{ .allocator = b.allocator })) |conf| { - config = conf; - } else |err| { - print("Could not parse {s} ({s}).\n", .{ config_path, @errorName(err) }); - return err; - } + // @panic("Config file not supported yet"); + std.debug.print("Config file: TODO\n", .{}); + // const bytes = file.readToEndAlloc(b.allocator, 1 * 1000 * 1000) catch |err| { + // print("Unexpected error reading {s}: {s}\n", .{ config_path, @errorName(err) }); + // return err; + // }; + // var stream = std.json.TokenStream.init(bytes); + // if (std.json.parse(UserConfig, &stream, .{ .allocator = b.allocator })) |conf| { + // config = conf; + // } else |err| { + // print("Could not parse {s} ({s}).\n", .{ config_path, @errorName(err) }); + // return err; + // } } else |err| switch (err) { error.FileNotFound => { config_dirty = true; @@ -99,10 +101,10 @@ pub fn findUserConfig(b: *Builder, versions: Sdk.ToolchainVersions) !UserConfig const LSTATUS = u32; const DWORD = u32; - // const HKEY_CLASSES_ROOT = @intToPtr(HKEY, 0x80000000); - const HKEY_CURRENT_USER = @intToPtr(HKEY, 0x80000001); - const HKEY_LOCAL_MACHINE = @intToPtr(HKEY, 0x80000002); - // const HKEY_USERS = @intToPtr(HKEY, 0x80000003); + // const HKEY_CLASSES_ROOT: HKEY= @ptrFromInt(0x80000000); + const HKEY_CURRENT_USER: HKEY = @ptrFromInt(0x80000001); + const HKEY_LOCAL_MACHINE: HKEY = @ptrFromInt(0x80000002); + // const HKEY_USERS: HKEY= @ptrFromInt(0x80000003); // const RRF_RT_ANY: DWORD = 0xFFFF; // const RRF_RT_REG_BINARY: DWORD = 0x08; @@ -142,7 +144,7 @@ pub fn findUserConfig(b: *Builder, versions: Sdk.ToolchainVersions) !UserConfig // get the data const buffer = allocator.alloc(u8, len) catch unreachable; - len = @intCast(DWORD, buffer.len); + len = @as(DWORD, @intCast(buffer.len)); res = RegGetValueA(key, null, value, RRF_RT_REG_SZ, null, buffer.ptr, &len); if (res == ERROR_SUCCESS) { for (buffer[0..len], 0..) |c, i| { diff --git a/strings.xml b/strings.xml new file mode 100644 index 0000000..e69de29