Files
openwrt/tools/rust/Makefile
T
Daniel Golle 32f1c26eb6 build: add Rust package build helpers
Add rust-package.mk for target packages written in Rust. The Cargo
environment carries the target triple, CPU flags, the C and C++ flags
for cc-rs, the linker and the ABI-specific sysroot of libstd-rust,
while native build scripts and proc macros keep the host sysroot and
linker. Builds run in the crate's working directory with an explicit
target and offline from the vendor tarball; with a source tree override
(git-src, USE_SOURCE_DIR), which has none, Cargo fetches the locked
crates. Target packages depend on libstd-rust at build time and track
its configuration inputs for rebuilds.

rustc runs the linker with arguments of its own, without TARGET_LDFLAGS.
The linker selection and the -z options that hardening.mk and
package.mk set for every package are passed to rustc, for packages and
for libstd-rust.

When the target triple is the build host's, cc-rs takes the host
compiler for target C code as well. For that triple the C and C++
compiler of cc-rs is scripts/rust-cc-wrapper.sh, linked into
$(STAGING_DIR_HOST)/bin by tools/rust. It runs the target compiler with
the target flags for build scripts whose OUT_DIR lies in the target
triple's directory, and the host compiler for the others.

RUST_PREFER_DYNAMIC is available with any Rust toolchain, controls the
default userspace standard-library linkage and defaults to shared
linking. Packages can override it. Release builds use LTO either way;
rustc runs LTO next to -Cprefer-dynamic only with the unstable
-Zdylib-lto. RUST_PKG_DEPENDS carries the architecture dependencies
and, with shared linking, the libstd-rust runtime dependency. For
packages that keep the default, that dependency is conditional on
RUST_PREFER_DYNAMIC, as .config is not read while package metadata is
collected.

Release builds emit no unwind tables and take the size options of
unstable rustc that libstd-rust is built with: panics without location,
derived Debug output reduced to type names and, with
RUST_PANIC_IMMEDIATE_ABORT, -Cpanic=immediate-abort. Permitting unstable
options for every crate with RUSTC_BOOTSTRAP=1 changes the code of
dependencies: the build scripts of anyhow, thiserror and proc-macro2
then detect unstable features and use them.
scripts/rust-target-wrapper.sh, the RUSTC_WRAPPER of package builds,
adds the options, and -Zdylib-lto with a shared libstd, only to the
target crates cargo compiles, recognised by --target, -C metadata and
--error-format=json, and permits unstable options for that crate alone.
Build scripts and their probes see neither. A package keeps panic
locations with RUST_PKG_LOCATION_DETAIL:=1 and selects the Debug output
with RUST_PKG_FMT_DEBUG. None of this applies with CONFIG_DEBUG.
Packages build the release profile, as maturin, CMake and autotools
projects do, and CONFIG_DEBUG gives it the settings of the dev profile,
ahead of the profile settings of the package. Build paths are mapped out
of packages as out of libstd-rust.

Without a Rust toolchain of the build system, Rust packages build with
rust/host of the packages feed, which installs to $(STAGING_DIR)/host,
so that packages can move from the Rust support of the packages feed
one at a time. RUSTC and CARGO point there, there is neither a sysroot
of libstd-rust nor a shared libstd, the mips64 triples are those its
std is built for, and on the build host's triple the target linker is
set through the Cargo variable. The build dependencies, on libstd-rust
or rust/host, follow USE_RUST_TOOLCHAIN in the package metadata, also
for host builds through rust-host-build.mk. After a switch from None to
a Rust toolchain, the cargo of rust/host still comes first in PATH, so
the help of the toolchain choice names its clean-up.

The file is derived from lang/rust in the packages feed by Luca Barbato
and Donald Hoskins.

Reported-by: Andre Heider <a.heider@gmail.com>
Co-developed-by: Brian Redbeard <redbeard@dead-city.org>
Signed-off-by: Brian Redbeard <redbeard@dead-city.org>
Co-developed-by: Aditya Nugraha <vortexilation@gmail.com>
Signed-off-by: Aditya Nugraha <vortexilation@gmail.com>
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
2026-10-30 12:06:08 +02:00

235 lines
7.0 KiB
Makefile

# SPDX-License-Identifier: GPL-2.0-only
#
# Copyright (C) 2023 Luca Barbato and Donald Hoskins
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/rust-values.mk
PKG_NAME:=rust
PKG_VERSION:=$(RUST_VERSION)
PKG_RELEASE:=1
PKG_LICENSE:=Apache-2.0 MIT
PKG_LICENSE_FILES:=LICENSE-APACHE LICENSE-MIT
PKG_CPE_ID:=cpe:/a:rust-lang:rust
ifdef CONFIG_USE_RUST_BUILD
PKG_SOURCE:=rustc-$(PKG_VERSION)-src.tar.xz
PKG_SOURCE_URL:=https://static.rust-lang.org/dist/
PKG_HASH:=$(RUST_SRC_TARBALL_HASH)
endif
# All modes share one build directory, so that a mode change prepares anew.
HOST_PREPARED_DEPENDS:= \
CONFIG_USE_RUST_BUILD CONFIG_USE_RUST_PREBUILT CONFIG_USE_RUST_HOST \
CONFIG_RUST_TOOLCHAIN_HOST_PATH
include $(INCLUDE_DIR)/host-build.mk
TAR_OPTIONS+= --strip-components 1
TAR_CMD=$(HOST_TAR) -C $(1) $(TAR_OPTIONS)
RUST_PREFIX_DIR:=$(HOST_BUILD_DIR)/prefix
RUST_FILE_LIST:=$(STAGING_DIR_HOST)/stamp/.rust_files
RUST_DIR_LIST:=$(STAGING_DIR_HOST)/stamp/.rust_dirs
RUST_CC_WRAPPER:=$(subst $(space),,$(foreach dir, \
$(subst /, ,$(patsubst $(TOPDIR)/%,%,$(STAGING_DIR_HOST)/bin)),../))scripts/rust-cc-wrapper.sh
# The rust-installer manifests name absolute paths, which its uninstaller
# and the next install.sh into the same prefix delete.
RUST_INSTALLER_DATA:=$(addprefix $(RUST_PREFIX_DIR)/lib/rustlib/, \
components install.log uninstall.sh rust-installer-version manifest-*)
define Host/Install/RemoveInstallerData
rm -f $(RUST_INSTALLER_DATA)
endef
# Cargo reads .cargo/config.toml in every parent of its working directory.
define Host/CheckCargoConfig
dir="$$$$(cd $(TOPDIR) && pwd -P)"; \
while :; do \
for file in "$$$$dir/.cargo/config" "$$$$dir/.cargo/config.toml"; do \
[ ! -e "$$$$file" ] || { echo "Cargo would read $$$$file"; exit 1; }; \
done; \
[ "$$$$dir" != / ] || break; \
dir="$$$$(dirname "$$$$dir")"; \
done
endef
Hooks/HostPrepare/Pre += Host/CheckCargoConfig
define Host/Uninstall
if [ -f $(RUST_FILE_LIST) ]; then \
cd $(STAGING_DIR_HOST) && \
while read -r file; do rm -f "$$$$file"; done < $(RUST_FILE_LIST) && \
while read -r dir; do rmdir "$$$$dir" 2>/dev/null || true; done < $(RUST_DIR_LIST); \
fi
rm -f $(RUST_FILE_LIST) $(RUST_DIR_LIST)
endef
ifdef CONFIG_USE_RUST_BUILD
HOST_CONFIGURE_VARS += CARGO_HOME="$(CARGO_HOME)"
# Only the host compiler is built here; libstd-rust builds the cross rust-std.
HOST_CONFIGURE_ARGS = \
--build=$(RUSTC_HOST_ARCH) \
--target=$(RUSTC_HOST_ARCH) \
--host=$(RUSTC_HOST_ARCH) \
--prefix=$(STAGING_DIR_HOST) \
--bindir=$(STAGING_DIR_HOST)/bin \
--libdir=$(STAGING_DIR_HOST)/lib \
--sysconfdir=$(STAGING_DIR_HOST)/etc \
--datadir=$(STAGING_DIR_HOST)/share \
--mandir=$(STAGING_DIR_HOST)/man \
--dist-compression-formats=gz \
--disable-sanitizers \
--release-channel=stable \
--enable-cargo-native-static \
--bootstrap-cache-path=$(DL_DIR)/rustc \
"--set=llvm.targets=$(RUST_LLVM_TARGETS)" \
"--set=llvm.experimental-targets=$(RUST_LLVM_EXPERIMENTAL_TARGETS)" \
--set=llvm.download-ci-llvm=false
# bootstrap leaves the job count of ninja and cargo to the jobserver named in
# MAKEFLAGS, and runs one job per CPU without one.
define Host/Compile
+CARGO_HOME=$(CARGO_HOME) \
MAKEFLAGS="$(MAKE_JOBSERVER)" \
$(PYTHON) $(HOST_BUILD_DIR)/x.py \
--build-dir $(HOST_BUILD_DIR)/build \
$(if $(MAKE_JOBSERVER),,--jobs 1) \
dist rustc cargo rust-std llvm-tools rust-src
endef
define Host/Install/Prefix
rm -rf $(RUST_PREFIX_DIR)
( \
cd $(HOST_BUILD_DIR)/build/dist ; \
for targz in rustc-* cargo-* rust-std-* llvm-tools-* rust-src-*.tar.gz; do \
[ -f "$$$$targz" ] || continue; \
$(STAGING_DIR_HOST)/bin/libdeflate-gzip -dc "$$$$targz" | tar -xf - ; \
done ; \
find . -mindepth 2 -maxdepth 2 -type f -name install.sh \
-execdir bash '{}' --prefix=$(RUST_PREFIX_DIR) --disable-ldconfig \; ; \
)
endef
else
RUST_UNPACK_DIR:=$(HOST_BUILD_DIR)/unpacked
$(if $(RUST_RUSTC_HASH),,$(error No prebuilt Rust components for the build host $(RUSTC_HOST_ARCH)))
define Download/rust-src
FILE:=rust-src-$(PKG_VERSION).tar.xz
URL:=$(RUST_DIST_URL)
HASH:=$(RUST_SRC_HASH)
endef
$(eval $(call Download,rust-src))
define Download/rust-llvm-tools
FILE:=llvm-tools-$(PKG_VERSION)-$(RUSTC_HOST_ARCH).tar.xz
URL:=$(RUST_DIST_URL)
HASH:=$(RUST_LLVM_TOOLS_HASH)
endef
$(eval $(call Download,rust-llvm-tools))
RUST_DIST_FILES:= \
$(DL_DIR)/rust-src-$(PKG_VERSION).tar.xz \
$(DL_DIR)/llvm-tools-$(PKG_VERSION)-$(RUSTC_HOST_ARCH).tar.xz
ifdef CONFIG_USE_RUST_PREBUILT
define Download/rustc
FILE:=rustc-$(PKG_VERSION)-$(RUSTC_HOST_ARCH).tar.xz
URL:=$(RUST_DIST_URL)
HASH:=$(RUST_RUSTC_HASH)
endef
$(eval $(call Download,rustc))
define Download/cargo
FILE:=cargo-$(PKG_VERSION)-$(RUSTC_HOST_ARCH).tar.xz
URL:=$(RUST_DIST_URL)
HASH:=$(RUST_CARGO_HASH)
endef
$(eval $(call Download,cargo))
define Download/rust-std
FILE:=rust-std-$(PKG_VERSION)-$(RUSTC_HOST_ARCH).tar.xz
URL:=$(RUST_DIST_URL)
HASH:=$(RUST_STD_HASH)
endef
$(eval $(call Download,rust-std))
RUST_DIST_FILES+= \
$(DL_DIR)/rustc-$(PKG_VERSION)-$(RUSTC_HOST_ARCH).tar.xz \
$(DL_DIR)/cargo-$(PKG_VERSION)-$(RUSTC_HOST_ARCH).tar.xz \
$(DL_DIR)/rust-std-$(PKG_VERSION)-$(RUSTC_HOST_ARCH).tar.xz
define Host/Prepare/Toolchain
endef
else
RUST_HOST_SYSROOT:=$(or $(call qstrip,$(CONFIG_RUST_TOOLCHAIN_HOST_PATH)),$(shell PATH=$(ORIG_PATH) rustc --print sysroot 2>/dev/null))
define Host/Prepare/Toolchain
[ "$$$$($(RUST_HOST_SYSROOT)/bin/rustc --version | cut -d' ' -f2)" = "$(PKG_VERSION)" ] || { \
echo "rustc in '$(RUST_HOST_SYSROOT)' is not version $(PKG_VERSION)"; \
exit 1; \
}
$(INSTALL_DIR) $(RUST_PREFIX_DIR)/bin
$(INSTALL_BIN) $(RUST_HOST_SYSROOT)/bin/rustc $(RUST_HOST_SYSROOT)/bin/cargo \
$(RUST_PREFIX_DIR)/bin/
$(CP) $(RUST_HOST_SYSROOT)/lib $(RUST_PREFIX_DIR)/
endef
define Host/Install/Check
[ "$$$$($(STAGING_DIR_HOST)/bin/rustc --print sysroot)" = "$(shell realpath $(STAGING_DIR_HOST))" ] || { \
echo "rustc from '$(RUST_HOST_SYSROOT)' is not relocatable, use a rustup toolchain or an unpacked official Rust distribution"; \
exit 1; \
}
endef
endif
# install.sh renames every file it finds in the prefix to *.old, so the host
# toolchain is copied over the installed components.
define Host/Prepare
$(INSTALL_DIR) $(RUST_PREFIX_DIR) $(RUST_UNPACK_DIR)
for tarball in $(RUST_DIST_FILES); do \
tar -xf "$$$$tarball" -C $(RUST_UNPACK_DIR); \
done
for installer in $(RUST_UNPACK_DIR)/*/install.sh; do \
$(BASH) "$$$$installer" --prefix=$(RUST_PREFIX_DIR) --disable-ldconfig; \
done
$(call Host/Prepare/Toolchain)
endef
define Host/Patch
endef
define Host/Configure
endef
define Host/Compile
endef
endif
define Host/Install
$(call Host/Install/Prefix)
$(call Host/Install/RemoveInstallerData)
ln -sf $(RUST_CC_WRAPPER) $(RUST_PREFIX_DIR)/bin/rust-cc-wrapper
ln -sf $(RUST_CC_WRAPPER) $(RUST_PREFIX_DIR)/bin/rust-cxx-wrapper
$(call Host/Uninstall)
$(INSTALL_DIR) $(STAGING_DIR_HOST)/stamp
cd $(RUST_PREFIX_DIR) && \
find . ! -type d > $(RUST_FILE_LIST) && \
find . -mindepth 1 -depth -type d > $(RUST_DIR_LIST)
$(CP) $(RUST_PREFIX_DIR)/. $(STAGING_DIR_HOST)/
$(call Host/Install/Check)
endef
$(eval $(call HostBuild))