Cross-compiling

From PINE64
Revision as of 01:45, 29 May 2021 by Theintern (talk | contribs)
Jump to navigation Jump to search

The Pinephone's triple for cross-compiling is aarch64-unknown-linux-gnu.

Rust

In order to cross-compile Rust applications for the Pinephone, you need to have a gcc cross-compiler installed and the Rust dependencies, usually the std crate, cross compiled for the target system. A more extensive explanation can be found on https://github.com/japaric/rust-cross. This instruction is based on it's description.

Installing gcc cross-compiler

The cross-compiler might have a different name depending on the operating system. Further along this instruction the name for the gcc cross-compiler will be used. Replace all occurences of $gcc_name with the name on your distribution. Under Arch Linux the cross-compilers name is aarch64-linux-gnu-gcc and it can be installed with:

 $ sudo pacman -S aarch64-linux-gnu-gcc

Ubuntu:

 $ sudo apt install gcc-aarch64-linux-gnu

Other distributions:

 Please add

Installing Rust dependencies

The necessary dependencies can easily be installed with rustup:

 $ rustup target add aarch64-unknown-linux-gnu

OR it can be installed with multirust [Is this still accurate???]:

 $ multirust add-target nightly aarch64-unknown-linux-gnu

Compiling

rustc

When using rustc just add the two flags --target=aarch64-unknown-linux-gnu and -C linker=$gcc_name. Under Arch, this would look like:

 $ rustc --target=aarch64-unknown-linux-gnu -C linker=aarch64-linux-gnu-gcc main.rs

To test it, run the program on your Pinephone

 $ scp main user@ipadress:/home/user/Downloads
 $ ssh user@ipadress /home/user/Downloads/main
 Hello, world!

cargo

To cross-compile a project with cargo, open the folder of the project in a terminal. Then create a new folder and a file for cargo.

 $ mkdir .cargo
 $ cat >.cargo/config <<EOF
 > [target.aarch64-unknown-linux-gnu]
 > linker = "$gcc_name"
 > EOF

Then you can compile it with

 $ cargo build --target=aarch64-unknown-linux-gnu

To test it, copy the file on your Pinephone

 $ scp target/aarch64-unknown-linux-gnu/debug/main user@ipadress:/home/user/Downloads

Then you can execute it by

 $ ssh user@ipadress ./main -h
 Hello, world!

Possible errors

If you encounter an error saying

 Cross compilation detected. Use PKG_CONFIG_ALLOW_CROSS=1 to override

just add that variable in front of your command e.g.

 PKG_CONFIG_ALLOW_CROSS=1 cargo build --target=aarch64-unknown-linux-gnu