From 8d30b00dd1adc7d32a44c121a86f12c156373289 Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 21 Aug 2024 20:40:08 +0200 Subject: [PATCH 1/5] makefiles: Split Rust related checks out from building target --- makefiles/cargo-targets.inc.mk | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/makefiles/cargo-targets.inc.mk b/makefiles/cargo-targets.inc.mk index de45a0c7c1f6..a31a0e522129 100644 --- a/makefiles/cargo-targets.inc.mk +++ b/makefiles/cargo-targets.inc.mk @@ -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 .' ;\ exit 1) @@ -58,7 +57,9 @@ $(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 \ From 184ffc89abe30725e8c726a9b34c20680791ae83 Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 21 Aug 2024 21:20:46 +0200 Subject: [PATCH 2/5] makefiles: Align cargo build command's setup with upcoming cargo-command --- makefiles/cargo-targets.inc.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefiles/cargo-targets.inc.mk b/makefiles/cargo-targets.inc.mk index a31a0e522129..b327aca47a06 100644 --- a/makefiles/cargo-targets.inc.mk +++ b/makefiles/cargo-targets.inc.mk @@ -62,9 +62,9 @@ $(CARGO_LIB): cargo-preflight $(RIOTBUILD_CONFIG_HEADER_C) $(BUILDDEPS) $(CARGO_ @# 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) From d260ec88a67b71766b34e207af224da6659e38c8 Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 21 Aug 2024 21:47:34 +0200 Subject: [PATCH 3/5] makefiles: Add "cargo-command" target for executing cargo Examples of executed commands are `cargo check`, `cargo fix` or `cargo doc`. --- makefiles/cargo-targets.inc.mk | 9 +++++++++ makefiles/info.inc.mk | 1 + 2 files changed, 10 insertions(+) diff --git a/makefiles/cargo-targets.inc.mk b/makefiles/cargo-targets.inc.mk index b327aca47a06..d0924fe88c82 100644 --- a/makefiles/cargo-targets.inc.mk +++ b/makefiles/cargo-targets.inc.mk @@ -68,6 +68,15 @@ $(CARGO_LIB): cargo-preflight $(RIOTBUILD_CONFIG_HEADER_C) $(BUILDDEPS) $(CARGO_ --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. diff --git a/makefiles/info.inc.mk b/makefiles/info.inc.mk index a7b19dc405e9..d9a09a4ef82f 100644 --- a/makefiles/info.inc.mk +++ b/makefiles/info.inc.mk @@ -254,3 +254,4 @@ info-rust: @echo "and export these environment variables:" @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"\`; beware that the way the profile is passed in is not consistent across cargo commands, and adding \`--profile $(CARGO_PROFILE)\` as part of CARGO_COMMAND may be necessary." From a5c7705e1f4b18a4c4816718c37862363554e36e Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 21 Aug 2024 21:51:02 +0200 Subject: [PATCH 4/5] makefiles/doc: Clarify that CARGO_OPTIONS is only used for `cargo build` The options passed to cargo are not universal, and thus can not apply to all commands as was previously documented. --- makefiles/cargo-settings.inc.mk | 2 +- makefiles/info.inc.mk | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/makefiles/cargo-settings.inc.mk b/makefiles/cargo-settings.inc.mk index 3f4c1a8a2e9c..4202113b5166 100644 --- a/makefiles/cargo-settings.inc.mk +++ b/makefiles/cargo-settings.inc.mk @@ -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) diff --git a/makefiles/info.inc.mk b/makefiles/info.inc.mk index d9a09a4ef82f..271a42831343 100644 --- a/makefiles/info.inc.mk +++ b/makefiles/info.inc.mk @@ -250,8 +250,9 @@ 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 " --target $(RUST_TARGET) --profile $(CARGO_PROFILE) $(CARGO_OPTIONS)" @echo "and export these environment variables:" @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"\`; beware that the way the profile is passed in is not consistent across cargo commands, and adding \`--profile $(CARGO_PROFILE)\` as part of CARGO_COMMAND may be necessary." + @echo "You can also call cargo related commands with \`make cargo-command CARGO_COMMAND=\"cargo check\"\`." + @echo "Beware that the way the profile and other flags 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." From 4beff4e9d4f449e30591951488bd3335a2acf2a1 Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 21 Aug 2024 21:53:10 +0200 Subject: [PATCH 5/5] makefiles: Align cargo-info output with what happens in the build system This simplifies the explanation of what might need adjustments depending on which cargo command is invoked. --- makefiles/info.inc.mk | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/makefiles/info.inc.mk b/makefiles/info.inc.mk index 271a42831343..9c9f683d01fc 100644 --- a/makefiles/info.inc.mk +++ b/makefiles/info.inc.mk @@ -250,9 +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) $(CARGO_OPTIONS)" + @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 the profile and other flags 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." + @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."