blob: 5e70007377578483a0018c61b5043289d95ab73c [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7inherit python3native
8inherit rust-target-config
9
10# Common variables used by all Rust builds
11export rustlibdir = "${libdir}/rustlib/${RUST_HOST_SYS}/lib"
12FILES:${PN} += "${rustlibdir}/*.so"
13FILES:${PN}-dev += "${rustlibdir}/*.rlib ${rustlibdir}/*.rmeta"
14FILES:${PN}-dbg += "${rustlibdir}/.debug"
15
16RUSTLIB = "-L ${STAGING_DIR_HOST}${rustlibdir}"
17RUST_DEBUG_REMAP = "--remap-path-prefix=${WORKDIR}=/usr/src/debug/${PN}/${EXTENDPE}${PV}-${PR}"
18RUSTFLAGS += "${RUSTLIB} ${RUST_DEBUG_REMAP}"
19RUSTLIB_DEP ?= "libstd-rs"
20RUST_PANIC_STRATEGY ?= "unwind"
21
22def 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
39target_is_armv7[vardepvalue] = "${@target_is_armv7(d)}"
40
41# Responsible for taking Yocto triples and converting it to Rust triples
42def 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
56 # When bootstrapping rust-native, BUILD must be the same as upstream snapshot tarballs
57 bpn = d.getVar('BPN')
58 if thing == "BUILD" and bpn in ["rust"]:
59 return arch + "-unknown-linux-gnu"
60
61 vendor = d.getVar('{}_VENDOR'.format(thing))
62
63 # Default to glibc
64 libc = "-gnu"
65 os = d.getVar('{}_OS'.format(thing))
66 # This catches ARM targets and appends the necessary hard float bits
67 if os == "linux-gnueabi" or os == "linux-musleabi":
68 libc = bb.utils.contains('TUNE_FEATURES', 'callconvention-hard', 'hf', '', d)
69 elif "musl" in os:
70 libc = "-musl"
71 os = "linux"
72
73 return arch + vendor + '-' + os + libc
74
75
76# In some cases uname and the toolchain differ on their idea of the arch name
77RUST_BUILD_ARCH = "${@oe.rust.arch_to_rust_arch(d.getVar('BUILD_ARCH'))}"
78
79# Naming explanation
80# Yocto
81# - BUILD_SYS - Yocto triple of the build environment
82# - HOST_SYS - What we're building for in Yocto
83# - TARGET_SYS - What we're building for in Yocto
84#
85# So when building '-native' packages BUILD_SYS == HOST_SYS == TARGET_SYS
86# When building packages for the image HOST_SYS == TARGET_SYS
87# This is a gross over simplification as there are other modes but
88# currently this is all that's supported.
89#
90# Rust
91# - TARGET - the system where the binary will run
92# - HOST - the system where the binary is being built
93#
94# Rust additionally will use two additional cases:
95# - undecorated (e.g. CC) - equivalent to TARGET
96# - triple suffix (e.g. CC:x86_64_unknown_linux_gnu) - both
Patrick Williams6ad2fb62023-06-15 12:50:14 -050097# see: https://github.com/rust-lang/cc-rs
Patrick Williams92b42cb2022-09-03 06:53:57 -050098# The way that Rust's internal triples and Yocto triples are mapped together
99# its likely best to not use the triple suffix due to potential confusion.
100
101RUST_BUILD_SYS = "${@rust_base_triple(d, 'BUILD')}"
102RUST_BUILD_SYS[vardepvalue] = "${RUST_BUILD_SYS}"
103RUST_HOST_SYS = "${@rust_base_triple(d, 'HOST')}"
104RUST_HOST_SYS[vardepvalue] = "${RUST_HOST_SYS}"
105RUST_TARGET_SYS = "${@rust_base_triple(d, 'TARGET')}"
106RUST_TARGET_SYS[vardepvalue] = "${RUST_TARGET_SYS}"
107
108# wrappers to get around the fact that Rust needs a single
109# binary but Yocto's compiler and linker commands have
110# arguments. Technically the archiver is always one command but
111# this is necessary for builds that determine the prefix and then
112# use those commands based on the prefix.
113WRAPPER_DIR = "${WORKDIR}/wrapper"
114RUST_BUILD_CC = "${WRAPPER_DIR}/build-rust-cc"
115RUST_BUILD_CXX = "${WRAPPER_DIR}/build-rust-cxx"
116RUST_BUILD_CCLD = "${WRAPPER_DIR}/build-rust-ccld"
117RUST_BUILD_AR = "${WRAPPER_DIR}/build-rust-ar"
118RUST_TARGET_CC = "${WRAPPER_DIR}/target-rust-cc"
119RUST_TARGET_CXX = "${WRAPPER_DIR}/target-rust-cxx"
120RUST_TARGET_CCLD = "${WRAPPER_DIR}/target-rust-ccld"
121RUST_TARGET_AR = "${WRAPPER_DIR}/target-rust-ar"
122
123create_wrapper_rust () {
124 file="$1"
125 shift
126 extras="$1"
127 shift
Patrick Williams6ad2fb62023-06-15 12:50:14 -0500128 crate_cc_extras="$1"
129 shift
Patrick Williams92b42cb2022-09-03 06:53:57 -0500130
131 cat <<- EOF > "${file}"
132 #!/usr/bin/env python3
133 import os, sys
134 orig_binary = "$@"
135 extras = "${extras}"
Patrick Williams6ad2fb62023-06-15 12:50:14 -0500136
137 # Apply a required subset of CC crate compiler flags
138 # when we build a target recipe for a non-bare-metal target.
139 # https://github.com/rust-lang/cc-rs/blob/main/src/lib.rs#L1614
140 if "CRATE_CC_NO_DEFAULTS" in os.environ.keys() and \
141 "TARGET" in os.environ.keys() and not "-none-" in os.environ["TARGET"]:
142 orig_binary += "${crate_cc_extras}"
143
Patrick Williams92b42cb2022-09-03 06:53:57 -0500144 binary = orig_binary.split()[0]
145 args = orig_binary.split() + sys.argv[1:]
146 if extras:
147 args.append(extras)
148 os.execvp(binary, args)
149 EOF
150 chmod +x "${file}"
151}
152
153WRAPPER_TARGET_CC = "${CC}"
154WRAPPER_TARGET_CXX = "${CXX}"
155WRAPPER_TARGET_CCLD = "${CCLD}"
156WRAPPER_TARGET_LDFLAGS = "${LDFLAGS}"
157WRAPPER_TARGET_EXTRALD = ""
158WRAPPER_TARGET_AR = "${AR}"
159
160# compiler is used by gcc-rs
161# linker is used by rustc/cargo
162# archiver is used by the build of libstd-rs
163do_rust_create_wrappers () {
164 mkdir -p "${WRAPPER_DIR}"
165
166 # Yocto Build / Rust Host C compiler
Patrick Williams6ad2fb62023-06-15 12:50:14 -0500167 create_wrapper_rust "${RUST_BUILD_CC}" "" "${CRATE_CC_FLAGS}" "${BUILD_CC}"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500168 # Yocto Build / Rust Host C++ compiler
Patrick Williams6ad2fb62023-06-15 12:50:14 -0500169 create_wrapper_rust "${RUST_BUILD_CXX}" "" "${CRATE_CC_FLAGS}" "${BUILD_CXX}"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500170 # Yocto Build / Rust Host linker
Patrick Williams6ad2fb62023-06-15 12:50:14 -0500171 create_wrapper_rust "${RUST_BUILD_CCLD}" "" "" "${BUILD_CCLD}" "${BUILD_LDFLAGS}"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500172 # Yocto Build / Rust Host archiver
Patrick Williams6ad2fb62023-06-15 12:50:14 -0500173 create_wrapper_rust "${RUST_BUILD_AR}" "" "" "${BUILD_AR}"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500174
175 # Yocto Target / Rust Target C compiler
Patrick Williams6ad2fb62023-06-15 12:50:14 -0500176 create_wrapper_rust "${RUST_TARGET_CC}" "${WRAPPER_TARGET_EXTRALD}" "${CRATE_CC_FLAGS}" "${WRAPPER_TARGET_CC}" "${WRAPPER_TARGET_LDFLAGS}"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500177 # Yocto Target / Rust Target C++ compiler
Patrick Williams6ad2fb62023-06-15 12:50:14 -0500178 create_wrapper_rust "${RUST_TARGET_CXX}" "${WRAPPER_TARGET_EXTRALD}" "${CRATE_CC_FLAGS}" "${WRAPPER_TARGET_CXX}" "${CXXFLAGS}"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500179 # Yocto Target / Rust Target linker
Patrick Williams6ad2fb62023-06-15 12:50:14 -0500180 create_wrapper_rust "${RUST_TARGET_CCLD}" "${WRAPPER_TARGET_EXTRALD}" "" "${WRAPPER_TARGET_CCLD}" "${WRAPPER_TARGET_LDFLAGS}"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500181 # Yocto Target / Rust Target archiver
Patrick Williams6ad2fb62023-06-15 12:50:14 -0500182 create_wrapper_rust "${RUST_TARGET_AR}" "" "" "${WRAPPER_TARGET_AR}"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500183
184}
185
186addtask rust_create_wrappers before do_configure after do_patch do_prepare_recipe_sysroot
187do_rust_create_wrappers[dirs] += "${WRAPPER_DIR}"