From e113b1389b088939a7f577bab1159d3c698af1b7 Mon Sep 17 00:00:00 2001 From: Jae B Date: Sat, 8 Mar 2025 12:51:10 +1100 Subject: [PATCH 1/5] start work on updating to Zig 0.14.0 --- README.md | 2 +- build.zig.zon | 3 +- examples/minimal/build.zig | 8 +- examples/minimal/build.zig.zon | 5 +- examples/sdl2/android/src/ZigSDLActivity.java | 1 - examples/sdl2/build.zig | 16 +- examples/sdl2/build.zig.zon | 5 +- examples/sdl2/third-party/sdl2/build.zig | 151 ++++++++---------- examples/sdl2/third-party/sdl2/build.zig.zon | 4 +- src/android/android.zig | 4 +- src/androidbuild/androidbuild.zig | 9 +- src/androidbuild/apk.zig | 14 +- 12 files changed, 106 insertions(+), 116 deletions(-) diff --git a/README.md b/README.md index 5d8fd1c..359e5ef 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Add the following to your build.zig.zon file and run `zig build`. ```zig .{ .dependencies = .{ - .@"zig-android-sdk" = .{ + .android = .{ .path = "https://github.com/silbinarywolf/zig-android-sdk/archive/REPLACE_WITH_WANTED_COMMIT.tar.gz", // .hash = REPLACE_WITH_HASH_FROM_BUILD_ERROR }, diff --git a/build.zig.zon b/build.zig.zon index 5c5ed07..40f55d2 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,5 +1,5 @@ .{ - .name = "zig-android-sdk", + .name = .android, .version = "0.1.0", .dependencies = .{}, .paths = .{ @@ -7,4 +7,5 @@ "build.zig.zon", "src", }, + .fingerprint = 0x92bcb62d42fb2cee, } diff --git a/examples/minimal/build.zig b/examples/minimal/build.zig index 19eb9e2..d53f0ec 100644 --- a/examples/minimal/build.zig +++ b/examples/minimal/build.zig @@ -1,6 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); -const android = @import("zig-android-sdk"); +const android = @import("android"); pub fn build(b: *std.Build) void { const exe_name: []const u8 = "minimal"; @@ -37,7 +37,7 @@ pub fn build(b: *std.Build) void { }; for (targets) |target| { - var exe: *std.Build.Step.Compile = if (target.result.isAndroid()) b.addSharedLibrary(.{ + var exe: *std.Build.Step.Compile = if (target.result.abi.isAndroid()) b.addSharedLibrary(.{ .name = exe_name, .root_source_file = b.path("src/minimal.zig"), .target = target, @@ -52,9 +52,9 @@ pub fn build(b: *std.Build) void { // if building as library for Android, add this target // NOTE: Android has different CPU targets so you need to build a version of your // code for x86, x86_64, arm, arm64 and more - if (target.result.isAndroid()) { + if (target.result.abi.isAndroid()) { const apk: *android.APK = android_apk orelse @panic("Android APK should be initialized"); - const android_dep = b.dependency("zig-android-sdk", .{ + const android_dep = b.dependency("android", .{ .optimize = optimize, .target = target, }); diff --git a/examples/minimal/build.zig.zon b/examples/minimal/build.zig.zon index 919d486..cc8982c 100644 --- a/examples/minimal/build.zig.zon +++ b/examples/minimal/build.zig.zon @@ -1,8 +1,8 @@ .{ - .name = "minimal", + .name = .minimal, .version = "0.0.0", .dependencies = .{ - .@"zig-android-sdk" = .{ + .android = .{ .path = "../..", }, }, @@ -11,4 +11,5 @@ "build.zig.zon", "src", }, + .fingerprint = 0x4037f28ad1eb4832, } diff --git a/examples/sdl2/android/src/ZigSDLActivity.java b/examples/sdl2/android/src/ZigSDLActivity.java index 79e6336..998ed62 100644 --- a/examples/sdl2/android/src/ZigSDLActivity.java +++ b/examples/sdl2/android/src/ZigSDLActivity.java @@ -9,7 +9,6 @@ public class ZigSDLActivity extends SDLActivity { // @Override // protected String[] getLibraries() { // return new String[] { - // "hidapi", // "SDL2", // // "SDL2_image", // // "SDL2_mixer", diff --git a/examples/sdl2/build.zig b/examples/sdl2/build.zig index c92ffbf..6bbc26d 100644 --- a/examples/sdl2/build.zig +++ b/examples/sdl2/build.zig @@ -1,6 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); -const android = @import("zig-android-sdk"); +const android = @import("android"); pub fn build(b: *std.Build) void { const root_target = b.standardTargetOptions(.{}); @@ -47,7 +47,7 @@ pub fn build(b: *std.Build) void { for (targets) |target| { const exe_name: []const u8 = "sdl-zig-demo"; - var exe: *std.Build.Step.Compile = if (target.result.isAndroid()) b.addSharedLibrary(.{ + var exe: *std.Build.Step.Compile = if (target.result.abi.isAndroid()) b.addSharedLibrary(.{ .name = exe_name, .root_source_file = b.path("src/sdl-zig-demo.zig"), .target = target, @@ -65,7 +65,7 @@ pub fn build(b: *std.Build) void { .optimize = .ReleaseFast, .target = target, }); - if (target.result.os.tag == .linux and !target.result.isAndroid()) { + if (target.result.os.tag == .linux and !target.result.abi.isAndroid()) { // The SDL package doesn't work for Linux yet, so we rely on system // packages for now. exe.linkSystemLibrary("SDL2"); @@ -75,12 +75,6 @@ pub fn build(b: *std.Build) void { exe.linkLibrary(sdl_lib); } - // NOTE(jae): 2024-09-22 - // Load additional dynamic libraries that SDLActivity.java loads such as hidapi - if (target.result.isAndroid()) { - exe.linkLibrary(sdl_dep.artifact("hidapi")); - } - const sdl_module = sdl_dep.module("sdl"); exe.root_module.addImport("sdl", sdl_module); } @@ -88,9 +82,9 @@ pub fn build(b: *std.Build) void { // if building as library for Android, add this target // NOTE: Android has different CPU targets so you need to build a version of your // code for x86, x86_64, arm, arm64 and more - if (target.result.isAndroid()) { + if (target.result.abi.isAndroid()) { const apk: *android.APK = android_apk orelse @panic("Android APK should be initialized"); - const android_dep = b.dependency("zig-android-sdk", .{ + const android_dep = b.dependency("android", .{ .optimize = optimize, .target = target, }); diff --git a/examples/sdl2/build.zig.zon b/examples/sdl2/build.zig.zon index 9d39231..ca57d66 100644 --- a/examples/sdl2/build.zig.zon +++ b/examples/sdl2/build.zig.zon @@ -1,8 +1,8 @@ .{ - .name = "minimal", + .name = .sdl2, .version = "0.0.0", .dependencies = .{ - .@"zig-android-sdk" = .{ + .android = .{ .path = "../..", }, .sdl2 = .{ @@ -14,4 +14,5 @@ "build.zig.zon", "src", }, + .fingerprint = 0x168fc6b9a7d0df48, } diff --git a/examples/sdl2/third-party/sdl2/build.zig b/examples/sdl2/third-party/sdl2/build.zig index ffc2995..ae5af38 100644 --- a/examples/sdl2/third-party/sdl2/build.zig +++ b/examples/sdl2/third-party/sdl2/build.zig @@ -72,89 +72,78 @@ pub fn build(b: *std.Build) !void { lib.linkFramework("CoreHaptics"); }, else => { - switch (target.result.abi) { - .android => { + if (target.result.abi.isAndroid()) { + lib.root_module.addCSourceFiles(.{ + .root = sdl_path, + .files = &android_src_files, + }); + + // This is needed for "src/render/opengles/SDL_render_gles.c" to compile + lib.root_module.addCMacro("GL_GLEXT_PROTOTYPES", "1"); + + // Add Java files to dependency + const java_dir = sdl_dep.path("android-project/app/src/main/java/org/libsdl/app"); + const java_files: []const []const u8 = &.{ + "SDL.java", + "SDLSurface.java", + "SDLActivity.java", + "SDLAudioManager.java", + "SDLControllerManager.java", + "HIDDevice.java", + "HIDDeviceUSB.java", + "HIDDeviceManager.java", + "HIDDeviceBLESteamController.java", + }; + const java_write_files = b.addNamedWriteFiles("sdljava"); + for (java_files) |java_file_basename| { + _ = java_write_files.addCopyFile(java_dir.path(b, java_file_basename), java_file_basename); + } + + // https://github.com/libsdl-org/SDL/blob/release-2.30.6/Android.mk#L82C62-L82C69 + lib.linkSystemLibrary("dl"); + lib.linkSystemLibrary("GLESv1_CM"); + lib.linkSystemLibrary("GLESv2"); + lib.linkSystemLibrary("OpenSLES"); + lib.linkSystemLibrary("log"); + lib.linkSystemLibrary("android"); + + // SDLActivity.java's getMainFunction defines the entrypoint as "SDL_main" + // So your main / root file will need something like this for Android + // + // fn android_sdl_main() callconv(.C) void { + // _ = std.start.callMain(); + // } + // comptime { + // if (builtin.abi == .android) @export(android_sdl_main, .{ .name = "SDL_main", .linkage = .strong }); + // } + + // TODO(jae): 2025-03-08 + // Need to investigate why hidapi is failing as of Zig 0.14.0 + const use_hidapi = false; + if (!use_hidapi) { + lib.root_module.addCMacro("SDL_HIDAPI_DISABLED", ""); + } else { + // NOTE(jae): 2024-09-22 + // Build settings taken from: src/hidapi/android/jni/Android.mk + // SDLActivity.java by default expects to be able to load this library lib.root_module.addCSourceFiles(.{ .root = sdl_path, - .files = &android_src_files, + .files = &[_][]const u8{ + "src/hidapi/android/hid.cpp", + }, + .flags = &.{"-std=c++11"}, }); - - // This is needed for "src/render/opengles/SDL_render_gles.c" to compile - lib.root_module.addCMacro("GL_GLEXT_PROTOTYPES", "1"); - - // Add Java files to dependency - const java_dir = sdl_dep.path("android-project/app/src/main/java/org/libsdl/app"); - const java_files: []const []const u8 = &.{ - "SDL.java", - "SDLSurface.java", - "SDLActivity.java", - "SDLAudioManager.java", - "SDLControllerManager.java", - "HIDDevice.java", - "HIDDeviceUSB.java", - "HIDDeviceManager.java", - "HIDDeviceBLESteamController.java", - }; - const java_write_files = b.addNamedWriteFiles("sdljava"); - for (java_files) |java_file_basename| { - _ = java_write_files.addCopyFile(java_dir.path(b, java_file_basename), java_file_basename); - } - - // https://github.com/libsdl-org/SDL/blob/release-2.30.6/Android.mk#L82C62-L82C69 - lib.linkSystemLibrary("dl"); - lib.linkSystemLibrary("GLESv1_CM"); - lib.linkSystemLibrary("GLESv2"); - lib.linkSystemLibrary("OpenSLES"); - lib.linkSystemLibrary("log"); - lib.linkSystemLibrary("android"); - - // SDLActivity.java's getMainFunction defines the entrypoint as "SDL_main" - // So your main / root file will need something like this for Android - // - // fn android_sdl_main() callconv(.C) void { - // _ = std.start.callMain(); - // } - // comptime { - // if (builtin.abi == .android) @export(android_sdl_main, .{ .name = "SDL_main", .linkage = .strong }); - // } - - const use_hidapi = true; - if (!use_hidapi) { - lib.root_module.addCMacro("SDL_HIDAPI_DISABLED", ""); - } else { - // NOTE(jae): 2024-09-22 - // Build settings taken from: src/hidapi/android/jni/Android.mk - // SDLActivity.java by default expects to be able to load this library. - const hidapi_lib = b.addSharedLibrary(.{ - .name = "hidapi", - .target = target, - .optimize = optimize, - .link_libc = true, - }); - hidapi_lib.addIncludePath(sdl_include_path); - hidapi_lib.root_module.addCSourceFiles(.{ - .root = sdl_path, - .files = &[_][]const u8{ - "src/hidapi/android/hid.cpp", - }, - .flags = &.{"-std=c++11"}, - }); - hidapi_lib.linkSystemLibrary("log"); - hidapi_lib.linkLibCpp(); - lib.linkLibrary(hidapi_lib); - b.installArtifact(hidapi_lib); - } - }, - else => { - const config_header = b.addConfigHeader(.{ - .style = .{ .cmake = sdl_include_path.path(b, "SDL_config.h.cmake") }, - .include_path = "SDL/SDL_config.h", - }, .{}); - sdl_config_header = config_header; - - lib.addConfigHeader(config_header); - lib.installConfigHeader(config_header); - }, + lib.linkLibCpp(); + } + } else { + const config_header = b.addConfigHeader(.{ + .style = .{ .cmake = sdl_include_path.path(b, "SDL_config.h.cmake") }, + .include_path = "SDL/SDL_config.h", + }, .{}); + sdl_config_header = config_header; + + lib.addConfigHeader(config_header); + lib.installConfigHeader(config_header); } }, } @@ -167,6 +156,8 @@ pub fn build(b: *std.Build) !void { b.installArtifact(lib); var module = b.addModule("sdl", .{ + .target = b.graph.host, + .optimize = .ReleaseFast, .root_source_file = b.path("src/sdl.zig"), }); if (sdl_config_header) |config_header| { diff --git a/examples/sdl2/third-party/sdl2/build.zig.zon b/examples/sdl2/third-party/sdl2/build.zig.zon index e427b20..cfe2e9f 100644 --- a/examples/sdl2/third-party/sdl2/build.zig.zon +++ b/examples/sdl2/third-party/sdl2/build.zig.zon @@ -5,8 +5,8 @@ .sdl2 = .{ // NOTE(jae): 2024-06-30 // Using ".zip" as "tar.gz" fails on Windows for Zig 0.13.0 due to symlink issue with something in the android folders - .url = "https://github.com/libsdl-org/SDL/archive/refs/tags/release-2.30.4.zip", - .hash = "122086865a4d5e822c068528a4170cb7da52afad38e8232afa3e74789c4d52d27791", + .url = "https://github.com/libsdl-org/SDL/archive/refs/tags/release-2.32.2.zip", + .hash = "12204a4a9e9f41fc906decd762be78b9e80de65a7bdec428aa0dfdf03f46e7614d9e", }, }, .paths = .{ diff --git a/src/android/android.zig b/src/android/android.zig index 7218030..0628e7f 100644 --- a/src/android/android.zig +++ b/src/android/android.zig @@ -306,7 +306,7 @@ pub const Panic = struct { fn dumpStackTrace(stack_trace: std.builtin.StackTrace) void { nosuspend { - if (comptime builtin.target.isWasm()) { + if (comptime builtin.target.cpu.arch.isWasm()) { if (native_os == .wasi) { const stderr = io.getStdErr().writer(); stderr.print("Unable to dump stack trace: not implemented for Wasm\n", .{}) catch return; @@ -341,7 +341,7 @@ pub const Panic = struct { const writeCurrentStackTrace = std.debug.writeCurrentStackTrace; fn dumpCurrentStackTrace(start_addr: ?usize) void { nosuspend { - if (comptime builtin.target.isWasm()) { + if (comptime builtin.target.cpu.arch.isWasm()) { if (native_os == .wasi) { const stderr = io.getStdErr().writer(); stderr.print("Unable to dump stack trace: not implemented for Wasm\n", .{}) catch return; diff --git a/src/androidbuild/androidbuild.zig b/src/androidbuild/androidbuild.zig index 49b97fc..f8f6392 100644 --- a/src/androidbuild/androidbuild.zig +++ b/src/androidbuild/androidbuild.zig @@ -45,7 +45,7 @@ pub const KeyStore = struct { }; pub fn getAndroidTriple(target: ResolvedTarget) error{InvalidAndroidTarget}![]const u8 { - if (target.result.abi != .android) return error.InvalidAndroidTarget; + if (!target.result.abi.isAndroid()) return error.InvalidAndroidTarget; return switch (target.result.cpu.arch) { .x86 => "i686-linux-android", .x86_64 => "x86_64-linux-android", @@ -66,7 +66,7 @@ pub fn standardTargets(b: *std.Build, target: ResolvedTarget) []ResolvedTarget { if (all_targets) { return getAllAndroidTargets(b); } - if (target.result.abi != .android) { + if (!target.result.abi.isAndroid()) { return &[0]ResolvedTarget{}; } if (target.result.os.tag != .linux) { @@ -130,7 +130,7 @@ const AndroidTargetQuery = struct { return .{ .os_tag = .linux, .cpu_model = .baseline, - .abi = .android, + .abi = if (android_target.cpu_arch != .arm) .android else .androideabi, .cpu_arch = android_target.cpu_arch, .cpu_features_add = android_target.cpu_features_add, }; @@ -154,6 +154,9 @@ const supported_android_targets = [_]AndroidTargetQuery{ // TODO(jae): 2024-09-08 // This doesn't work for compiling C code like SDL2 or OpenXR due to "__ARM_ARCH" not being "7" // or similar. I might be messing something up here but not sure. + // + // TODO(jae): 2025-03-08 + // Look into fixing: src/cpuinfo/SDL_cpuinfo.c:93:10: error: 'cpu-features.h' file not found // .{ // // arm-linux-androideabi // .cpu_arch = .arm, diff --git a/src/androidbuild/apk.zig b/src/androidbuild/apk.zig index ddff07e..c1d9a31 100644 --- a/src/androidbuild/apk.zig +++ b/src/androidbuild/apk.zig @@ -180,7 +180,7 @@ pub const APK = struct { } } if (artifact.root_module.resolved_target) |target| { - if (!target.result.isAndroid()) { + if (!target.result.abi.isAndroid()) { try errors.append(b.fmt("artifact[{}]: must be targetting Android abi", .{i})); continue; } @@ -379,7 +379,7 @@ pub const APK = struct { } } apk.tools.setLibCFile(artifact); - apk.addLibraryPaths(&artifact.root_module); + apk.addLibraryPaths(artifact.root_module); artifact.linkLibC(); } @@ -423,9 +423,9 @@ pub const APK = struct { "utf8", "-cp", apk.tools.root_jar, - // NOTE(jae): 2024-09-19 - // Debug issues with the SDL.java classes - // "-Xlint:deprecation", + // NOTE(jae): 2024-09-19 + // Debug issues with the SDL.java classes + // "-Xlint:deprecation", }); javac_cmd.setName(runNameContext("javac")); javac_cmd.addArg("-d"); @@ -611,12 +611,12 @@ pub const APK = struct { // other_step.root_module.addCMacro("__ARM_ARCH", "7"); // '__ARM_ARCH' macro redefined // other_step.root_module.addCMacro("_M_ARM", ""); // } - // other_step.root_module.addCMacro("__ANDROID__", ""); + // artifact.root_module.addCMacro("__ANDROID__", "1"); apk.tools.setLibCFile(artifact); } // Add library paths to find "android", "log", etc - apk.addLibraryPaths(&artifact.root_module); + apk.addLibraryPaths(artifact.root_module); // Update libraries linked to this library apk.updateLinkObjects(artifact, so_dir, raw_top_level_apk_files); From e79586cba0df3ec41015d47cda06e0a52a0f4476 Mon Sep 17 00:00:00 2001 From: Jae B Date: Sun, 9 Mar 2025 18:37:02 +1100 Subject: [PATCH 2/5] fix SDL2 example --- README.md | 2 +- examples/minimal/src/minimal.zig | 4 +- examples/sdl2/src/sdl-zig-demo.zig | 6 +-- examples/sdl2/third-party/sdl2/build.zig | 48 ++++++++++++++---------- 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 359e5ef..2422094 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ zig build -Dandroid=true ```zig // This is an overly simplified example to give you the gist // of how this library works, see: examples/minimal/build.zig -const android = @import("zig-android-sdk"); +const android = @import("android"); pub fn build(b: *std.Build) !void { const android_tools = android.Tools.create(b, ...); diff --git a/examples/minimal/src/minimal.zig b/examples/minimal/src/minimal.zig index f3e8222..fd69a33 100644 --- a/examples/minimal/src/minimal.zig +++ b/examples/minimal/src/minimal.zig @@ -5,7 +5,7 @@ const androidbind = @import("android-bind.zig"); const log = std.log; /// custom standard options for Android -pub const std_options: std.Options = if (builtin.abi == .android) +pub const std_options: std.Options = if (builtin.abi.isAndroid()) .{ .logFn = android.logFn, } @@ -13,7 +13,7 @@ else .{}; /// custom panic handler for Android -pub const panic = if (builtin.abi == .android) +pub const panic = if (builtin.abi.isAndroid()) android.panic else std.builtin.default_panic; diff --git a/examples/sdl2/src/sdl-zig-demo.zig b/examples/sdl2/src/sdl-zig-demo.zig index 01d5e66..cbba322 100644 --- a/examples/sdl2/src/sdl-zig-demo.zig +++ b/examples/sdl2/src/sdl-zig-demo.zig @@ -7,7 +7,7 @@ const log = std.log; const assert = std.debug.assert; /// custom standard options for Android -pub const std_options: std.Options = if (builtin.abi == .android) +pub const std_options: std.Options = if (builtin.abi.isAndroid()) .{ .logFn = android.logFn, } @@ -15,14 +15,14 @@ else .{}; /// custom panic handler for Android -pub const panic = if (builtin.abi == .android) +pub const panic = if (builtin.abi.isAndroid()) android.panic else std.builtin.default_panic; /// This needs to be exported for Android builds export fn SDL_main() callconv(.C) void { - if (builtin.abi == .android) { + if (builtin.abi.isAndroid()) { _ = std.start.callMain(); } else { @panic("SDL_main should not be called outside of Android builds"); diff --git a/examples/sdl2/third-party/sdl2/build.zig b/examples/sdl2/third-party/sdl2/build.zig index ae5af38..ee23f92 100644 --- a/examples/sdl2/third-party/sdl2/build.zig +++ b/examples/sdl2/third-party/sdl2/build.zig @@ -9,7 +9,7 @@ pub fn build(b: *std.Build) !void { const sdl_path = sdl_dep.path(""); const sdl_include_path = sdl_path.path(b, "include"); - const is_shared_library = target.result.abi == .android; // NOTE(jae): 2024-09-22: Android uses shared library as SDL2 loads it as part of SDLActivity.java + const is_shared_library = target.result.abi.isAndroid(); // NOTE(jae): 2024-09-22: Android uses shared library as SDL2 loads it as part of SDLActivity.java const lib = if (!is_shared_library) b.addStaticLibrary(.{ .name = "SDL2", .target = target, @@ -117,24 +117,34 @@ pub fn build(b: *std.Build) !void { // if (builtin.abi == .android) @export(android_sdl_main, .{ .name = "SDL_main", .linkage = .strong }); // } - // TODO(jae): 2025-03-08 - // Need to investigate why hidapi is failing as of Zig 0.14.0 - const use_hidapi = false; - if (!use_hidapi) { - lib.root_module.addCMacro("SDL_HIDAPI_DISABLED", ""); - } else { - // NOTE(jae): 2024-09-22 - // Build settings taken from: src/hidapi/android/jni/Android.mk - // SDLActivity.java by default expects to be able to load this library - lib.root_module.addCSourceFiles(.{ - .root = sdl_path, - .files = &[_][]const u8{ - "src/hidapi/android/hid.cpp", - }, - .flags = &.{"-std=c++11"}, - }); - lib.linkLibCpp(); - } + const hidapi_lib = b.addStaticLibrary(.{ + .name = "hidapi", + .target = target, + .optimize = optimize, + .link_libc = true, + }); + hidapi_lib.addIncludePath(sdl_include_path); + + // Avoid linking with linkLibCpp() as that causes issues as Zig 0.14.0 attempts to mix + // its own C++ includes with those auto-included by the Zig Android SDK. + // + // However, not linking c++ means when loading on X86_64 systems, you get + // unresolved symbol "_Unwind_Resume" when SDL2 is loaded, so to workaround that + // we link the "unwind" library + hidapi_lib.linkSystemLibrary("unwind"); + + // NOTE(jae): 2024-09-22 + // Build settings taken from: SDL2-2.32.2/src/hidapi/android/jni/Android.mk + // SDLActivity.java by default expects to be able to load this library + hidapi_lib.root_module.linkSystemLibrary("log", .{}); + hidapi_lib.root_module.addCSourceFiles(.{ + .root = sdl_path, + .files = &[_][]const u8{ + "src/hidapi/android/hid.cpp", + }, + .flags = &.{"-std=c++11"}, + }); + lib.linkLibrary(hidapi_lib); } else { const config_header = b.addConfigHeader(.{ .style = .{ .cmake = sdl_include_path.path(b, "SDL_config.h.cmake") }, From 52d2b872d537a254da0916ff7a0cd7ac5cb50089 Mon Sep 17 00:00:00 2001 From: Jae B Date: Sun, 9 Mar 2025 18:38:45 +1100 Subject: [PATCH 3/5] add minimum zig version to zon --- build.zig.zon | 1 + 1 file changed, 1 insertion(+) diff --git a/build.zig.zon b/build.zig.zon index 40f55d2..8b19a3c 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -7,5 +7,6 @@ "build.zig.zon", "src", }, + .minimum_zig_version = "0.14.0", .fingerprint = 0x92bcb62d42fb2cee, } From 37e0951599b165ea940adb8b133ded6ac9798b8d Mon Sep 17 00:00:00 2001 From: Jae B Date: Sun, 9 Mar 2025 19:22:58 +1100 Subject: [PATCH 4/5] fix ARM build failing for SDL2 --- .github/workflows/ci.yml | 18 ++++++------------ examples/minimal/build.zig | 2 +- examples/sdl2/build.zig | 11 ++++++++++- src/androidbuild/androidbuild.zig | 18 +++++++----------- src/androidbuild/apk.zig | 13 +++++++++++++ 5 files changed, 37 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 092c061..9487e38 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,21 +36,21 @@ jobs: - name: Setup Android SDK uses: android-actions/setup-android@v3 with: - packages: 'tools platform-tools platforms;android-35 build-tools;35.0.0 ndk;27.0.12077973' + packages: 'tools platform-tools platforms;android-35 build-tools;35.0.0 ndk;29.0.13113456' - - name: Setup Zig 0.13.0 + - name: Setup Zig 0.14.0 # note(jae): 2024-09-15 # Uses download mirror first as preferred by Zig Foundation # see: https://ziglang.org/news/migrate-to-self-hosting/ uses: mlugg/setup-zig@v1 with: - version: "0.13.0" + version: "0.14.0" - - name: Build Minimal Example (Zig 0.13.0) + - name: Build Minimal Example (Zig Nightly) run: zig build -Dandroid=true --verbose working-directory: examples/minimal - - name: Build SDL2 Example (Zig 0.13.0) + - name: Build SDL2 Example (Zig Nightly) run: zig build -Dandroid=true --verbose working-directory: examples/sdl2 @@ -64,11 +64,5 @@ jobs: working-directory: examples/minimal - name: Build SDL2 Example (Zig Nightly) - # "zig build -Dandroid=true" fails for 0.14.0-dev.1632 - # - # android/sdk/ndk/27.0.12077973/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/android/hardware_buffer.h:322:42: - # error: expression is not an integral constant expression - # - # See: https://github.com/silbinarywolf/zig-android-sdk/actions/runs/10979711793/job/30484520174?pr=5#step:10:30 - run: zig build -Dtarget=x86_64-linux-android --verbose + run: zig build -Dandroid=true --verbose working-directory: examples/sdl2 diff --git a/examples/minimal/build.zig b/examples/minimal/build.zig index d53f0ec..6b8837a 100644 --- a/examples/minimal/build.zig +++ b/examples/minimal/build.zig @@ -22,7 +22,7 @@ pub fn build(b: *std.Build) void { const android_tools = android.Tools.create(b, .{ .api_level = .android15, .build_tools_version = "35.0.0", - .ndk_version = "27.0.12077973", + .ndk_version = "29.0.13113456", }); const apk = android.APK.create(b, android_tools); diff --git a/examples/sdl2/build.zig b/examples/sdl2/build.zig index 6bbc26d..eec6e45 100644 --- a/examples/sdl2/build.zig +++ b/examples/sdl2/build.zig @@ -21,7 +21,16 @@ pub fn build(b: *std.Build) void { const android_tools = android.Tools.create(b, .{ .api_level = .android15, .build_tools_version = "35.0.0", - .ndk_version = "27.0.12077973", + .ndk_version = "29.0.13113456", + // NOTE(jae): 2025-03-09 + // Previously this example used 'ndk' "27.0.12077973". + // + // However that has issues with the latest SDL2 version when including 'hardware_buffer.h' + // for 32-bit builds. + // + // - AHARDWAREBUFFER_USAGE_FRONT_BUFFER = 1UL << 32 + // - ndk/27.0.12077973/toolchains/llvm/prebuilt/{OS}-x86_64/sysroot/usr/include/android/hardware_buffer.h:322:42: + // - error: expression is not an integral constant expression }); const apk = android.APK.create(b, android_tools); diff --git a/src/androidbuild/androidbuild.zig b/src/androidbuild/androidbuild.zig index f8f6392..e422bbf 100644 --- a/src/androidbuild/androidbuild.zig +++ b/src/androidbuild/androidbuild.zig @@ -151,15 +151,11 @@ const supported_android_targets = [_]AndroidTargetQuery{ .cpu_arch = .aarch64, .cpu_features_add = Target.aarch64.featureSet(&.{.v8a}), }, - // TODO(jae): 2024-09-08 - // This doesn't work for compiling C code like SDL2 or OpenXR due to "__ARM_ARCH" not being "7" - // or similar. I might be messing something up here but not sure. - // - // TODO(jae): 2025-03-08 - // Look into fixing: src/cpuinfo/SDL_cpuinfo.c:93:10: error: 'cpu-features.h' file not found - // .{ - // // arm-linux-androideabi - // .cpu_arch = .arm, - // .cpu_features_add = Target.arm.featureSet(&.{.v7a}), - // }, + // NOTE(jae): 2024-09-08 + // 'arm-linux-androideabi' previously didn't work with Zig 0.13.0 for compiling C code like SDL2 or OpenXR due to "__ARM_ARCH" not being "7" + .{ + // arm-linux-androideabi + .cpu_arch = .arm, + .cpu_features_add = Target.arm.featureSet(&.{.v7a}), + }, }; diff --git a/src/androidbuild/apk.zig b/src/androidbuild/apk.zig index c1d9a31..b039daa 100644 --- a/src/androidbuild/apk.zig +++ b/src/androidbuild/apk.zig @@ -129,6 +129,19 @@ pub const APK = struct { // - "java.lang.UnsatisfiedLinkError: dlopen failed: TLS symbol "_ZZN8gwp_asan15getThreadLocalsEvE6Locals" in dlopened" const android_api_version: u32 = @intFromEnum(apk.tools.api_level); + // NOTE(jae): 2025-03-09 + // Resolve issue where building SDL2 gets the following error for 'arm-linux-androideabi' + // SDL2-2.32.2/src/cpuinfo/SDL_cpuinfo.c:93:10: error: 'cpu-features.h' file not found + // + // This include is specifically needed for: #if defined(__ANDROID__) && defined(__arm__) && !defined(HAVE_GETAUXVAL) + // + // ie. $ANDROID_HOME/ndk/{ndk_version}/sources/android/cpufeatures + if (module.resolved_target) |resolved_target| { + if (resolved_target.result.cpu.arch == .arm) { + module.addIncludePath(.{ .cwd_relative = b.fmt("{s}/ndk/{s}/sources/android/cpufeatures", .{ apk.tools.android_sdk_path, apk.tools.ndk_version }) }); + } + } + // ie. $ANDROID_HOME/ndk/{ndk_version}/toolchains/llvm/prebuilt/{host_os_and_arch}/sysroot ++ usr/lib/aarch64-linux-android/35 module.addLibraryPath(.{ .cwd_relative = b.fmt("{s}/usr/lib/{s}/{d}", .{ android_ndk_sysroot, system_target, android_api_version }) }); // ie. $ANDROID_HOME/ndk/{ndk_version}/toolchains/llvm/prebuilt/{host_os_and_arch}/sysroot ++ /usr/lib/aarch64-linux-android From 99a83afc37edc4bd4b8d4e5f135a788878d39c98 Mon Sep 17 00:00:00 2001 From: Jae B Date: Sun, 9 Mar 2025 20:37:05 +1100 Subject: [PATCH 5/5] fix ci settings --- .github/workflows/ci.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9487e38..465b4fa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,6 @@ jobs: include: - os: "ubuntu-22.04" - os: "windows-latest" - - os: "macos-12" # x86_64 - os: "macos-14" # arm64 as per table: https://github.com/actions/runner-images/blob/8a1eeaf6ac70c66f675a04078d1a7222edd42008/README.md#available-images runs-on: ${{matrix.os}} @@ -38,7 +37,7 @@ jobs: with: packages: 'tools platform-tools platforms;android-35 build-tools;35.0.0 ndk;29.0.13113456' - - name: Setup Zig 0.14.0 + - name: Setup Zig Stable (0.14.0) # note(jae): 2024-09-15 # Uses download mirror first as preferred by Zig Foundation # see: https://ziglang.org/news/migrate-to-self-hosting/ @@ -46,11 +45,11 @@ jobs: with: version: "0.14.0" - - name: Build Minimal Example (Zig Nightly) + - name: Build Minimal Example (Zig Stable) run: zig build -Dandroid=true --verbose working-directory: examples/minimal - - name: Build SDL2 Example (Zig Nightly) + - name: Build SDL2 Example (Zig Stable) run: zig build -Dandroid=true --verbose working-directory: examples/sdl2