@@ -72,89 +72,78 @@ pub fn build(b: *std.Build) !void {
7272 lib .linkFramework ("CoreHaptics" );
7373 },
7474 else = > {
75- switch (target .result .abi ) {
76- .android = > {
75+ if (target .result .abi .isAndroid ()) {
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+ // 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
77129 lib .root_module .addCSourceFiles (.{
78130 .root = sdl_path ,
79- .files = & android_src_files ,
131+ .files = &[_ ][]const u8 {
132+ "src/hidapi/android/hid.cpp" ,
133+ },
134+ .flags = &.{"-std=c++11" },
80135 });
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- },
136+ lib .linkLibCpp ();
137+ }
138+ } else {
139+ const config_header = b .addConfigHeader (.{
140+ .style = .{ .cmake = sdl_include_path .path (b , "SDL_config.h.cmake" ) },
141+ .include_path = "SDL/SDL_config.h" ,
142+ }, .{});
143+ sdl_config_header = config_header ;
144+
145+ lib .addConfigHeader (config_header );
146+ lib .installConfigHeader (config_header );
158147 }
159148 },
160149 }
@@ -167,6 +156,8 @@ pub fn build(b: *std.Build) !void {
167156 b .installArtifact (lib );
168157
169158 var module = b .addModule ("sdl" , .{
159+ .target = b .graph .host ,
160+ .optimize = .ReleaseFast ,
170161 .root_source_file = b .path ("src/sdl.zig" ),
171162 });
172163 if (sdl_config_header ) | config_header | {
0 commit comments