summaryrefslogtreecommitdiffstats
path: root/tools/rust/Makefile
blob: 5be4dd9ea8f17ce8ee9bf35ad5932b09083722d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# 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))