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
8 changes: 8 additions & 0 deletions .github/workflows/artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:
- uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.rust_target }}
- name: Compile bindings and target checks
run: zig build --build-file build.tests.zig check -Dtarget=${{ matrix.zig_target }} -Doptimize=ReleaseFast --summary all
- name: Build static and dynamic libraries
run: zig build -Dtarget=${{ matrix.zig_target }} -Doptimize=ReleaseFast -p dist/wgpu-linux-${{ matrix.arch }}
- uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -75,6 +77,8 @@ jobs:
- uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.rust_target }}
- name: Compile bindings and target checks
run: zig build --build-file build.tests.zig check -Dtarget=${{ matrix.zig_target }} -Doptimize=ReleaseFast --summary all
- name: Build static and dynamic libraries
run: zig build -Dtarget=${{ matrix.zig_target }} -Doptimize=ReleaseFast -p dist/${{ matrix.artifact }}
- uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -114,6 +118,8 @@ jobs:
with:
toolchain: ${{ matrix.rust_toolchain }}
targets: ${{ matrix.rust_target }}
- name: Compile bindings and target checks
run: zig build --build-file build.tests.zig check -Dtarget=${{ matrix.zig_target }} -Doptimize=ReleaseFast --summary all
- name: Build static and dynamic libraries
run: zig build -Dtarget=${{ matrix.zig_target }} -Doptimize=ReleaseFast -p dist/${{ matrix.artifact }}
- uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -153,6 +159,8 @@ jobs:
run: |
"$ANDROID_SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" --install "ndk;27.0.12077973"
echo "ANDROID_NDK_HOME=$ANDROID_SDK_ROOT/ndk/27.0.12077973" >> "$GITHUB_ENV"
- name: Compile bindings and target checks
run: zig build --build-file build.tests.zig check -Dtarget=${{ matrix.zig_target }} -Doptimize=ReleaseFast --summary all
- name: Build static and dynamic libraries
run: zig build -Dtarget=${{ matrix.zig_target }} -Doptimize=ReleaseFast -p dist/wgpu-android-${{ matrix.abi }}
- uses: actions/upload-artifact@v4
Expand Down
227 changes: 161 additions & 66 deletions README.md

Large diffs are not rendered by default.

17 changes: 13 additions & 4 deletions build/Library.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ pub const Result = struct {
return self.platform.kind == .ohos;
}

pub fn isAndroid(self: Result) bool {
return self.platform.kind == .android;
}

pub fn isIos(self: Result) bool {
return self.target.result.os.tag == .ios;
}

pub fn linkModule(
self: Result,
b: *std.Build,
mod: *std.Build.Module,
) void {
mod.link_libcpp = true;
if (self.platform.kind != .apple and
self.platform.kind != .android)
{
mod.link_libcpp = true;
}
Platform.configureModule(
b,
self.platform,
Expand All @@ -45,10 +57,8 @@ pub const Result = struct {

pub fn linkTestModule(
self: Result,
b: *std.Build,
mod: *std.Build.Module,
) void {
self.linkModule(b, mod);
Platform.configureTest(
self.platform,
mod,
Expand Down Expand Up @@ -126,7 +136,6 @@ pub fn build(b: *std.Build, options: Options) ?Result {
.wgpu_mod = wgpu_mod,
.wgpu_c_mod = wgpu_c_mod,
};
result.linkModule(b, wgpu_mod);
result.linkModule(b, wgpu_c_mod);
install(b, artifact);
return result;
Expand Down
122 changes: 95 additions & 27 deletions build/platform/android.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,8 @@ pub fn configureSource(
cargo: *std.Build.Step.Run,
config: types.Config,
) void {
const ndk_root = b.graph.environ_map.get("ANDROID_NDK_HOME") orelse
b.graph.environ_map.get("ANDROID_NDK_ROOT") orelse
std.debug.panic(
"Building {s} requires ANDROID_NDK_HOME or ANDROID_NDK_ROOT",
.{config.rust_target},
);
const host_dir = switch (b.graph.host.result.os.tag) {
.linux => "linux-x86_64",
.macos => "darwin-x86_64",
.windows => "windows-x86_64",
else => std.debug.panic("Unsupported Android NDK host", .{}),
};
const toolchain_root = b.pathJoin(&.{
ndk_root,
"toolchains",
"llvm",
"prebuilt",
host_dir,
});
const api_level = b.graph.environ_map.get("ANDROID_API_LEVEL") orelse "21";
const toolchain_root = toolchainRoot(b, config);
const api_level = apiLevel(b, config);
const clang_target = config.clang_target.?;
const linker = b.pathJoin(&.{
toolchain_root,
Expand All @@ -84,16 +66,63 @@ pub fn configureSource(
}

pub fn configureModule(
_: *std.Build,
_: types.Config,
_: *std.Build.Module,
b: *std.Build,
config: types.Config,
mod: *std.Build.Module,
_: std.builtin.LinkMode,
) void {}
) void {
// Zig does not bundle an Android libc. Link the NDK runtime and platform
// stubs directly instead of requesting Zig's libc/libc++ builds.
mod.link_libc = false;
mod.link_libcpp = false;

const target_library_dir: std.Build.LazyPath = .{
.cwd_relative = b.pathJoin(&.{
toolchainRoot(b, config),
"sysroot",
"usr",
"lib",
systemIncludeTarget(config),
}),
};
const platform_library_dir = target_library_dir.path(
b,
apiLevel(b, config),
);
mod.addLibraryPath(target_library_dir);
mod.addLibraryPath(platform_library_dir);

mod.addObjectFile(target_library_dir.path(b, "libc++_static.a"));
mod.addObjectFile(target_library_dir.path(b, "libc++abi.a"));
inline for ([_][]const u8{
"libc.so",
"libm.so",
"libdl.so",
"libandroid.so",
"liblog.so",
}) |library| {
mod.addObjectFile(platform_library_dir.path(b, library));
}
}

pub fn configureTranslateC(
_: types.Config,
_: *std.Build.Step.TranslateC,
) void {}
config: types.Config,
translate_c: *std.Build.Step.TranslateC,
) void {
const b = translate_c.step.owner;
const sysroot = b.pathJoin(&.{ toolchainRoot(b, config), "sysroot" });
translate_c.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{
sysroot,
"usr",
"include",
}) });
translate_c.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{
sysroot,
"usr",
"include",
systemIncludeTarget(config),
}) });
}

pub fn configureCompile(_: types.Config, _: *std.Build.Step.Compile) void {}

Expand All @@ -102,3 +131,42 @@ pub fn configureTest(
_: *std.Build.Module,
_: std.builtin.LinkMode,
) void {}

fn toolchainRoot(b: *std.Build, config: types.Config) []const u8 {
const ndk_root = b.graph.environ_map.get("ANDROID_NDK_HOME") orelse
b.graph.environ_map.get("ANDROID_NDK_ROOT") orelse
std.debug.panic(
"Building {s} requires ANDROID_NDK_HOME or ANDROID_NDK_ROOT",
.{config.rust_target},
);
const host_dir = switch (b.graph.host.result.os.tag) {
.linux => "linux-x86_64",
.macos => "darwin-x86_64",
.windows => "windows-x86_64",
else => std.debug.panic("Unsupported Android NDK host", .{}),
};
return b.pathJoin(&.{
ndk_root,
"toolchains",
"llvm",
"prebuilt",
host_dir,
});
}

fn apiLevel(b: *std.Build, config: types.Config) []const u8 {
return b.graph.environ_map.get("ANDROID_API_LEVEL") orelse b.fmt(
"{d}",
.{config.target.result.os.version_range.linux.android},
);
}

fn systemIncludeTarget(config: types.Config) []const u8 {
return switch (config.target.result.cpu.arch) {
.aarch64 => "aarch64-linux-android",
.arm => "arm-linux-androideabi",
.x86 => "i686-linux-android",
.x86_64 => "x86_64-linux-android",
else => unreachable,
};
}
36 changes: 31 additions & 5 deletions build/platform/apple.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,23 @@ pub fn configureModule(
) void {
// Explicit Apple targets do not inherit native SDK search paths.
if (config.apple_sdk_root) |sdk_root| {
mod.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{
// linkSystemLibrary("c++") is normalized to Zig's bundled libc++.
// Apple targets must instead link the SDK-provided text stub directly.
mod.link_libcpp = false;
mod.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{
sdk_root,
"usr",
"lib",
"include",
}) });
const sdk_library_dir: std.Build.LazyPath = .{
.cwd_relative = b.pathJoin(&.{
sdk_root,
"usr",
"lib",
}),
};
mod.addLibraryPath(sdk_library_dir);
mod.addObjectFile(sdk_library_dir.path(b, "libc++.tbd"));
mod.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{
sdk_root,
"System",
Expand All @@ -94,9 +106,23 @@ pub fn configureModule(
}

pub fn configureTranslateC(
_: types.Config,
_: *std.Build.Step.TranslateC,
) void {}
config: types.Config,
translate_c: *std.Build.Step.TranslateC,
) void {
const sdk_root = config.apple_sdk_root orelse return;
const b = translate_c.step.owner;
translate_c.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{
sdk_root,
"usr",
"include",
}) });
translate_c.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{
sdk_root,
"System",
"Library",
"Frameworks",
}) });
}

pub fn configureCompile(_: types.Config, _: *std.Build.Step.Compile) void {}

Expand Down
12 changes: 11 additions & 1 deletion examples/bmp.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
const std = @import("std");

pub fn write24BitBMP(io: std.Io, file_name: []const u8, comptime width: u32, comptime height: u32, bgra_data: *[width * height * 4]u8) !void {
pub fn write24BitBMP(
io: std.Io,
file_name: []const u8,
comptime width: u32,
comptime height: u32,
bgra_data: []const u8,
) !void {
if (bgra_data.len != width * height * 4) {
return error.InvalidImageDataLength;
}

const file = try std.Io.Dir.cwd().createFile(io, file_name, .{});
defer file.close(io);

Expand Down
Loading
Loading