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