apache / nuttx-apps

Apache NuttX Apps is a collection of tools, shells, network utilities, libraries, interpreters and can be used with the NuttX RTOS
https://nuttx.apache.org/
Apache License 2.0
292 stars 547 forks source link

[BUG] User led Rust example doesn't compile #2836

Open lvanasse opened 2 weeks ago

lvanasse commented 2 weeks ago

Description / Steps to reproduce the issue

To reproduce the issue:

  1. Configure for the nucleo-l452re config: ./tools/configure.sh -E -l nucleo-l452re:nsh
  2. In the make menuconfig enable the USERLED option for LED Driver
  3. In the make menuconfig enable the EXAMPLES_LEDS_RUST option, and leave everything as default
  4. Compile with make -j

And you should have this error message:

arm-none-eabi-ld: /home/ludovic/Code/nuttx_ws/nuttx/staging/libapps.a(leds_rust_main.rs.home.ludovic.Code.nuttx_ws.apps.examples.leds_rust_1.o): in function `leds_rust_main::nuttx::safe_puts':
/home/ludovic/Code/nuttx_ws/apps/examples/leds_rust/nuttx.rs:108: undefined reference to `__aeabi_memclr'
arm-none-eabi-ld: /home/ludovic/Code/nuttx_ws/apps/examples/leds_rust/nuttx.rs:108: undefined reference to `__aeabi_memclr'
arm-none-eabi-ld: /home/ludovic/Code/nuttx_ws/nuttx/staging/libapps.a(leds_rust_main.rs.home.ludovic.Code.nuttx_ws.apps.examples.leds_rust_1.o): in function `leds_rust_main::nuttx::safe_open':
/home/ludovic/Code/nuttx_ws/apps/examples/leds_rust/nuttx.rs:77: undefined reference to `__aeabi_memclr'
arm-none-eabi-ld: /home/ludovic/Code/nuttx_ws/nuttx/staging/libapps.a(leds_rust_main.rs.home.ludovic.Code.nuttx_ws.apps.examples.leds_rust_1.o): in function `leds_rust_main::nuttx::safe_puts':
/home/ludovic/Code/nuttx_ws/apps/examples/leds_rust/nuttx.rs:108: undefined reference to `__aeabi_memclr'
arm-none-eabi-ld: /home/ludovic/Code/nuttx_ws/apps/examples/leds_rust/nuttx.rs:108: undefined reference to `__aeabi_memclr4'
arm-none-eabi-ld: /home/ludovic/Code/nuttx_ws/apps/examples/leds_rust/nuttx.rs:108: undefined reference to `__aeabi_memclr'
make[1]: *** [Makefile:212: nuttx] Error 1
make: *** [tools/Unix.mk:551: nuttx] Error 2

On which OS does this issue occur?

[OS: Linux]

What is the version of your OS?

Ubuntu 22.04 LTS

NuttX Version

master - e2a21337ac5be663a8e6d9a82baa64b93cc19755 and stable 12.7

Issue Architecture

[Arch: arm]

Issue Area

[Area: Examples]

Verification

lvanasse commented 2 weeks ago

I've asked on the Discord server and as always @lupyuen was very useful and giving me ideas to test :), here's the transcript of his response:

(1) Isn't __aeabi_memclr defined in glibc? https://codebrowser.dev/glibc/glibc/sysdeps/arm/aeabi_memclr.c.html (2) Could you run make --trace to see which libraries we're linking with? (3) Can you reproduce this problem with qemu-armv7a:nsh? https://nuttx.apache.org/docs/latest/platforms/arm/qemu/boards/qemu-armv7a/index.html (4) no1wudi is doing great work on the upcoming Rust Standard Library for NuttX, which is way more advanced than the Rust Core Library that's called by leds_rust. As discussed here: https://lupyuen.github.io/articles/rust6#all-things-considered (5) Rust Core Library is a little wonky on NuttX, but it ought to work. Let's track down this bug thanks 🙂

lvanasse commented 2 weeks ago

Here's the make -j -trace output:

https://gist.github.com/lvanasse/f62e9fa59f8382dd9881178fc1904211

lvanasse commented 2 weeks ago

When trying for the qemu-armv7a:nsh

nuttx_ws/nuttx [master●] » make -j 
error[E0463]: can't find crate for `core`
  |
  = note: the `armv7a-none-eabihf` target may not be installed
  = help: consider downloading the target with `rustup target add armv7a-none-eabihf`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0463`.
make[2]: *** [/home/ludovic/Code/nuttx_ws/apps/Application.mk:330: leds_rust_main.rs.home.ludovic.Code.nuttx_ws.apps.examples.leds_rust.o] Error 1
make[1]: *** [Makefile:52: /home/ludovic/Code/nuttx_ws/apps/examples/leds_rust_all] Error 2
make: *** [tools/LibTargets.mk:248: /home/ludovic/Code/nuttx_ws/apps/libapps.a] Error 2
make: *** Waiting for unfinished jobs....
nuttx_ws/nuttx [master●] » 

And when I try to download the target here's the message I get:

nuttx_ws/nuttx [master●] » rustup target add armv7a-none-eabihf
error: toolchain 'stable-x86_64-unknown-linux-gnu' does not support target 'armv7a-none-eabihf'; did you mean 'armv7r-none-eabihf'?
note: you can see a list of supported targets with `rustc --print=target-list`
note: if you are adding support for a new target to rustc itself, see https://rustc-dev-guide.rust-lang.org/building/new-target.html

I also did try with nightly but same result.

lupyuen commented 2 weeks ago

@lvanasse I was able to reproduce the problem with Docker: https://gist.github.com/nuttxpr/a881eea7923c9471d026ac2bfcbdac82

It seems the problem is caused by the incompatibility between Rust Compiler and GCC Linker used by NuttX:

So it seems we have to wait for NuttX support to become official in Rust:

lvanasse commented 2 weeks ago

You're awesome @lupyuen ! I'm a bit ashamed I was not to be able to figure this out on my own, thank you very much for helping and teaching me :).

Is there a way I could be useful in the process of making this possible to use on ARM? Could @no1wudi use some help?

Maybe we could do our own call in the Rust code for the __aeabi_memclr call? Like they do in the Rust for STM32 article?

lupyuen commented 2 weeks ago

@lvanasse No worries :-) Yep we could code it ourselves. Actually it could have been simpler if we used Rust Libraries (Crates), but NuttX doesn't support them sigh: https://lupyuen.github.io/articles/rust6#no-crates-in-nuttx

The best we can do right now: Put the code into a Rust Source File (like nuttx.rs) and share it across our Rust Projects. Like this: https://github.com/apache/nuttx-apps/blob/master/examples/leds_rust/nuttx.rs

lvanasse commented 2 weeks ago

Awesome! Thank you for the guidance, I'll try to give it a go when I have the energy/time for it :).