Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 1 | inherit python3native |
| 2 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 3 | # Common variables used by all Rust builds |
| 4 | export rustlibdir = "${libdir}/rust" |
| 5 | FILES:${PN} += "${rustlibdir}/*.so" |
| 6 | FILES:${PN}-dev += "${rustlibdir}/*.rlib ${rustlibdir}/*.rmeta" |
| 7 | FILES:${PN}-dbg += "${rustlibdir}/.debug" |
| 8 | |
| 9 | RUSTLIB = "-L ${STAGING_LIBDIR}/rust" |
| 10 | RUST_DEBUG_REMAP = "--remap-path-prefix=${WORKDIR}=/usr/src/debug/${PN}/${EXTENDPE}${PV}-${PR}" |
| 11 | RUSTFLAGS += "${RUSTLIB} ${RUST_DEBUG_REMAP}" |
| 12 | RUSTLIB_DEP ?= "libstd-rs" |
| 13 | export RUST_TARGET_PATH = "${STAGING_LIBDIR_NATIVE}/rustlib" |
| 14 | RUST_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. |
| 18 | RUST_LIBC = "${TCLIBC}" |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 19 | RUST_LIBC:class-crosssdk = "glibc" |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 20 | RUST_LIBC:class-native = "glibc" |
| 21 | |
| 22 | def 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 | |
| 37 | def 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 |
| 54 | target_is_armv7[vardepvalue] = "${@target_is_armv7(d)}" |
| 55 | |
| 56 | # Responsible for taking Yocto triples and converting it to Rust triples |
| 57 | def 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: |
Andrew Jeffery | ecdf5f1 | 2022-03-01 01:09:46 +1030 | [diff] [blame] | 69 | arch = oe.rust.arch_to_rust_arch(d.getVar('{}_ARCH'.format(thing))) |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 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 | |
Andrew Jeffery | 1d89bf8 | 2022-03-01 01:09:47 +1030 | [diff] [blame] | 93 | |
| 94 | # In some cases uname and the toolchain differ on their idea of the arch name |
| 95 | RUST_BUILD_ARCH = "${@oe.rust.arch_to_rust_arch(d.getVar('BUILD_ARCH'))}" |
| 96 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 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 | |
| 119 | RUST_BUILD_SYS = "${@rust_base_triple(d, 'BUILD')}" |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 120 | RUST_BUILD_SYS[vardepvalue] = "${RUST_BUILD_SYS}" |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 121 | RUST_HOST_SYS = "${@rust_base_triple(d, 'HOST')}" |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 122 | RUST_HOST_SYS[vardepvalue] = "${RUST_HOST_SYS}" |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 123 | RUST_TARGET_SYS = "${@rust_base_triple(d, 'TARGET')}" |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 124 | RUST_TARGET_SYS[vardepvalue] = "${RUST_TARGET_SYS}" |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 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. |
| 131 | WRAPPER_DIR = "${WORKDIR}/wrapper" |
| 132 | RUST_BUILD_CC = "${WRAPPER_DIR}/build-rust-cc" |
| 133 | RUST_BUILD_CXX = "${WRAPPER_DIR}/build-rust-cxx" |
| 134 | RUST_BUILD_CCLD = "${WRAPPER_DIR}/build-rust-ccld" |
| 135 | RUST_BUILD_AR = "${WRAPPER_DIR}/build-rust-ar" |
| 136 | RUST_TARGET_CC = "${WRAPPER_DIR}/target-rust-cc" |
| 137 | RUST_TARGET_CXX = "${WRAPPER_DIR}/target-rust-cxx" |
| 138 | RUST_TARGET_CCLD = "${WRAPPER_DIR}/target-rust-ccld" |
| 139 | RUST_TARGET_AR = "${WRAPPER_DIR}/target-rust-ar" |
| 140 | |
| 141 | create_wrapper () { |
| 142 | file="$1" |
| 143 | shift |
| 144 | |
| 145 | cat <<- EOF > "${file}" |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 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) |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 152 | EOF |
| 153 | chmod +x "${file}" |
| 154 | } |
| 155 | |
| 156 | export WRAPPER_TARGET_CC = "${CC}" |
| 157 | export WRAPPER_TARGET_CXX = "${CXX}" |
| 158 | export WRAPPER_TARGET_CCLD = "${CCLD}" |
| 159 | export WRAPPER_TARGET_LDFLAGS = "${LDFLAGS}" |
| 160 | export 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 |
| 165 | do_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 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 188 | addtask rust_create_wrappers before do_configure after do_patch do_prepare_recipe_sysroot |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 189 | do_rust_create_wrappers[dirs] += "${WRAPPER_DIR}" |