Difference between revisions of "Cross-compiling"

From PINE64
Jump to navigation Jump to search
(Restructure page into having a C section (haha), capitalisation/formatting changes)
Line 22: Line 22:


=== GNU Make ===
=== GNU Make ===
==== Kernel Makefile ====


For each invocation of <code>make</code>, be sure to pass the options <code>ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-</code> like this:
For each invocation of <code>make</code>, be sure to pass the options <code>ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-</code> like this:
   $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
   $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
==== Other ====
If you are using a handwritten Makefile (not autogenerated by a meta-buildsystem such as meson, automake, etc.) then simply override <code>CC</code> and <code>LD</code> as you need.


=== automake ===
=== automake ===


   $ ./configure --host=aarch64-linux-gnu
   $ ./configure --host=aarch64-linux-gnu
=== meson ===
See [https://mesonbuild.com/Cross-compilation.html the official meson documentation] on the topic. In general, the introduction is a good read for any cross-compiling novice.


= Rust =
= Rust =

Revision as of 02:00, 31 May 2021

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

C/++

Installing the Toolchain

Please add instructions for other distributions to this section if you know them!

First, you'll need to install the gcc cross-compilation toolchain.

On Arch Linux

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

On Ubuntu/Debian

 $ sudo apt install gcc-aarch64-linux-gnu

On Void Linux

 $ sudo xbps-install cross-aarch64-linux-gnu

Using the Toolchain

Note: If you are trying to build an Arch Linux package with makepkg, also make sure to export CARCH=aarch64.

GNU Make

Kernel Makefile

For each invocation of make, be sure to pass the options ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- like this:

 $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-

Other

If you are using a handwritten Makefile (not autogenerated by a meta-buildsystem such as meson, automake, etc.) then simply override CC and LD as you need.

automake

 $ ./configure --host=aarch64-linux-gnu

meson

See the official meson documentation on the topic. In general, the introduction is a good read for any cross-compiling novice.

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 a 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.

For how to install the gcc cross-compilation toolchain on your distribution, please see Cross-compiling#Installing The Toolchain

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