Skip to content

Commit

Permalink
Rust: Add crates-to-module adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Nov 15, 2021
1 parent ef1c3a1 commit 73f7489
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
7 changes: 5 additions & 2 deletions makefiles/cargo-settings.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#
# This does not have a sane default, and needs to be set in the architecture
# files.
# CARGO_TARGET = ...
#
# It is set by the architecture (and thus eventually the CPU), and exported to
# be available when building Rust modules.
export CARGO_TARGET

# Flags that need to be added to the RIOT_CFLAGS passed to cargo in order to
# make bindgen happy
Expand All @@ -22,5 +25,5 @@ CARGO_PROFILE ?= release
# override this here or in rustup)
CARGO_CHANNEL ?=

# The single Rust library to be built.
# The single Rust library to be built for the application
CARGO_LIB = target/$(CARGO_TARGET)/${CARGO_PROFILE}/lib$(APPLICATION_RUST_MODULE).a
15 changes: 15 additions & 0 deletions sys/rust_lib_catchall/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "rust_lib_catchall"
version = "0.1.0"
authors = ["Christian Amsüss <chrysn@fsfe.org>"]
edition = "2018"
resolver = "2"

[lib]
crate-type = ["staticlib"]

[dependencies]
# Provides the panic_handler
riot-wrappers = "0.7"

riot-shell-commands = { git = "https://gitlab.com/etonomy/riot-module-examples", optional = true }
34 changes: 34 additions & 0 deletions sys/rust_lib_catchall/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
include $(RIOTBASE)/Makefile.base

# FIXME: These are the defaults; I guess we'll need to pass down everything, and not just CARGO_TARGET
include ../../makefiles/cargo-settings.inc.mk

# The single Rust library to be built collection modules
# Not placing this in $(BINDIR)/rust_lib_catchall/target/... as that'd mean
# that the find in _LINK can catch on to some .o files not intended to be found
CARGO_LIB_MODULES = $(BINDIR)/rust_lib_catchall-target/$(CARGO_TARGET)/${CARGO_PROFILE}/librust_lib_catchall.a

# FIXME: This is the point where any RIOT modules written in Rust (which would
# be pseudomodules depending on this) are turned into a list of features to be
# enabled
CARGO_OPTIONS += --features riot-shell-commands

$(CARGO_LIB_MODULES): $(RIOTBUILD_CONFIG_HEADER_C) $(BUILDDEPS) FORCE
$(Q)[ x"${CARGO_TARGET}" != x"" ] || (echo "Error: No CARGO_TARGET was set for this platform"; exit 1)
# differenct to standalone: this uses only CFLAGS and not
# CFLAGS_WITGH_MACROS, and then hacks the modules back from the
# exported USEMODULE variable (which may generally be a good idea until
# compile-commands are available)
$(Q)CC= CFLAGS= CPPFLAGS= CXXFLAGS= RIOT_CC="${CC}" RIOT_CFLAGS="$(CFLAGS) $(patsubst %,-DMODULE_%,$(USEMODULE)) $(INCLUDES) $(CARGO_EXTRACFLAGS)" cargo +$(CARGO_CHANNEL) build --target $(CARGO_TARGET) `if [ x$(CARGO_PROFILE) = xrelease ]; then echo --release; else if [ x$(CARGO_PROFILE) '!=' xdebug ]; then echo "--profile $(CARGO_PROFILE)"; fi; fi` $(CARGO_OPTIONS) --target-dir $(BINDIR)/rust_lib_catchall-target

rust_lib_catchall.module: $(CARGO_LIB_MODULES) FORCE
$(Q)# Ensure no old object files persist. These would lead to duplicate
$(Q)# symbols, or worse, lingering behaivor of XFA entries.
$(Q)rm -rf $(BINDIR)/rust_lib_catchall/
$(Q)mkdir -p $(BINDIR)/rust_lib_catchall/
# difference to standalone: no PWD as it' an absolute path
$(Q)cd $(BINDIR)/rust_lib_catchall/ && $(AR) x $<

FORCE:

.PHONY: FORCE
11 changes: 11 additions & 0 deletions sys/rust_lib_catchall/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![no_std]

// As we're pulling all crates in only for their side effects of having symbols (be they required
// on the Rust side like riot_wrappers' panic_handler, or to be used by RIOT like the XFA symbols
// emittted by riot-shell-commands), all these crates have to be extern-crate'd to be pulled in
// even though they're note used on the language level.

extern crate riot_wrappers;

#[cfg(feature = "riot-shell-commands")]
extern crate riot_shell_commands;

0 comments on commit 73f7489

Please sign in to comment.