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

Rust module support #732

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,9 @@
[submodule "src/lib/secure_udp"]
path = src/lib/secure_udp
url = [email protected]:tiiuae/px4-lib-secure-udp.git
[submodule "src/lib/rust_px4_nuttx"]
path = src/lib/rust_px4_nuttx
url = [email protected]:tiiuae/rust_px4_nuttx_lib.git
[submodule "src/modules/rust_module_example"]
path = src/modules/rust_module_example
url = [email protected]:tiiuae/rust_module_example.git
2 changes: 2 additions & 0 deletions clone_public.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ do
[[ "${repo}" == *pf_crypto ]] || \
[[ "${repo}" == *px4_fw_update_client ]] || \
[[ "${repo}" == *secure_udp ]] || \
[[ "${repo}" == *rust_px4_nuttx ]] || \
[[ "${repo}" == *rust_module_example ]] || \
[[ "${repo}" == *process ]] && continue
git submodule update --init --recursive "${repo}"
done <<< "$(git submodule status | awk '{print $2}')"
48 changes: 47 additions & 1 deletion cmake/px4_add_module.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,35 @@ function(px4_add_module)

px4_parse_function_args(
NAME px4_add_module
ONE_VALUE MODULE MAIN STACK_MAIN STACK_MAX PRIORITY
ONE_VALUE MODULE MAIN STACK_MAIN STACK_MAX PRIORITY LANG RUST_MOD
MULTI_VALUE COMPILE_FLAGS LINK_FLAGS SRCS INCLUDES DEPENDS MODULE_CONFIG
OPTIONS EXTERNAL DYNAMIC UNITY_BUILD NO_DAEMON
REQUIRED MODULE MAIN
ARGN ${ARGN})

set(rust_build false)
if(LANG)
string( TOLOWER "${LANG}" lang_lcase)
if (lang_lcase STREQUAL "rust")
if (NOT RUST_MOD)
message(FATAL_ERROR "px4_add_module: 'LANG: rust' requires argument RUST_MOD")
endif()
set(rust_build true)
endif()
endif()

if(${rust_build})
# Generate stub library file
file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MAIN}_stub.c
CONTENT "#include <stdio.h>\nvoid rust_${RUST_MOD}_main(void) { puts(\"ERROR: [stub library]\"); }\n")

file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${MAIN}_main.c
CONTENT "// Autogenerated, DO NOT EDIT\nvoid rust_${RUST_MOD}_main(void);\nint ${MAIN}_main(int argc, char *argv[]) { rust_${RUST_MOD}_main(); return 0; }\n")

# Build Rust code as a library for the px4 module
list(APPEND SRCS ${CMAKE_CURRENT_BINARY_DIR}/${MAIN}_main.c)
endif()

if(UNITY_BUILD AND (${PX4_PLATFORM} STREQUAL "nuttx"))
# build standalone test library to catch compilation errors and provide sane output
add_library(${MODULE}_original STATIC EXCLUDE_FROM_ALL ${SRCS})
Expand Down Expand Up @@ -257,4 +280,27 @@ function(px4_add_module)
set_property(GLOBAL APPEND PROPERTY PX4_MODULE_CONFIG_FILES ${CMAKE_CURRENT_SOURCE_DIR}/${module_config})
endforeach()
endif()

if(${rust_build})
set(RUST_LIB lib__${MAIN})

# Build stub library with dummy rust_main function
px4_add_library(${RUST_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${MAIN}_stub.c)

# Build Rust code as a library for the px4 module
set(RUST_TARGET_DIR ${CMAKE_CURRENT_BINARY_DIR}/target)
add_custom_target(rust_build_${MODULE} ALL
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${RUST_MOD}
COMMAND cargo build ${CMAKE_RUST_COMPILER_FLAGS} --release --target-dir ${RUST_TARGET_DIR}
)

# Before linking (PRE_LINK), replace the original stub library with Rust build output library
add_custom_command(TARGET ${MODULE} PRE_LINK
COMMAND cp ${RUST_TARGET_DIR}/${CMAKE_RUST_TARGET}/release/lib${RUST_MOD}.a ${CMAKE_CURRENT_BINARY_DIR}/lib${RUST_LIB}.a
)

# Link Rust library to the PX4 module
target_link_libraries(${MODULE} PRIVATE ${RUST_LIB})
endif()

endfunction()
4 changes: 4 additions & 0 deletions packaging/Dockerfile.build_env
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ RUN mkdir -p /artifacts && chown -R builder:builder /artifacts

USER builder

# Define Rust compiler
ENV RUSTUP_HOME=/opt/rust CARGO_HOME=/home/builder/.cargo
ENV PATH=$PATH:/opt/rust/bin

VOLUME /px4-firmware/sources
WORKDIR /px4-firmware/sources

Expand Down
6 changes: 6 additions & 0 deletions packaging/Dockerfile.build_env_pre
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ RUN apt-get update && apt-get install -y --no-install-recommends \

RUN mkdir -p /tools

# Install Rust compiler
ENV RUSTUP_HOME /opt/rust
ENV CARGO_HOME /opt/rust
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --no-modify-path
RUN /opt/rust/bin/rustup target add riscv64gc-unknown-none-elf

RUN curl -LOs https://static.dev.sifive.com/dev-tools/freedom-tools/v2020.12/riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14.tar.gz && \
tar xf riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14.tar.gz -C /tools && \
rm -f riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-linux-ubuntu14.tar.gz
Expand Down
4 changes: 4 additions & 0 deletions platforms/nuttx/NuttX/Make.defs.in
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ NM = ${CMAKE_NM}
OBJCOPY = ${CMAKE_OBJCOPY}
OBJDUMP = ${CMAKE_OBJDUMP}

# Rust compiler
RUSTC = ${CMAKE_RUST_COMPILER}
RUSTFLAGS += ${CMAKE_RUST_COMPILER_FLAGS}

ARCHCCVERSION = $(shell $(CC) -v 2>&1 | sed -n '/^gcc version/p' | sed -e 's/^gcc version \([0-9\.]\)/\1/g' -e 's/[-\ ].*//g' -e '1q')
ARCHCCMAJOR = $(shell echo $(ARCHCCVERSION) | cut -d'.' -f1)

Expand Down
4 changes: 4 additions & 0 deletions platforms/nuttx/cmake/Toolchain-riscv64-unknown-elf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ find_program(CMAKE_OBJDUMP ${TOOLCHAIN_PREFIX}-objdump)
find_program(CMAKE_RANLIB ${TOOLCHAIN_PREFIX}-gcc-ranlib)
find_program(CMAKE_STRIP ${TOOLCHAIN_PREFIX}-strip)

set(CMAKE_RUST_TARGET riscv64gc-unknown-none-elf)
set(CMAKE_RUST_COMPILER rustc)
set(CMAKE_RUST_COMPILER_FLAGS --target ${CMAKE_RUST_TARGET})

set(CMAKE_FIND_ROOT_PATH get_file_component(${CMAKE_C_COMPILER} PATH))
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
Expand Down
1 change: 1 addition & 0 deletions src/lib/rust_px4_nuttx
Submodule rust_px4_nuttx added at 65526d
1 change: 1 addition & 0 deletions src/modules/rust_module_example
Submodule rust_module_example added at a0cd0e
Loading