Patryk27 / avr-tester

`#[test]` meets simavr!
MIT License
20 stars 2 forks source link

FFI errors on cargo test #5

Open brainstorm opened 3 months ago

brainstorm commented 3 months ago

Hello @Patryk27, thanks for this crate, I was really glad to see there was a simavr wrapper for my project:

https://github.com/brainstorm/embassy-as5600/tree/avr_tester

But I'm facing some FFI/compile issues before reaching the tests themselves (barebones for now):

$ cargo test
   Compiling embedded-hal v1.0.0
   Compiling critical-section v1.1.2
   Compiling vcell v0.1.3
   Compiling bare-metal v1.0.0
   Compiling nb v1.1.0
   Compiling cfg-if v1.0.0
   Compiling simavr-ffi v0.1.0
   Compiling ufmt-write v0.1.0
   Compiling void v1.0.2
error[E0152]: duplicate lang item in crate `core` (which `rustc_std_workspace_core` depends on): `sized`.
  |
  = note: the lang item is first defined in crate `core` (which `critical_section` depends on)
  = note: first definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-f59a350232ae0162.rmeta
  = note: second definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-3afaea4e527ac9e0.rmeta

error[E0152]: duplicate lang item in crate `core` (which `rustc_std_workspace_core` depends on): `sized`.
  |
  = note: the lang item is first defined in crate `core` (which `vcell` depends on)
  = note: first definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-f59a350232ae0162.rmeta
  = note: second definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-3afaea4e527ac9e0.rmeta

For more information about this error, try `rustc --explain E0152`.
error[E0152]: duplicate lang item in crate `core` (which `rustc_std_workspace_core` depends on): `sized`.
  |
  = note: the lang item is first defined in crate `core` (which `bare_metal` depends on)
  = note: first definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-f59a350232ae0162.rmeta
  = note: second definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-3afaea4e527ac9e0.rmeta

error: could not compile `vcell` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `critical-section` (lib) due to 1 previous error
error: could not compile `critical-section` (lib) due to 1 previous error
error[E0152]: duplicate lang item in crate `core` (which `rustc_std_workspace_core` depends on): `sized`.
  |
  = note: the lang item is first defined in crate `core` (which `cfg_if` depends on)
  = note: first definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-f59a350232ae0162.rmeta
  = note: second definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-3afaea4e527ac9e0.rmeta

error: could not compile `bare-metal` (lib) due to 1 previous error
error[E0152]: duplicate lang item in crate `core` (which `rustc_std_workspace_core` depends on): `sized`.
  |
  = note: the lang item is first defined in crate `core` (which `nb` depends on)
  = note: first definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-f59a350232ae0162.rmeta
  = note: second definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-3afaea4e527ac9e0.rmeta

error: could not compile `vcell` (lib) due to 1 previous error
error[E0152]: duplicate lang item in crate `core` (which `rustc_std_workspace_core` depends on): `sized`.
  |
  = note: the lang item is first defined in crate `core` (which `embedded_hal` depends on)
  = note: first definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-f59a350232ae0162.rmeta
  = note: second definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-3afaea4e527ac9e0.rmeta

error: could not compile `cfg-if` (lib) due to 1 previous error
error: could not compile `nb` (lib) due to 1 previous error
error: could not compile `cfg-if` (lib) due to 1 previous error
error[E0152]: duplicate lang item in crate `core` (which `rustc_std_workspace_core` depends on): `sized`.
  |
  = note: the lang item is first defined in crate `core` (which `ufmt_write` depends on)
  = note: first definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-f59a350232ae0162.rmeta
  = note: second definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-3afaea4e527ac9e0.rmeta

error: could not compile `embedded-hal` (lib) due to 1 previous error
error: could not compile `embedded-hal` (lib) due to 1 previous error
error: could not compile `ufmt-write` (lib) due to 1 previous error
error[E0152]: duplicate lang item in crate `core` (which `rustc_std_workspace_core` depends on): `sized`.
  |
  = note: the lang item is first defined in crate `core` (which `void` depends on)
  = note: first definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-f59a350232ae0162.rmeta
  = note: second definition in `core` loaded from /home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/deps/libcore-3afaea4e527ac9e0.rmeta

error: could not compile `void` (lib) due to 1 previous error
error: could not compile `bare-metal` (lib) due to 1 previous error
error: could not compile `nb` (lib) due to 1 previous error
error: could not compile `ufmt-write` (lib) due to 1 previous error
error: failed to run custom build command for `simavr-ffi v0.1.0`

Caused by:
  process didn't exit successfully: `/home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/debug/build/simavr-ffi-5c0c3354040de387/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-changed=build.rs
  => Building simavr
  make: Nothing to be done for 'libsimavr'.
  cargo:rustc-link-search=/home/rvalls/dev/personal/embassy-avr-as5600-encoder/target/avr-atmega2560/debug/build/simavr-ffi-a1a32d682dd698ec/out/simavr
  cargo:rustc-link-lib=static=simavr
  => Generating simavr bindings
  -> Found header: vendor/simavr/simavr/sim/sim_gdb.h
  -> Found header: vendor/simavr/simavr/sim/avr_usb.h
  -> Found header: vendor/simavr/simavr/sim/sim_elf.h
  -> Found header: vendor/simavr/simavr/sim/sim_utils.h
  -> Found header: vendor/simavr/simavr/sim/sim_io.h
  -> Found header: vendor/simavr/simavr/sim/avr_adc.h
  -> Found header: vendor/simavr/simavr/sim/avr_lin.h
  -> Found header: vendor/simavr/simavr/sim/avr_twi.h
  -> Found header: vendor/simavr/simavr/sim/sim_vcd_file.h
  -> Found header: vendor/simavr/simavr/sim/sim_avr_types.h
  -> Found header: vendor/simavr/simavr/sim/avr_watchdog.h
  -> Found header: vendor/simavr/simavr/sim/avr_spi.h
  -> Found header: vendor/simavr/simavr/sim/avr_bitbang.h
  -> Found header: vendor/simavr/simavr/sim/avr_extint.h
  -> Found header: vendor/simavr/simavr/sim/avr_timer.h
  -> Found header: vendor/simavr/simavr/sim/sim_hex.h
  -> Found header: vendor/simavr/simavr/sim/avr_ioport.h
  -> Found header: vendor/simavr/simavr/sim/avr_acomp.h
  -> Found header: vendor/simavr/simavr/sim/sim_irq.h
  -> Found header: vendor/simavr/simavr/sim/sim_interrupts.h
  -> Found header: vendor/simavr/simavr/sim/sim_regbit.h
  -> Found header: vendor/simavr/simavr/sim/sim_cycle_timers.h
  -> Found header: vendor/simavr/simavr/sim/avr_flash.h
  -> Found header: vendor/simavr/simavr/sim/avr_eeprom.h
  -> Found header: vendor/simavr/simavr/sim/sim_avr.h
  -> Found header: vendor/simavr/simavr/sim/fifo_declare.h
  -> Found header: vendor/simavr/simavr/sim/sim_network.h
  -> Found header: vendor/simavr/simavr/sim/avr_uart.h
  -> Found header: vendor/simavr/simavr/sim/sim_time.h

  --- stderr
  fatal: No names found, cannot describe anything.
  warning: no target microcontroller specified on command line, cannot link standard libraries, please pass -mmcu=<mcu name> [-Wavr-rtlib-linking-quirks]
  ./vendor/simavr/simavr/sim/sim_gdb.h:40:18: error: unknown type name 'avr_t'
  ./vendor/simavr/simavr/sim/sim_gdb.h:42:21: error: unknown type name 'avr_t'
  ./vendor/simavr/simavr/sim/sim_gdb.h:45:23: error: unknown type name 'avr_t'
  ./vendor/simavr/simavr/sim/sim_gdb.h:48:33: error: unknown type name 'avr_t'
  ./vendor/simavr/simavr/sim/sim_gdb.h:48:44: error: unknown type name 'uint16_t'
  ./vendor/simavr/simavr/sim/sim_gdb.h:49:27: error: unknown type name 'avr_t'
  /usr/include/features-time64.h:20:10: fatal error: 'bits/wordsize.h' file not found
  warning: no target microcontroller specified on command line, cannot link standard libraries, please pass -mmcu=<mcu name> [-Wavr-rtlib-linking-quirks], err: false
  ./vendor/simavr/simavr/sim/sim_gdb.h:40:18: error: unknown type name 'avr_t', err: true
  ./vendor/simavr/simavr/sim/sim_gdb.h:42:21: error: unknown type name 'avr_t', err: true
  ./vendor/simavr/simavr/sim/sim_gdb.h:45:23: error: unknown type name 'avr_t', err: true
  ./vendor/simavr/simavr/sim/sim_gdb.h:48:33: error: unknown type name 'avr_t', err: true
  ./vendor/simavr/simavr/sim/sim_gdb.h:48:44: error: unknown type name 'uint16_t', err: true
  ./vendor/simavr/simavr/sim/sim_gdb.h:49:27: error: unknown type name 'avr_t', err: true
  /usr/include/features-time64.h:20:10: fatal error: 'bits/wordsize.h' file not found, err: true
  thread 'main' panicked at /home/rvalls/.cargo/registry/src/index.crates.io-6f17d22bba15001f/simavr-ffi-0.1.0/build.rs:105:10:
  Couldn't generate simavr's bindings: ()
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I do have all the Ubuntu 23.10 dependencies listed in simavr-ffi and simavr works fine independently for this project.

Before I dig into this more deeply I wanted to ask if you have you seen those errors before or perhaps can spot right away what am I doing wrong?

Thanks in advance!

/cc @jubeormk1

Patryk27 commented 3 months ago

Hi,

The issue is that avr-tester must be run on your local machine (e.g. x86/aarch64 Linux/MacOS), but having this:

https://github.com/brainstorm/embassy-as5600/blob/avr_tester/.cargo/config.toml#L2

... I think causes the compiler to think it that should compile simavr for avr, which is bound to fail 😅

You should create a separate directory for your firmware and separate directory for your tests, like:

Under this structure, running the tests requires entering the firmware directory, executing cargo build --release, going into the firmware-tests directory and running cargo test there.

Lemme know if that helps!

brainstorm commented 3 months ago

That makes a ton of sense, thanks for your answer!

I'd like to keep the tests on the same directory as any other "standard" Rust project to reduce DX friction: I do think that cargo [run|build|test] should run transparently across targets and that your simavr wrapper could benefit from finding out how to pull that off.

I tried naively adding the following in my .cargo/config.toml:

[test]
target = "aarch64-apple-darwin" <--- my current host target

But of course, the cargo build is targeting AVR anyways with the line you pointed out.

Then I searched a bit and it's a tricky problem with no clear solutions (i.e defmt-test could work, but unfortunately that just targets cortex-m, not AVR).

Here are some interesting pointers, I'm not ready to give up and go with the directory split you're proposing... yet :P

https://users.rust-lang.org/t/cargo-test-for-embedded-target-fails/98926

https://nexte.st/book/target-runners.html

https://github.com/rust-lang/wg-cargo-std-aware/issues/72

https://github.com/probe-rs/embedded-test

I'll poke a bit more on this issue, I think it'd be interesting to get it "right" without splitting the project.

Patryk27 commented 3 months ago

Yeah, having more streamlined experience would be better, but I'm out of ideas myself 😅

I've been trying to utilize artifact dependencies as well (https://doc.rust-lang.org/nightly/cargo/reference/unstable.html?highlight=feature#artifact-dependencies), but without any luck.