Skip to content

Commit 22dbbf4

Browse files
authored
Merge pull request #20319 from chrysn-pull-requests/rust-simplifications
makefiles/cargo: Remove CARGO_CHANNEL and other simplifications
2 parents 98f5056 + 50c9d93 commit 22dbbf4

File tree

12 files changed

+66
-77
lines changed

12 files changed

+66
-77
lines changed

doc/doxygen/src/using-rust.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ maintained in coordination with the riot-wrappers crate.
3939
[riot-module-examples]: https://gitlab.com/etonomy/riot-module-examples
4040
[additional examples]: https://gitlab.com/etonomy/riot-examples/
4141

42+
IDE / editor setup
43+
------------------
44+
45+
Users of Rust often take advantage of autocompletion or inline help.
46+
To use this on RIOT projects,
47+
some flags and environment variables have to be set,
48+
which are listed by `make info-rust`.
49+
These can be configured in the IDE's project setup
50+
or exported as environment variables.
51+
4252
How it works
4353
------------
4454

@@ -108,19 +118,17 @@ To install the necessary Rust components, it is easiest use [**rustup**, install
108118

109119
Using Rust on RIOT needs the latest stable version of Rust.
110120

111-
Make sure you have the stable **toolchain**
112-
and the core library for the CPU (**target**) of your choice available:
121+
Make sure you have the core library for the CPU (**target**) of your choice available:
113122

114123
```
115-
$ rustup toolchain add stable
116-
$ rustup target add thumbv7m-none-eabi --toolchain stable
124+
$ rustup target add thumbv7m-none-eabi
117125
```
118126

119127
Substitute thumbv7m-none-eabi with the value of `RUST_TARGET`
120128
in the output of `make info-build` of an application that has your current board selected
121129
(or just add it later whenever the Rust compiler complains about not finding the core library for a given target).
122-
Using a beta or nightly will work just as well,
123-
but you may need to set `CARGO_CHANNEL=nightly` on your shell or in your Makefiles.
130+
Using the beta or nightly toolchains will work just as well
131+
if they are selected through rustup's override mechanism.
124132

125133

126134
While Rust comes with its own [cargo] dependency tracker for any Rust code,

examples/rust-gcoap/Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/rust-gcoap/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ BASELIBS += $(APPLICATION_RUST_MODULE).module
4141

4242
FEATURES_REQUIRED += rust_target
4343

44-
CARGO_CHANNEL ?= stable
45-
4644
# Currently unknown, something related to the LED_PORT definition that doesn't
4745
# pass C2Rust's transpilation
4846
BOARD_BLACKLIST := ek-lm4f120xl

examples/rust-hello-world/Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/rust-hello-world/Makefile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ BASELIBS += $(APPLICATION_RUST_MODULE).module
2121

2222
FEATURES_REQUIRED += rust_target
2323

24-
# All Rust components RIOT uses work on stable Rust. If any extra libraries
25-
# were to require a more recent version, switch to `CARGO_CHANNEL =
26-
# $(CARGO_CHANNEL_NIGHTLY)` to use whichever nightly version is available.
27-
CARGO_CHANNEL ?= stable
28-
2924
# Currently unknown, something related to the LED_PORT definition that doesn't
3025
# pass C2Rust's transpilation
3126
BOARD_BLACKLIST := ek-lm4f120xl

makefiles/cargo-settings.inc.mk

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,8 @@
1-
# Setting anything other than "debug" or "release" will necessitate additional
2-
# -Z unstable-options as of 2021-03 nightlies.
3-
CARGO_PROFILE ?= release
4-
5-
# Value for CARGO_CHANNEL when using nightly
6-
#
7-
# As different environments have different versions of nightly installed, but
8-
# rustup / cargo does not take "the latest installed nightly" for a toolchain,
9-
# a good value is determined dynamically. Typical values this takes are
10-
# `nightly` (on regular installations) and `nightly-2022-03-08` (or whichever
11-
# date it is currently pinned to) in riotbuild.
12-
#
13-
# Workaround-For: https://github.com/rust-lang/rustup/issues/3015
14-
#
15-
# This does not get evaluated unless actually used; if rustup is not installed,
16-
# the default value will likely not be usable but at least set the user on the
17-
# right track.
18-
CARGO_CHANNEL_NIGHTLY = $(shell rustup toolchain list | sed 's/ .*//' |grep nightly | tail -n1 || echo nightly)
19-
20-
# The Rust version to use.
1+
# The profile with which to build Rust usually `release` or `dev`.
212
#
22-
# Examples should set this to either `stable` or `$(CARGO_CHANNEL_NIGHTLY)`.
23-
# The default is empty, which is suitable for applications that select their
24-
# version through a `rust-toolchain.yaml` file.
25-
CARGO_CHANNEL ?=
3+
# This needs to be known to the build scripts because the path of the produced
4+
# binary is derived from this.
5+
CARGO_PROFILE ?= release
266

277
# Note that if we did not set this explicitly, CARGO_LIB would have to
288
# understand which value cargo uses in absence of CARGO_TARGET_DIR, which would
@@ -42,4 +22,17 @@ CARGO_CHANNEL ?=
4222
CARGO_TARGET_DIR = $(BINDIR)/target
4323

4424
# The single Rust library to be built.
45-
CARGO_LIB = $(CARGO_TARGET_DIR)/$(RUST_TARGET)/${CARGO_PROFILE}/lib$(APPLICATION_RUST_MODULE).a
25+
#
26+
# The dev->debug and bench->release substitutions represent a historical
27+
# peculiarity in cargo: "For historical reasons, the `dev` and `test` profiles
28+
# are stored in the `debug` directory, and the `release` and `bench` profiles
29+
# are stored in the `release` directory. User-defined profiles are stored in a
30+
# directory with the same name as the profile".
31+
CARGO_LIB = $(CARGO_TARGET_DIR)/$(RUST_TARGET)/$(patsubst test,debug,$(patsubst dev,debug,$(patsubst bench,release,${CARGO_PROFILE})))/lib$(APPLICATION_RUST_MODULE).a
32+
33+
# Options passed into all Cargo commands, in particular to the build command.
34+
#
35+
# Most of these are populated by RIOT modules that are backed by Rust. Popular
36+
# options added by the user are `-Zbuild-std=core` (only available on nightly)
37+
# to apply LTO and profile configuration to the core library.
38+
CARGO_OPTIONS ?=

makefiles/cargo-targets.inc.mk

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,30 @@ $(CARGO_COMPILE_COMMANDS): $(BUILDDEPS)
4242

4343

4444
$(CARGO_LIB): $(RIOTBUILD_CONFIG_HEADER_C) $(BUILDDEPS) $(CARGO_COMPILE_COMMANDS) FORCE
45-
$(Q)command -v cargo >/dev/null || ($(COLOR_ECHO) \
45+
@command -v cargo >/dev/null || ($(COLOR_ECHO) \
4646
'$(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>.' ;\
4747
exit 1)
48-
$(Q)command -v $${C2RUST:-c2rust} >/dev/null || ($(COLOR_ECHO) \
48+
@command -v $${C2RUST:-c2rust} >/dev/null || ($(COLOR_ECHO) \
4949
'$(COLOR_RED)Error: `'$${C2RUST:-c2rust}'` command missing to build Rust modules.$(COLOR_RESET) Please install as described on <https://doc.riot-os.org/using-rust.html>.' ;\
5050
exit 1)
51-
$(Q)command -v rustup >/dev/null || ($(COLOR_ECHO) \
51+
@command -v rustup >/dev/null || ($(COLOR_ECHO) \
5252
'$(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.' ;\
5353
exit 1)
54-
$(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)
55-
$(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.
56-
$(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.
57-
$(Q)[ x"$(findstring build-std,$(CARGO_OPTIONS))" != x"" ] || \
58-
(rustup component list $(patsubst %,--toolchain %,$(CARGO_CHANNEL)) --installed | grep 'rust-std-$(RUST_TARGET)$$' -q) || \
54+
@[ 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)
55+
@# 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.
56+
@# 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.
57+
@[ x"$(findstring build-std,$(CARGO_OPTIONS))" != x"" ] || \
58+
(rustup component list --installed | grep 'rust-std-$(RUST_TARGET)$$' -q) || \
5959
($(COLOR_ECHO) \
60-
'$(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`.'; \
60+
'$(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`.'; \
6161
exit 1)
62-
$(Q)# finally call out to cargo. mind the "+" to pass down make's jobserver.
62+
@# finally call out to cargo. mind the "+" to pass down make's jobserver.
6363
$(Q)+ CC= CFLAGS= CPPFLAGS= CXXFLAGS= \
6464
RIOT_COMPILE_COMMANDS_JSON="$(CARGO_COMPILE_COMMANDS)" \
65-
RIOT_USEMODULE="$(USEMODULE)" \
66-
cargo $(patsubst +,,+${CARGO_CHANNEL}) \
65+
cargo \
6766
build \
6867
--target $(RUST_TARGET) \
69-
`if [ x$(CARGO_PROFILE) = xrelease ]; then echo --release; else if [ x$(CARGO_PROFILE) '!=' xdebug ]; then echo "--profile $(CARGO_PROFILE)"; fi; fi` \
68+
--profile $(CARGO_PROFILE) \
7069
$(CARGO_OPTIONS)
7170

7271
$(APPLICATION_RUST_MODULE).module: $(CARGO_LIB) FORCE

makefiles/info.inc.mk

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ info-build:
9090
@echo -e 'CXXEXFLAGS:$(patsubst %, \n\t%, $(CXXEXFLAGS))'
9191
@echo ''
9292
@echo 'RUST_TARGET: $(RUST_TARGET)'
93-
@echo 'CARGO_CHANNEL: $(CARGO_CHANNEL)'
9493
@echo 'CARGO_PROFILE: $(CARGO_PROFILE)'
9594
@echo 'CARGO_OPTIONS: $(CARGO_OPTIONS)'
9695
@echo ''
@@ -250,5 +249,10 @@ info-programmers-supported:
250249
@echo $(sort $(PROGRAMMERS_SUPPORTED))
251250

252251
info-rust:
253-
cargo $(patsubst +,,+${CARGO_CHANNEL}) version
252+
cargo version
254253
c2rust --version
254+
@echo "To use this setup of Rust in an IDE, add these command line arguments to the \`cargo check\` or \`rust-analyzer\`:"
255+
@echo " --target $(RUST_TARGET) --profile $(CARGO_PROFILE)"
256+
@echo "and export these environment variables:"
257+
@echo " RIOT_COMPILE_COMMANDS_JSON=\"$(CARGO_COMPILE_COMMANDS)\""
258+
@echo " RIOTBUILD_CONFIG_HEADER_C=\"$(RIOTBUILD_CONFIG_HEADER_C)\""

sys/rust_riotmodules_standalone/Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/rust_libs/Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ USEMODULE += shell_democommands
55

66
FEATURES_REQUIRED += rust_target
77

8-
# Testing on stable to ensure that no nightly features are needed when Rust is
9-
# pulled in through modules.
10-
CARGO_CHANNEL = stable
11-
128
# Currently unknown, something related to the LED_PORT definition that doesn't
139
# pass C2Rust's transpilation
1410
BOARD_BLACKLIST := ek-lm4f120xl

0 commit comments

Comments
 (0)