blob: 02a538258af662ec75eeed082db8462cb4c4f999 [file] [log] [blame]
Andrew Geisslereff27472021-10-29 15:35:00 -05001inherit python3native
2
Andrew Geisslerd159c7f2021-09-02 21:05:58 -05003# 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}"
Andrew Geissler9aee5002022-03-30 16:27:02 +000019RUST_LIBC:class-crosssdk = "glibc"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050020RUST_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:
Andrew Jefferyecdf5f12022-03-01 01:09:46 +103069 arch = oe.rust.arch_to_rust_arch(d.getVar('{}_ARCH'.format(thing)))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050070
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 Jeffery1d89bf82022-03-01 01:09:47 +103093
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
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050097# 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_HOST_SYS = "${@rust_base_triple(d, 'HOST')}"
121RUST_TARGET_SYS = "${@rust_base_triple(d, 'TARGET')}"
122
123# wrappers to get around the fact that Rust needs a single
124# binary but Yocto's compiler and linker commands have
125# arguments. Technically the archiver is always one command but
126# this is necessary for builds that determine the prefix and then
127# use those commands based on the prefix.
128WRAPPER_DIR = "${WORKDIR}/wrapper"
129RUST_BUILD_CC = "${WRAPPER_DIR}/build-rust-cc"
130RUST_BUILD_CXX = "${WRAPPER_DIR}/build-rust-cxx"
131RUST_BUILD_CCLD = "${WRAPPER_DIR}/build-rust-ccld"
132RUST_BUILD_AR = "${WRAPPER_DIR}/build-rust-ar"
133RUST_TARGET_CC = "${WRAPPER_DIR}/target-rust-cc"
134RUST_TARGET_CXX = "${WRAPPER_DIR}/target-rust-cxx"
135RUST_TARGET_CCLD = "${WRAPPER_DIR}/target-rust-ccld"
136RUST_TARGET_AR = "${WRAPPER_DIR}/target-rust-ar"
137
138create_wrapper () {
139 file="$1"
140 shift
141
142 cat <<- EOF > "${file}"
Andrew Geisslereff27472021-10-29 15:35:00 -0500143 #!/usr/bin/env python3
144 import os, sys
145 orig_binary = "$@"
146 binary = orig_binary.split()[0]
147 args = orig_binary.split() + sys.argv[1:]
148 os.execvp(binary, args)
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500149 EOF
150 chmod +x "${file}"
151}
152
153export WRAPPER_TARGET_CC = "${CC}"
154export WRAPPER_TARGET_CXX = "${CXX}"
155export WRAPPER_TARGET_CCLD = "${CCLD}"
156export WRAPPER_TARGET_LDFLAGS = "${LDFLAGS}"
157export WRAPPER_TARGET_AR = "${AR}"
158
159# compiler is used by gcc-rs
160# linker is used by rustc/cargo
161# archiver is used by the build of libstd-rs
162do_rust_create_wrappers () {
163 mkdir -p "${WRAPPER_DIR}"
164
165 # Yocto Build / Rust Host C compiler
166 create_wrapper "${RUST_BUILD_CC}" "${BUILD_CC}"
167 # Yocto Build / Rust Host C++ compiler
168 create_wrapper "${RUST_BUILD_CXX}" "${BUILD_CXX}"
169 # Yocto Build / Rust Host linker
170 create_wrapper "${RUST_BUILD_CCLD}" "${BUILD_CCLD}" "${BUILD_LDFLAGS}"
171 # Yocto Build / Rust Host archiver
172 create_wrapper "${RUST_BUILD_AR}" "${BUILD_AR}"
173
174 # Yocto Target / Rust Target C compiler
175 create_wrapper "${RUST_TARGET_CC}" "${WRAPPER_TARGET_CC}" "${WRAPPER_TARGET_LDFLAGS}"
176 # Yocto Target / Rust Target C++ compiler
177 create_wrapper "${RUST_TARGET_CXX}" "${WRAPPER_TARGET_CXX}"
178 # Yocto Target / Rust Target linker
179 create_wrapper "${RUST_TARGET_CCLD}" "${WRAPPER_TARGET_CCLD}" "${WRAPPER_TARGET_LDFLAGS}"
180 # Yocto Target / Rust Target archiver
181 create_wrapper "${RUST_TARGET_AR}" "${WRAPPER_TARGET_AR}"
182
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500183}
184
Andrew Geissler5199d832021-09-24 16:47:35 -0500185addtask rust_create_wrappers before do_configure after do_patch do_prepare_recipe_sysroot
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500186do_rust_create_wrappers[dirs] += "${WRAPPER_DIR}"