| Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame^] | 1 | # Common variables used by all Rust builds | 
|  | 2 | export rustlibdir = "${libdir}/rust" | 
|  | 3 | FILES:${PN} += "${rustlibdir}/*.so" | 
|  | 4 | FILES:${PN}-dev += "${rustlibdir}/*.rlib ${rustlibdir}/*.rmeta" | 
|  | 5 | FILES:${PN}-dbg += "${rustlibdir}/.debug" | 
|  | 6 |  | 
|  | 7 | RUSTLIB = "-L ${STAGING_LIBDIR}/rust" | 
|  | 8 | RUST_DEBUG_REMAP = "--remap-path-prefix=${WORKDIR}=/usr/src/debug/${PN}/${EXTENDPE}${PV}-${PR}" | 
|  | 9 | RUSTFLAGS += "${RUSTLIB} ${RUST_DEBUG_REMAP}" | 
|  | 10 | RUSTLIB_DEP ?= "libstd-rs" | 
|  | 11 | export RUST_TARGET_PATH = "${STAGING_LIBDIR_NATIVE}/rustlib" | 
|  | 12 | RUST_PANIC_STRATEGY ?= "unwind" | 
|  | 13 |  | 
|  | 14 | # Native builds are not effected by TCLIBC. Without this, rust-native | 
|  | 15 | # thinks it's "target" (i.e. x86_64-linux) is a musl target. | 
|  | 16 | RUST_LIBC = "${TCLIBC}" | 
|  | 17 | RUST_LIBC:class-native = "glibc" | 
|  | 18 |  | 
|  | 19 | def determine_libc(d, thing): | 
|  | 20 | '''Determine which libc something should target''' | 
|  | 21 |  | 
|  | 22 | # BUILD is never musl, TARGET may be musl or glibc, | 
|  | 23 | # HOST could be musl, but only if a compiler is built to be run on | 
|  | 24 | # target in which case HOST_SYS != BUILD_SYS. | 
|  | 25 | if thing == 'TARGET': | 
|  | 26 | libc = d.getVar('RUST_LIBC') | 
|  | 27 | elif thing == 'BUILD' and (d.getVar('HOST_SYS') != d.getVar('BUILD_SYS')): | 
|  | 28 | libc = d.getVar('RUST_LIBC') | 
|  | 29 | else: | 
|  | 30 | libc = d.getVar('RUST_LIBC:class-native') | 
|  | 31 |  | 
|  | 32 | return libc | 
|  | 33 |  | 
|  | 34 | def target_is_armv7(d): | 
|  | 35 | '''Determine if target is armv7''' | 
|  | 36 | # TUNE_FEATURES may include arm* even if the target is not arm | 
|  | 37 | # in the case of *-native packages | 
|  | 38 | if d.getVar('TARGET_ARCH') != 'arm': | 
|  | 39 | return False | 
|  | 40 |  | 
|  | 41 | feat = d.getVar('TUNE_FEATURES') | 
|  | 42 | feat = frozenset(feat.split()) | 
|  | 43 | mach_overrides = d.getVar('MACHINEOVERRIDES') | 
|  | 44 | mach_overrides = frozenset(mach_overrides.split(':')) | 
|  | 45 |  | 
|  | 46 | v7=frozenset(['armv7a', 'armv7r', 'armv7m', 'armv7ve']) | 
|  | 47 | if mach_overrides.isdisjoint(v7) and feat.isdisjoint(v7): | 
|  | 48 | return False | 
|  | 49 | else: | 
|  | 50 | return True | 
|  | 51 | target_is_armv7[vardepvalue] = "${@target_is_armv7(d)}" | 
|  | 52 |  | 
|  | 53 | # Responsible for taking Yocto triples and converting it to Rust triples | 
|  | 54 | def rust_base_triple(d, thing): | 
|  | 55 | ''' | 
|  | 56 | Mangle bitbake's *_SYS into something that rust might support (see | 
|  | 57 | rust/mk/cfg/* for a list) | 
|  | 58 |  | 
|  | 59 | Note that os is assumed to be some linux form | 
|  | 60 | ''' | 
|  | 61 |  | 
|  | 62 | # The llvm-target for armv7 is armv7-unknown-linux-gnueabihf | 
|  | 63 | if thing == "TARGET" and target_is_armv7(d): | 
|  | 64 | arch = "armv7" | 
|  | 65 | else: | 
|  | 66 | arch = d.getVar('{}_ARCH'.format(thing)) | 
|  | 67 |  | 
|  | 68 | # All the Yocto targets are Linux and are 'unknown' | 
|  | 69 | vendor = "-unknown" | 
|  | 70 | os = d.getVar('{}_OS'.format(thing)) | 
|  | 71 | libc = determine_libc(d, thing) | 
|  | 72 |  | 
|  | 73 | # Prefix with a dash and convert glibc -> gnu | 
|  | 74 | if libc == "glibc": | 
|  | 75 | libc = "-gnu" | 
|  | 76 | elif libc == "musl": | 
|  | 77 | libc = "-musl" | 
|  | 78 |  | 
|  | 79 | # Don't double up musl (only appears to be the case on aarch64) | 
|  | 80 | if os == "linux-musl": | 
|  | 81 | if libc != "-musl": | 
|  | 82 | bb.fatal("{}_OS was '{}' but TCLIBC was not 'musl'".format(thing, os)) | 
|  | 83 | os = "linux" | 
|  | 84 |  | 
|  | 85 | # This catches ARM targets and appends the necessary hard float bits | 
|  | 86 | if os == "linux-gnueabi" or os == "linux-musleabi": | 
|  | 87 | libc = bb.utils.contains('TUNE_FEATURES', 'callconvention-hard', 'hf', '', d) | 
|  | 88 | return arch + vendor + '-' + os + libc | 
|  | 89 |  | 
|  | 90 | # Naming explanation | 
|  | 91 | # Yocto | 
|  | 92 | # - BUILD_SYS - Yocto triple of the build environment | 
|  | 93 | # - HOST_SYS - What we're building for in Yocto | 
|  | 94 | # - TARGET_SYS - What we're building for in Yocto | 
|  | 95 | # | 
|  | 96 | # So when building '-native' packages BUILD_SYS == HOST_SYS == TARGET_SYS | 
|  | 97 | # When building packages for the image HOST_SYS == TARGET_SYS | 
|  | 98 | # This is a gross over simplification as there are other modes but | 
|  | 99 | # currently this is all that's supported. | 
|  | 100 | # | 
|  | 101 | # Rust | 
|  | 102 | # - TARGET - the system where the binary will run | 
|  | 103 | # - HOST - the system where the binary is being built | 
|  | 104 | # | 
|  | 105 | # Rust additionally will use two additional cases: | 
|  | 106 | # - undecorated (e.g. CC) - equivalent to TARGET | 
|  | 107 | # - triple suffix (e.g. CC:x86_64_unknown_linux_gnu) - both | 
|  | 108 | #   see: https://github.com/alexcrichton/gcc-rs | 
|  | 109 | # The way that Rust's internal triples and Yocto triples are mapped together | 
|  | 110 | # its likely best to not use the triple suffix due to potential confusion. | 
|  | 111 |  | 
|  | 112 | RUST_BUILD_SYS = "${@rust_base_triple(d, 'BUILD')}" | 
|  | 113 | RUST_HOST_SYS = "${@rust_base_triple(d, 'HOST')}" | 
|  | 114 | RUST_TARGET_SYS = "${@rust_base_triple(d, 'TARGET')}" | 
|  | 115 |  | 
|  | 116 | # wrappers to get around the fact that Rust needs a single | 
|  | 117 | # binary but Yocto's compiler and linker commands have | 
|  | 118 | # arguments. Technically the archiver is always one command but | 
|  | 119 | # this is necessary for builds that determine the prefix and then | 
|  | 120 | # use those commands based on the prefix. | 
|  | 121 | WRAPPER_DIR = "${WORKDIR}/wrapper" | 
|  | 122 | RUST_BUILD_CC = "${WRAPPER_DIR}/build-rust-cc" | 
|  | 123 | RUST_BUILD_CXX = "${WRAPPER_DIR}/build-rust-cxx" | 
|  | 124 | RUST_BUILD_CCLD = "${WRAPPER_DIR}/build-rust-ccld" | 
|  | 125 | RUST_BUILD_AR = "${WRAPPER_DIR}/build-rust-ar" | 
|  | 126 | RUST_TARGET_CC = "${WRAPPER_DIR}/target-rust-cc" | 
|  | 127 | RUST_TARGET_CXX = "${WRAPPER_DIR}/target-rust-cxx" | 
|  | 128 | RUST_TARGET_CCLD = "${WRAPPER_DIR}/target-rust-ccld" | 
|  | 129 | RUST_TARGET_AR = "${WRAPPER_DIR}/target-rust-ar" | 
|  | 130 |  | 
|  | 131 | create_wrapper () { | 
|  | 132 | file="$1" | 
|  | 133 | shift | 
|  | 134 |  | 
|  | 135 | cat <<- EOF > "${file}" | 
|  | 136 | #!/bin/sh | 
|  | 137 | exec $@ "\$@" | 
|  | 138 | EOF | 
|  | 139 | chmod +x "${file}" | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | export WRAPPER_TARGET_CC = "${CC}" | 
|  | 143 | export WRAPPER_TARGET_CXX = "${CXX}" | 
|  | 144 | export WRAPPER_TARGET_CCLD = "${CCLD}" | 
|  | 145 | export WRAPPER_TARGET_LDFLAGS = "${LDFLAGS}" | 
|  | 146 | export WRAPPER_TARGET_AR = "${AR}" | 
|  | 147 |  | 
|  | 148 | # compiler is used by gcc-rs | 
|  | 149 | # linker is used by rustc/cargo | 
|  | 150 | # archiver is used by the build of libstd-rs | 
|  | 151 | do_rust_create_wrappers () { | 
|  | 152 | mkdir -p "${WRAPPER_DIR}" | 
|  | 153 |  | 
|  | 154 | # Yocto Build / Rust Host C compiler | 
|  | 155 | create_wrapper "${RUST_BUILD_CC}" "${BUILD_CC}" | 
|  | 156 | # Yocto Build / Rust Host C++ compiler | 
|  | 157 | create_wrapper "${RUST_BUILD_CXX}" "${BUILD_CXX}" | 
|  | 158 | # Yocto Build / Rust Host linker | 
|  | 159 | create_wrapper "${RUST_BUILD_CCLD}" "${BUILD_CCLD}" "${BUILD_LDFLAGS}" | 
|  | 160 | # Yocto Build / Rust Host archiver | 
|  | 161 | create_wrapper "${RUST_BUILD_AR}" "${BUILD_AR}" | 
|  | 162 |  | 
|  | 163 | # Yocto Target / Rust Target C compiler | 
|  | 164 | create_wrapper "${RUST_TARGET_CC}" "${WRAPPER_TARGET_CC}" "${WRAPPER_TARGET_LDFLAGS}" | 
|  | 165 | # Yocto Target / Rust Target C++ compiler | 
|  | 166 | create_wrapper "${RUST_TARGET_CXX}" "${WRAPPER_TARGET_CXX}" | 
|  | 167 | # Yocto Target / Rust Target linker | 
|  | 168 | create_wrapper "${RUST_TARGET_CCLD}" "${WRAPPER_TARGET_CCLD}" "${WRAPPER_TARGET_LDFLAGS}" | 
|  | 169 | # Yocto Target / Rust Target archiver | 
|  | 170 | create_wrapper "${RUST_TARGET_AR}" "${WRAPPER_TARGET_AR}" | 
|  | 171 |  | 
|  | 172 | # Need to filter out LD_LIBRARY_PATH from the linker without using shell | 
|  | 173 | mv ${RUST_BUILD_CCLD} ${RUST_BUILD_CCLD}.real | 
|  | 174 | ${BUILD_CC} ${COREBASE}/meta/files/rust-ccld-wrapper.c -o ${RUST_BUILD_CCLD} | 
|  | 175 | mv ${RUST_TARGET_CCLD} ${RUST_TARGET_CCLD}.real | 
|  | 176 | ${BUILD_CC} ${COREBASE}/meta/files/rust-ccld-wrapper.c -o ${RUST_TARGET_CCLD} | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | addtask rust_create_wrappers before do_configure after do_patch | 
|  | 180 | do_rust_create_wrappers[dirs] += "${WRAPPER_DIR}" |