Nocker
Nocker

Reputation: 259

Cross-compiling Rust's SerialPort crate

I am developing my Rust project on Ubuntu 20.04.2 LTS 64bit. I have setup the project to compile for the host architecture (using cargo build) and to cross-compile for the target archicture of ARM 64bit (using cargo build --target=aarch64-unknown-linux-gnu). The project has been cross-compiling fine up until now. On the target hardware platform there are serial ports that the application will be interacting with so I have added the Serial Port crate to my project. The project still compiles fine for the Ubuntu host architecture but I can no longer get it to cross-compile for the aarch64 taget.

I have added the target to rustup:

$ rustup target list
...
aarch64-fuchsia
aarch64-linux-android
aarch64-pc-windows-msvc
aarch64-unknown-linux-gnu (installed)
aarch64-unknown-linux-musl
aarch64-unknown-none
aarch64-unknown-none-softfloat
...

And I have installed the toolchain to rustup:

$ rustup toolchain list
stable-aarch64-unknown-linux-gnu
stable-x86_64-unknown-linux-gnu (default)

When I run the cargo build command of cargo build --target=aarch64-unknown-linux-gnu I get the following error during compiling:

$ cargo build --target=aarch64-unknown-linux-gnu
   Compiling libudev-sys v0.1.4
error: failed to run custom build command for `libudev-sys v0.1.4`

Caused by:
  process didn't exit successfully: `/mnt/hgfs/trunk/src/application/target/debug/build/libudev-sys-c6234f1891f55461/build-script-build` (exit code: 101)
  --- stdout
  cargo:rerun-if-env-changed=LIBUDEV_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_aarch64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_aarch64_unknown_linux_gnu
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
  cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
  cargo:rerun-if-env-changed=PKG_CONFIG_aarch64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_aarch64_unknown_linux_gnu
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_aarch64_unknown_linux_gnu
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR

  --- stderr
  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "pkg-config has not been configured to support cross-compilation.\n\n                Install a sysroot for the target platform and configure it via\n                PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a\n                cross-compiling wrapper for pkg-config and set it via\n                PKG_CONFIG environment variable."', /home/nocker/.cargo/registry/src/github.com-1ecc6299db9ec823/libudev-sys-0.1.4/build.rs:38:41
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: build failed

I have installed pkg-config and libudev-dev. From the terminal I can get this output to confirm that pkg-config is installed:

$ pkg-config --version
0.29.1

but when I try to check that libudev is installed I get an error response:

$ libudev
libudev: command not found
$ libudev-dev
libudev-dev: command not found

When I try to install libudev again I get:

$ sudo apt install libudev-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libudev-dev is already the newest version (245.4-4ubuntu3.4).
0 upgraded, 0 newly installed, 0 to remove and 15 not upgraded.

When I try to install libudev-sys I get:

$ sudo apt install libudev-sys
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package libudev-sys

I'm suspecting the reason that I cannot cross-compile is because of libudev but cannot figure this out. I have been trying to research how to solve this issue for a few days now but no luck with a solution. Any assistance or direction will be greatly appreciated. TIA.

Upvotes: 2

Views: 2385

Answers (1)

user15916460
user15916460

Reputation: 61

If you are not dependent on some extra features you can disable the default libudev feature. Adapt the Cargo.toml this way:

serialport = {version = "4.0.1", default-features = false}

See also the description here: https://crates.io/crates/serialport

Otherwise you have to install a sysroot or wrapper. But I was not able to figure out how to do this (the solution above works fine for me). The error message I got is a little bit more meaningful:

pkg-config has not been configured to support cross-compilation.
Install a sysroot for the target platform and configure it via
PKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a
cross-compiling wrapper for pkg-config and set it via PKG_CONFIG
environment variable.

Upvotes: 6

Related Questions