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