1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7##
8## Purpose:
9## This class is to support building with cargo. It
10## must be different than cargo.bbclass because Rust
11## now builds with Cargo but cannot use cargo.bbclass
12## due to dependencies and assumptions in cargo.bbclass
13## that Rust & Cargo are already installed. So this
14## is used by cargo.bbclass and Rust
15##
16
17# add crate fetch support
18inherit rust-common
19
20# Where we download our registry and dependencies to
21export CARGO_HOME = "${WORKDIR}/cargo_home"
22
23# The pkg-config-rs library used by cargo build scripts disables itself when
24# cross compiling unless this is defined. We set up pkg-config appropriately
25# for cross compilation, so tell it we know better than it.
26export PKG_CONFIG_ALLOW_CROSS = "1"
27
28# Don't instruct cargo to use crates downloaded by bitbake. Some rust packages,
29# for example the rust compiler itself, come with their own vendored sources.
30# Specifying two [source.crates-io] will not work.
31CARGO_DISABLE_BITBAKE_VENDORING ?= "0"
32
33# Used by libstd-rs to point to the vendor dir included in rustc src
34CARGO_VENDORING_DIRECTORY ?= "${CARGO_HOME}/bitbake"
35
36CARGO_RUST_TARGET_CCLD ?= "${RUST_TARGET_CCLD}"
37cargo_common_do_configure () {
38	mkdir -p ${CARGO_HOME}/bitbake
39
40	cat <<- EOF > ${CARGO_HOME}/config
41	# EXTRA_OECARGO_PATHS
42	paths = [
43	$(for p in ${EXTRA_OECARGO_PATHS}; do echo \"$p\",; done)
44	]
45	EOF
46
47	cat <<- EOF >> ${CARGO_HOME}/config
48
49	# Local mirror vendored by bitbake
50	[source.bitbake]
51	directory = "${CARGO_VENDORING_DIRECTORY}"
52	EOF
53
54	if [ ${CARGO_DISABLE_BITBAKE_VENDORING} = "0" ]; then
55		cat <<- EOF >> ${CARGO_HOME}/config
56
57		[source.crates-io]
58		replace-with = "bitbake"
59		local-registry = "/nonexistant"
60		EOF
61	fi
62
63	cat <<- EOF >> ${CARGO_HOME}/config
64
65	[http]
66	# Multiplexing can't be enabled because http2 can't be enabled
67	# in curl-native without dependency loops
68	multiplexing = false
69
70	# Ignore the hard coded and incorrect path to certificates
71	cainfo = "${STAGING_ETCDIR_NATIVE}/ssl/certs/ca-certificates.crt"
72
73	EOF
74
75	cat <<- EOF >> ${CARGO_HOME}/config
76
77	# HOST_SYS
78	[target.${RUST_HOST_SYS}]
79	linker = "${CARGO_RUST_TARGET_CCLD}"
80	EOF
81
82	if [ "${RUST_HOST_SYS}" != "${RUST_BUILD_SYS}" ]; then
83		cat <<- EOF >> ${CARGO_HOME}/config
84
85		# BUILD_SYS
86		[target.${RUST_BUILD_SYS}]
87		linker = "${RUST_BUILD_CCLD}"
88		EOF
89	fi
90
91	if [ "${RUST_TARGET_SYS}" != "${RUST_BUILD_SYS}" -a "${RUST_TARGET_SYS}" != "${RUST_HOST_SYS}" ]; then
92		cat <<- EOF >> ${CARGO_HOME}/config
93
94		# TARGET_SYS
95		[target.${RUST_TARGET_SYS}]
96		linker = "${RUST_TARGET_CCLD}"
97		EOF
98	fi
99
100	# Put build output in build directory preferred by bitbake instead of
101	# inside source directory unless they are the same
102	if [ "${B}" != "${S}" ]; then
103		cat <<- EOF >> ${CARGO_HOME}/config
104
105		[build]
106		# Use out of tree build destination to avoid poluting the source tree
107		target-dir = "${B}/target"
108		EOF
109	fi
110
111	cat <<- EOF >> ${CARGO_HOME}/config
112
113	[term]
114	progress.when = 'always'
115	progress.width = 80
116	EOF
117}
118
119oe_cargo_fix_env () {
120	export CC="${RUST_TARGET_CC}"
121	export CXX="${RUST_TARGET_CXX}"
122	export CFLAGS="${CFLAGS}"
123	export CXXFLAGS="${CXXFLAGS}"
124	export AR="${AR}"
125	export TARGET_CC="${RUST_TARGET_CC}"
126	export TARGET_CXX="${RUST_TARGET_CXX}"
127	export TARGET_CFLAGS="${CFLAGS}"
128	export TARGET_CXXFLAGS="${CXXFLAGS}"
129	export TARGET_AR="${AR}"
130	export HOST_CC="${RUST_BUILD_CC}"
131	export HOST_CXX="${RUST_BUILD_CXX}"
132	export HOST_CFLAGS="${BUILD_CFLAGS}"
133	export HOST_CXXFLAGS="${BUILD_CXXFLAGS}"
134	export HOST_AR="${BUILD_AR}"
135}
136
137EXTRA_OECARGO_PATHS ??= ""
138
139EXPORT_FUNCTIONS do_configure
140