[03/10] make.sh: Add some commands to build Rust packages

Message ID 20220201135246.4096955-3-michael.tremer@ipfire.org
State Accepted
Commit 4f2a9813c00aac7cdeb2fd85bd5ead941d7528f7
Headers
Series [01/10] make.sh: Set a good default for rustc flags |

Commit Message

Michael Tremer Feb. 1, 2022, 1:52 p.m. UTC
  This is a small set of commands that will be needed to build Rust
packages.

The idea is to have a couple of macros which do not have to rewritten,
but can be customised across the lfs files.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
---
 config/rootfiles/common/aarch64/rust |  1 +
 config/rootfiles/common/armv6l/rust  |  1 +
 config/rootfiles/common/x86_64/rust  |  1 +
 lfs/Config                           | 80 +++++++++++++++++++++++++++-
 lfs/rust                             |  3 ++
 5 files changed, 84 insertions(+), 2 deletions(-)
  

Patch

diff --git a/config/rootfiles/common/aarch64/rust b/config/rootfiles/common/aarch64/rust
index c26d4591b..f51308f7a 100644
--- a/config/rootfiles/common/aarch64/rust
+++ b/config/rootfiles/common/aarch64/rust
@@ -64,6 +64,7 @@ 
 #usr/lib/rustlib/rust-installer-version
 #usr/lib/rustlib/uninstall.sh
 #usr/libexec/cargo-credential-1password
+#usr/share/cargo/registry
 #usr/share/doc/cargo
 #usr/share/doc/cargo/LICENSE-APACHE
 #usr/share/doc/cargo/LICENSE-MIT
diff --git a/config/rootfiles/common/armv6l/rust b/config/rootfiles/common/armv6l/rust
index ffe8318ab..7b374d94d 100644
--- a/config/rootfiles/common/armv6l/rust
+++ b/config/rootfiles/common/armv6l/rust
@@ -59,6 +59,7 @@ 
 #usr/lib/rustlib/rust-installer-version
 #usr/lib/rustlib/uninstall.sh
 #usr/libexec/cargo-credential-1password
+#usr/share/cargo/registry
 #usr/share/doc/cargo
 #usr/share/doc/cargo/LICENSE-APACHE
 #usr/share/doc/cargo/LICENSE-MIT
diff --git a/config/rootfiles/common/x86_64/rust b/config/rootfiles/common/x86_64/rust
index 6f4c7fd45..5b5080e00 100644
--- a/config/rootfiles/common/x86_64/rust
+++ b/config/rootfiles/common/x86_64/rust
@@ -62,6 +62,7 @@ 
 #usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunicode_width-ca628c5eca5e5caf.rlib
 #usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-5c4d6c9d7595f844.rlib
 #usr/libexec/cargo-credential-1password
+#usr/share/cargo/registry
 #usr/share/doc/cargo
 #usr/share/doc/cargo/LICENSE-APACHE
 #usr/share/doc/cargo/LICENSE-MIT
diff --git a/lfs/Config b/lfs/Config
index 76a07b088..d7a59c5a6 100644
--- a/lfs/Config
+++ b/lfs/Config
@@ -34,6 +34,18 @@ 
 unexport BUILD_ARCH BUILD_PLATFORM BUILDTARGET CROSSTARGET TOOLCHAIN TOOLS_DIR
 unexport XZ_OPT
 
+# Basic Variables
+EMPTY :=
+COMMA := ,
+SPACE := $(EMPTY) $(EMPTY)
+define NEWLINE
+
+
+endef
+
+# Basic Functions
+join-with	= $(subst $(SPACE),$(1),$(strip $(2)))
+
 PARALLELISM = $(shell echo $$( \
 	if [ -n "$(MAX_PARALLELISM)" ] && [ $(MAX_PARALLELISM) -lt 1 ]; then \
 		echo 1 ; \
@@ -140,7 +152,71 @@  ifeq "$(BUILD_ARCH)" "aarch64"
 endif
 
 # Rust
-export CARGOPATH = $(HOME)/.cargo
+CARGO_PATH     = $(DIR_APP)/.cargo
+CARGO_REGISTRY = /usr/share/cargo/registry
+
+CRATE_NAME = $(patsubst rust-%,%,$(firstword $(MAKEFILE_LIST)))
+CRATE_VER  = $(VER)
+CRATE_PATH = $(CARGO_REGISTRY)/$(CRATE_NAME)-$(CRATE_VER)
+
+define CARGO_CONFIG
+[build]
+rustflags = [$(call join-with,$(COMMA)$(SPACE),$(foreach flag,$(RUSTFLAGS),"$(flag)"))]
+
+[env]
+CFLAGS = "$(CFLAGS)"
+CXXFLAGS = "$(CXXFLAGS)"
+LDFLAGS = "$(LDFLAGS)"
+
+[term]
+verbose = true
+
+[source]
+
+[source.local-registry]
+directory = "$(CARGO_REGISTRY)"
+
+[source.crates-io]
+registry = "https://crates.io"
+replace-with = "local-registry"
+endef
+export CARGO_CONFIG
+
+CARGO = \
+	CARGOPATH=$(CARGO_PATH) \
+	cargo
+
+define CARGO_PREPARE
+	mkdir -p $(CARGO_PATH) && \
+	echo "$${CARGO_CONFIG}" > $(CARGO_PATH)/config && \
+	rm -f Cargo.lock
+endef
+
+CARGO_BUILD = \
+	$(CARGO) \
+	build \
+	$(MAKETUNING) \
+	--release
+
+# Checks whether this crate has a right taregt
+CARGO_TARGET_CHECK = cargo metadata --format-version 1 | \
+	jq -e ".packages[].targets[].kind | any(. == \"$(1)\")" | grep -q "true"
+
+define CARGO_INSTALL
+	mkdir -pv "$(CRATE_PATH)" && \
+	if $(call CARGO_TARGET_CHECK,lib); then \
+		awk \
+			'/^\\\[((.+\\\.)?((dev|build)-)?dependencies|features)/{f=1;next} /^\\\[/{f=0}; !f' \
+			< Cargo.toml > Cargo.toml.deps && \
+		$(CARGO) package -l | grep -wEv "Cargo.(lock|toml.orig)" \
+			| xargs -d "\n" cp --parents -a -t $(CRATE_PATH) && \
+		install -v -m 644 Cargo.toml.deps $(CRATE_PATH)/Cargo.toml && \
+		echo "{\"files\":{},\"package\":\"\"}" > $(CRATE_PATH)/.cargo-checksum.json; \
+	fi && \
+	if $(call CARGO_TARGET_CHECK,bin); then \
+		$(CARGO) install --no-track --path .; \
+	fi
+endef
 
 ###############################################################################
 # Common Macro Definitions
@@ -183,7 +259,7 @@  define POSTBUILD
 	@echo "Updating linker cache..."
 	@type -p ldconfig >/dev/null && ldconfig || :
 	@echo "Install done; saving file list to $(TARGET) ..."
-	@rm -rf $(GOPATH) $(CARGOPATH)
+	@rm -rf $(GOPATH)
 	@$(FIND_FILES) > $(DIR_SRC)/lsalrnew
 	@diff $(DIR_SRC)/lsalr $(DIR_SRC)/lsalrnew | grep '^> ' | sed 's/^> //' > $(TARGET)_diff
 	@cp -f $(DIR_SRC)/lsalrnew $(DIR_SRC)/lsalr
diff --git a/lfs/rust b/lfs/rust
index 7ff84379b..2fbc3254e 100644
--- a/lfs/rust
+++ b/lfs/rust
@@ -90,5 +90,8 @@  $(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects))
 	# Remove LLVM tools
 	rm -vf /usr/lib/rustlib/$(RUST_BOOTSTRAP)/bin/rust-ll{d,vm-dwp}
 
+	# Create local registry
+	mkdir -pv $(CARGO_REGISTRY)
+
 	@rm -rf $(DIR_APP)
 	@$(POSTBUILD)