Skip to content

Commit

Permalink
Port build script to Rust [WIP]
Browse files Browse the repository at this point in the history
This will allow the code to be shared between Unix and Windows builds.
  • Loading branch information
dae committed Jun 22, 2023
1 parent 8081b20 commit ff5c374
Show file tree
Hide file tree
Showing 17 changed files with 290 additions and 234 deletions.
4 changes: 2 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
target-dir = "target"

[env]
STRINGS_JSON = { value = "anki/out/strings.json", relative = true }
DESCRIPTORS_BIN = { value = "anki/out/descriptors.bin", relative = true }
DESCRIPTORS_BIN = { value = "anki/out/rslib/proto/descriptors.bin", relative = true }
STRINGS_JSON_ANKIDROID = { value = "anki/out/strings.json", relative = true }
PROTOC = { value = "anki/out/extracted/protoc/bin/protoc", relative = true }
GENERATED_BACKEND_DIR = { value = "rsdroid/build/generated/source/backend", relative = true }
28 changes: 5 additions & 23 deletions .github/workflows/build-all.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build AAR and Robo (all platforms)
name: Build release (from macOS)
on:
workflow_dispatch:
inputs:
Expand Down Expand Up @@ -44,9 +44,6 @@ jobs:
distribution: "adopt"
java-version: "11" # minimum for Android API31

- name: Install Android Command Line Tools
uses: android-actions/setup-android@v2

- name: Install Windows cross compiler
run: brew install mingw-w64 && x86_64-w64-mingw32-gcc -v

Expand All @@ -56,8 +53,6 @@ jobs:
brew install x86_64-unknown-linux-gnu
x86_64-unknown-linux-gnu-gcc -v
- name: Install NDK
run: .github/scripts/install_ndk.sh 25.2.9519653
- name: Rust Cache
uses: actions/cache@v3
Expand All @@ -66,28 +61,15 @@ jobs:
~/.cargo/registry
~/.cargo/git
target
anki/out/rust
anki/out/extracted
key: ${{ runner.os }}-rust-release-v5-${{ hashFiles('Cargo.lock') }}
restore-keys: |
${{ runner.os }}-rust-release-v5
${{ runner.os }}-rust-release
- name: Anki cache
uses: actions/cache@v3
with:
path: |
anki/out/node_modules
anki/out/rust
anki/out/extracted
key: ${{ runner.os }}-anki-v5-${{ hashFiles('Cargo.lock', 'anki/yarn.lock') }}
restore-keys: |
${{ runner.os }}-anki-v5
${{ runner.os }}-anki
- name: Build Android/All
run: ./build-aar.sh

- name: Build Robolectric/All
run: ./build-robo.sh
- name: Build all
run: ./build.sh

- name: Check Compiled Libraries
run: >
Expand Down
58 changes: 4 additions & 54 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build AAR and Robo (single platform)
name: Build quick (each platform)
on:
workflow_dispatch:
push:
Expand Down Expand Up @@ -42,64 +42,22 @@ jobs:
distribution: "adopt"
java-version: "11" # minimum for Android API31

- name: Install Android Command Line Tools
uses: android-actions/setup-android@v2

- name: Install NDK
if: matrix.os != 'windows-latest'
run: .github/scripts/install_ndk.sh 25.2.9519653

- name: Install NDK (Windows)
if: matrix.os == 'windows-latest'
run: |
Write-Host "NDK Install Started"
(. sdkmanager.bat --install "ndk;25.2.9519653" --sdk_root="$Env:ANDROID_SDK_ROOT") | out-null
Write-Host "NDK Install Completed"
- name: Rust Cache
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
target
anki/out/rust
anki/out/extracted
key: ${{ runner.os }}-rust-debug-v5-${{ hashFiles('Cargo.lock') }}
restore-keys: |
${{ runner.os }}-rust-debug-v5
${{ runner.os }}-rust-debug
- name: Anki cache (Unix)
uses: actions/cache@v3
if: matrix.os != 'windows-latest'
with:
path: |
anki/out/node_modules
anki/out/rust
anki/out/extracted
key: ${{ runner.os }}-anki-v6-${{ hashFiles('Cargo.lock', 'anki/yarn.lock') }}
restore-keys: |
${{ runner.os }}-anki-v6
${{ runner.os }}-anki
- name: Anki cache (Windows)
uses: actions/cache@v3
if: matrix.os == 'windows-latest'
with:
path: |
anki/out/rust
anki/out/extracted
key: ${{ runner.os }}-anki-v6-${{ hashFiles('Cargo.lock', 'anki/yarn.lock') }}
restore-keys: |
${{ runner.os }}-anki-v6
${{ runner.os }}-anki
- name: Build Android/X86_64
if: matrix.os != 'windows-latest'
run: ./build-aar.sh

- name: Build Android/X86_64 (Win)
if: matrix.os == 'windows-latest'
run: ./build-aar.bat
run: cargo run -p build_rust

- name: Check Rust (Linux)
if: matrix.os == 'ubuntu-latest'
Expand All @@ -116,14 +74,6 @@ jobs:
profile: Nexus 6
script: ./check-droid.sh

- name: Build Robolectric/X86_64
if: matrix.os != 'windows-latest'
run: ./build-robo.sh

- name: Build Robolectric/X86_64 (Win)
if: matrix.os == 'windows-latest'
run: ./build-robo.bat

- name: Upload rsdroid AAR as artifact
uses: actions/upload-artifact@v2
with:
Expand Down
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
members = [
"rslib-bridge",
"anki/rslib",
"build_rust"
]
exclude = []
resolver = "2"
Expand Down
22 changes: 5 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ eg on Linux:

```
export ANDROID_SDK_ROOT=$HOME/Android/Sdk
export ANDROID_NDK_HOME=$HOME/Android/Sdk/ndk/25.2.9519653
```

Or macOS:

```
export ANDROID_SDK_ROOT=$HOME/Library/Android/sdk
export ANDROID_NDK_HOME=$HOME/Library/Android/sdk/ndk/25.2.9519653
```

If you don't have Java installed, you may be able to use the version bundled
Expand All @@ -109,24 +111,10 @@ or Windows:
set JAVA_HOME=C:\Program Files\Android\Android Studio\jre
```

Now build the .aar:
Now build with `./build.sh` or `build.bat`.

```
./build-aar.sh
```

If you have 'python3' on your system but not 'python', you can specify
the name:

```
RUST_ANDROID_GRADLE_PYTHON_COMMAND=python3 ./build-aar.sh
```

Assuming success, then build the .jar file:

```
./build-robo.sh
```
After you've confirmed building works, you may want to build again with the env
var RELEASE=1 defined, to build a faster version.

## Modify AnkiDroid to use built library

Expand Down
2 changes: 1 addition & 1 deletion anki
3 changes: 0 additions & 3 deletions build-aar.bat

This file was deleted.

6 changes: 0 additions & 6 deletions build-robo.bat

This file was deleted.

1 change: 1 addition & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cargo run -p build_rust
122 changes: 2 additions & 120 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,121 +1,3 @@
#!/bin/bash
#
# Builds:
# - desktop web components
# - Android JNI library and auto-generated Kotlin interface
# - Robolectric JNI library.
#
# The first two will be bundled into an .aar by gradle, and the last
# is bundled into a .jar by gradle. Gradle will be automatically invoked
# if this script is run from the command line.
#
# By default, only the library for the current architecture is built
# (eg X86_64, or Arm64 on Apple M1+).
#
# Define ALL_ARCHS=1 to build a multi-platform bundle, which takes about 4x
# longer to download and build.
#
# Define RELEASE=1 to compile the Rust in release mode, which builds slower
# but runs faster.
#

set -e

if [ "$RUNNING_FROM_BUILD_SCRIPT" = "1" ]; then
# this script invoked gradle which invoked this script; nothing left to do
exit 0;
fi

# Android code built into target/, so we don't clobber robolectric cache
export CARGO_TARGET_DIR=target
ARTIFACTS_DIR=rsdroid/build/generated/anki_artifacts
JNI_DIR=rsdroid/build/generated/jniLibs

echo "*** Building desktop web components"
mkdir -p $ARTIFACTS_DIR/web
(cd anki && ./ninja extract:protoc ts:reviewer:reviewer_extras_bundle.js qt:aqt:data:web:pages)
cp anki/out/qt/_aqt/data/web/pages/* anki/out/ts/reviewer/reviewer_extras_bundle.js $ARTIFACTS_DIR/web/
cp anki/cargo/licenses.json $ARTIFACTS_DIR/web/licenses-cargo.json
cp anki/ts/licenses.json $ARTIFACTS_DIR/web/licenses-ts.json
chmod -R a+w $ARTIFACTS_DIR

# determine android target archs
if [ "$ALL_ARCHS" = "1" ]; then
rustup target add armv7-linux-androideabi # arm
rustup target add i686-linux-android # x86
rustup target add aarch64-linux-android # arm64
rustup target add x86_64-linux-android # x86_64
targets="-t armv7 -t i686 -t aarch64 -t x86_64"
else
if [[ "$OSTYPE" == "darwin"* && $(arch) == "arm64" ]]; then
rustup target add aarch64-linux-android
targets="-t aarch64"
else
rustup target add x86_64-linux-android
targets="-t x86_64"
fi
fi

if [ "$RELEASE" = "1" ]; then
release="--release"
release_dir="release"
else
release=""
release_dir="debug"
fi

echo
echo "*** Building Android JNI library + backend interface"
rm -rf $JNI_DIR
cargo install cargo-ndk@3.2.0
cargo ndk -o $JNI_DIR $targets build -p rsdroid $release

echo
echo "*** Building Robolectric JNI library"
# Robolectric build cache can be shared with desktop, as it's the same arch
export CARGO_TARGET_DIR=anki/out/rust
if [ "$ALL_ARCHS" = "1" ]; then
if [[ "$OSTYPE" != "darwin"* ]]; then
echo "Must be on macOS to do a multi-arch build."
exit 1
fi

# Mac
for target in \
x86_64-apple-darwin \
aarch64-apple-darwin
do
rustup target add $target
cargo build -p rsdroid $release --target $target
done
lipo -create target/x86_64-apple-darwin/$release_dir/librsdroid.dylib \
target/aarch64-apple-darwin/$release_dir/librsdroid.dylib \
-output rsdroid-testing/assets/librsdroid.dylib

# Linux
target=x86_64-unknown-linux-gnu
rustup target add $target
CC=x86_64-unknown-linux-gnu-gcc \
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-unknown-linux-gnu-gcc \
cargo build -p rsdroid $release --target $target
cp -v target/$target/$release_dir/librsdroid.so rsdroid-testing/assets


# Windows
target=x86_64-pc-windows-gnu
rustup target add $target
cargo build -p rsdroid $release --target $target
cp -v target/$target/$release_dir/rsdroid.dll rsdroid-testing/assets
else
# Just build for current architecture
cargo build -p rsdroid $release
for file in target/$release_dir/librsdroid.{dylib,so}; do
test -f $file && cp -v $file rsdroid-testing/assets/
done
fi

export RUNNING_FROM_BUILD_SCRIPT=1
if [ "$RUNNING_FROM_GRADLE" != "1" ]; then
# run gradle if invoked from command line
./gradlew assembleRelease rsdroid-testing:build
fi
# Just a shortcut
cargo run -p build_rust
Loading

0 comments on commit ff5c374

Please sign in to comment.