| 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 | The `vendored` feature is the current recommended way to cross compile dbus-rs: |
| 17 | |
| 18 | ``` |
| 19 | dbus = {version = "0.9.7", features = ["vendored"]} |
| 20 | ``` |
| 21 | |
| 22 | Then simply build your project with cross: |
| 23 | |
| 24 | ``` |
| 25 | cross build --target arm-unknown-linux-musleabihf |
| 26 | ``` |
| 27 | |
| 28 | #### Legacy Instructions: |
| 29 | |
| 30 | Thanks to [jobale](https://github.com/jobale) for providing these instructions |
| 31 | (taken from [issue 292](https://github.com/diwic/dbus-rs/issues/292)). |
| 32 | |
| 33 | Tested on Ubuntu 20.04 | rustc 1.47.0 |
| 34 | |
| 35 | - Install [rust-embedded/cross](https://github.com/rust-embedded/cross) |
| 36 | - In your project directory, create a **Cross.toml** file: `touch Cross.toml` |
| 37 | Add this code in it: |
| 38 | |
| 39 | ``` |
| 40 | [target.armv7-unknown-linux-gnueabihf] |
| 41 | image = "rustcross:dbus-armhf" |
| 42 | |
| 43 | [build.env] |
| 44 | passthrough = [ |
| 45 | "RUSTFLAGS", |
| 46 | ] |
| 47 | ``` |
| 48 | |
| 49 | |
| 50 | - In your project directory create a **Dockerfile**: `touch Dockerfile` |
| 51 | Put this code in it: |
| 52 | |
| 53 | ``` |
| 54 | # Base image for rapsberrypi 3 target |
| 55 | FROM rustembedded/cross:armv7-unknown-linux-gnueabihf |
| 56 | |
| 57 | # Install libdbus libraries and pkg-config |
| 58 | RUN dpkg --add-architecture armhf && \ |
| 59 | apt-get update && \ |
| 60 | apt-get install --assume-yes libdbus-1-dev libdbus-1-dev:armhf pkg-config |
| 61 | ``` |
| 62 | |
| 63 | For whatever reason, _in the docker image_, armhf libraries are installed in at least 2 locations. That's the reason of all my troubles: |
| 64 | - /usr/arm-linux-gnueabihf/lib/ |
| 65 | - /usr/lib/arm-linux-gnueabihf/ |
| 66 | |
| 67 | - 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: |
| 68 | |
| 69 | ``` |
| 70 | #!/bin/bash |
| 71 | set -o errexit |
| 72 | set -o nounset |
| 73 | set -o pipefail |
| 74 | set -o xtrace |
| 75 | |
| 76 | readonly TARGET_ARCH=armv7-unknown-linux-gnueabihf |
| 77 | readonly LINK_FLAGS='-L /usr/arm-linux-gnueabihf/lib/ -L /usr/lib/arm-linux-gnueabihf/' |
| 78 | |
| 79 | RUSTFLAGS=${LINK_FLAGS} cross build --release --target=${TARGET_ARCH} |
| 80 | |
| 81 | ``` |
| 82 | |
| 83 | |
| 84 | Some more explanations for newcomers like me: |
| 85 | |
| 86 | - Cross command act as cargo command (e.g: `cross build` is the same as `cargo build` but for cross-compiling). |
| 87 | - In Cross.toml, `passthrough = [ "RUSTFLAGS",]` is what enable us to pass flags/parameters to the compiler in the docker image. |
| 88 | |
| 89 | |
| 90 | |
| 91 | Setting up cross compiling manually |
| 92 | =================================== |
| 93 | |
| 94 | A cross linker |
| 95 | -------------- |
| 96 | |
| 97 | 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. |
| 98 | |
| 99 | **Install it** - here follow whatever guide you have for the target arch. Distributions may also ship with cross toolchains. Example for Ubuntu 18.04: |
| 100 | |
| 101 | `sudo apt install gcc-8-multilib-arm-linux-gnueabihf` |
| 102 | |
| 103 | **Tell rustc where to find it** - in [.cargo/config](https://doc.rust-lang.org/cargo/reference/config.html) add the following: |
| 104 | |
| 105 | ``` |
| 106 | [target.armv7-unknown-linux-gnueabihf] |
| 107 | linker = "arm-linux-gnueabihf-gcc-8" |
| 108 | ``` |
| 109 | |
| 110 | Target rust std |
| 111 | --------------- |
| 112 | |
| 113 | This one's easy, just run rustup: |
| 114 | |
| 115 | `rustup target add armv7-unknown-linux-gnueabihf` |
| 116 | |
| 117 | |
| 118 | Target dbus libraries |
| 119 | --------------------- |
| 120 | |
| 121 | **Installing the libraries** |
| 122 | |
| 123 | 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. |
| 124 | |
| 125 | Getting an entire rootfs/image is probably the easiest option. The rootfs needs to have `libdbus-1-dev` installed. I e: |
| 126 | |
| 127 | * 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. |
| 128 | * 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`. |
| 129 | * 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. |
| 130 | |
| 131 | **Finding the libraries** |
| 132 | |
| 133 | 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. |
| 134 | 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. |
| 135 | |
| 136 | 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. |
| 137 | |
| 138 | And so we add the following to [.cargo/config](https://doc.rust-lang.org/cargo/reference/config.html): |
| 139 | |
| 140 | ``` |
| 141 | [target.armv7-unknown-linux-gnueabihf.dbus] |
| 142 | rustc-link-search = ["/usr/arm-linux-gnueabihf/lib", "/tmp/mnt/usr/lib/arm-linux-gnueabihf"] |
| 143 | rustc-link-lib = ["dbus-1"] |
| 144 | ``` |
| 145 | |
| 146 | |
| 147 | Finally |
| 148 | ------- |
| 149 | |
| 150 | If we are all set up, you should be able to successfully compile with: |
| 151 | |
| 152 | `cargo build --target=armv7-unknown-linux-gnueabihf` |
| 153 | |
| 154 | |
| 155 | Docker compose |
| 156 | ============== |
| 157 | |
| 158 | Dockerfile |
| 159 | ```docker |
| 160 | FROM rust:1.64.0-slim-bullseye |
| 161 | |
| 162 | RUN apt update && apt upgrade -y |
| 163 | RUN apt install -y g++-arm-linux-gnueabihf libc6-dev-armhf-cross |
| 164 | |
| 165 | RUN rustup target add armv7-unknown-linux-gnueabihf |
| 166 | RUN rustup toolchain install stable-armv7-unknown-linux-gnueabihf |
| 167 | |
| 168 | RUN dpkg --add-architecture armhf |
| 169 | RUN apt update |
| 170 | RUN apt install --assume-yes libdbus-1-dev libdbus-1-dev:armhf pkg-config |
| 171 | |
| 172 | WORKDIR /app |
| 173 | |
| 174 | ENV CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=arm-linux-gnueabihf-gcc |
| 175 | ENV CC_armv7_unknown_Linux_gnueabihf=arm-linux-gnueabihf-gcc |
| 176 | ENV CXX_armv7_unknown_linux_gnueabihf=arm-linux-gnueabihf-g++ |
| 177 | ENV CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=/usr/bin/arm-linux-gnueabihf-gcc |
| 178 | ENV PKG_CONFIG_ALLOW_CROSS="true" |
| 179 | ENV PKG_CONFIG_PATH="/usr/lib/arm-linux-gnueabihf/pkgconfig" |
| 180 | ENV RUSTFLAGS="-L /usr/arm-linux-gnueabihf/lib/ -L /usr/lib/arm-linux-gnueabihf/" |
| 181 | ``` |
| 182 | |
| 183 | docker-compose.yml |
| 184 | ```yaml |
| 185 | version: "3" |
| 186 | |
| 187 | services: |
| 188 | compilation: |
| 189 | build: . |
| 190 | volumes: |
| 191 | - ./:/app |
| 192 | command: "cargo build --release --target=armv7-unknown-linux-gnueabihf" |
| 193 | ``` |
| 194 | |
| 195 | You just have to add these two files to your project root and run `docker-compose up`. |