Skip to content

Commit 11e15ab

Browse files
authored
Merge pull request raysan5#4870 from lumenkeyes/add-zig-android-target
Add Android Target to build.zig
2 parents 1a67dcb + d52687e commit 11e15ab

File tree

1 file changed

+72
-16
lines changed

1 file changed

+72
-16
lines changed

build.zig

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fn setDesktopPlatform(raylib: *std.Build.Step.Compile, platform: PlatformBackend
1616
.glfw => raylib.root_module.addCMacro("PLATFORM_DESKTOP_GLFW", ""),
1717
.rgfw => raylib.root_module.addCMacro("PLATFORM_DESKTOP_RGFW", ""),
1818
.sdl => raylib.root_module.addCMacro("PLATFORM_DESKTOP_SDL", ""),
19+
.android => raylib.root_module.addCMacro("PLATFORM_ANDROID", ""),
1920
else => {},
2021
}
2122
}
@@ -181,7 +182,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
181182
.windows => {
182183
switch (options.platform) {
183184
.glfw => try c_source_files.append("src/rglfw.c"),
184-
.rgfw, .sdl, .drm => {},
185+
.rgfw, .sdl, .drm, .android => {},
185186
}
186187

187188
raylib.linkSystemLibrary("winmm");
@@ -191,7 +192,71 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
191192
setDesktopPlatform(raylib, options.platform);
192193
},
193194
.linux => {
194-
if (options.platform != .drm) {
195+
196+
if (options.platform == .drm) {
197+
if (options.opengl_version == .auto) {
198+
raylib.linkSystemLibrary("GLESv2");
199+
raylib.root_module.addCMacro("GRAPHICS_API_OPENGL_ES2", "");
200+
}
201+
202+
raylib.linkSystemLibrary("EGL");
203+
raylib.linkSystemLibrary("gbm");
204+
raylib.linkSystemLibrary2("libdrm", .{ .use_pkg_config = .force });
205+
206+
raylib.root_module.addCMacro("PLATFORM_DRM", "");
207+
raylib.root_module.addCMacro("EGL_NO_X11", "");
208+
raylib.root_module.addCMacro("DEFAULT_BATCH_BUFFER_ELEMENT", "");
209+
} else if (target.result.abi == .android) {
210+
211+
//these are the only tag options per https://developer.android.com/ndk/guides/other_build_systems
212+
const hostTuple = switch(builtin.target.os.tag) {
213+
.linux => "linux-x86_64",
214+
.windows => "windows-x86_64",
215+
.macos => "darwin-x86_64",
216+
else => {
217+
@panic("unsupported host OS");
218+
}
219+
};
220+
221+
const androidTriple = try target.result.linuxTriple(b.allocator);
222+
const androidNdkPathString: []const u8 = options.android_ndk;
223+
if(androidNdkPathString.len < 1) @panic("no ndk path provided and ANDROID_NDK_HOME is not set");
224+
const androidApiLevel: []const u8 = options.android_api_version;
225+
226+
const androidSysroot = try std.fs.path.join(b.allocator, &.{androidNdkPathString, "/toolchains/llvm/prebuilt/", hostTuple, "/sysroot"});
227+
const androidLibPath = try std.fs.path.join(b.allocator, &.{androidSysroot, "/usr/lib/", androidTriple});
228+
const androidApiSpecificPath = try std.fs.path.join(b.allocator, &.{androidLibPath, androidApiLevel});
229+
const androidIncludePath = try std.fs.path.join(b.allocator, &.{androidSysroot, "/usr/include"});
230+
const androidArchIncludePath = try std.fs.path.join(b.allocator, &.{androidIncludePath, androidTriple});
231+
const androidAsmPath = try std.fs.path.join(b.allocator, &.{androidIncludePath, "/asm-generic"});
232+
const androidGluePath = try std.fs.path.join(b.allocator, &.{androidNdkPathString, "/sources/android/native_app_glue/"});
233+
234+
raylib.addLibraryPath(.{ .cwd_relative = androidLibPath});
235+
raylib.root_module.addLibraryPath(.{ .cwd_relative = androidApiSpecificPath });
236+
raylib.addSystemIncludePath(.{ .cwd_relative = androidIncludePath });
237+
raylib.addSystemIncludePath(.{ .cwd_relative = androidArchIncludePath});
238+
raylib.addSystemIncludePath( .{ .cwd_relative = androidAsmPath});
239+
raylib.addSystemIncludePath(.{ .cwd_relative = androidGluePath});
240+
241+
const libcFile = try std.fs.cwd().createFile("android-libc.txt", .{});
242+
const writer = libcFile.writer();
243+
const libc = std.zig.LibCInstallation{
244+
.include_dir = androidIncludePath,
245+
.sys_include_dir = androidIncludePath,
246+
.crt_dir = androidApiSpecificPath,
247+
};
248+
try libc.render(writer);
249+
libcFile.close();
250+
251+
raylib.setLibCFile(b.path("android-libc.txt"));
252+
253+
if (options.opengl_version == .auto) {
254+
raylib.root_module.linkSystemLibrary("GLESv2", .{});
255+
raylib.root_module.addCMacro("GRAPHICS_API_OPENGL_ES2", "");
256+
}
257+
258+
setDesktopPlatform(raylib, .android);
259+
} else {
195260
try c_source_files.append("src/rglfw.c");
196261

197262
if (options.linux_display_backend == .X11 or options.linux_display_backend == .Both) {
@@ -229,21 +294,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
229294
waylandGenerate(b, raylib, "xdg-activation-v1.xml", "xdg-activation-v1-client-protocol");
230295
waylandGenerate(b, raylib, "idle-inhibit-unstable-v1.xml", "idle-inhibit-unstable-v1-client-protocol");
231296
}
232-
233297
setDesktopPlatform(raylib, options.platform);
234-
} else {
235-
if (options.opengl_version == .auto) {
236-
raylib.linkSystemLibrary("GLESv2");
237-
raylib.root_module.addCMacro("GRAPHICS_API_OPENGL_ES2", "");
238-
}
239-
240-
raylib.linkSystemLibrary("EGL");
241-
raylib.linkSystemLibrary("gbm");
242-
raylib.linkSystemLibrary2("libdrm", .{ .use_pkg_config = .force });
243-
244-
raylib.root_module.addCMacro("PLATFORM_DRM", "");
245-
raylib.root_module.addCMacro("EGL_NO_X11", "");
246-
raylib.root_module.addCMacro("DEFAULT_BATCH_BUFFER_ELEMENT", "");
247298
}
248299
},
249300
.freebsd, .openbsd, .netbsd, .dragonfly => {
@@ -335,6 +386,8 @@ pub const Options = struct {
335386
shared: bool = false,
336387
linux_display_backend: LinuxDisplayBackend = .Both,
337388
opengl_version: OpenglVersion = .auto,
389+
android_ndk: []const u8 = "",
390+
android_api_version: []const u8 = "35",
338391
/// config should be a list of space-separated cflags, eg, "-DSUPPORT_CUSTOM_FRAME_CONTROL"
339392
config: []const u8 = &.{},
340393

@@ -352,6 +405,8 @@ pub const Options = struct {
352405
.linux_display_backend = b.option(LinuxDisplayBackend, "linux_display_backend", "Linux display backend to use") orelse defaults.linux_display_backend,
353406
.opengl_version = b.option(OpenglVersion, "opengl_version", "OpenGL version to use") orelse defaults.opengl_version,
354407
.config = b.option([]const u8, "config", "Compile with custom define macros overriding config.h") orelse &.{},
408+
.android_ndk = b.option([]const u8, "android_ndk", "specify path to android ndk") orelse std.process.getEnvVarOwned(b.allocator, "ANDROID_NDK_HOME") catch "",
409+
.android_api_version = b.option([]const u8, "android_api_version", "specify target android API level") orelse defaults.android_api_version,
355410
};
356411
}
357412
};
@@ -389,6 +444,7 @@ pub const PlatformBackend = enum {
389444
rgfw,
390445
sdl,
391446
drm,
447+
android
392448
};
393449

394450
pub fn build(b: *std.Build) !void {

0 commit comments

Comments
 (0)