Skip to content

Commit

Permalink
Merge pull request #20828 from chrysn-pull-requests/cargo-customization
Browse files Browse the repository at this point in the history
Rust: Add `make cargo-command` and refactoring around it
  • Loading branch information
chrysn authored Aug 22, 2024
2 parents fa4f501 + 4beff4e commit 9041fcb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion makefiles/cargo-settings.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CARGO_TARGET_DIR = $(BINDIR)/target
# directory with the same name as the profile".
CARGO_LIB = $(CARGO_TARGET_DIR)/$(RUST_TARGET)/$(patsubst test,debug,$(patsubst dev,debug,$(patsubst bench,release,${CARGO_PROFILE})))/lib$(APPLICATION_RUST_MODULE).a

# Options passed into all Cargo commands, in particular to the build command.
# Options passed into the Cargo build command.
#
# Most of these are populated by RIOT modules that are backed by Rust. Popular
# options added by the user are `-Zbuild-std=core` (only available on nightly)
Expand Down
18 changes: 14 additions & 4 deletions makefiles/cargo-targets.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ $(CARGO_COMPILE_COMMANDS): $(BUILDDEPS)
-e 's/"riscv64-elf"/"riscv32"/g' \
| $(LAZYSPONGE) $@


$(CARGO_LIB): $(RIOTBUILD_CONFIG_HEADER_C) $(BUILDDEPS) $(CARGO_COMPILE_COMMANDS) FORCE
cargo-preflight: FORCE
@command -v cargo >/dev/null || ($(COLOR_ECHO) \
'$(COLOR_RED)Error: `cargo` command missing to build Rust modules.$(COLOR_RESET) Please install as described on <https://doc.riot-os.org/using-rust.html>.' ;\
exit 1)
Expand All @@ -58,15 +57,26 @@ $(CARGO_LIB): $(RIOTBUILD_CONFIG_HEADER_C) $(BUILDDEPS) $(CARGO_COMPILE_COMMANDS
($(COLOR_ECHO) \
'$(COLOR_RED)Error: No Rust libraries are installed for the board'"'"'s CPU.$(COLOR_RESET) Run\n $(COLOR_GREEN)$$$(COLOR_RESET) rustup target add $(RUST_TARGET)\nor set `CARGO_OPTIONS=-Zbuild-std=core`.'; \
exit 1)
@# finally call out to cargo. mind the "+" to pass down make's jobserver.

$(CARGO_LIB): cargo-preflight $(RIOTBUILD_CONFIG_HEADER_C) $(BUILDDEPS) $(CARGO_COMPILE_COMMANDS) FORCE
@# mind the "+" to pass down make's jobserver.
$(Q)+ CC= CFLAGS= CPPFLAGS= CXXFLAGS= \
RIOT_COMPILE_COMMANDS_JSON="$(CARGO_COMPILE_COMMANDS)" \
CARGO_BUILD_TARGET="$(RUST_TARGET)" \
cargo \
build \
--target $(RUST_TARGET) \
--profile $(CARGO_PROFILE) \
$(CARGO_OPTIONS)

cargo-command: cargo-preflight $(RIOTBUILD_CONFIG_HEADER_C) $(CARGO_COMPILE_COMMANDS) FORCE
@[ x"$(CARGO_COMMAND)" != x"" ] || ($(COLOR_ECHO) "$(COLOR_RED)Error: Running cargo-command requires a CARGO_COMMAND to be set.$(COLOR_RESET) Set CARGO_COMMAND=\"cargo clippy --release --fix\" or any other cargo command to run with the right RIOT environment."; exit 1)
@# mind the "+" to pass down make's jobserver.
$(Q)+ CC= CFLAGS= CPPFLAGS= CXXFLAGS= \
RIOT_COMPILE_COMMANDS_JSON="$(CARGO_COMPILE_COMMANDS)" \
CARGO_BUILD_TARGET="$(RUST_TARGET)" \
PROFILE="$(CARGO_PROFILE)" \
$(CARGO_COMMAND)

$(APPLICATION_RUST_MODULE).module: $(CARGO_LIB) FORCE
$(Q)# Ensure no old object files persist. These would lead to duplicate
$(Q)# symbols, or worse, lingering behaivor of XFA entries.
Expand Down
5 changes: 4 additions & 1 deletion makefiles/info.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ info-rust:
cargo version
c2rust --version
@echo "To use this setup of Rust in an IDE, add these command line arguments to the \`cargo check\` or \`rust-analyzer\`:"
@echo " --target $(RUST_TARGET) --profile $(CARGO_PROFILE)"
@echo " --profile $(CARGO_PROFILE) $(CARGO_OPTIONS)"
@echo "and export these environment variables:"
@echo " CARGO_BUILD_TARGET=\"$(RUST_TARGET)\""
@echo " RIOT_COMPILE_COMMANDS_JSON=\"$(CARGO_COMPILE_COMMANDS)\""
@echo " RIOTBUILD_CONFIG_HEADER_C=\"$(RIOTBUILD_CONFIG_HEADER_C)\""
@echo "You can also call cargo related commands with \`make cargo-command CARGO_COMMAND=\"cargo check\"\`."
@echo "Beware that the way command line arguments are passed in is not consistent across cargo commands, so adding \`--profile $(CARGO_PROFILE)\` or other flags from above as part of CARGO_COMMAND may be necessary."

0 comments on commit 9041fcb

Please sign in to comment.