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