Difference between revisions of "Cross-compiling"

From PINE64
Jump to navigation Jump to search
(Fixes)
 
(2 intermediate revisions by one other user not shown)
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 ====
 
===== 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:
 
  $ 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 ====
 
  $ ./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 ==
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.


== Installing Rust dependencies ==
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:
The necessary dependencies can easily be installed with rustup:
   $ rustup target add aarch64-unknown-linux-gnu
   $ rustup target add aarch64-unknown-linux-gnu
OR it can be installed with multirust [Is this still accurate???]:
OR it can be installed with multirust [Is this still accurate???]:
   $ multirust add-target nightly aarch64-unknown-linux-gnu
   $ multirust add-target nightly aarch64-unknown-linux-gnu


== Compiling ==
=== Compiling ===


=== rustc ===  
==== 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:


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
   $ rustc --target=aarch64-unknown-linux-gnu -C linker=aarch64-linux-gnu-gcc main.rs


Line 35: Line 73:
   Hello, world!
   Hello, world!


=== cargo ===  
==== 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.
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
   $ mkdir .cargo
   $ cat >.cargo/config <<EOF
   $ cat >.cargo/config <<EOF
Line 42: Line 82:
   > linker = "$gcc_name"
   > linker = "$gcc_name"
   > EOF
   > EOF
Then you can compile it with
Then you can compile it with
   $ cargo build --target=aarch64-unknown-linux-gnu
   $ cargo build --target=aarch64-unknown-linux-gnu


To test it, copy the file on your Pinephone
To test it, copy the file on your Pinephone
   $ scp target/aarch64-unknown-linux-gnu/debug/main user@ipadress:/home/user/Downloads
   $ scp target/aarch64-unknown-linux-gnu/debug/main user@ipadress:/home/user/Downloads
Then you can execute it by
Then you can execute it by
   $ ssh user@ipadress ./main -h
   $ ssh user@ipadress ./main -h
   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
just add that variable in front of your command e.g.
just add that variable in front of your command e.g.
   PKG_CONFIG_ALLOW_CROSS=1 cargo build --target=aarch64-unknown-linux-gnu
   PKG_CONFIG_ALLOW_CROSS=1 cargo build --target=aarch64-unknown-linux-gnu


[[Category:PinePhone]]
[[Category:PinePhone]]

Latest revision as of 21:31, 2 April 2023

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