Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
| 7 | inherit python3native |
| 8 | inherit rust-target-config |
| 9 | |
| 10 | # Common variables used by all Rust builds |
| 11 | export rustlibdir = "${libdir}/rustlib/${RUST_HOST_SYS}/lib" |
| 12 | FILES:${PN} += "${rustlibdir}/*.so" |
| 13 | FILES:${PN}-dev += "${rustlibdir}/*.rlib ${rustlibdir}/*.rmeta" |
| 14 | FILES:${PN}-dbg += "${rustlibdir}/.debug" |
| 15 | |
| 16 | RUSTLIB = "-L ${STAGING_DIR_HOST}${rustlibdir}" |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 17 | RUST_DEBUG_REMAP = "--remap-path-prefix=${WORKDIR}=${TARGET_DBGSRC_DIR}" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 18 | RUSTFLAGS += "${RUSTLIB} ${RUST_DEBUG_REMAP}" |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame] | 19 | RUSTLIB_DEP ??= "libstd-rs" |
| 20 | RUST_PANIC_STRATEGY ??= "unwind" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 21 | |
| 22 | def target_is_armv7(d): |
| 23 | '''Determine if target is armv7''' |
| 24 | # TUNE_FEATURES may include arm* even if the target is not arm |
| 25 | # in the case of *-native packages |
| 26 | if d.getVar('TARGET_ARCH') != 'arm': |
| 27 | return False |
| 28 | |
| 29 | feat = d.getVar('TUNE_FEATURES') |
| 30 | feat = frozenset(feat.split()) |
| 31 | mach_overrides = d.getVar('MACHINEOVERRIDES') |
| 32 | mach_overrides = frozenset(mach_overrides.split(':')) |
| 33 | |
| 34 | v7=frozenset(['armv7a', 'armv7r', 'armv7m', 'armv7ve']) |
| 35 | if mach_overrides.isdisjoint(v7) and feat.isdisjoint(v7): |
| 36 | return False |
| 37 | else: |
| 38 | return True |
| 39 | target_is_armv7[vardepvalue] = "${@target_is_armv7(d)}" |
| 40 | |
| 41 | # Responsible for taking Yocto triples and converting it to Rust triples |
| 42 | def rust_base_triple(d, thing): |
| 43 | ''' |
| 44 | Mangle bitbake's *_SYS into something that rust might support (see |
| 45 | rust/mk/cfg/* for a list) |
| 46 | |
| 47 | Note that os is assumed to be some linux form |
| 48 | ''' |
| 49 | |
| 50 | # The llvm-target for armv7 is armv7-unknown-linux-gnueabihf |
| 51 | if d.getVar('{}_ARCH'.format(thing)) == d.getVar('TARGET_ARCH') and target_is_armv7(d): |
| 52 | arch = "armv7" |
| 53 | else: |
| 54 | arch = oe.rust.arch_to_rust_arch(d.getVar('{}_ARCH'.format(thing))) |
| 55 | |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame] | 56 | # Substituting "unknown" when vendor is empty will match rust's standard |
| 57 | # targets when building native recipes (including rust-native itself) |
| 58 | vendor = d.getVar('{}_VENDOR'.format(thing)) or "-unknown" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 59 | |
| 60 | # Default to glibc |
| 61 | libc = "-gnu" |
| 62 | os = d.getVar('{}_OS'.format(thing)) |
| 63 | # This catches ARM targets and appends the necessary hard float bits |
| 64 | if os == "linux-gnueabi" or os == "linux-musleabi": |
| 65 | libc = bb.utils.contains('TUNE_FEATURES', 'callconvention-hard', 'hf', '', d) |
Patrick Williams | da29531 | 2023-12-05 16:48:56 -0600 | [diff] [blame] | 66 | elif os == "linux-gnux32" or os == "linux-muslx32": |
| 67 | libc = "" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 68 | elif "musl" in os: |
| 69 | libc = "-musl" |
| 70 | os = "linux" |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 71 | elif "elf" in os: |
| 72 | libc = "-elf" |
| 73 | os = "none" |
| 74 | elif "eabi" in os: |
| 75 | libc = "-eabi" |
| 76 | os = "none" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 77 | |
| 78 | return arch + vendor + '-' + os + libc |
| 79 | |
| 80 | |
| 81 | # In some cases uname and the toolchain differ on their idea of the arch name |
| 82 | RUST_BUILD_ARCH = "${@oe.rust.arch_to_rust_arch(d.getVar('BUILD_ARCH'))}" |
| 83 | |
| 84 | # Naming explanation |
| 85 | # Yocto |
| 86 | # - BUILD_SYS - Yocto triple of the build environment |
| 87 | # - HOST_SYS - What we're building for in Yocto |
| 88 | # - TARGET_SYS - What we're building for in Yocto |
| 89 | # |
| 90 | # So when building '-native' packages BUILD_SYS == HOST_SYS == TARGET_SYS |
| 91 | # When building packages for the image HOST_SYS == TARGET_SYS |
| 92 | # This is a gross over simplification as there are other modes but |
| 93 | # currently this is all that's supported. |
| 94 | # |
| 95 | # Rust |
| 96 | # - TARGET - the system where the binary will run |
| 97 | # - HOST - the system where the binary is being built |
| 98 | # |
| 99 | # Rust additionally will use two additional cases: |
| 100 | # - undecorated (e.g. CC) - equivalent to TARGET |
| 101 | # - triple suffix (e.g. CC:x86_64_unknown_linux_gnu) - both |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 102 | # see: https://github.com/rust-lang/cc-rs |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 103 | # The way that Rust's internal triples and Yocto triples are mapped together |
| 104 | # its likely best to not use the triple suffix due to potential confusion. |
| 105 | |
| 106 | RUST_BUILD_SYS = "${@rust_base_triple(d, 'BUILD')}" |
| 107 | RUST_BUILD_SYS[vardepvalue] = "${RUST_BUILD_SYS}" |
| 108 | RUST_HOST_SYS = "${@rust_base_triple(d, 'HOST')}" |
| 109 | RUST_HOST_SYS[vardepvalue] = "${RUST_HOST_SYS}" |
| 110 | RUST_TARGET_SYS = "${@rust_base_triple(d, 'TARGET')}" |
| 111 | RUST_TARGET_SYS[vardepvalue] = "${RUST_TARGET_SYS}" |
| 112 | |
| 113 | # wrappers to get around the fact that Rust needs a single |
| 114 | # binary but Yocto's compiler and linker commands have |
| 115 | # arguments. Technically the archiver is always one command but |
| 116 | # this is necessary for builds that determine the prefix and then |
| 117 | # use those commands based on the prefix. |
| 118 | WRAPPER_DIR = "${WORKDIR}/wrapper" |
| 119 | RUST_BUILD_CC = "${WRAPPER_DIR}/build-rust-cc" |
| 120 | RUST_BUILD_CXX = "${WRAPPER_DIR}/build-rust-cxx" |
| 121 | RUST_BUILD_CCLD = "${WRAPPER_DIR}/build-rust-ccld" |
| 122 | RUST_BUILD_AR = "${WRAPPER_DIR}/build-rust-ar" |
| 123 | RUST_TARGET_CC = "${WRAPPER_DIR}/target-rust-cc" |
| 124 | RUST_TARGET_CXX = "${WRAPPER_DIR}/target-rust-cxx" |
| 125 | RUST_TARGET_CCLD = "${WRAPPER_DIR}/target-rust-ccld" |
| 126 | RUST_TARGET_AR = "${WRAPPER_DIR}/target-rust-ar" |
| 127 | |
| 128 | create_wrapper_rust () { |
| 129 | file="$1" |
| 130 | shift |
| 131 | extras="$1" |
| 132 | shift |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 133 | crate_cc_extras="$1" |
| 134 | shift |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 135 | |
| 136 | cat <<- EOF > "${file}" |
| 137 | #!/usr/bin/env python3 |
| 138 | import os, sys |
| 139 | orig_binary = "$@" |
| 140 | extras = "${extras}" |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 141 | |
| 142 | # Apply a required subset of CC crate compiler flags |
| 143 | # when we build a target recipe for a non-bare-metal target. |
| 144 | # https://github.com/rust-lang/cc-rs/blob/main/src/lib.rs#L1614 |
| 145 | if "CRATE_CC_NO_DEFAULTS" in os.environ.keys() and \ |
| 146 | "TARGET" in os.environ.keys() and not "-none-" in os.environ["TARGET"]: |
| 147 | orig_binary += "${crate_cc_extras}" |
| 148 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 149 | binary = orig_binary.split()[0] |
| 150 | args = orig_binary.split() + sys.argv[1:] |
| 151 | if extras: |
| 152 | args.append(extras) |
| 153 | os.execvp(binary, args) |
| 154 | EOF |
| 155 | chmod +x "${file}" |
| 156 | } |
| 157 | |
| 158 | WRAPPER_TARGET_CC = "${CC}" |
| 159 | WRAPPER_TARGET_CXX = "${CXX}" |
| 160 | WRAPPER_TARGET_CCLD = "${CCLD}" |
| 161 | WRAPPER_TARGET_LDFLAGS = "${LDFLAGS}" |
| 162 | WRAPPER_TARGET_EXTRALD = "" |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 163 | # see recipes-devtools/gcc/gcc/0018-Add-ssp_nonshared-to-link-commandline-for-musl-targe.patch |
| 164 | # we need to link with ssp_nonshared on musl to avoid "undefined reference to `__stack_chk_fail_local'" |
| 165 | # when building MACHINE=qemux86 for musl |
| 166 | WRAPPER_TARGET_EXTRALD:libc-musl = "-lssp_nonshared" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 167 | WRAPPER_TARGET_AR = "${AR}" |
| 168 | |
| 169 | # compiler is used by gcc-rs |
| 170 | # linker is used by rustc/cargo |
| 171 | # archiver is used by the build of libstd-rs |
| 172 | do_rust_create_wrappers () { |
| 173 | mkdir -p "${WRAPPER_DIR}" |
| 174 | |
| 175 | # Yocto Build / Rust Host C compiler |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 176 | create_wrapper_rust "${RUST_BUILD_CC}" "" "${CRATE_CC_FLAGS}" "${BUILD_CC}" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 177 | # Yocto Build / Rust Host C++ compiler |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 178 | create_wrapper_rust "${RUST_BUILD_CXX}" "" "${CRATE_CC_FLAGS}" "${BUILD_CXX}" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 179 | # Yocto Build / Rust Host linker |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 180 | create_wrapper_rust "${RUST_BUILD_CCLD}" "" "" "${BUILD_CCLD}" "${BUILD_LDFLAGS}" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 181 | # Yocto Build / Rust Host archiver |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 182 | create_wrapper_rust "${RUST_BUILD_AR}" "" "" "${BUILD_AR}" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 183 | |
| 184 | # Yocto Target / Rust Target C compiler |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 185 | create_wrapper_rust "${RUST_TARGET_CC}" "${WRAPPER_TARGET_EXTRALD}" "${CRATE_CC_FLAGS}" "${WRAPPER_TARGET_CC}" "${WRAPPER_TARGET_LDFLAGS}" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 186 | # Yocto Target / Rust Target C++ compiler |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 187 | create_wrapper_rust "${RUST_TARGET_CXX}" "${WRAPPER_TARGET_EXTRALD}" "${CRATE_CC_FLAGS}" "${WRAPPER_TARGET_CXX}" "${CXXFLAGS}" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 188 | # Yocto Target / Rust Target linker |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 189 | create_wrapper_rust "${RUST_TARGET_CCLD}" "${WRAPPER_TARGET_EXTRALD}" "" "${WRAPPER_TARGET_CCLD}" "${WRAPPER_TARGET_LDFLAGS}" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 190 | # Yocto Target / Rust Target archiver |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 191 | create_wrapper_rust "${RUST_TARGET_AR}" "" "" "${WRAPPER_TARGET_AR}" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 192 | |
| 193 | } |
| 194 | |
| 195 | addtask rust_create_wrappers before do_configure after do_patch do_prepare_recipe_sysroot |
| 196 | do_rust_create_wrappers[dirs] += "${WRAPPER_DIR}" |