Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions examples/sdl2/third-party/sdl2/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,7 @@ pub fn build(b: *std.Build) !void {
b.installArtifact(lib);

var sdl_c_module = b.addTranslateC(.{
// NOTE(jae): 2025-03-13
// Using host target platform to avoid include errors when targetting Android
// Otherwise we get errors like: 'sys/types.h' file not found
.target = b.graph.host,
.target = target,
.optimize = .ReleaseFast,
.root_source_file = b.path("src/sdl.h"),
});
Expand Down
23 changes: 23 additions & 0 deletions src/androidbuild/apk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,29 @@ fn doInstallApk(apk: *Apk) std.mem.Allocator.Error!*Step.InstallFile {
}
}

// Find TranslateC dependencies and add system path
var iter = artifact.root_module.import_table.iterator();
while (iter.next()) |it| {
const module = it.value_ptr.*;
const root_source_file = module.root_source_file orelse continue;
switch (root_source_file) {
.generated => |gen| {
const step = gen.file.step;
if (step.id != .translate_c) {
continue;
}
const c_translate_target = module.resolved_target orelse continue;
if (!c_translate_target.result.abi.isAndroid()) {
continue;
}
const translate_c: *std.Build.Step.TranslateC = @fieldParentPtr("step", step);
translate_c.addIncludePath(.{ .cwd_relative = apk.tools.include_path });
translate_c.addSystemIncludePath(.{ .cwd_relative = apk.tools.getSystemIncludePath(c_translate_target) });
},
else => continue,
}
}

// update linked libraries that use C or C++ to:
// - use Android LibC file
// - add Android NDK library paths. (libandroid, liblog, etc)
Expand Down
13 changes: 10 additions & 3 deletions src/androidbuild/tools.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ api_level: APILevel,
ndk_version: []const u8,
/// ie. "$ANDROID_HOME/ndk/{ndk_version}/toolchains/llvm/prebuilt/{host_os_and_arch}/sysroot"
ndk_sysroot_path: []const u8,
/// ie. "$ANDROID_HOME/ndk/{ndk_version}/toolchains/llvm/prebuilt/{host_os_and_arch}/sysroot/usr/include"
include_path: []const u8,
/// ie. "$ANDROID_HOME/Sdk/platforms/android-{api_level}/android.jar"
root_jar: []const u8,
// $JDK_HOME, $JAVA_HOME or auto-discovered from java binaries found in $PATH
Expand Down Expand Up @@ -320,6 +322,7 @@ pub fn create(b: *std.Build, options: Options) *Tools {
.api_level = options.api_level,
.ndk_version = options.ndk_version,
.ndk_sysroot_path = android_ndk_sysroot,
.include_path = b.fmt("{s}/usr/include", .{tools.ndk_sysroot_path}),
.root_jar = root_jar,
.jdk_path = jdk_path,
.build_tools = .{
Expand Down Expand Up @@ -439,9 +442,7 @@ pub fn createKeyStore(tools: *const Tools, options: CreateKey) KeyStore {
pub fn setLibCFile(tools: *const Tools, compile: *Step.Compile) void {
const b = tools.b;

const target: ResolvedTarget = compile.root_module.resolved_target orelse {
@panic(b.fmt("no 'target' set on Android module", .{}));
};
const target: ResolvedTarget = compile.root_module.resolved_target orelse @panic("no 'target' set on Android module");
const system_target = getAndroidTriple(target) catch |err| @panic(@errorName(err));

const android_libc_path = createLibC(
Expand All @@ -455,6 +456,12 @@ pub fn setLibCFile(tools: *const Tools, compile: *Step.Compile) void {
compile.setLibCFile(android_libc_path);
}

pub fn getSystemIncludePath(tools: *const Tools, target: ResolvedTarget) []const u8 {
const b = tools.b;
const system_target = getAndroidTriple(target) catch |err| @panic(@errorName(err));
return b.fmt("{s}/{s}", .{ tools.include_path, system_target });
}

fn createLibC(b: *std.Build, system_target: []const u8, android_version: APILevel, ndk_sysroot_path: []const u8, ndk_version: []const u8) LazyPath {
const libc_file_format =
\\# Generated by zig-android-sdk. DO NOT EDIT.
Expand Down