Skip to content

Commit 6bd4b0e

Browse files
authored
Merge pull request #179 from REG-Linux/add-pkg-rust-infrastructure
Add REG specific pkg-rust infrastructure
2 parents 5699074 + b0e68dd commit 6bd4b0e

File tree

2 files changed

+311
-0
lines changed

2 files changed

+311
-0
lines changed

package/Makefile.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,5 @@ include package/pkg-golang.mk
466466
include package/pkg-meson.mk
467467
include package/pkg-qmake.mk
468468
include package/pkg-cargo.mk
469+
# REG add rust pkg infrastructure
470+
include package/pkg-rust.mk

package/pkg-rust.mk

Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
################################################################################
2+
#
3+
# REG-Linux
4+
#
5+
################################################################################
6+
# Rust package infrastructure
7+
#
8+
# This file implements an infrastructure that eases development of package
9+
# .mk files for Rust packages. It should be used for all Rust packages that use
10+
# Cargo as their build system.
11+
#
12+
# See the Buildroot documentation for details on the usage of this
13+
# infrastructure
14+
#
15+
# In terms of implementation, this Cargo infrastructure requires the .mk file
16+
# to only specify metadata information about the package: name, version,
17+
# download URL, etc.
18+
#
19+
# We still allow the package .mk file to override what the different steps
20+
# are doing, if needed. For example, if <PKG>_BUILD_CMDS is already defined,
21+
# it is used as the list of commands to perform to build the package,
22+
# instead of the default Cargo behaviour. The package can also define some
23+
# post operation hooks.
24+
#
25+
################################################################################
26+
27+
BR_CARGO_HOME = $(DL_DIR)/br-cargo-home
28+
29+
PKG_COMMON_RUST_ENV = \
30+
CARGO_HOME=$(BR_CARGO_HOME)
31+
32+
# __CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS is needed to allow
33+
# passing the -Z target-applies-to-host, which is needed together with
34+
# CARGO_TARGET_APPLIES_TO_HOST to fix build problems when target
35+
# architecture == host architecture.
36+
37+
# __CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS="nightly" is to allow
38+
# using nighly features on stable releases, i.e features that are not
39+
# yet considered stable.
40+
#
41+
# CARGO_UNSTABLE_HOST_CONFIG="true" enables the host specific
42+
# configuration feature
43+
#
44+
# CARGO_UNSTABLE_TARGET_APPLIES_TO_HOST="true" enables the nightly
45+
# configuration option target-applies-to-host value to be set
46+
#
47+
# CARGO_TARGET_APPLIES_TO_HOST="false" is actually setting the value
48+
# for this feature, which we disable, to make sure builds where target
49+
# arch == host arch work correctly
50+
PKG_RUST_ENV = \
51+
$(PKG_COMMON_RUST_ENV) \
52+
__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS="nightly" \
53+
CARGO_UNSTABLE_HOST_CONFIG="true" \
54+
CARGO_UNSTABLE_TARGET_APPLIES_TO_HOST="true" \
55+
CARGO_TARGET_APPLIES_TO_HOST="false" \
56+
CARGO_BUILD_TARGET="$(RUSTC_TARGET_NAME)" \
57+
CARGO_HOST_RUSTFLAGS="$(addprefix -C link-args=,$(HOST_LDFLAGS))" \
58+
CARGO_TARGET_$(call UPPERCASE,$(RUSTC_TARGET_NAME))_LINKER=$(notdir $(TARGET_CROSS))gcc
59+
60+
# We always set both CARGO_PROFILE_DEV and CARGO_PROFILE_RELEASE
61+
# as we are unable to select a build profile using the environment.
62+
#
63+
# Other cargo profiles generally derive from these two profiles.
64+
65+
# Disable incremental compilation to match release default.
66+
#
67+
# Set codegen-units to release default.
68+
#
69+
# Set split-debuginfo to default off for ELF platforms.
70+
PKG_RUST_ENV += \
71+
CARGO_PROFILE_DEV_INCREMENTAL="false" \
72+
CARGO_PROFILE_RELEASE_INCREMENTAL="false" \
73+
CARGO_PROFILE_DEV_CODEGEN_UNITS="16" \
74+
CARGO_PROFILE_RELEASE_CODEGEN_UNITS="16" \
75+
CARGO_PROFILE_DEV_SPLIT_DEBUGINFO="off" \
76+
CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO="off"
77+
78+
# Set the optimization level with the release default as fallback.
79+
ifeq ($(BR2_OPTIMIZE_0),y)
80+
PKG_RUST_ENV += \
81+
CARGO_PROFILE_DEV_OPT_LEVEL="0" \
82+
CARGO_PROFILE_RELEASE_OPT_LEVEL="0"
83+
else ifeq ($(BR2_OPTIMIZE_1),y)
84+
PKG_RUST_ENV += \
85+
CARGO_PROFILE_DEV_OPT_LEVEL="1" \
86+
CARGO_PROFILE_RELEASE_OPT_LEVEL="1"
87+
else ifeq ($(BR2_OPTIMIZE_2),y)
88+
PKG_RUST_ENV += \
89+
CARGO_PROFILE_DEV_OPT_LEVEL="2" \
90+
CARGO_PROFILE_RELEASE_OPT_LEVEL="2"
91+
else ifeq ($(BR2_OPTIMIZE_3),y)
92+
PKG_RUST_ENV += \
93+
CARGO_PROFILE_DEV_OPT_LEVEL="3" \
94+
CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
95+
else ifeq ($(BR2_OPTIMIZE_G),y)
96+
PKG_RUST_ENV += \
97+
CARGO_PROFILE_DEV_OPT_LEVEL="0" \
98+
CARGO_PROFILE_RELEASE_OPT_LEVEL="0"
99+
else ifeq ($(BR2_OPTIMIZE_S),y)
100+
PKG_RUST_ENV += \
101+
CARGO_PROFILE_DEV_OPT_LEVEL="s" \
102+
CARGO_PROFILE_RELEASE_OPT_LEVEL="s"
103+
else ifeq ($(BR2_OPTIMIZE_FAST),y)
104+
PKG_RUST_ENV += \
105+
CARGO_PROFILE_DEV_OPT_LEVEL="3" \
106+
CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
107+
else
108+
PKG_RUST_ENV += \
109+
CARGO_PROFILE_DEV_OPT_LEVEL="3" \
110+
CARGO_PROFILE_RELEASE_OPT_LEVEL="3"
111+
endif
112+
113+
ifeq ($(BR2_ENABLE_LTO),y)
114+
PKG_RUST_ENV += \
115+
CARGO_PROFILE_DEV_LTO="true" \
116+
CARGO_PROFILE_RELEASE_LTO="true"
117+
else
118+
PKG_RUST_ENV += \
119+
CARGO_PROFILE_DEV_LTO="false" \
120+
CARGO_PROFILE_RELEASE_LTO="false"
121+
endif
122+
123+
124+
ifeq ($(BR2_ENABLE_DEBUG),y)
125+
ifeq ($(BR2_DEBUG_3),y)
126+
# full debug info
127+
PKG_RUST_ENV += \
128+
CARGO_PROFILE_DEV_DEBUG="2" \
129+
CARGO_PROFILE_RELEASE_DEBUG="2"
130+
else
131+
# line tables only
132+
PKG_RUST_ENV += \
133+
CARGO_PROFILE_DEV_DEBUG="1" \
134+
CARGO_PROFILE_RELEASE_DEBUG="1"
135+
endif
136+
else
137+
# no debug info
138+
PKG_RUST_ENV += \
139+
CARGO_PROFILE_DEV_DEBUG="0" \
140+
CARGO_PROFILE_RELEASE_DEBUG="0"
141+
endif
142+
143+
# Enabling debug-assertions enables the runtime debug_assert! macro.
144+
#
145+
# Enabling overflow-checks enables runtime panic on integer overflow.
146+
ifeq ($(BR2_ENABLE_RUNTIME_DEBUG),y)
147+
PKG_RUST_ENV += \
148+
CARGO_PROFILE_DEV_DEBUG_ASSERTIONS="true" \
149+
CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS="true" \
150+
CARGO_PROFILE_DEV_OVERFLOW_CHECKS="true" \
151+
CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS="true"
152+
else
153+
PKG_RUST_ENV += \
154+
CARGO_PROFILE_DEV_DEBUG_ASSERTIONS="false" \
155+
CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS="false" \
156+
CARGO_PROFILE_DEV_OVERFLOW_CHECKS="false" \
157+
CARGO_PROFILE_RELEASE_OVERFLOW_CHECKS="false"
158+
endif
159+
160+
#
161+
# This is a workaround for https://github.com/rust-lang/compiler-builtins/issues/420
162+
# and should be removed when fixed upstream
163+
#
164+
ifeq ($(NORMALIZED_ARCH),arm)
165+
PKG_RUST_ENV += \
166+
CARGO_TARGET_$(call UPPERCASE,$(RUSTC_TARGET_NAME))_RUSTFLAGS="-Clink-arg=-Wl,--allow-multiple-definition"
167+
endif
168+
169+
HOST_PKG_RUST_ENV = \
170+
$(PKG_COMMON_RUST_ENV) \
171+
RUSTFLAGS="$(addprefix -C link-args=,$(HOST_LDFLAGS))"
172+
173+
################################################################################
174+
# inner-cargo-package -- defines how the configuration, compilation and
175+
# installation of a cargo package should be done, implements a few hooks
176+
# to tune the build process for cargo specifities and calls the generic
177+
# package infrastructure to generate the necessary make targets
178+
#
179+
# argument 1 is the lowercase package name
180+
# argument 2 is the uppercase package name, including a HOST_ prefix
181+
# for host packages
182+
# argument 3 is the uppercase package name, without the HOST_ prefix
183+
# for host packages
184+
# argument 4 is the type (target or host)
185+
################################################################################
186+
187+
define inner-cargo-package
188+
189+
# We need host-rustc to run cargo at download time (for vendoring),
190+
# and at build and install time.
191+
$(2)_DOWNLOAD_DEPENDENCIES += host-rustc
192+
$(2)_DEPENDENCIES += host-rustc
193+
194+
$(2)_DOWNLOAD_POST_PROCESS = cargo
195+
$(2)_DL_ENV += CARGO_HOME=$$(BR_CARGO_HOME)
196+
197+
# If building in a sub directory, use that to find the Cargo.toml
198+
ifneq ($$($(2)_SUBDIR),)
199+
$(2)_DOWNLOAD_POST_PROCESS_OPTS += -m$$($(2)_SUBDIR)/Cargo.toml
200+
endif
201+
202+
# Because we append vendored info, we can't rely on the values being empty
203+
# once we eventually get into the generic-package infra. So, we duplicate
204+
# the heuristics here
205+
ifndef $(2)_LICENSE
206+
ifdef $(3)_LICENSE
207+
$(2)_LICENSE = $$($(3)_LICENSE)
208+
endif
209+
endif
210+
211+
# Due to vendoring, it is pretty likely that not all licenses are
212+
# listed in <pkg>_LICENSE. If the license is unset, it is "unknown"
213+
# so adding unknowns to some unknown is still some other unknown,
214+
# so don't append the blurb in that case.
215+
ifneq ($$($(2)_LICENSE),)
216+
$(2)_LICENSE += , vendored dependencies licenses probably not listed
217+
endif
218+
219+
# Note: in all the steps below, we "cd" into the build directory to
220+
# execute the "cargo" tool instead of passing $(@D)/Cargo.toml as the
221+
# manifest-path. Indeed while the latter seems to work, it in fact
222+
# breaks in subtle ways as the way cargo searches for its
223+
# configuration file is based (among other rules) on the current
224+
# directory. This means that if cargo is started outside of a package
225+
# directory, its configuration file will not be taken into account.
226+
#
227+
228+
#
229+
# Build step. Only define it if not already defined by the package .mk
230+
# file.
231+
#
232+
ifndef $(2)_BUILD_CMDS
233+
ifeq ($(4),target)
234+
define $(2)_BUILD_CMDS
235+
cd $$($$(PKG)_SRCDIR) && \
236+
$$(TARGET_MAKE_ENV) \
237+
$$(TARGET_CONFIGURE_OPTS) \
238+
$$(PKG_RUST_ENV) \
239+
$$($(2)_CARGO_ENV) \
240+
cargo build \
241+
$$(if $$(BR2_ENABLE_DEBUG),,--release) \
242+
--manifest-path Cargo.toml \
243+
$$($(2)_CARGO_BUILD_OPTS)
244+
endef
245+
else # ifeq ($(4),target)
246+
define $(2)_BUILD_CMDS
247+
cd $$($$(PKG)_SRCDIR) && \
248+
$$(HOST_MAKE_ENV) \
249+
$$(HOST_CONFIGURE_OPTS) \
250+
$$(HOST_PKG_RUST_ENV) \
251+
$$($(2)_CARGO_ENV) \
252+
cargo build \
253+
--release \
254+
--manifest-path Cargo.toml \
255+
$$($(2)_CARGO_BUILD_OPTS)
256+
endef
257+
endif # ifeq ($(4),target)
258+
endif # ifndef $(2)_BUILD_CMDS
259+
260+
#
261+
# Target installation step. Only define it if not already defined by
262+
# the package .mk file.
263+
#
264+
ifndef $(2)_INSTALL_TARGET_CMDS
265+
define $(2)_INSTALL_TARGET_CMDS
266+
cd $$($$(PKG)_SRCDIR) && \
267+
$$(TARGET_MAKE_ENV) \
268+
$$(TARGET_CONFIGURE_OPTS) \
269+
$$(PKG_RUST_ENV) \
270+
$$($(2)_CARGO_ENV) \
271+
cargo install \
272+
--root $$(TARGET_DIR)/usr/ \
273+
--bins \
274+
--no-track \
275+
--force \
276+
--locked \
277+
-Z target-applies-to-host \
278+
$$($(2)_CARGO_INSTALL_OPTS)
279+
endef
280+
endif
281+
282+
ifndef $(2)_INSTALL_CMDS
283+
define $(2)_INSTALL_CMDS
284+
cd $$($$(PKG)_SRCDIR) && \
285+
$$(HOST_MAKE_ENV) \
286+
$$(HOST_CONFIGURE_OPTS) \
287+
$$(HOST_PKG_RUST_ENV) \
288+
$$($(2)_CARGO_ENV) \
289+
cargo install \
290+
--root $$(HOST_DIR) \
291+
--bins \
292+
--no-track \
293+
--force \
294+
$$($(2)_CARGO_INSTALL_OPTS)
295+
endef
296+
endif
297+
298+
# Call the generic package infrastructure to generate the necessary
299+
# make targets
300+
$(call inner-generic-package,$(1),$(2),$(3),$(4))
301+
302+
endef
303+
304+
################################################################################
305+
# rust-package -- the target generator macro for Cargo packages
306+
################################################################################
307+
308+
rust-package = $(call inner-cargo-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
309+
host-rust-package = $(call inner-cargo-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)

0 commit comments

Comments
 (0)