Skip to content

Commit 3ef024e

Browse files
add system include paths and add include paths to c-translated modules (#26)
add system include paths and add include paths to c-translated modules
1 parent 00a64c2 commit 3ef024e

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,7 @@ pub fn build(b: *std.Build) !void {
170170
b.installArtifact(lib);
171171

172172
var sdl_c_module = b.addTranslateC(.{
173-
// NOTE(jae): 2025-03-13
174-
// Using host target platform to avoid include errors when targetting Android
175-
// Otherwise we get errors like: 'sys/types.h' file not found
176-
.target = b.graph.host,
173+
.target = target,
177174
.optimize = .ReleaseFast,
178175
.root_source_file = b.path("src/sdl.h"),
179176
});

src/androidbuild/apk.zig

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,29 @@ fn doInstallApk(apk: *Apk) std.mem.Allocator.Error!*Step.InstallFile {
412412
}
413413
}
414414

415+
// Find TranslateC dependencies and add system path
416+
var iter = artifact.root_module.import_table.iterator();
417+
while (iter.next()) |it| {
418+
const module = it.value_ptr.*;
419+
const root_source_file = module.root_source_file orelse continue;
420+
switch (root_source_file) {
421+
.generated => |gen| {
422+
const step = gen.file.step;
423+
if (step.id != .translate_c) {
424+
continue;
425+
}
426+
const c_translate_target = module.resolved_target orelse continue;
427+
if (!c_translate_target.result.abi.isAndroid()) {
428+
continue;
429+
}
430+
const translate_c: *std.Build.Step.TranslateC = @fieldParentPtr("step", step);
431+
translate_c.addIncludePath(.{ .cwd_relative = apk.tools.include_path });
432+
translate_c.addSystemIncludePath(.{ .cwd_relative = apk.tools.getSystemIncludePath(c_translate_target) });
433+
},
434+
else => continue,
435+
}
436+
}
437+
415438
// update linked libraries that use C or C++ to:
416439
// - use Android LibC file
417440
// - add Android NDK library paths. (libandroid, liblog, etc)

src/androidbuild/tools.zig

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ api_level: APILevel,
2828
ndk_version: []const u8,
2929
/// ie. "$ANDROID_HOME/ndk/{ndk_version}/toolchains/llvm/prebuilt/{host_os_and_arch}/sysroot"
3030
ndk_sysroot_path: []const u8,
31+
/// ie. "$ANDROID_HOME/ndk/{ndk_version}/toolchains/llvm/prebuilt/{host_os_and_arch}/sysroot/usr/include"
32+
include_path: []const u8,
3133
/// ie. "$ANDROID_HOME/Sdk/platforms/android-{api_level}/android.jar"
3234
root_jar: []const u8,
3335
// $JDK_HOME, $JAVA_HOME or auto-discovered from java binaries found in $PATH
@@ -320,6 +322,7 @@ pub fn create(b: *std.Build, options: Options) *Tools {
320322
.api_level = options.api_level,
321323
.ndk_version = options.ndk_version,
322324
.ndk_sysroot_path = android_ndk_sysroot,
325+
.include_path = b.fmt("{s}/usr/include", .{tools.ndk_sysroot_path}),
323326
.root_jar = root_jar,
324327
.jdk_path = jdk_path,
325328
.build_tools = .{
@@ -439,9 +442,7 @@ pub fn createKeyStore(tools: *const Tools, options: CreateKey) KeyStore {
439442
pub fn setLibCFile(tools: *const Tools, compile: *Step.Compile) void {
440443
const b = tools.b;
441444

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

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

459+
pub fn getSystemIncludePath(tools: *const Tools, target: ResolvedTarget) []const u8 {
460+
const b = tools.b;
461+
const system_target = getAndroidTriple(target) catch |err| @panic(@errorName(err));
462+
return b.fmt("{s}/{s}", .{ tools.include_path, system_target });
463+
}
464+
458465
fn createLibC(b: *std.Build, system_target: []const u8, android_version: APILevel, ndk_sysroot_path: []const u8, ndk_version: []const u8) LazyPath {
459466
const libc_file_format =
460467
\\# Generated by zig-android-sdk. DO NOT EDIT.

0 commit comments

Comments
 (0)