From 040c6b8e29fcbe06e8800b775214a5bef9fa51c8 Mon Sep 17 00:00:00 2001 From: tiann Date: Tue, 21 Jun 2022 15:06:49 +0800 Subject: [PATCH] Add docs for cross compile --- CrossCompile.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 ++ 2 files changed, 54 insertions(+) create mode 100644 CrossCompile.md diff --git a/CrossCompile.md b/CrossCompile.md new file mode 100644 index 0000000..ef66960 --- /dev/null +++ b/CrossCompile.md @@ -0,0 +1,52 @@ +# Cross Compile + +To do cross compile, you should set the toolchains correctly, espicially the target linker, please read the [Cargo target reference](https://doc.rust-lang.org/cargo/reference/config.html#target) first. + +Here are some examples for cross compile. + +## Build on macOS and target Linux + +1. Install the rustc toolchain for Linux + +```sh +rustup target add x86_64-unknown-linux-gnu +``` + +2. Install a prebuilt cross compiler for Linux + +```sh +brew tap SergioBenitez/osxct +brew install x86_64-unknown-linux-gnu +``` + +3. Do cross compile: + +```sh +CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-unknown-linux-gnu-gcc \ +cargo build --target=x86_64-unknown-linux-gnu +``` + +The `CARGO_TARGET__LINKER` environment is used to specify the linker for cross compiling. + +## Build on macOS and target Android + +1. Install the rustc toolchain for Android + +```sh +rustup target add arm-linux-androideabi aarch64-linux-android +``` + +2. Install ndk for Android, you can download it in [here](https://developer.android.com/ndk/downloads) +3. Set the linker for cross compile: + +```sh +CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER=$NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android30-clang +``` + +the `$NDK` is the ndk installation directory. + +4. Do cross compile: + +```sh +cargo build --target aarch64-linux-android +``` diff --git a/README.md b/README.md index a90f31f..435584b 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ You may also change the target architecture with the `--target` flag cargo build ``` +If you want to do cross compile, read [CrossCompile](./CrossCompile.md) + ## Run ```bash