Skip to content

[BUG] Fix incorrect Rust library path in nuttx_add_rust.cmake #3473

@toku-mac

Description

@toku-mac

Description / Steps to reproduce the issue

Description

When building a Rust crate using CMake with a specific target triple (e.g., thumbv8m.main-nuttx-eabihf), the linker fails to find the generated static library (lib<crate>.a).

This is because nuttx_add_rust.cmake incorrectly constructs the build directory path by appending the target triple to the --target-dir argument. Since Cargo itself also appends the target triple to the directory specified by --target-dir, it results in a nested directory structure like:
.../target/<target-triple>/<target-triple>/debug/lib<crate>.a

However, the CMake script expects the library to be at:
.../target/<target-triple>/debug/lib<crate>.a

Steps to Reproduce

  1. Configure NuttX for a board that supports Rust (e.g., nucleo-h563zi:nsh).
  2. Use CMake to build the project:
    cmake -S . -B build-debug -DBOARD_CONFIG=nucleo-h563zi:nsh -GNinja
    cmake --build build-debug -t olddefconfig
    cmake --build build-debug -j 6
  3. The build fails during the linking stage with an error similar to:
    /path/to/arm-none-eabi/bin/ld: cannot find apps/examples/rust/hello/hello/target/thumbv8m.main-nuttx-eabihf/debug/libhello.a: No such file or directory
    

Expected Behavior

The Rust library should be correctly located by the linker, and the build should complete successfully.

Actual Behavior

The linker fails because it looks for the library in the wrong location due to the nested directory structure created by the incorrect --target-dir usage.

Proposed Fix

Modify apps/cmake/nuttx_add_rust.cmake to set RUST_BUILD_DIR without the target triple and update RUST_LIB_PATH to include the target triple in the expected location.

# Before
set(RUST_BUILD_DIR
    ${CMAKE_CURRENT_BINARY_DIR}/${CRATE_NAME}/target/${TARGET_BASE})
set(RUST_LIB_PATH ${RUST_BUILD_DIR}/${RUST_PROFILE}/lib${CRATE_NAME}.a)

# After
set(RUST_BUILD_DIR
    ${CMAKE_CURRENT_BINARY_DIR}/${CRATE_NAME}/target)
set(RUST_LIB_PATH ${RUST_BUILD_DIR}/${TARGET_BASE}/${RUST_PROFILE}/lib${CRATE_NAME}.a)

On which OS does this issue occur?

[OS: Mac]

What is the version of your OS?

26.4.1

NuttX Version

12.13.0

Issue Architecture

[Arch: arm]

Issue Area

[Area: Examples]

Host information

No response

Verification

  • I have verified before submitting the report.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: BugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions