@@ -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