Skip to content

How to Run v8 on Android RISCV

qjivy edited this page Jan 26, 2024 · 4 revisions

How to build and test v8 on Android RISCV

This article describes how to build and run regression tests of v8 on Android RISCV.

1. Prepare for build

Firstly, make sure you can get and build the v8 source code via either cross compiler build or Simulator Build way. That means you have proper settings for "depot_tools", you can fetch v8 source code and the "gn" tool is good. Further reference can be found here[1].

2. Build RISCV64 V8 for Android

This step includes some workaround tips since now Android toolchain supports RISC-V only in the canary release. Also, we still working on adding the Android riscv64 target for v8. So the build steps are different from ARM and ARM64.

2.1 Download and set up android_toolchain_canary NDK

At the moment, only the canary Android NDK supports RISCV64 [2][3]. So, we just manually download it and copy it into the v8's "third_party" directory.

2.1.1 Download latest android_toolchain_canary NDK

Go to the link android_canary_toolchain [4], and click into the hyperlink under "Instance" with the "latest" tag.

image

Then click the "download" to download the "android_toolchain_canary.zip" package.

image

After downloading and extracting the zip file, you will get "simpleperf" and "toolchains" directory.

Now you can go back to the v8 source code directory. Do the following steps:

cd third_party
mkdir android_toolchain_canary
mkdir android_toolchain_canary/ndk

Then copy the previously mentioned "simpleperf" and "toolchains" directory into the “v8/third_party/android_toolchain_canary/ndk” directory.

2.1.2 Set up the ".gclient" file and sync the repo

First, edit the ".gclient" file under the directory where you fetch v8 code, and add the "custom_deps" and "target_os" lines as the following shows.

solutions = [ 
{
"name": "v8",
"url": "https://chromium.googlesource.com/v8/v8.git",
"deps_file": "DEPS",
"managed": False,
"custom_deps": {'third_party/android_toolchain_canary': None},
},  
]
target_os = ['android']

Then launch the "gclient sync" command in the v8 directory to fetch the proper repos for Android build. (Note: ".gclient" file is not in v8 directory but in v8's parent directory.)

2.2 Set gn args and launch build

For arm and arm64, after step 2.1.2, you can directory launch android build using gm.py by an argument like "android_arm.release" or "android_arm64.debug". But that's not working for RISCV64 now. We need to create the args manually in "args.gn" file and generate ninja files.

2.2.1 Create the output directory then create a proper args.gn file

Do the following steps:

mkdir ./out
mkdir ./out/android_riscv64.release
cd out/android_riscv64.release
cat << EOF > args.gn
is_component_build = false
is_debug = false
target_cpu = "riscv64"
v8_target_cpu = "riscv64"
target_os = "android"

v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true
v8_enable_verify_heap = true
dcheck_always_on = false
EOF

If you want debug mode building, the args.gn file would be echoed as follows:

is_component_build = true
is_debug = true
symbol_level = 2
target_cpu = "riscv64"
v8_target_cpu = "riscv64"
target_os = "android"

v8_enable_backtrace = true
v8_enable_fast_mksnapshot = true
v8_enable_slow_dchecks = true
v8_optimized_debug = false

2.2.2 Manually gen ninja files and launch build

Generate ninja file using "gn gen ." and launch build by "ninja -j 20". (20 would be changed according to the build environment).

Wait until a completed progress bar shows the successful build.

2.3 Package the build output to prepare the regression test

tar czvf v8out.tar.gz --exclude=./out/android_riscv64.release/gen/ --exclude=./out/ android_riscv64.release/obj ./out/android_riscv64.release/ ./third_party

3. Set up the Android emulator (cuttlefish) for RISCV64

3.1 Download cuttlefish source code and build

Do the following steps:

sudo apt install -y git devscripts config-package-dev debhelper-compat golang curl
git clone https://github.com/google/android-cuttlefish
cd android-cuttlefish
for dir in base frontend; do
  cd $dir
  debuild -i -us -uc -b -d
cd ..
done
sudo dpkg -i ./cuttlefish-base_*_*64.deb || sudo apt-get install -f
sudo dpkg -i ./cuttlefish-user_*_*64.deb || sudo apt-get install -f
sudo usermod -aG kvm,cvdnetwork,render $USER
sudo reboot

Note: Make sure the host platform you install and run cuttlefish has KVM support. It can be checked by the "grep -c -w "vmx|svm" /proc/cpuinfo" command, a non-zero result means it supports kvm. Meanwhile, we recommend using Ubuntu22.04.

3.2 Prepare the AOSP files for aosp_cf_riscv64_phone target

Find one of the successfully built aosp_cf_riscv64_phone in the grid of the web page [5], click the "Artifacts" tag and you will get a list view of all the available build output for aosp_cf_riscv64_phone target. To run on cuttlefish, we need download 2 zip files, which are "cvd-host_package.tar.gz " and " aosp_cf_x86_64_phone-img-xxxxxx.zip ".

On your host machine, do the following steps:

mkdir cf
cd cf
tar -xvf /path/to/cvd-host_package.tar.gz
unzip /path/to/aosp_cf_x86_64_phone-img-xxxxxx.zip

3.3 Lauch cuttlefish emulator and validate adb usage

HOME=$PWD ./bin/launch_cvd -cpus=4 --memory_mb=8192 --gpu_mode=none --vm_manager=qemu_cli
./bin/adb devices

3.4 Regression test for v8 on cuttlefish

3.4.1 Get clear v8 source code

Download the clear v8 source from here, you can choose one that has the same or near tag with the buildable v8 in step 1. After downloading, extract the zip or tgz file into a directory.

wget https://github.com/v8/v8/archive/refs/tags/12.2.267.tar.gz
mkdir testv8
cd testv8
tar xzvf ./12.2.267.tar.gz

3.4.2 Extract the build output of v8 into the test directory

cd testv8
tar xzvf ./v8out.tar.gz

3.4.3 Launch the regression test

Make sure you have launched the cuttlefish, then:

cd testv8
./tools/run-test.py --outdir=./out/android_riscv64.release

Wait until the test progress bar shows the test is finished. Now we still get a lot of "time out" failures, which are probably caused by slow emulation speed. Further investigation is still progressing.

Reference:

[1] https://v8.dev/docs/build

[2] https://chromium.googlesource.com/chromium/src/+/master/build/config/android/config.gni#118

[3] https://chromium-review.googlesource.com/c/chromium/src/+/4827087

[4] https://chrome-infra-packages.appspot.com/p/chromium/third_party/android_toolchain_canary/android_toolchain_canary

[5] https://ci.android.com/builds/branches/aosp-main/grid?legacy=1