Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trouble cross compiling for a raspberry pi #7

Open
ryepup opened this issue Feb 26, 2021 · 2 comments
Open

trouble cross compiling for a raspberry pi #7

ryepup opened this issue Feb 26, 2021 · 2 comments

Comments

@ryepup
Copy link

ryepup commented Feb 26, 2021

I'm getting some novice cross-compiling errors that I'm not sure how to resolve. I think I don't have libudev-dev setup right, but I'm unsure how to get them in the right place.

libudev-sys is a transitive dependency of a program I'm trying to compile on my ubuntu laptop and copy a binary over to the pi.

My build script:

#!/bin/sh

SYSROOT=/build/root

export PKG_CONFIG_DIR=
export PKG_CONFIG_LIBDIR=${SYSROOT}/usr/lib/pkgconfig:${SYSROOT}/usr/share/pkgconfig
export PKG_CONFIG_SYSROOT_DIR=${SYSROOT}
export PKG_CONFIG_ALLOW_CROSS=1

cargo build --release --target=armv7-unknown-linux-gnueabihf

A sample run:

$ ./build.sh
   Compiling libc v0.2.82
   Compiling pkg-config v0.3.19
   Compiling void v1.0.2
   Compiling slab v0.4.2
   Compiling nix v0.14.1
   Compiling nix v0.17.0
   Compiling pin-project-lite v0.1.11
   Compiling bytes v0.5.6
   Compiling futures-core v0.3.13
   Compiling futures-sink v0.3.13
   Compiling pin-utils v0.1.0
   Compiling pin-project-lite v0.2.4
   Compiling futures-task v0.3.13
   Compiling futures-util v0.3.13
   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: `/home/.../target/release/build/libudev-sys-dac8bee601eaad20/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_armv7-unknown-linux-gnueabihf
  cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_armv7_unknown_linux_gnueabihf
  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
  cargo:rerun-if-env-changed=LIBUDEV_STATIC
  cargo:rerun-if-env-changed=LIBUDEV_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_armv7-unknown-linux-gnueabihf
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_armv7_unknown_linux_gnueabihf
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_armv7-unknown-linux-gnueabihf
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_armv7_unknown_linux_gnueabihf
  cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_armv7-unknown-linux-gnueabihf
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_armv7_unknown_linux_gnueabihf
  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\" \"--libs\" \"--cflags\" \"libudev\"` did not exit successfully: exit code: 1\n--- stderr\nPackage libudev was not found in the pkg-config search path.\nPerhaps you should add the directory containing `libudev.pc\'\nto the PKG_CONFIG_PATH environment variable\nNo package \'libudev\' found\n"', /home/.../.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'm getting some clear advice, but I don't know how to follow it:

Package libudev was not found in the pkg-config search path.
Perhaps you should add the directory containing `libudev.pc'
to the PKG_CONFIG_PATH environment variable

I've run a apt install libudev-dev to get the package installed, and that provided /usr/lib/x86_64-linux-gnu/pkgconfig/libudev.pc; I don't think that's suitable for a armv7-unknown-linux-gnueabihf binary. I tried adding that x86_64 path to my PKG_CONFIG_PATH and was able to get through compilation, but failed at linking:

/usr/lib/gcc-cross/arm-linux-gnueabihf/9/../../../../arm-linux-gnueabihf/bin/ld: cannot find -ludev

Any pointers on how I could get a libudev.pc that's suitable?

@ryepup
Copy link
Author

ryepup commented Feb 26, 2021

Ok, after more googling and guess-and-check I think I figured it out. The missing piece was getting my laptop configured to install packages for other architectures, found on Ask Ubuntu: Cross-compile for armhf and install a static library.

This is what worked for me:

  • update my /etc/apt/sources.list to add a [arch=amd64] to the existing sources, and add new sources to pull armhf packages from ports.ubuntu.com

      # deb cdrom:[Ubuntu 20.04 LTS _Focal Fossa_ - Release amd64 (20200423)]/ focal main restricted
      deb [arch=amd64] http://archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse
      deb [arch=amd64] http://security.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse
      deb [arch=amd64] http://archive.ubuntu.com/ubuntu/ focal-updates main restricted universe multiverse
      # added these two:
      deb [arch=armhf] http://ports.ubuntu.com/ubuntu-ports focal main universe
      deb [arch=armhf] http://ports.ubuntu.com/ubuntu-ports focal-updates main universe
    
  • update packages so all these changes take effect: sudo apt update

  • let dpkg know I'm interested in more architectures

      sudo dpkg --add-architecture armhf
    
  • install the libudev headers for armhf: sudo apt install libudev-dev:armhf - this wanted a bunch of related packages and I said yes

  • install a compiler capable of generating armhf code: sudo apt install gcc-arm-linux-gnueabihf

  • add some more environment variables in my build script:

#!/bin/sh

SYSROOT=/build/root

export PKG_CONFIG_DIR=
export PKG_CONFIG_LIBDIR=${SYSROOT}/usr/lib/pkgconfig:${SYSROOT}/usr/share/pkgconfig
export PKG_CONFIG_SYSROOT_DIR=${SYSROOT}
export PKG_CONFIG_ALLOW_CROSS=1
# tell pkg-config where to find libudev.pc
export PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabihf/pkgconfig
# tell cargo to link with an armhf compatible linker
export CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=arm-linux-gnueabihf-gcc

cargo build --release --target=armv7-unknown-linux-gnueabihf

Do you think any of this should end up in the README here? I'd be happy to submit a PR, but unsure what level of detail would be useful.

@zhang-ray
Copy link

I got the similar error message in rust docker container:

error: failed to run custom build command for `libudev-sys v0.1.4`
Caused by:
  process didn't exit successfully: `/builds/zhangruihuai/playground-rust/tci-linux/target/debug/build/libudev-sys-d387404a27cfd86d/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-env-changed=LIBUDEV_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=LIBUDEV_STATIC
  cargo:rerun-if-env-changed=LIBUDEV_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_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\" \"--libs\" \"--cflags\" \"libudev\"` did not exit successfully: exit status: 1\nerror: could not find system library 'libudev' required by the 'libudev-sys' crate\n\n--- stderr\nPackage libudev was not found in the pkg-config search path.\nPerhaps you should add the directory containing `libudev.pc'\nto the PKG_CONFIG_PATH environment variable\nNo package 'libudev' found\n"', /usr/local/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: Job failed: exit code 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants