blob: 2118faec8fcb87468b0c80f01988db62ceb1eabc [file] [log] [blame]
Andrew Geissler517393d2023-01-13 08:55:19 -06001SUMMARY = "Rust compiler and runtime libaries"
2HOMEPAGE = "http://www.rust-lang.org"
3SECTION = "devel"
4LICENSE = "(MIT | Apache-2.0) & Unicode-TOU"
Patrick Williams864cc432023-02-09 14:54:44 -06005LIC_FILES_CHKSUM = "file://COPYRIGHT;md5=c2cccf560306876da3913d79062a54b9"
Andrew Geissler517393d2023-01-13 08:55:19 -06006
7inherit rust
8inherit cargo_common
9
10DEPENDS += "file-native python3-native"
11DEPENDS:append:class-native = " rust-llvm-native"
12DEPENDS:append:class-nativesdk = " nativesdk-rust-llvm"
13
14DEPENDS += "rust-llvm (=${PV})"
15
16# Otherwise we'll depend on what we provide
17INHIBIT_DEFAULT_RUST_DEPS:class-native = "1"
18# We don't need to depend on gcc-native because yocto assumes it exists
19PROVIDES:class-native = "virtual/${TARGET_PREFIX}rust"
20
21S = "${RUSTSRC}"
22
23# Use at your own risk, accepted values are stable, beta and nightly
24RUST_CHANNEL ?= "stable"
25PV .= "${@bb.utils.contains('RUST_CHANNEL', 'stable', '', '-${RUST_CHANNEL}', d)}"
26
27export FORCE_CRATE_HASH="${BB_TASKHASH}"
28
29RUST_ALTERNATE_EXE_PATH ?= "${STAGING_LIBDIR}/llvm-rust/bin/llvm-config"
30RUST_ALTERNATE_EXE_PATH_NATIVE = "${STAGING_LIBDIR_NATIVE}/llvm-rust/bin/llvm-config"
31
32# We don't want to use bitbakes vendoring because the rust sources do their
33# own vendoring.
34CARGO_DISABLE_BITBAKE_VENDORING = "1"
35
36# We can't use RUST_BUILD_SYS here because that may be "musl" if
37# TCLIBC="musl". Snapshots are always -unknown-linux-gnu
38setup_cargo_environment () {
39 # The first step is to build bootstrap and some early stage tools,
40 # these are build for the same target as the snapshot, e.g.
41 # x86_64-unknown-linux-gnu.
42 # Later stages are build for the native target (i.e. target.x86_64-linux)
43 cargo_common_do_configure
44}
45
46inherit rust-target-config
47
48do_rust_setup_snapshot () {
49 for installer in "${WORKDIR}/rust-snapshot-components/"*"/install.sh"; do
50 "${installer}" --prefix="${WORKDIR}/rust-snapshot" --disable-ldconfig
51 done
52
53 # Some versions of rust (e.g. 1.18.0) tries to find cargo in stage0/bin/cargo
54 # and fail without it there.
55 mkdir -p ${RUSTSRC}/build/${BUILD_SYS}
56 ln -sf ${WORKDIR}/rust-snapshot/ ${RUSTSRC}/build/${BUILD_SYS}/stage0
57
58 # Need to use uninative's loader if enabled/present since the library paths
59 # are used internally by rust and result in symbol mismatches if we don't
60 if [ ! -z "${UNINATIVE_LOADER}" -a -e "${UNINATIVE_LOADER}" ]; then
61 for bin in cargo rustc rustdoc; do
62 patchelf-uninative ${WORKDIR}/rust-snapshot/bin/$bin --set-interpreter ${UNINATIVE_LOADER}
63 done
64 fi
65}
66addtask rust_setup_snapshot after do_unpack before do_configure
67do_rust_setup_snapshot[dirs] += "${WORKDIR}/rust-snapshot"
68do_rust_setup_snapshot[vardepsexclude] += "UNINATIVE_LOADER"
69
70python do_configure() {
71 import json
72 try:
73 import configparser
74 except ImportError:
75 import ConfigParser as configparser
76
77 # toml is rather similar to standard ini like format except it likes values
78 # that look more JSON like. So for our purposes simply escaping all values
79 # as JSON seem to work fine.
80
81 e = lambda s: json.dumps(s)
82
83 config = configparser.RawConfigParser()
84
85 # [target.ARCH-poky-linux]
Patrick Williams864cc432023-02-09 14:54:44 -060086 host_section = "target.{}".format(d.getVar('RUST_HOST_SYS'))
Andrew Geissler517393d2023-01-13 08:55:19 -060087 config.add_section(host_section)
88
89 llvm_config_target = d.expand("${RUST_ALTERNATE_EXE_PATH}")
90 llvm_config_build = d.expand("${RUST_ALTERNATE_EXE_PATH_NATIVE}")
91 config.set(host_section, "llvm-config", e(llvm_config_target))
92
93 config.set(host_section, "cxx", e(d.expand("${RUST_TARGET_CXX}")))
94 config.set(host_section, "cc", e(d.expand("${RUST_TARGET_CC}")))
95 config.set(host_section, "linker", e(d.expand("${RUST_TARGET_CCLD}")))
96 if "musl" in host_section:
97 config.set(host_section, "musl-root", e(d.expand("${STAGING_DIR_HOST}${exec_prefix}")))
98
99 # If we don't do this rust-native will compile it's own llvm for BUILD.
100 # [target.${BUILD_ARCH}-unknown-linux-gnu]
Patrick Williams864cc432023-02-09 14:54:44 -0600101 build_section = "target.{}".format(d.getVar('RUST_BUILD_SYS'))
Andrew Geissler517393d2023-01-13 08:55:19 -0600102 if build_section != host_section:
103 config.add_section(build_section)
104
105 config.set(build_section, "llvm-config", e(llvm_config_build))
106
107 config.set(build_section, "cxx", e(d.expand("${RUST_BUILD_CXX}")))
108 config.set(build_section, "cc", e(d.expand("${RUST_BUILD_CC}")))
109 config.set(build_section, "linker", e(d.expand("${RUST_BUILD_CCLD}")))
110
Patrick Williams864cc432023-02-09 14:54:44 -0600111 target_section = "target.{}".format(d.getVar('RUST_TARGET_SYS'))
Andrew Geissler517393d2023-01-13 08:55:19 -0600112 if target_section != host_section and target_section != build_section:
113 config.add_section(target_section)
114
115 config.set(target_section, "llvm-config", e(llvm_config_target))
116
117 config.set(target_section, "cxx", e(d.expand("${RUST_TARGET_CXX}")))
118 config.set(target_section, "cc", e(d.expand("${RUST_TARGET_CC}")))
119 config.set(target_section, "linker", e(d.expand("${RUST_TARGET_CCLD}")))
120
121 # [llvm]
122 config.add_section("llvm")
123 config.set("llvm", "static-libstdcpp", e(False))
124 if "llvm" in (d.getVar('TC_CXX_RUNTIME') or ""):
125 config.set("llvm", "use-libcxx", e(True))
126
127 # [rust]
128 config.add_section("rust")
129 config.set("rust", "rpath", e(True))
130 config.set("rust", "channel", e(d.expand("${RUST_CHANNEL}")))
131
132 # Whether or not to optimize the compiler and standard library
133 config.set("rust", "optimize", e(True))
134
135 # Emits extraneous output from tests to ensure that failures of the test
136 # harness are debuggable just from logfiles
137 config.set("rust", "verbose-tests", e(True))
138
139 # [build]
140 config.add_section("build")
141 config.set("build", "submodules", e(False))
142 config.set("build", "docs", e(False))
143
144 rustc = d.expand("${WORKDIR}/rust-snapshot/bin/rustc")
145 config.set("build", "rustc", e(rustc))
146
147 # Support for the profiler runtime to generate e.g. coverage report,
148 # PGO etc.
149 config.set("build", "profiler", e(True))
150
151 cargo = d.expand("${WORKDIR}/rust-snapshot/bin/cargo")
152 config.set("build", "cargo", e(cargo))
153
154 config.set("build", "vendor", e(True))
155
156 if not "targets" in locals():
Patrick Williams864cc432023-02-09 14:54:44 -0600157 targets = [d.getVar("RUST_TARGET_SYS")]
Andrew Geissler517393d2023-01-13 08:55:19 -0600158 config.set("build", "target", e(targets))
159
160 if not "hosts" in locals():
Patrick Williams864cc432023-02-09 14:54:44 -0600161 hosts = [d.getVar("RUST_HOST_SYS")]
Andrew Geissler517393d2023-01-13 08:55:19 -0600162 config.set("build", "host", e(hosts))
163
164 # We can't use BUILD_SYS since that is something the rust snapshot knows
165 # nothing about when trying to build some stage0 tools (like fabricate)
Patrick Williams864cc432023-02-09 14:54:44 -0600166 config.set("build", "build", e(d.getVar("RUST_BUILD_SYS")))
Andrew Geissler517393d2023-01-13 08:55:19 -0600167
168 # [install]
169 config.add_section("install")
170 # ./x.py install doesn't have any notion of "destdir"
171 # but we can prepend ${D} to all the directories instead
Patrick Williams864cc432023-02-09 14:54:44 -0600172 config.set("install", "prefix", e(d.getVar("D") + d.getVar("prefix")))
173 config.set("install", "bindir", e(d.getVar("D") + d.getVar("bindir")))
174 config.set("install", "libdir", e(d.getVar("D") + d.getVar("libdir")))
175 config.set("install", "datadir", e(d.getVar("D") + d.getVar("datadir")))
176 config.set("install", "mandir", e(d.getVar("D") + d.getVar("mandir")))
Andrew Geissler517393d2023-01-13 08:55:19 -0600177
178 with open("config.toml", "w") as f:
179 f.write('changelog-seen = 2\n\n')
180 config.write(f)
181
182 # set up ${WORKDIR}/cargo_home
183 bb.build.exec_func("setup_cargo_environment", d)
184}
185
186rust_runx () {
187 echo "COMPILE ${PN}" "$@"
188
189 # CFLAGS, LDFLAGS, CXXFLAGS, CPPFLAGS are used by rust's build for a
190 # wide range of targets (not just TARGET). Yocto's settings for them will
191 # be inappropriate, avoid using.
192 unset CFLAGS
193 unset LDFLAGS
194 unset CXXFLAGS
195 unset CPPFLAGS
196
197 export RUSTFLAGS="${RUST_DEBUG_REMAP}"
198
199 # Copy the natively built llvm-config into the target so we can run it. Horrible,
200 # but works!
201 if [ ${RUST_ALTERNATE_EXE_PATH_NATIVE} != ${RUST_ALTERNATE_EXE_PATH} ]; then
202 mkdir -p `dirname ${RUST_ALTERNATE_EXE_PATH}`
203 cp ${RUST_ALTERNATE_EXE_PATH_NATIVE} ${RUST_ALTERNATE_EXE_PATH}
204 chrpath -d ${RUST_ALTERNATE_EXE_PATH}
205 fi
206
207 oe_cargo_fix_env
208
209 python3 src/bootstrap/bootstrap.py ${@oe.utils.parallel_make_argument(d, '-j %d')} "$@" --verbose
210}
211rust_runx[vardepsexclude] += "PARALLEL_MAKE"
212
213require rust-source.inc
214require rust-snapshot.inc
215
216INSANE_SKIP:${PN}:class-native = "already-stripped"
217FILES:${PN} += "${libdir}/rustlib"
218FILES:${PN} += "${libdir}/*.so"
219FILES:${PN}-dev = ""
220
221do_compile () {
222 rust_runx build --stage 2
223}
224
225do_compile:append:class-target () {
226 rust_runx build --stage 2 src/tools/clippy
227 rust_runx build --stage 2 src/tools/rustfmt
228}
229
230do_compile:append:class-nativesdk () {
231 rust_runx build --stage 2 src/tools/clippy
232 rust_runx build --stage 2 src/tools/rustfmt
233}
234
235ALLOW_EMPTY:${PN} = "1"
236
237PACKAGES =+ "${PN}-tools-clippy ${PN}-tools-rustfmt"
238FILES:${PN}-tools-clippy = "${bindir}/cargo-clippy ${bindir}/clippy-driver"
239FILES:${PN}-tools-rustfmt = "${bindir}/rustfmt"
240RDEPENDS:${PN}-tools-clippy = "${PN}"
241RDEPENDS:${PN}-tools-rustfmt = "${PN}"
242
243SUMMARY:${PN}-tools-clippy = "A collection of lints to catch common mistakes and improve your Rust code"
244SUMMARY:${PN}-tools-rustfmt = "A tool for formatting Rust code according to style guidelines"
245
246do_install () {
247 rust_do_install
248}
249
250rust_do_install() {
251 rust_runx install
252}
253
254rust_do_install:class-nativesdk() {
255 export PSEUDO_UNLOAD=1
256 rust_runx install
257 unset PSEUDO_UNLOAD
258
259 install -d ${D}${bindir}
260 for i in cargo-clippy clippy-driver rustfmt; do
261 cp build/${RUST_BUILD_SYS}/stage2-tools/${RUST_HOST_SYS}/release/$i ${D}${bindir}
262 chrpath -r "\$ORIGIN/../lib" ${D}${bindir}/$i
263 done
264
265 chown root:root ${D}/ -R
266 rm ${D}${libdir}/rustlib/uninstall.sh
267 rm ${D}${libdir}/rustlib/install.log
268 rm ${D}${libdir}/rustlib/manifest*
269}
270
271EXTRA_TOOLS ?= "cargo-clippy clippy-driver rustfmt"
272rust_do_install:class-target() {
273 export PSEUDO_UNLOAD=1
274 rust_runx install
275 unset PSEUDO_UNLOAD
276
277 install -d ${D}${bindir}
278 for i in ${EXTRA_TOOLS}; do
279 cp build/${RUST_BUILD_SYS}/stage2-tools/${RUST_HOST_SYS}/release/$i ${D}${bindir}
280 chrpath -r "\$ORIGIN/../lib" ${D}${bindir}/$i
281 done
282
283 install -d ${D}${libdir}/rustlib/${RUST_HOST_SYS}
284 install -m 0644 ${WORKDIR}/rust-targets/${RUST_HOST_SYS}.json ${D}${libdir}/rustlib/${RUST_HOST_SYS}/target.json
285
286 chown root:root ${D}/ -R
287 rm ${D}${libdir}/rustlib/uninstall.sh
288 rm ${D}${libdir}/rustlib/install.log
289 rm ${D}${libdir}/rustlib/manifest*
290}
291
Patrick Williams864cc432023-02-09 14:54:44 -0600292addtask do_update_snapshot after do_patch
293do_update_snapshot[nostamp] = "1"
294
295# Run with `bitbake -c update_snapshot rust` to update `rust-snapshot.inc`
296# with the checksums for the rust snapshot associated with this rustc-src
297# tarball.
298python do_update_snapshot() {
299 import json
300 import re
301 import sys
302
303 from collections import defaultdict
304
305 with open(os.path.join(d.getVar("S"), "src", "stage0.json")) as f:
306 j = json.load(f)
307
308 config_dist_server = j['config']['dist_server']
309 compiler_date = j['compiler']['date']
310 compiler_version = j['compiler']['version']
311
312 src_uri = defaultdict(list)
313 for k, v in j['checksums_sha256'].items():
314 m = re.search(f"dist/{compiler_date}/(?P<component>.*)-{compiler_version}-(?P<arch>.*)-unknown-linux-gnu\\.tar\\.xz", k)
315 if m:
316 component = m.group('component')
317 arch = m.group('arch')
318 src_uri[arch].append(f"SRC_URI[{component}-snapshot-{arch}.sha256sum] = \"{v}\"")
319
320 snapshot = """\
321## This is information on the rust-snapshot (binary) used to build our current release.
322## snapshot info is taken from rust/src/stage0.json
323## Rust is self-hosting and bootstraps itself with a pre-built previous version of itself.
324## The exact (previous) version that has been used is specified in the source tarball.
325## The version is replicated here.
326
327SNAPSHOT_VERSION = "%s"
328
329""" % compiler_version
330
331 for arch, components in src_uri.items():
332 snapshot += "\n".join(components) + "\n\n"
333
334 snapshot += """\
335SRC_URI += " \\
336 ${RUST_DIST_SERVER}/dist/${RUST_STD_SNAPSHOT}.tar.xz;name=rust-std-snapshot-${RUST_BUILD_ARCH};subdir=rust-snapshot-components \\
337 ${RUST_DIST_SERVER}/dist/${RUSTC_SNAPSHOT}.tar.xz;name=rustc-snapshot-${RUST_BUILD_ARCH};subdir=rust-snapshot-components \\
338 ${RUST_DIST_SERVER}/dist/${CARGO_SNAPSHOT}.tar.xz;name=cargo-snapshot-${RUST_BUILD_ARCH};subdir=rust-snapshot-components \\
339"
340
341RUST_DIST_SERVER = "%s"
342
343RUST_STD_SNAPSHOT = "rust-std-${SNAPSHOT_VERSION}-${RUST_BUILD_ARCH}-unknown-linux-gnu"
344RUSTC_SNAPSHOT = "rustc-${SNAPSHOT_VERSION}-${RUST_BUILD_ARCH}-unknown-linux-gnu"
345CARGO_SNAPSHOT = "cargo-${SNAPSHOT_VERSION}-${RUST_BUILD_ARCH}-unknown-linux-gnu"
346""" % config_dist_server
347
348 with open(os.path.join(d.getVar("THISDIR"), "rust-snapshot.inc"), "w") as f:
349 f.write(snapshot)
350}
351
Andrew Geissler517393d2023-01-13 08:55:19 -0600352RUSTLIB_DEP:class-nativesdk = ""
353
354# musl builds include libunwind.a
355INSANE_SKIP:${PN} = "staticdev"
356
357BBCLASSEXTEND = "native nativesdk"