Skip to content

Commit 941fc65

Browse files
improve C++ include support so that openxr_loader will build for Android with Zig (#55)
improve C++ header include support so that openxr_loader will at least build for Android with Zig
1 parent abeec4e commit 941fc65

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,18 @@ pub fn build(b: *std.Build) !void {
4848

4949
## Installation
5050

51-
Add the following to your build.zig.zon file and run `zig build`.
51+
Option A. Install with package manager
52+
```sh
53+
zig fetch --save https://github.com/silbinarywolf/zig-android-sdk/archive/REPLACE_WITH_WANTED_COMMIT.tar.gz"
54+
```
5255
56+
Option B. Copy-paste the dependency into your project directly and put in a `third-party` folder. This is recommended if you want to easily hack on it or tweak it.
5357
```zig
5458
.{
59+
.name = .yourzigproject,
5560
.dependencies = .{
5661
.android = .{
57-
.path = "https://github.com/silbinarywolf/zig-android-sdk/archive/REPLACE_WITH_WANTED_COMMIT.tar.gz",
58-
// .hash = REPLACE_WITH_HASH_FROM_BUILD_ERROR
62+
.path = "third-party/zig-android-sdk",
5963
},
6064
},
6165
}

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.{
22
.name = .android,
3-
.version = "0.1.0",
3+
.version = "0.2.0",
44
.dependencies = .{},
55
.paths = .{
66
"build.zig",

src/androidbuild/apk.zig

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const builtin = @import("builtin");
23
const androidbuild = @import("androidbuild.zig");
34
const Sdk = @import("tools.zig");
45
const BuiltinOptionsUpdate = @import("builtin_options_update.zig");
@@ -394,7 +395,11 @@ fn doInstallApk(apk: *Apk) std.mem.Allocator.Error!*Step.InstallFile {
394395
});
395396
aapt2packagename.setName(runNameContext("aapt2 dump packagename"));
396397
aapt2packagename.addFileArg(resources_apk);
397-
break :blk aapt2packagename.captureStdOut();
398+
const aapt2_package_name_file = if (builtin.zig_version.major == 0 and builtin.zig_version.minor <= 15)
399+
aapt2packagename.captureStdOut()
400+
else
401+
aapt2packagename.captureStdOut(.{});
402+
break :blk aapt2_package_name_file;
398403
};
399404

400405
const android_builtin = blk: {
@@ -770,7 +775,7 @@ fn updateLinkObjects(apk: *Apk, root_artifact: *Step.Compile, so_dir: []const u8
770775
// Update libraries linked to this library
771776
apk.updateLinkObjects(artifact, so_dir, raw_top_level_apk_files);
772777

773-
// Apply workaround for Zig 0.14.0
778+
// Apply workaround for Zig 0.14.0 and Zig 0.15.X
774779
apk.applyLibLinkCppWorkaroundIssue19(artifact);
775780
},
776781
else => continue,
@@ -806,6 +811,11 @@ fn applyLibLinkCppWorkaroundIssue19(apk: *Apk, artifact: *Step.Compile) void {
806811

807812
artifact.root_module.addLibraryPath(libcppabi_dir);
808813

814+
// NOTE(jae): 2025-11-18
815+
// Due to Android include files not being provided by Zig, we should provide them if the library is linking against C++
816+
// This resolves an issue where if you are trying to build the openxr_loader C++ code from source, it can't find standard library includes like <string> or <algorithm>
817+
artifact.addIncludePath(.{ .cwd_relative = b.fmt("{s}/usr/include/c++/v1", .{apk.ndk.sysroot_path}) });
818+
809819
if (artifact.root_module.link_libcpp == true) {
810820
// NOTE(jae): 2025-04-06
811821
// Don't explicitly linkLibCpp

src/androidbuild/tools.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,10 @@ pub fn createKeyStore(sdk: *const Sdk, options: CreateKey) KeyStore {
313313
// ignore stderr, it just gives you an output like:
314314
// "Generating 4,096 bit RSA key pair and self-signed certificate (SHA384withRSA) with a validity of 10,000 days
315315
// for: CN=example.com, OU=ID, O=Example, L=Doe, ST=Jane, C=GB"
316-
_ = keytool.captureStdErr();
316+
_ = if (builtin.zig_version.major == 0 and builtin.zig_version.minor <= 15)
317+
keytool.captureStdErr()
318+
else
319+
keytool.captureStdErr(.{});
317320
return .{
318321
.file = keystore_file,
319322
.password = options.password,

0 commit comments

Comments
 (0)