Skip to content

Commit

Permalink
android support for 64bit architecture
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Sep 15, 2020
1 parent 9bedd96 commit 2ea029b
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.aarch64-linux-android]
linker = "./third_party/android_ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang"

[target.x86_64-linux-android]
linker = "./third_party/android_ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android21-clang"
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!build
122 changes: 88 additions & 34 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,66 @@ jobs:
- os: macOS-latest
target: x86_64-apple-darwin
variant: debug
cargo: cargo

- os: macOS-latest
target: x86_64-apple-darwin
variant: release
cargo: cargo

- os: ubuntu-latest-xl
target: x86_64-unknown-linux-gnu
variant: debug
cargo: cargo

- os: ubuntu-latest-xl
target: x86_64-unknown-linux-gnu
variant: release
cargo: cargo

- os: windows-2019
target: x86_64-pc-windows-msvc
variant: release # Note: we do not support windows debug builds.
cargo: cargo

- os: ubuntu-latest-xl
target: aarch64-unknown-linux-gnu
variant: debug
cargo: cargo

- os: ubuntu-latest-xl
target: aarch64-unknown-linux-gnu
variant: release
cargo: cargo

- os: ubuntu-latest-xl
target: aarch64-linux-android
variant: debug
cargo: $HOME/.cargo/bin/cross

- os: ubuntu-latest-xl
target: aarch64-linux-android
variant: release
cargo: $HOME/.cargo/bin/cross

- os: ubuntu-latest-xl
target: x86_64-linux-android
variant: debug
cargo: $HOME/.cargo/bin/cross

- os: ubuntu-latest-xl
target: x86_64-linux-android
variant: release
cargo: $HOME/.cargo/bin/cross

env:
V8_FROM_SOURCE: true
CARGO_VARIANT_FLAG: ${{ matrix.config.variant == 'release' && '--release' || '' }}
LIB_NAME: ${{ contains(matrix.config.target, 'windows') && 'rusty_v8' || 'librusty_v8' }}
LIB_EXT: ${{ contains(matrix.config.target, 'windows') && 'lib' || 'a' }}
SCCACHE_DIR: ${{ github.workspace }}/target/sccache
SCCACHE_CACHE_SIZE: 128M
SCCACHE_IDLE_TIMEOUT: 0

steps:
- name: Configure git
Expand All @@ -71,6 +101,9 @@ jobs:
components: clippy, rustfmt
rust-version: 1.46.0

- name: Install rust tools
run: rustup component add clippy rustfmt

- name: Install python
uses: actions/setup-python@v1
with:
Expand Down Expand Up @@ -108,38 +141,63 @@ jobs:
target/*/.*
target/*/build
target/*/deps
key:
${{ matrix.config.target }}-${{ matrix.config.variant }}-${{
hashFiles('./Cargo.lock', './v8/include/v8-version.h') }}
target/*/docker
key: ${{ matrix.config.target }}-${{ matrix.config.variant }}-${{
hashFiles('./Cargo.lock', './v8/include/v8-version.h') }}
restore-keys:
${{ matrix.config.target }}-${{ matrix.config.variant }}-


# It seems that the 'target' directory does not always get restored
# from cache correctly on MacOS. In the build log we see the following:
# It seems that the 'target' directory does not always get restored
# from cache correctly on MacOS. In the build log we see the following:
#
# Fresh serde_derive v1.0.115
#
# But a little while after that Cargo aborts because 'serde_derive' is
# now nowhere to be found. We're not the only ones experiencing this,
# see https://github.com/actions-rs/cargo/issues/111.
#
# error[E0463]: can't find crate for `serde_derive`
# ##[error] --> /Users/runner/.cargo/registry/src/git.luolix.top-
# | 1ecc6299db9ec823/serde-1.0.115/src/lib.rs:285:1
# |
# 285 | extern crate serde_derive;
# | ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
- name: Work around MacOS + Cargo + Github Actions cache bug
if: runner.os == 'macOS'
run: cargo clean -p serde_derive
# But a little while after that Cargo aborts because 'serde_derive' is
# now nowhere to be found. We're not the only ones experiencing this,
# see https://github.com/actions-rs/cargo/issues/111.
#
# error[E0463]: can't find crate for `serde_derive`
# ##[error] --> /Users/runner/.cargo/registry/src/git.luolix.top-
# | 1ecc6299db9ec823/serde-1.0.115/src/lib.rs:285:1
# |
# 285 | extern crate serde_derive;
# | ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
- name: Work around MacOS + Cargo + Github Actions cache bug
if: runner.os == 'macOS'
run: cargo clean -p serde_derive

- name: Install rust-embedded/cross
# see https://github.com/rust-embedded/cross
if: contains(matrix.config.target, 'android')
run: |
cargo install cross
# echo 'alias cargo=cross' >> $HOME/.bash_profile
- name: Build android docker images
if: contains(matrix.config.target, 'android')
run: |
sudo curl -sSL https://get.docker.com/ | sh
cache_path=target/${{ matrix.config.target }}/docker
image_name=denoland/rusty_v8:${{ matrix.config.target }}
image_path=$cache_path/image.tar.gz
shasum_path=$cache_path/Dockerfile.sha1sum
shasum=$([ ! -f "$shasum_path" ] || cat $shasum_path)
[ -d "$cache_path" ] || mkdir -p $cache_path
# rebuild image if doesn't exists or dockerfile changed
if [ ! -f "$image_path" ] || [ "$shasum" != "$(sha1sum Dockerfile)" ]
then
docker build --target ${{ matrix.config.target }} --tag $image_name .
docker save $image_name | gzip > $image_path
echo $shasum > $shasum_path
else
sha1sum Dockerfile > $shasum_path
docker load -i $image_path
fi
- name: Install and start sccache
shell: pwsh
env:
SCCACHE_DIR: ${{ github.workspace }}/target/sccache
SCCACHE_CACHE_SIZE: 128M
SCCACHE_IDLE_TIMEOUT: 0
if: ${{ !contains(matrix.config.target, 'android') }}
run: |
$version = "0.2.12"
$platform =
Expand All @@ -159,17 +217,15 @@ jobs:
echo "::add-path::$(pwd)/$basename"
- name: Test
run:
cargo test -vv --all-targets --locked ${{ env.CARGO_VARIANT_FLAG }}
run: ${{ matrix.config.cargo }} test -vv --all-targets --locked ${{ env.CARGO_VARIANT_FLAG }}
--target ${{ matrix.config.target }}

- name: Clippy
run:
cargo clippy --all-targets --locked ${{ env.CARGO_VARIANT_FLAG }}
run: ${{ matrix.config.cargo }} clippy --all-targets --locked ${{ env.CARGO_VARIANT_FLAG }}
--target ${{ matrix.config.target }} -- -D clippy::all

- name: Rustfmt
run: cargo fmt -- --check
run: ${{ matrix.config.cargo }} fmt -- --check

- name: Prepare binary publish
run: cp
Expand All @@ -178,9 +234,7 @@ jobs:

- name: Binary publish
uses: softprops/action-gh-release@v1
if: >-
github.repository == 'denoland/rusty_v8' &&
startsWith(github.ref, 'refs/tags/')
if: github.repository == 'denoland/rusty_v8' && startsWith(github.ref, 'refs/tags/')
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
Expand Down Expand Up @@ -208,5 +262,5 @@ jobs:
run: cargo publish -vv

- name: Stop sccache
if: always()
if: ${{ !contains(matrix.config.target, 'android') && always() }}
run: sccache --stop-server
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
/.vscode/
/.idea/
/target/

third_party/android_ndk
third_party/android_platform
third_party/catapult
16 changes: 16 additions & 0 deletions Cross.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[build.env]
passthrough = [
"RUST_LOG",
"RUST_BACKTRACE",
"V8_FROM_SOURCE",
"CLANG_BASE_PATH",
"GN_ARGS",
"SCCACHE_CACHE_SIZE",
"SCCACHE_IDLE_TIMEOUT"
]

[target.aarch64-linux-android]
image = "denoland/rusty_v8:aarch64-linux-android"

[target.x86_64-linux-android]
image = "denoland/rusty_v8:x86_64-linux-android"
53 changes: 53 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# sccache
FROM ubuntu:16.04 AS sccache

ENV TZ=Etc/UTC
RUN \
DEBIAN_FRONTEND=noninteractive \
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
&& apt-get update && apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/*

ARG sccache_version="0.2.12"
ARG sccache_platform="x86_64-unknown-linux-musl"
ARG sccache_basename="sccache-$sccache_version-$sccache_platform"
ARG sccache_url="https://github.com/mozilla/sccache/releases/download/$sccache_version/$sccache_basename.tar.gz"
RUN \
cd / \
&& echo $sccache_url \
&& curl -LO "$sccache_url" \
&& tar -xzvf "$sccache_basename.tar.gz" \
&& mv $sccache_basename/sccache /usr/local/bin/sccache \
&& rm -rf $sccache_basename

# aarch64-linux-android
FROM rustembedded/cross:aarch64-linux-android AS aarch64-linux-android

ENV TZ=Etc/UTC
COPY ./build/*.sh /chromium_build/
RUN \
DEBIAN_FRONTEND=noninteractive \
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
&& apt-get update && apt-get install -y lsb-release sudo \
&& /chromium_build/install-build-deps-android.sh \
&& rm -rf /chromium_build \
&& rm -rf /var/lib/apt/lists/*

COPY --from=sccache /usr/local/bin/sccache /usr/local/bin/sccache
ENV SCCACHE_DIR=./target/sccache

# x86_64-linux-android
FROM rustembedded/cross:x86_64-linux-android AS x86_64-linux-android

ENV TZ=Etc/UTC
COPY ./build/*.sh /chromium_build/
RUN \
DEBIAN_FRONTEND=noninteractive \
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
&& apt-get update && apt-get install -y lsb-release sudo \
&& /chromium_build/install-build-deps-android.sh \
&& rm -rf /chromium_build \
&& rm -rf /var/lib/apt/lists/*

COPY --from=sccache /usr/local/bin/sccache /usr/local/bin/sccache
ENV SCCACHE_DIR=./target/sccache
42 changes: 41 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,34 @@ fn build_v8() {
maybe_install_sysroot("amd64");
};

if env::var("CARGO_CFG_TARGET_OS").unwrap().as_str() == "android" {
let cpu = match env::var("CARGO_CFG_TARGET_ARCH").unwrap().as_str() {
"aarch64" => "arm64",
"x86_64" => "x64",
_ => unimplemented!(),
};

gn_args.push("target_os=\"android\"".to_string());
gn_args.push(format!("target_cpu=\"{}\"", cpu));

let chromium_uri = "https://chromium.googlesource.com";
maybe_clone_repo(
"./third_party/android_ndk",
&format!("{}/android_ndk.git", chromium_uri),
);
maybe_clone_repo(
"./third_party/android_platform",
&format!(
"{}/chromium/src/third_party/android_platform.git",
chromium_uri
),
);
maybe_clone_repo(
"./third_party/catapult",
&format!("{}/catapult.git", chromium_uri),
);
}

let gn_root = env::var("CARGO_MANIFEST_DIR").unwrap();

let gn_out = cargo_gn::maybe_gen(&gn_root, gn_args);
Expand All @@ -110,6 +138,19 @@ fn build_v8() {
cargo_gn::build("rusty_v8", None);
}

fn maybe_clone_repo(dest: &str, repo: &str) {
if !Path::new(&dest).exists() {
assert!(Command::new("git")
.arg("clone")
.arg("--depth=1")
.arg(repo)
.arg(dest)
.status()
.unwrap()
.success());
}
}

fn maybe_install_sysroot(arch: &str) {
let sysroot_path = format!("build/linux/debian_sid_{}-sysroot", arch);
if !PathBuf::from(sysroot_path).is_dir() {
Expand Down Expand Up @@ -303,7 +344,6 @@ fn find_compatible_system_clang() -> Option<PathBuf> {
return Some(base_path.to_path_buf());
}
}

println!("using Chromiums clang");
None
}
Expand Down

0 comments on commit 2ea029b

Please sign in to comment.