koute / not-perf

A sampling CPU profiler for Linux
Apache License 2.0
871 stars 41 forks source link

Cross-compiling for ARMV7? #21

Open bwesen opened 3 years ago

bwesen commented 3 years ago

Hi! There are some suggestions in the README that this perf-version was designed with cross compilation in mind etc and there are some very brief instructions on how to cross compile it. Could you please expand this into a working example?

For example, on Ubuntu 20.04 you can install gcc-9-arm-linux-gnueabihf and in it you can find a gcc (the "linker" for Rust .config) but where do you find the "sys-root"?

koute commented 3 years ago

You might not necessarily need to specify the sysroot. (It depends on how the compiler was configured/built.)

For Ubuntu Focal something like this should work I think:

  1. Install the cross compiler: apt install g++-arm-linux-gnueabihf libc6-dev-armhf-cross
  2. Install Rust.
  3. Install Rust's ARM target: rustup target add armv7-unknown-linux-gnueabihf
  4. Put this in your .cargo/.config:
[target.armv7-unknown-linux-gnueabihf]
linker = "/usr/bin/arm-linux-gnueabihf-gcc"
  1. Compile with cargo build --release --target=armv7-unknown-linux-gnueabihf.
bwesen commented 3 years ago

Thanks, yeah I eventually managed to compile it but I had to spend many hours scourging stack overflow confused posts from a lot of other people who also had problems cross-compiling with Rust :) (not compiling not-perf in particular)

The main issue is that when you install the Ubuntu 20.04 rust packages you don't get rustup and they don't provide the necessary pre-built std/core crates for armv7 either, so you get confused and nothing works and the various tutorials on the net are confusing if you try to use the Debian packages; there are multiple ways to set this up it seems. And you just get weird error messages from rust about the std/core crates missing and if you don't know anything about Rust this just adds to the confusion :)

I ended up having to remove all of the Ubuntu rust-related packages, and instead use the rust shell script to set it up, do the rustup target add and added the cross compiler to the .config like you wrote and then it compiled.

bwesen commented 3 years ago

However, while it compiled properly, I never got any binaries. In the release dir there are only a couple of libs it seems, any suggestions?


~/tmp/not-perf/target/armv7-unknown-linux-gnueabihf/release$ ls -l
total 7904
drwxrwxr-x 28 bjorn bjorn    4096 Jul  7 18:05 build
drwxrwxr-x  2 bjorn bjorn   16384 Jul  7 18:06 deps
drwxrwxr-x  2 bjorn bjorn    4096 Jul  7 17:11 examples
drwxrwxr-x  2 bjorn bjorn    4096 Jul  7 17:11 incremental
-rw-rw-r--  1 bjorn bjorn    2290 Jul  7 18:06 libnperf_core.d
-rw-rw-r--  2 bjorn bjorn 8058580 Jul  7 18:06 libnperf_core.rlib
bjorn3 commented 3 years ago

Try cargo build --release --target=armv7-unknown-linux-gnueabihf --workspace. The root dir defines the nperf_core crate it seems. The cli is found in a different crate in a subdirectory. --workspace causes the whole workspace to be built instead of just the root nperf_core.

koute commented 3 years ago

Ah, right, I forgot to update README after that change! Yes, the bin crate is no longer in the root; if you go into the cli directory and build from there it will build properly.

bwesen commented 3 years ago

Thanks both, going into cli/ and running the build with the appropriate --target produced the nperf binary (in the top-level target/ structure). And it seems to execute on my target.

Now for finding out how to use the tool :)