gfx-rs / wgpu-native

Native WebGPU implementation based on wgpu-core
Apache License 2.0
852 stars 98 forks source link

Android and iOS build instructions #183

Open kerrhome opened 2 years ago

kerrhome commented 2 years ago

I've looked around and I'm unable to find build instructions for Android or iOS. Can you share them or point me to the right resource please?

shanzez commented 2 years ago

I'd be happy if Android and iOS builds were added to your CD. We'd rather not build it ourselves.

rajveermalviya commented 2 years ago

For android atleast you can use this, and equivalent is used for my workflow.

#!/bin/bash

set -xe

MIN_SDK_VERSION=21
NDK_VERSION=25.0.8775105

# workaround missing libgcc in ndk r23+
mkdir -p tmp-lib
echo "INPUT(-lunwind)" | tee tmp-lib/libgcc.a
export RUSTFLAGS="-L $PWD/tmp-lib"

# common
export LIBCLANG_PATH=$ANDROID_SDK_ROOT/ndk/$NDK_VERSION/toolchains/llvm/prebuilt/linux-x86_64/lib64/libclang.so.13 # depends on NDK_VERSION
export LLVM_CONFIG_PATH=$ANDROID_SDK_ROOT/ndk/$NDK_VERSION/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-config
export BINDGEN_EXTRA_CLANG_ARGS="-isysroot $ANDROID_SDK_ROOT/ndk/$NDK_VERSION/toolchains/llvm/prebuilt/linux-x86_64/sysroot/"

# aarch64
export CLANG_PATH=$ANDROID_SDK_ROOT/ndk/$NDK_VERSION/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android$MIN_SDK_VERSION-clang
export CC=$CLANG_PATH
export CXX=$CLANG_PATH++
export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=$CLANG_PATH
export CARGO_BUILD_TARGET=aarch64-linux-android

rustup target add $CARGO_BUILD_TARGET
cargo build --release

# armv7a
export CLANG_PATH=$ANDROID_SDK_ROOT/ndk/$NDK_VERSION/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi$MIN_SDK_VERSION-clang
export CC=$CLANG_PATH
export CXX=$CLANG_PATH++
export CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER=$CLANG_PATH
export CARGO_BUILD_TARGET=armv7-linux-androideabi

rustup target add $CARGO_BUILD_TARGET
cargo build --release

# x86_64
export CLANG_PATH=$ANDROID_SDK_ROOT/ndk/$NDK_VERSION/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android$MIN_SDK_VERSION-clang
export CC=$CLANG_PATH
export CXX=$CLANG_PATH++
export CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER=$CLANG_PATH
export CARGO_BUILD_TARGET=x86_64-linux-android

rustup target add $CARGO_BUILD_TARGET
cargo build --release

# i686
export CLANG_PATH=$ANDROID_SDK_ROOT/ndk/$NDK_VERSION/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android$MIN_SDK_VERSION-clang
export CC=$CLANG_PATH
export CXX=$CLANG_PATH++
export CARGO_TARGET_I686_LINUX_ANDROID_LINKER=$CLANG_PATH
export CARGO_BUILD_TARGET=i686-linux-android

rustup target add $CARGO_BUILD_TARGET
cargo build --release

after completion .so & .a will be in each ./target/$CARGO_BUILD_TARGET directories

almarklein commented 2 years ago

I'd be happy if Android and iOS builds were added to your CD

A PR is very welcome (I have no affinity with creating build for Android myself). If you create a branch called cd on your fork, the CD will run on each push, and you can iterate rather quickly. When done you can squash the commits and create a PR.

edit: The CD has a job for each target. A bit more replication, but that way each build is self-contained, and easier to manage (by different people, if needed). You can probably take an existing job and adjust it.

There's also an Android build in the CI, BTW: https://github.com/gfx-rs/wgpu-native/blob/0ff888a666e6e787af9bc9c9afc35a5055547b5a/.github/workflows/ci.yml#L159-L175

kerrhome commented 2 years ago

Thank you very much @rajveermalviya and @almarklein. Almar, I saw that. Now please correct me if I'm wrong, but isn't cargo clippy just a lint type execution? I think that was a bit incomplete for actually building (find, of course, for linting). It would need to know where required libs are etc. I think I tried using that (swapping clippy with build) and didn't get it to work.

I will definitely try out your suggestions from the cli and create a PR if I can get the time for CD integration. I think I should be able to.

almarklein commented 2 years ago

Now please correct me if I'm wrong, but isn't cargo clippy just a lint type execution?

Ah, you're right. Looks like it only checks if it's likely to compile for Android, or something? @rajveermalviya's code may be more useful then :)

kerrhome commented 2 years ago

@rajveermalviya I am having a bit of trouble. Here is my env:

CLANG_PATH=/opt/android-sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android28-clang
MIN_SDK_VERSION=28
PWD=/wgpu-native
CXX=/opt/android-sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android28-clang++
LIBCLANG_PATH=/opt/android-sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/linux-x86_64/lib64/libclang.so.12git
CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=/opt/android-sdk/ndk//toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-clang
CARGO_BUILD_TARGET=aarch64-linux-android
HOME=/root
BUILD_TOOLS_VERSION=30.0.3
TERM=xterm
SHLVL=1
ANDROID_SDK_ROOT=/opt/android-sdk
LLVM_CONFIG_PATH=/opt/android-sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-config
BINDGEN_EXTRA_CLANG_ARGS=-isysroot /opt/android-sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/linux-x86_64/sysroot/
NDK_VERSION=23.1.7779620
PATH=/root/.cargo/bin:/opt/android-sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/linux-x86_64/bin:/opt/android-sdk/build-tools/30.0.3:/usr/lib/jvm/java-11-openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
CC=/opt/android-sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android28-clang

Here is my cargo clippy output:

root@b34346ede3df:/wgpu-native# cargo clippy
   Compiling libc v0.2.122
   Compiling proc-macro2 v1.0.37
   Compiling unicode-xid v0.2.2
   Compiling version_check v0.9.4
    Checking cfg-if v1.0.0
   Compiling syn v1.0.91
   Compiling autocfg v1.1.0
   Compiling serde_derive v1.0.136
    Checking bitflags v1.3.2
   Compiling serde v1.0.136
   Compiling memchr v2.4.1
    Checking once_cell v1.10.0
   Compiling glob v0.3.0
   Compiling log v0.4.16
   Compiling parking_lot_core v0.8.5
   Compiling unicode-width v0.1.9
   Compiling regex-syntax v0.6.25
    Checking bit-vec v0.6.3
    Checking scopeguard v1.1.0
error: linker `cc` not found
  |
  = note: No such file or directory (os error 2)

error: could not compile `log` due to previous error
warning: build failed, waiting for other jobs to finish...
error: build failed

It is failing to build too for the linker cc not found reason. If you have guidance, I would appreciate it. Thank you.

rajveermalviya commented 2 years ago

i am guessing a host compiler (gcc or clang) is also needed & it is missing in your environment.

kerrhome commented 2 years ago

i am guessing a host compiler (gcc or clang) is also needed & it is missing in your environment.

Oh man, yes sorry. I thought I was in a docker image with it installed already and it just didn't like it. Thanks. Much better now.

kerrhome commented 2 years ago

I was getting the error you noted @rajveermalviya with https://github.com/rust-lang/rust/pull/85806, but I moved back to NDK 22 and all is well. Really appreciate the help.

Now on to iOS.

kvark commented 2 years ago

We should capture your experience somewhere. If not documentation, than at least a wiki page.

MHDtA-dev commented 3 months ago

Are there any instructions for iOS? I built wgpu-native with cargo build --target aarch64-apple-ios. I got .dylib and .a libraries, then linked my project with libwgpu_native.dylib, marked libwgpu_native.dylib as "Embed & Sign" in General tab. The project was built successfully, but the application crashes with an error:

dyld[41134]: Library not loaded: /Users/user/Libs/wgpu-native/target/aarch64-apple-ios/debug/deps/libwgpu_native.dylib
  Referenced from: <D6E2E5DA-EFEF-364D-AEDA-2CBA8DF12C2A> /private/var/containers/Bundle/Application/41DAE166-0192-473B-AE52-09E52897C996/WGPU Test.app/WGPU Test
  Reason: tried: '/usr/lib/system/introspection/libwgpu_native.dylib' (no such file, not in dyld cache), '/Users/user/Libs/wgpu-native/target/aarch64-apple-ios/debug/deps/libwgpu_native.dylib' (no such file), '/private/preboot/Cryptexes/OS/Users/user/Libs/wgpu-native/target/aarch64-apple-ios/debug/deps/libwgpu_native.dylib' (no such file), '/Users/user/Libs/wgpu-native/target/aarch64-apple-ios/debug/deps/libwgpu_native.dylib' (no such file)

Then I tried to link libwgpu_native.an instead of libwgpu_native.dylib, but for some reason I got the same error(build cleaning didn't help). Then I wanted to run this in simulator. I built library with cargo build --target x86_64-apple-ios, did the same steps I described above and it worked. But on the real device it doesn't want to work.

It would be nice to have some instructions on how to build and link wgpu-native with iOS Xcode project correctly.

Some additional info, if someone can help me:

UPD: I got it worked after I removed "cdylib" from crate-type in Cargo.toml, rebuilt library, linked with libwgpu_native.a, Metal.framework and libobjc.tbd.