| 1 |
Cross compiling dbus |
| 2 |
==================== |
| 3 |
|
| 4 |
There are (at least) three different approaches to cross compiling dbus. Choose which one fits you best: |
| 5 |
|
| 6 |
* Using rust-embedded/cross, see below |
| 7 |
* There is an alternative guide at [issue 184](https://github.com/diwic/dbus-rs/issues/184#issuecomment-520228758), which also contains a [powershell script](https://github.com/diwic/dbus-rs/issues/184#issuecomment-520791888) that set things up for you. |
| 8 |
* Setting things up manually, see below |
| 9 |
|
| 10 |
The examples below all assume you're trying to compile for Raspberry Pi 2 or 3 running Raspbian. Adjust target triples accordingly if your target is something else. |
| 11 |
|
| 12 |
|
| 13 |
Cross compiling using rust-embedded/cross |
| 14 |
========================================= |
| 15 |
|
| 16 |
Thanks to [jobale](https://github.com/jobale) for providing these instructions |
| 17 |
(taken from [issue 292](https://github.com/diwic/dbus-rs/issues/292)). |
| 18 |
|
| 19 |
Tested on Ubuntu 20.04 | rustc 1.47.0 |
| 20 |
|
| 21 |
- Install [rust-embedded/cross](https://github.com/rust-embedded/cross) |
| 22 |
- In your project directory, create a **Cross.toml** file: `touch Cross.toml` |
| 23 |
Add this code in it: |
| 24 |
|
| 25 |
``` |
| 26 |
[target.armv7-unknown-linux-gnueabihf] |
| 27 |
image = "rustcross:dbus-armhf" |
| 28 |
|
| 29 |
[build.env] |
| 30 |
passthrough = [ |
| 31 |
"RUSTFLAGS", |
| 32 |
] |
| 33 |
``` |
| 34 |
|
| 35 |
|
| 36 |
- In your project directory create a **Dockerfile**: `touch Dockerfile` |
| 37 |
Put this code in it: |
| 38 |
|
| 39 |
``` |
| 40 |
# Base image for rapsberrypi 3 target |
| 41 |
FROM rustembedded/cross:armv7-unknown-linux-gnueabihf |
| 42 |
|
| 43 |
# Install libdbus libraries and pkg-config |
| 44 |
RUN dpkg --add-architecture armhf && \ |
| 45 |
apt-get update && \ |
| 46 |
apt-get install --assume-yes libdbus-1-dev libdbus-1-dev:armhf pkg-config |
| 47 |
``` |
| 48 |
|
| 49 |
For whatever reason, _in the docker image_, armhf libraries are installed in at least 2 locations. That's the reason of all my troubles: |
| 50 |
- /usr/arm-linux-gnueabihf/lib/ |
| 51 |
- /usr/lib/arm-linux-gnueabihf/ |
| 52 |
|
| 53 |
- Cross needs to know those locations. We pass them to the compiler through a command flag. For convenience I put it in a bash script: |
| 54 |
|
| 55 |
``` |
| 56 |
#!/bin/bash |
| 57 |
set -o errexit |
| 58 |
set -o nounset |
| 59 |
set -o pipefail |
| 60 |
set -o xtrace |
| 61 |
|
| 62 |
readonly TARGET_ARCH=armv7-unknown-linux-gnueabihf |
| 63 |
readonly LINK_FLAGS='-L /usr/arm-linux-gnueabihf/lib/ -L /usr/lib/arm-linux-gnueabihf/' |
| 64 |
|
| 65 |
RUSTFLAGS=${LINK_FLAGS} cross build --release --target=${TARGET_ARCH} |
| 66 |
|
| 67 |
``` |
| 68 |
|
| 69 |
|
| 70 |
Some more explanations for newcomers like me: |
| 71 |
|
| 72 |
- Cross command act as cargo command (e.g: `cross build` is the same as `cargo build` but for cross-compiling). |
| 73 |
- In Cross.toml, `passthrough = [ "RUSTFLAGS",]` is what enable us to pass flags/parameters to the compiler in the docker image. |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
Setting up cross compiling manually |
| 78 |
=================================== |
| 79 |
|
| 80 |
A cross linker |
| 81 |
-------------- |
| 82 |
|
| 83 |
Apparently, `rustc` in itself can generate code for many archs but not assemble the generated code into the final executable. Hence you need a cross linker. |
| 84 |
|
| 85 |
**Install it** - here follow whatever guide you have for the target arch. Distributions may also ship with cross toolchains. Example for Ubuntu 18.04: |
| 86 |
|
| 87 |
`sudo apt install gcc-8-multilib-arm-linux-gnueabihf` |
| 88 |
|
| 89 |
**Tell rustc where to find it** - in [.cargo/config](https://doc.rust-lang.org/cargo/reference/config.html) add the following: |
| 90 |
|
| 91 |
``` |
| 92 |
[target.armv7-unknown-linux-gnueabihf] |
| 93 |
linker = "arm-linux-gnueabihf-gcc-8" |
| 94 |
``` |
| 95 |
|
| 96 |
Target rust std |
| 97 |
--------------- |
| 98 |
|
| 99 |
This one's easy, just run rustup: |
| 100 |
|
| 101 |
`rustup target add armv7-unknown-linux-gnueabihf` |
| 102 |
|
| 103 |
|
| 104 |
Target dbus libraries |
| 105 |
--------------------- |
| 106 |
|
| 107 |
**Installing the libraries** |
| 108 |
|
| 109 |
Now to the more challenging part. Since we link to a C library `libdbus-1.so`, we also need the target version of that library. However, `libdbus-1.so` in itself links to a systemd library (at least it does so here) which in turn links to other libraries etc, so we need target versions of those libraries too. |
| 110 |
|
| 111 |
Getting an entire rootfs/image is probably the easiest option. The rootfs needs to have `libdbus-1-dev` installed. I e: |
| 112 |
|
| 113 |
* Boot your target (i e, a raspberry), install `libdbus-1-dev` on it, turn it off and put the SD card into your computer's SD card reader. Mount the partition. |
| 114 |
* If the above is not an option, you could download an image, mount it at the right offset, like this (ugly hack!) `sudo mount -o loop,offset=50331648 2019-04-08-raspbian-stretch-lite.img /tmp/mnt` and then, to manually make the symlink that `libdbus-1-dev` does for you: `cd /tmp/mnt/usr/lib/arm-linux-gnueabihf && ln -s ../../../lib/arm-linux-gnueabihf/libdbus-1.so.3 libdbus-1.so`. |
| 115 |
* Or you can use the alternative guide's approach to download, extract and post-process the relevant `.deb` files manually. This might be a preferrable option if an entire image/rootfs would be too large. |
| 116 |
|
| 117 |
**Finding the libraries** |
| 118 |
|
| 119 |
When not cross compiling, finding the right library is done by a `build.rs` script which calls `pkg-config`. This will not work when cross compiling because it will point to the `libdbus-1.so` on the host, not the `libdbus-1.so` of the target. |
| 120 |
Maybe it is possible to teach `pkg-config` how to return the target library instead, but I have not tried this approach. Instead we can override build script altogether and provide the same info manually. This is possible because `libdbus-sys` has a `links = dbus` line. |
| 121 |
|
| 122 |
For the example below we assume that we have mounted a Raspbian rootfs on `/tmp/mnt`, and that the cross linker came with some basic libraries (libc, libpthread etc) that are installed on `/usr/arm-linux-gnueabihf/lib`. Unfortunately, we can't use the basic libraries that are present on the image, because they might contain absolute paths. |
| 123 |
|
| 124 |
And so we add the following to [.cargo/config](https://doc.rust-lang.org/cargo/reference/config.html): |
| 125 |
|
| 126 |
``` |
| 127 |
[target.armv7-unknown-linux-gnueabihf.dbus] |
| 128 |
rustc-link-search = ["/usr/arm-linux-gnueabihf/lib", "/tmp/mnt/usr/lib/arm-linux-gnueabihf"] |
| 129 |
rustc-link-lib = ["dbus-1"] |
| 130 |
``` |
| 131 |
|
| 132 |
|
| 133 |
Finally |
| 134 |
------- |
| 135 |
|
| 136 |
If we are all set up, you should be able to successfully compile with: |
| 137 |
|
| 138 |
`cargo build --target=armv7-unknown-linux-gnueabihf` |