blob: 82ab25b59c59de77cff06e753cbbcbe5c9713912 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
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"
Patrick Williams2390b1b2022-11-03 13:47:49 -050059 local-registry = "/nonexistent"
Patrick Williams92b42cb2022-09-03 06:53:57 -050060 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]
Patrick Williams2390b1b2022-11-03 13:47:49 -0500106 # Use out of tree build destination to avoid polluting the source tree
Patrick Williams92b42cb2022-09-03 06:53:57 -0500107 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
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500119python cargo_common_do_patch_paths() {
120 cargo_config = os.path.join(d.getVar("CARGO_HOME"), "config")
121 if not os.path.exists(cargo_config):
122 return
123
124 src_uri = (d.getVar('SRC_URI') or "").split()
125 if len(src_uri) == 0:
126 return
127
128 patches = dict()
129 workdir = d.getVar('WORKDIR')
130 fetcher = bb.fetch2.Fetch(src_uri, d)
131 for url in fetcher.urls:
132 ud = fetcher.ud[url]
133 if ud.type == 'git':
134 name = ud.parm.get('name')
135 destsuffix = ud.parm.get('destsuffix')
136 if name is not None and destsuffix is not None:
137 if ud.user:
138 repo = '%s://%s@%s%s' % (ud.proto, ud.user, ud.host, ud.path)
139 else:
140 repo = '%s://%s%s' % (ud.proto, ud.host, ud.path)
141 path = '%s = { path = "%s" }' % (name, os.path.join(workdir, destsuffix))
142 patches.setdefault(repo, []).append(path)
143
144 with open(cargo_config, "a+") as config:
145 for k, v in patches.items():
146 print('\n[patch."%s"]' % k, file=config)
147 for name in v:
148 print(name, file=config)
149}
150do_configure[postfuncs] += "cargo_common_do_patch_paths"
151
Patrick Williams92b42cb2022-09-03 06:53:57 -0500152oe_cargo_fix_env () {
153 export CC="${RUST_TARGET_CC}"
154 export CXX="${RUST_TARGET_CXX}"
155 export CFLAGS="${CFLAGS}"
156 export CXXFLAGS="${CXXFLAGS}"
157 export AR="${AR}"
158 export TARGET_CC="${RUST_TARGET_CC}"
159 export TARGET_CXX="${RUST_TARGET_CXX}"
160 export TARGET_CFLAGS="${CFLAGS}"
161 export TARGET_CXXFLAGS="${CXXFLAGS}"
162 export TARGET_AR="${AR}"
163 export HOST_CC="${RUST_BUILD_CC}"
164 export HOST_CXX="${RUST_BUILD_CXX}"
165 export HOST_CFLAGS="${BUILD_CFLAGS}"
166 export HOST_CXXFLAGS="${BUILD_CXXFLAGS}"
167 export HOST_AR="${BUILD_AR}"
168}
169
170EXTRA_OECARGO_PATHS ??= ""
171
172EXPORT_FUNCTIONS do_configure