blob: 98d65970e8c077c7aa23543b99abbd191f1dd629 [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:
68 arch = d.getVar('{}_ARCH'.format(thing))
69
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
92# Naming explanation
93# Yocto
94# - BUILD_SYS - Yocto triple of the build environment
95# - HOST_SYS - What we're building for in Yocto
96# - TARGET_SYS - What we're building for in Yocto
97#
98# So when building '-native' packages BUILD_SYS == HOST_SYS == TARGET_SYS
99# When building packages for the image HOST_SYS == TARGET_SYS
100# This is a gross over simplification as there are other modes but
101# currently this is all that's supported.
102#
103# Rust
104# - TARGET - the system where the binary will run
105# - HOST - the system where the binary is being built
106#
107# Rust additionally will use two additional cases:
108# - undecorated (e.g. CC) - equivalent to TARGET
109# - triple suffix (e.g. CC:x86_64_unknown_linux_gnu) - both
110# see: https://github.com/alexcrichton/gcc-rs
111# The way that Rust's internal triples and Yocto triples are mapped together
112# its likely best to not use the triple suffix due to potential confusion.
113
114RUST_BUILD_SYS = "${@rust_base_triple(d, 'BUILD')}"
115RUST_HOST_SYS = "${@rust_base_triple(d, 'HOST')}"
116RUST_TARGET_SYS = "${@rust_base_triple(d, 'TARGET')}"
117
118# wrappers to get around the fact that Rust needs a single
119# binary but Yocto's compiler and linker commands have
120# arguments. Technically the archiver is always one command but
121# this is necessary for builds that determine the prefix and then
122# use those commands based on the prefix.
123WRAPPER_DIR = "${WORKDIR}/wrapper"
124RUST_BUILD_CC = "${WRAPPER_DIR}/build-rust-cc"
125RUST_BUILD_CXX = "${WRAPPER_DIR}/build-rust-cxx"
126RUST_BUILD_CCLD = "${WRAPPER_DIR}/build-rust-ccld"
127RUST_BUILD_AR = "${WRAPPER_DIR}/build-rust-ar"
128RUST_TARGET_CC = "${WRAPPER_DIR}/target-rust-cc"
129RUST_TARGET_CXX = "${WRAPPER_DIR}/target-rust-cxx"
130RUST_TARGET_CCLD = "${WRAPPER_DIR}/target-rust-ccld"
131RUST_TARGET_AR = "${WRAPPER_DIR}/target-rust-ar"
132
133create_wrapper () {
134 file="$1"
135 shift
136
137 cat <<- EOF > "${file}"
Andrew Geisslereff27472021-10-29 15:35:00 -0500138 #!/usr/bin/env python3
139 import os, sys
140 orig_binary = "$@"
141 binary = orig_binary.split()[0]
142 args = orig_binary.split() + sys.argv[1:]
143 os.execvp(binary, args)
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500144 EOF
145 chmod +x "${file}"
146}
147
148export WRAPPER_TARGET_CC = "${CC}"
149export WRAPPER_TARGET_CXX = "${CXX}"
150export WRAPPER_TARGET_CCLD = "${CCLD}"
151export WRAPPER_TARGET_LDFLAGS = "${LDFLAGS}"
152export WRAPPER_TARGET_AR = "${AR}"
153
154# compiler is used by gcc-rs
155# linker is used by rustc/cargo
156# archiver is used by the build of libstd-rs
157do_rust_create_wrappers () {
158 mkdir -p "${WRAPPER_DIR}"
159
160 # Yocto Build / Rust Host C compiler
161 create_wrapper "${RUST_BUILD_CC}" "${BUILD_CC}"
162 # Yocto Build / Rust Host C++ compiler
163 create_wrapper "${RUST_BUILD_CXX}" "${BUILD_CXX}"
164 # Yocto Build / Rust Host linker
165 create_wrapper "${RUST_BUILD_CCLD}" "${BUILD_CCLD}" "${BUILD_LDFLAGS}"
166 # Yocto Build / Rust Host archiver
167 create_wrapper "${RUST_BUILD_AR}" "${BUILD_AR}"
168
169 # Yocto Target / Rust Target C compiler
170 create_wrapper "${RUST_TARGET_CC}" "${WRAPPER_TARGET_CC}" "${WRAPPER_TARGET_LDFLAGS}"
171 # Yocto Target / Rust Target C++ compiler
172 create_wrapper "${RUST_TARGET_CXX}" "${WRAPPER_TARGET_CXX}"
173 # Yocto Target / Rust Target linker
174 create_wrapper "${RUST_TARGET_CCLD}" "${WRAPPER_TARGET_CCLD}" "${WRAPPER_TARGET_LDFLAGS}"
175 # Yocto Target / Rust Target archiver
176 create_wrapper "${RUST_TARGET_AR}" "${WRAPPER_TARGET_AR}"
177
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500178}
179
Andrew Geissler5199d832021-09-24 16:47:35 -0500180addtask rust_create_wrappers before do_configure after do_patch do_prepare_recipe_sysroot
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500181do_rust_create_wrappers[dirs] += "${WRAPPER_DIR}"