From 95cef2229c83f2903d0344901995b446fc91be41 Mon Sep 17 00:00:00 2001 From: chrysn Date: Thu, 16 Jun 2022 15:39:15 +0200 Subject: [PATCH 1/4] makefiles/rust: Err before trying to build without having the libraries in place This is also a workaround for Rust's [97685], but primarily to enhance the error message by pointing out that -Zbuild-std is an option, and generally presenting the error as RIOT usually does. [97685]: https://github.com/rust-lang/rust/issues/97685 --- makefiles/cargo-targets.inc.mk | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/makefiles/cargo-targets.inc.mk b/makefiles/cargo-targets.inc.mk index 0a208232f021..716b9ac85fa4 100644 --- a/makefiles/cargo-targets.inc.mk +++ b/makefiles/cargo-targets.inc.mk @@ -19,6 +19,13 @@ $(CARGO_COMPILE_COMMANDS): $(BUILDDEPS) $(CARGO_LIB): $(RIOTBUILD_CONFIG_HEADER_C) $(BUILDDEPS) $(CARGO_COMPILE_COMMANDS) FORCE $(Q)[ x"${RUST_TARGET}" != x"" ] || (echo "Error: No RUST_TARGET was set for this platform. (Set FEATURES_REQUIRED+=rust_target to catch this earlier)."; exit 1) + $(Q)# If distribution installed cargos ever grow the capacity to build RIOT, this absence of `rustup` might be OK. But that'd need them to both have cross tools around and cross core libs, none of which is currently the case. + $(Q)# Ad grepping for "std": We're not *actually* checking for std but more for core -- but rust-stc-$TARGET is the name of any standard libraries that'd be available for that target. + $(Q)[ x"$(findstring build-std,$(CARGO_OPTIONS))" != x"" ] || \ + (rustup component list $(patsubst %,--toolchain %,$(CARGO_CHANNEL)) --installed | grep 'rust-std-$(RUST_TARGET)$$' --quiet) || \ + ($(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) $(patsubst %,--toolchain %,$(CARGO_CHANNEL))\nor set `CARGO_OPTIONS=-Zbuild-std=core`.'; \ + exit 1) $(Q)CC= CFLAGS= CPPFLAGS= CXXFLAGS= RIOT_COMPILE_COMMANDS_JSON="$(CARGO_COMPILE_COMMANDS)" RIOT_USEMODULE="$(USEMODULE)" cargo $(patsubst +,,+${CARGO_CHANNEL}) build --target $(RUST_TARGET) `if [ x$(CARGO_PROFILE) = xrelease ]; then echo --release; else if [ x$(CARGO_PROFILE) '!=' xdebug ]; then echo "--profile $(CARGO_PROFILE)"; fi; fi` $(CARGO_OPTIONS) $(APPLICATION_RUST_MODULE).module: $(CARGO_LIB) FORCE From d129f3dc31ba485d50997db82e81b6a940e839ec Mon Sep 17 00:00:00 2001 From: chrysn Date: Thu, 16 Jun 2022 15:42:09 +0200 Subject: [PATCH 2/4] makefiles/rust: Colorful error output --- 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 716b9ac85fa4..a3d87fc9a0f3 100644 --- a/makefiles/cargo-targets.inc.mk +++ b/makefiles/cargo-targets.inc.mk @@ -18,7 +18,7 @@ $(CARGO_COMPILE_COMMANDS): $(BUILDDEPS) $(CARGO_LIB): $(RIOTBUILD_CONFIG_HEADER_C) $(BUILDDEPS) $(CARGO_COMPILE_COMMANDS) FORCE - $(Q)[ x"${RUST_TARGET}" != x"" ] || (echo "Error: No RUST_TARGET was set for this platform. (Set FEATURES_REQUIRED+=rust_target to catch this earlier)."; exit 1) + $(Q)[ x"${RUST_TARGET}" != x"" ] || ($(COLOR_ECHO) "$(COLOR_RED)Error: No RUST_TARGET was set for this platform.$(COLOR_RESET) Set FEATURES_REQUIRED+=rust_target to catch this earlier."; exit 1) $(Q)# If distribution installed cargos ever grow the capacity to build RIOT, this absence of `rustup` might be OK. But that'd need them to both have cross tools around and cross core libs, none of which is currently the case. $(Q)# Ad grepping for "std": We're not *actually* checking for std but more for core -- but rust-stc-$TARGET is the name of any standard libraries that'd be available for that target. $(Q)[ x"$(findstring build-std,$(CARGO_OPTIONS))" != x"" ] || \ From 36a029c07a45b4d8cda7914eef3becdf3f5301d2 Mon Sep 17 00:00:00 2001 From: chrysn Date: Thu, 16 Jun 2022 15:58:39 +0200 Subject: [PATCH 3/4] makefiles/rust: Line wrap for better readability --- makefiles/cargo-targets.inc.mk | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/makefiles/cargo-targets.inc.mk b/makefiles/cargo-targets.inc.mk index a3d87fc9a0f3..23a0d6583ab7 100644 --- a/makefiles/cargo-targets.inc.mk +++ b/makefiles/cargo-targets.inc.mk @@ -27,6 +27,14 @@ $(CARGO_LIB): $(RIOTBUILD_CONFIG_HEADER_C) $(BUILDDEPS) $(CARGO_COMPILE_COMMANDS '$(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) $(patsubst %,--toolchain %,$(CARGO_CHANNEL))\nor set `CARGO_OPTIONS=-Zbuild-std=core`.'; \ exit 1) $(Q)CC= CFLAGS= CPPFLAGS= CXXFLAGS= RIOT_COMPILE_COMMANDS_JSON="$(CARGO_COMPILE_COMMANDS)" RIOT_USEMODULE="$(USEMODULE)" cargo $(patsubst +,,+${CARGO_CHANNEL}) build --target $(RUST_TARGET) `if [ x$(CARGO_PROFILE) = xrelease ]; then echo --release; else if [ x$(CARGO_PROFILE) '!=' xdebug ]; then echo "--profile $(CARGO_PROFILE)"; fi; fi` $(CARGO_OPTIONS) + $(Q)CC= CFLAGS= CPPFLAGS= CXXFLAGS= \ + RIOT_COMPILE_COMMANDS_JSON="$(CARGO_COMPILE_COMMANDS)" \ + RIOT_USEMODULE="$(USEMODULE)" \ + cargo $(patsubst +,,+${CARGO_CHANNEL}) \ + build \ + --target $(RUST_TARGET) \ + `if [ x$(CARGO_PROFILE) = xrelease ]; then echo --release; else if [ x$(CARGO_PROFILE) '!=' xdebug ]; then echo "--profile $(CARGO_PROFILE)"; fi; fi` \ + $(CARGO_OPTIONS) $(APPLICATION_RUST_MODULE).module: $(CARGO_LIB) FORCE $(Q)# Ensure no old object files persist. These would lead to duplicate From 54fe5fc8c37021cee5972bbca6f7b5bd7b7e8407 Mon Sep 17 00:00:00 2001 From: chrysn Date: Thu, 16 Jun 2022 15:56:29 +0200 Subject: [PATCH 4/4] makefiles/rust: Extra checks to ensure a usable build environment ... and print actionable errors. Closes: https://github.com/RIOT-OS/RIOT/issues/18200 --- makefiles/cargo-targets.inc.mk | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/makefiles/cargo-targets.inc.mk b/makefiles/cargo-targets.inc.mk index 23a0d6583ab7..7b6628d8d84a 100644 --- a/makefiles/cargo-targets.inc.mk +++ b/makefiles/cargo-targets.inc.mk @@ -18,6 +18,15 @@ $(CARGO_COMPILE_COMMANDS): $(BUILDDEPS) $(CARGO_LIB): $(RIOTBUILD_CONFIG_HEADER_C) $(BUILDDEPS) $(CARGO_COMPILE_COMMANDS) FORCE + $(Q)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) + $(Q)command -v $${C2RUST:-c2rust} >/dev/null || ($(COLOR_ECHO) \ + '$(COLOR_RED)Error: `'$${C2RUST:-c2rust}'` command missing to build Rust modules.$(COLOR_RESET) Please install as described on .' ;\ + exit 1) + $(Q)command -v rustup >/dev/null || ($(COLOR_ECHO) \ + '$(COLOR_RED)Error: `rustup` command missing.$(COLOR_RESET) While it is not essential for building Rust modules, it is the only known way to install the target core libraries (or nightly for -Zbuild-std) needed to do so. If you do think that building should be possible, please edit this file, and file an issue about building Rust modules with the installation method you are using -- later checks in this file, based on rustup, will need to be adjusted for that.' ;\ + exit 1) $(Q)[ x"${RUST_TARGET}" != x"" ] || ($(COLOR_ECHO) "$(COLOR_RED)Error: No RUST_TARGET was set for this platform.$(COLOR_RESET) Set FEATURES_REQUIRED+=rust_target to catch this earlier."; exit 1) $(Q)# If distribution installed cargos ever grow the capacity to build RIOT, this absence of `rustup` might be OK. But that'd need them to both have cross tools around and cross core libs, none of which is currently the case. $(Q)# Ad grepping for "std": We're not *actually* checking for std but more for core -- but rust-stc-$TARGET is the name of any standard libraries that'd be available for that target.