Skip to content

Commit 8f635ee

Browse files
Fixes for Zig 0.13.0 (#17)
Various fixes for Zig 0.13.0 before we merge Zig 0.14.0 changes here: #16
1 parent ea49343 commit 8f635ee

File tree

12 files changed

+141
-138
lines changed

12 files changed

+141
-138
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ jobs:
1919
include:
2020
- os: "ubuntu-22.04"
2121
- os: "windows-latest"
22-
- os: "macos-12" # x86_64
2322
- os: "macos-14" # arm64 as per table: https://github.com/actions/runner-images/blob/8a1eeaf6ac70c66f675a04078d1a7222edd42008/README.md#available-images
2423

2524
runs-on: ${{matrix.os}}
@@ -36,7 +35,7 @@ jobs:
3635
- name: Setup Android SDK
3736
uses: android-actions/setup-android@v3
3837
with:
39-
packages: 'tools platform-tools platforms;android-35 build-tools;35.0.0 ndk;27.0.12077973'
38+
packages: 'tools platform-tools platforms;android-35 build-tools;35.0.0 ndk;29.0.13113456'
4039

4140
- name: Setup Zig 0.13.0
4241
# note(jae): 2024-09-15
@@ -53,22 +52,3 @@ jobs:
5352
- name: Build SDL2 Example (Zig 0.13.0)
5453
run: zig build -Dandroid=true --verbose
5554
working-directory: examples/sdl2
56-
57-
- name: Setup Zig Nightly
58-
uses: mlugg/setup-zig@v1
59-
with:
60-
version: "master"
61-
62-
- name: Build Minimal Example (Zig Nightly)
63-
run: zig build -Dandroid=true --verbose
64-
working-directory: examples/minimal
65-
66-
- name: Build SDL2 Example (Zig Nightly)
67-
# "zig build -Dandroid=true" fails for 0.14.0-dev.1632
68-
#
69-
# android/sdk/ndk/27.0.12077973/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/android/hardware_buffer.h:322:42:
70-
# error: expression is not an integral constant expression
71-
#
72-
# See: https://github.com/silbinarywolf/zig-android-sdk/actions/runs/10979711793/job/30484520174?pr=5#step:10:30
73-
run: zig build -Dtarget=x86_64-linux-android --verbose
74-
working-directory: examples/sdl2

README.md

Lines changed: 2 additions & 2 deletions
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, ...);
@@ -52,7 +52,7 @@ Add the following to your build.zig.zon file and run `zig build`.
5252
```zig
5353
.{
5454
.dependencies = .{
55-
.@"zig-android-sdk" = .{
55+
.android = .{
5656
.path = "https://github.com/silbinarywolf/zig-android-sdk/archive/REPLACE_WITH_WANTED_COMMIT.tar.gz",
5757
// .hash = REPLACE_WITH_HASH_FROM_BUILD_ERROR
5858
},

build.zig.zon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
.{
2-
.name = "zig-android-sdk",
2+
.name = "android",
33
.version = "0.1.0",
44
.dependencies = .{},
55
.paths = .{
66
"build.zig",
77
"build.zig.zon",
88
"src",
99
},
10+
.minimum_zig_version = "0.13.0",
11+
.fingerprint = 0x92bcb62d42fb2cee,
1012
}

examples/minimal/build.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const std = @import("std");
22
const builtin = @import("builtin");
3-
const android = @import("zig-android-sdk");
3+
const android = @import("android");
44

55
pub fn build(b: *std.Build) void {
66
const exe_name: []const u8 = "minimal";
@@ -22,7 +22,7 @@ pub fn build(b: *std.Build) void {
2222
const android_tools = android.Tools.create(b, .{
2323
.api_level = .android15,
2424
.build_tools_version = "35.0.0",
25-
.ndk_version = "27.0.12077973",
25+
.ndk_version = "29.0.13113456",
2626
});
2727
const apk = android.APK.create(b, android_tools);
2828

@@ -52,9 +52,9 @@ pub fn build(b: *std.Build) void {
5252
// if building as library for Android, add this target
5353
// NOTE: Android has different CPU targets so you need to build a version of your
5454
// code for x86, x86_64, arm, arm64 and more
55-
if (target.result.isAndroid()) {
55+
if (target.result.abi == .android) {
5656
const apk: *android.APK = android_apk orelse @panic("Android APK should be initialized");
57-
const android_dep = b.dependency("zig-android-sdk", .{
57+
const android_dep = b.dependency("android", .{
5858
.optimize = optimize,
5959
.target = target,
6060
});

examples/minimal/build.zig.zon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.name = "minimal",
33
.version = "0.0.0",
44
.dependencies = .{
5-
.@"zig-android-sdk" = .{
5+
.android = .{
66
.path = "../..",
77
},
88
},
@@ -11,4 +11,5 @@
1111
"build.zig.zon",
1212
"src",
1313
},
14+
.fingerprint = 0x4037f28ad1eb4832,
1415
}

examples/sdl2/android/src/ZigSDLActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public class ZigSDLActivity extends SDLActivity {
99
// @Override
1010
// protected String[] getLibraries() {
1111
// return new String[] {
12-
// "hidapi",
1312
// "SDL2",
1413
// // "SDL2_image",
1514
// // "SDL2_mixer",

examples/sdl2/build.zig

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const std = @import("std");
22
const builtin = @import("builtin");
3-
const android = @import("zig-android-sdk");
3+
const android = @import("android");
44

55
pub fn build(b: *std.Build) void {
66
const root_target = b.standardTargetOptions(.{});
@@ -21,7 +21,16 @@ pub fn build(b: *std.Build) void {
2121
const android_tools = android.Tools.create(b, .{
2222
.api_level = .android15,
2323
.build_tools_version = "35.0.0",
24-
.ndk_version = "27.0.12077973",
24+
.ndk_version = "29.0.13113456",
25+
// NOTE(jae): 2025-03-09
26+
// Previously this example used 'ndk' "27.0.12077973".
27+
//
28+
// However that has issues with the latest SDL2 version when including 'hardware_buffer.h'
29+
// for 32-bit builds.
30+
//
31+
// - AHARDWAREBUFFER_USAGE_FRONT_BUFFER = 1UL << 32
32+
// - ndk/27.0.12077973/toolchains/llvm/prebuilt/{OS}-x86_64/sysroot/usr/include/android/hardware_buffer.h:322:42:
33+
// - error: expression is not an integral constant expression
2534
});
2635
const apk = android.APK.create(b, android_tools);
2736

@@ -47,7 +56,7 @@ pub fn build(b: *std.Build) void {
4756

4857
for (targets) |target| {
4958
const exe_name: []const u8 = "sdl-zig-demo";
50-
var exe: *std.Build.Step.Compile = if (target.result.isAndroid()) b.addSharedLibrary(.{
59+
var exe: *std.Build.Step.Compile = if (target.result.abi == .android) b.addSharedLibrary(.{
5160
.name = exe_name,
5261
.root_source_file = b.path("src/sdl-zig-demo.zig"),
5362
.target = target,
@@ -65,7 +74,7 @@ pub fn build(b: *std.Build) void {
6574
.optimize = .ReleaseFast,
6675
.target = target,
6776
});
68-
if (target.result.os.tag == .linux and !target.result.isAndroid()) {
77+
if (target.result.os.tag == .linux and target.result.abi != .android) {
6978
// The SDL package doesn't work for Linux yet, so we rely on system
7079
// packages for now.
7180
exe.linkSystemLibrary("SDL2");
@@ -75,22 +84,16 @@ pub fn build(b: *std.Build) void {
7584
exe.linkLibrary(sdl_lib);
7685
}
7786

78-
// NOTE(jae): 2024-09-22
79-
// Load additional dynamic libraries that SDLActivity.java loads such as hidapi
80-
if (target.result.isAndroid()) {
81-
exe.linkLibrary(sdl_dep.artifact("hidapi"));
82-
}
83-
8487
const sdl_module = sdl_dep.module("sdl");
8588
exe.root_module.addImport("sdl", sdl_module);
8689
}
8790

8891
// if building as library for Android, add this target
8992
// NOTE: Android has different CPU targets so you need to build a version of your
9093
// code for x86, x86_64, arm, arm64 and more
91-
if (target.result.isAndroid()) {
94+
if (target.result.abi == .android) {
9295
const apk: *android.APK = android_apk orelse @panic("Android APK should be initialized");
93-
const android_dep = b.dependency("zig-android-sdk", .{
96+
const android_dep = b.dependency("android", .{
9497
.optimize = optimize,
9598
.target = target,
9699
});

examples/sdl2/build.zig.zon

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.{
2-
.name = "minimal",
2+
.name = "sdl2",
33
.version = "0.0.0",
44
.dependencies = .{
5-
.@"zig-android-sdk" = .{
5+
.android = .{
66
.path = "../..",
77
},
88
.sdl2 = .{
@@ -14,4 +14,5 @@
1414
"build.zig.zon",
1515
"src",
1616
},
17+
.fingerprint = 0x168fc6b9a7d0df48,
1718
}

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

Lines changed: 84 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -72,89 +72,88 @@ pub fn build(b: *std.Build) !void {
7272
lib.linkFramework("CoreHaptics");
7373
},
7474
else => {
75-
switch (target.result.abi) {
76-
.android => {
77-
lib.root_module.addCSourceFiles(.{
78-
.root = sdl_path,
79-
.files = &android_src_files,
80-
});
81-
82-
// This is needed for "src/render/opengles/SDL_render_gles.c" to compile
83-
lib.root_module.addCMacro("GL_GLEXT_PROTOTYPES", "1");
84-
85-
// Add Java files to dependency
86-
const java_dir = sdl_dep.path("android-project/app/src/main/java/org/libsdl/app");
87-
const java_files: []const []const u8 = &.{
88-
"SDL.java",
89-
"SDLSurface.java",
90-
"SDLActivity.java",
91-
"SDLAudioManager.java",
92-
"SDLControllerManager.java",
93-
"HIDDevice.java",
94-
"HIDDeviceUSB.java",
95-
"HIDDeviceManager.java",
96-
"HIDDeviceBLESteamController.java",
97-
};
98-
const java_write_files = b.addNamedWriteFiles("sdljava");
99-
for (java_files) |java_file_basename| {
100-
_ = java_write_files.addCopyFile(java_dir.path(b, java_file_basename), java_file_basename);
101-
}
102-
103-
// https://github.com/libsdl-org/SDL/blob/release-2.30.6/Android.mk#L82C62-L82C69
104-
lib.linkSystemLibrary("dl");
105-
lib.linkSystemLibrary("GLESv1_CM");
106-
lib.linkSystemLibrary("GLESv2");
107-
lib.linkSystemLibrary("OpenSLES");
108-
lib.linkSystemLibrary("log");
109-
lib.linkSystemLibrary("android");
110-
111-
// SDLActivity.java's getMainFunction defines the entrypoint as "SDL_main"
112-
// So your main / root file will need something like this for Android
113-
//
114-
// fn android_sdl_main() callconv(.C) void {
115-
// _ = std.start.callMain();
116-
// }
117-
// comptime {
118-
// if (builtin.abi == .android) @export(android_sdl_main, .{ .name = "SDL_main", .linkage = .strong });
119-
// }
120-
121-
const use_hidapi = true;
122-
if (!use_hidapi) {
123-
lib.root_module.addCMacro("SDL_HIDAPI_DISABLED", "");
124-
} else {
125-
// NOTE(jae): 2024-09-22
126-
// Build settings taken from: src/hidapi/android/jni/Android.mk
127-
// SDLActivity.java by default expects to be able to load this library.
128-
const hidapi_lib = b.addSharedLibrary(.{
129-
.name = "hidapi",
130-
.target = target,
131-
.optimize = optimize,
132-
.link_libc = true,
133-
});
134-
hidapi_lib.addIncludePath(sdl_include_path);
135-
hidapi_lib.root_module.addCSourceFiles(.{
136-
.root = sdl_path,
137-
.files = &[_][]const u8{
138-
"src/hidapi/android/hid.cpp",
139-
},
140-
.flags = &.{"-std=c++11"},
141-
});
142-
hidapi_lib.linkSystemLibrary("log");
143-
hidapi_lib.linkLibCpp();
144-
lib.linkLibrary(hidapi_lib);
145-
b.installArtifact(hidapi_lib);
146-
}
147-
},
148-
else => {
149-
const config_header = b.addConfigHeader(.{
150-
.style = .{ .cmake = sdl_include_path.path(b, "SDL_config.h.cmake") },
151-
.include_path = "SDL/SDL_config.h",
152-
}, .{});
153-
sdl_config_header = config_header;
154-
155-
lib.addConfigHeader(config_header);
156-
lib.installConfigHeader(config_header);
157-
},
75+
if (target.result.abi == .android) {
76+
lib.root_module.addCSourceFiles(.{
77+
.root = sdl_path,
78+
.files = &android_src_files,
79+
});
80+
81+
// This is needed for "src/render/opengles/SDL_render_gles.c" to compile
82+
lib.root_module.addCMacro("GL_GLEXT_PROTOTYPES", "1");
83+
84+
// Add Java files to dependency
85+
const java_dir = sdl_dep.path("android-project/app/src/main/java/org/libsdl/app");
86+
const java_files: []const []const u8 = &.{
87+
"SDL.java",
88+
"SDLSurface.java",
89+
"SDLActivity.java",
90+
"SDLAudioManager.java",
91+
"SDLControllerManager.java",
92+
"HIDDevice.java",
93+
"HIDDeviceUSB.java",
94+
"HIDDeviceManager.java",
95+
"HIDDeviceBLESteamController.java",
96+
};
97+
const java_write_files = b.addNamedWriteFiles("sdljava");
98+
for (java_files) |java_file_basename| {
99+
_ = java_write_files.addCopyFile(java_dir.path(b, java_file_basename), java_file_basename);
100+
}
101+
102+
// https://github.com/libsdl-org/SDL/blob/release-2.30.6/Android.mk#L82C62-L82C69
103+
lib.linkSystemLibrary("dl");
104+
lib.linkSystemLibrary("GLESv1_CM");
105+
lib.linkSystemLibrary("GLESv2");
106+
lib.linkSystemLibrary("OpenSLES");
107+
lib.linkSystemLibrary("log");
108+
lib.linkSystemLibrary("android");
109+
110+
// SDLActivity.java's getMainFunction defines the entrypoint as "SDL_main"
111+
// So your main / root file will need something like this for Android
112+
//
113+
// fn android_sdl_main() callconv(.C) void {
114+
// _ = std.start.callMain();
115+
// }
116+
// comptime {
117+
// if (builtin.abi == .android) @export(android_sdl_main, .{ .name = "SDL_main", .linkage = .strong });
118+
// }
119+
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);
148+
} else {
149+
const config_header = b.addConfigHeader(.{
150+
.style = .{ .cmake = sdl_include_path.path(b, "SDL_config.h.cmake") },
151+
.include_path = "SDL/SDL_config.h",
152+
}, .{});
153+
sdl_config_header = config_header;
154+
155+
lib.addConfigHeader(config_header);
156+
lib.installConfigHeader(config_header);
158157
}
159158
},
160159
}
@@ -167,6 +166,8 @@ pub fn build(b: *std.Build) !void {
167166
b.installArtifact(lib);
168167

169168
var module = b.addModule("sdl", .{
169+
.target = b.graph.host,
170+
.optimize = .ReleaseFast,
170171
.root_source_file = b.path("src/sdl.zig"),
171172
});
172173
if (sdl_config_header) |config_header| {

0 commit comments

Comments
 (0)