Skip to content

Commit e79586c

Browse files
committed
fix SDL2 example
1 parent e113b13 commit e79586c

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ zig build -Dandroid=true
1818
```zig
1919
// This is an overly simplified example to give you the gist
2020
// of how this library works, see: examples/minimal/build.zig
21-
const android = @import("zig-android-sdk");
21+
const android = @import("android");
2222
2323
pub fn build(b: *std.Build) !void {
2424
const android_tools = android.Tools.create(b, ...);

examples/minimal/src/minimal.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ const androidbind = @import("android-bind.zig");
55
const log = std.log;
66

77
/// custom standard options for Android
8-
pub const std_options: std.Options = if (builtin.abi == .android)
8+
pub const std_options: std.Options = if (builtin.abi.isAndroid())
99
.{
1010
.logFn = android.logFn,
1111
}
1212
else
1313
.{};
1414

1515
/// custom panic handler for Android
16-
pub const panic = if (builtin.abi == .android)
16+
pub const panic = if (builtin.abi.isAndroid())
1717
android.panic
1818
else
1919
std.builtin.default_panic;

examples/sdl2/src/sdl-zig-demo.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ const log = std.log;
77
const assert = std.debug.assert;
88

99
/// custom standard options for Android
10-
pub const std_options: std.Options = if (builtin.abi == .android)
10+
pub const std_options: std.Options = if (builtin.abi.isAndroid())
1111
.{
1212
.logFn = android.logFn,
1313
}
1414
else
1515
.{};
1616

1717
/// custom panic handler for Android
18-
pub const panic = if (builtin.abi == .android)
18+
pub const panic = if (builtin.abi.isAndroid())
1919
android.panic
2020
else
2121
std.builtin.default_panic;
2222

2323
/// This needs to be exported for Android builds
2424
export fn SDL_main() callconv(.C) void {
25-
if (builtin.abi == .android) {
25+
if (builtin.abi.isAndroid()) {
2626
_ = std.start.callMain();
2727
} else {
2828
@panic("SDL_main should not be called outside of Android builds");

examples/sdl2/third-party/sdl2/build.zig

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn build(b: *std.Build) !void {
99
const sdl_path = sdl_dep.path("");
1010
const sdl_include_path = sdl_path.path(b, "include");
1111

12-
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
12+
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
1313
const lib = if (!is_shared_library) b.addStaticLibrary(.{
1414
.name = "SDL2",
1515
.target = target,
@@ -117,24 +117,34 @@ pub fn build(b: *std.Build) !void {
117117
// if (builtin.abi == .android) @export(android_sdl_main, .{ .name = "SDL_main", .linkage = .strong });
118118
// }
119119

120-
// TODO(jae): 2025-03-08
121-
// Need to investigate why hidapi is failing as of Zig 0.14.0
122-
const use_hidapi = false;
123-
if (!use_hidapi) {
124-
lib.root_module.addCMacro("SDL_HIDAPI_DISABLED", "");
125-
} else {
126-
// NOTE(jae): 2024-09-22
127-
// Build settings taken from: src/hidapi/android/jni/Android.mk
128-
// SDLActivity.java by default expects to be able to load this library
129-
lib.root_module.addCSourceFiles(.{
130-
.root = sdl_path,
131-
.files = &[_][]const u8{
132-
"src/hidapi/android/hid.cpp",
133-
},
134-
.flags = &.{"-std=c++11"},
135-
});
136-
lib.linkLibCpp();
137-
}
120+
const hidapi_lib = b.addStaticLibrary(.{
121+
.name = "hidapi",
122+
.target = target,
123+
.optimize = optimize,
124+
.link_libc = true,
125+
});
126+
hidapi_lib.addIncludePath(sdl_include_path);
127+
128+
// Avoid linking with linkLibCpp() as that causes issues as Zig 0.14.0 attempts to mix
129+
// its own C++ includes with those auto-included by the Zig Android SDK.
130+
//
131+
// However, not linking c++ means when loading on X86_64 systems, you get
132+
// unresolved symbol "_Unwind_Resume" when SDL2 is loaded, so to workaround that
133+
// we link the "unwind" library
134+
hidapi_lib.linkSystemLibrary("unwind");
135+
136+
// NOTE(jae): 2024-09-22
137+
// Build settings taken from: SDL2-2.32.2/src/hidapi/android/jni/Android.mk
138+
// SDLActivity.java by default expects to be able to load this library
139+
hidapi_lib.root_module.linkSystemLibrary("log", .{});
140+
hidapi_lib.root_module.addCSourceFiles(.{
141+
.root = sdl_path,
142+
.files = &[_][]const u8{
143+
"src/hidapi/android/hid.cpp",
144+
},
145+
.flags = &.{"-std=c++11"},
146+
});
147+
lib.linkLibrary(hidapi_lib);
138148
} else {
139149
const config_header = b.addConfigHeader(.{
140150
.style = .{ .cmake = sdl_include_path.path(b, "SDL_config.h.cmake") },

0 commit comments

Comments
 (0)