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 1: Line 1:
The Pinephone's triple for cross-compiling is aarch64-unknown-linux-gnu.  
The Pinephone's triple for cross-compiling is <code>aarch64-unknown-linux-gnu</code>.  


= Rust =
= C/++ =
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 the Toolchain ==
{{note|Please add instructions for other distributions to this section if you know them!}}
 
First, you'll need to install the gcc cross-compilation toolchain.


== Installing gcc cross-compiler ==
=== On Arch Linux ===
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
   $ sudo pacman -S aarch64-linux-gnu-gcc


Ubuntu:
=== On Ubuntu/Debian ===
   $ sudo apt install gcc-aarch64-linux-gnu
   $ sudo apt install gcc-aarch64-linux-gnu


Other distributions:
=== On Void Linux ===
   Please add
  $ sudo xbps-install cross-aarch64-linux-gnu
 
== Using the Toolchain ==
 
''Note: If you are trying to build an Arch Linux package with <code>makepkg</code>, also make sure to <code>export CARCH=aarch64</code>.''
 
=== GNU Make ===
 
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-
 
=== automake ===
 
  $ ./configure --host=aarch64-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 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 <code>$gcc_name</code> 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 ==
== Installing Rust Dependencies ==


The necessary dependencies can easily be installed with rustup:
The necessary dependencies can easily be installed with rustup:
Line 51: Line 74:
   Hello, world!
   Hello, world!


== Possible errors ==
== Possible Errors ==
If you encounter an error saying
If you encounter an error saying
   Cross compilation detected. Use PKG_CONFIG_ALLOW_CROSS=1 to override
   Cross compilation detected. Use PKG_CONFIG_ALLOW_CROSS=1 to override

Revision as of 19:55, 29 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

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-

automake

 $ ./configure --host=aarch64-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 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