From aeaf220ce3d8a87c6e42639f6ba8e0d679711f9d Mon Sep 17 00:00:00 2001 From: Jomy10 Date: Tue, 12 Sep 2023 19:38:37 +0200 Subject: [PATCH 1/2] initial Zig 12 support --- Sdk.zig | 63 ++++++++++++++++++++++--------------------- build/auto-detect.zig | 34 ++++++++++++----------- strings.xml | 0 3 files changed, 50 insertions(+), 47 deletions(-) create mode 100644 strings.xml 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/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 From 0a3cd1cb2ac8bb007fd0cbca6791ad74c694ebc5 Mon Sep 17 00:00:00 2001 From: Jonas Everaert <62475953+Jomy10@users.noreply.github.com> Date: Tue, 26 Sep 2023 11:15:52 +0200 Subject: [PATCH 2/2] update to new JSON API Co-authored-by: Nigel Baillie --- build/auto-detect.zig | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/build/auto-detect.zig b/build/auto-detect.zig index 6b83097..670606c 100644 --- a/build/auto-detect.zig +++ b/build/auto-detect.zig @@ -26,17 +26,16 @@ pub fn findUserConfig(b: *Builder, versions: Sdk.ToolchainVersions) !UserConfig defer file.close(); // @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; - // } + const bytes = file.readToEndAlloc(b.allocator, 1 * 1000 * 1000) catch |err| { + print("Unexpected error reading {s}: {s}\n", .{ config_path, @errorName(err) }); + return err; + }; + if (std.json.parseFromSlice(UserConfig, b.allocator, bytes, .{})) |conf| { + config = conf.value; + } else |err| { + print("Could not parse {s} ({s}).\n", .{ config_path, @errorName(err) }); + return err; + } } else |err| switch (err) { error.FileNotFound => { config_dirty = true;