blob: f7f9cbbb2e032666d74b78773cabf017a7b3f760 [file] [log] [blame]
Andrew Geisslerd159c7f2021-09-02 21:05:58 -05001# Common variables used by all Rust builds
2export rustlibdir = "${libdir}/rust"
3FILES:${PN} += "${rustlibdir}/*.so"
4FILES:${PN}-dev += "${rustlibdir}/*.rlib ${rustlibdir}/*.rmeta"
5FILES:${PN}-dbg += "${rustlibdir}/.debug"
6
7RUSTLIB = "-L ${STAGING_LIBDIR}/rust"
8RUST_DEBUG_REMAP = "--remap-path-prefix=${WORKDIR}=/usr/src/debug/${PN}/${EXTENDPE}${PV}-${PR}"
9RUSTFLAGS += "${RUSTLIB} ${RUST_DEBUG_REMAP}"
10RUSTLIB_DEP ?= "libstd-rs"
11export RUST_TARGET_PATH = "${STAGING_LIBDIR_NATIVE}/rustlib"
12RUST_PANIC_STRATEGY ?= "unwind"
13
14# Native builds are not effected by TCLIBC. Without this, rust-native
15# thinks it's "target" (i.e. x86_64-linux) is a musl target.
16RUST_LIBC = "${TCLIBC}"
17RUST_LIBC:class-native = "glibc"
18
19def determine_libc(d, thing):
20 '''Determine which libc something should target'''
21
22 # BUILD is never musl, TARGET may be musl or glibc,
23 # HOST could be musl, but only if a compiler is built to be run on
24 # target in which case HOST_SYS != BUILD_SYS.
25 if thing == 'TARGET':
26 libc = d.getVar('RUST_LIBC')
27 elif thing == 'BUILD' and (d.getVar('HOST_SYS') != d.getVar('BUILD_SYS')):
28 libc = d.getVar('RUST_LIBC')
29 else:
30 libc = d.getVar('RUST_LIBC:class-native')
31
32 return libc
33
34def target_is_armv7(d):
35 '''Determine if target is armv7'''
36 # TUNE_FEATURES may include arm* even if the target is not arm
37 # in the case of *-native packages
38 if d.getVar('TARGET_ARCH') != 'arm':
39 return False
40
41 feat = d.getVar('TUNE_FEATURES')
42 feat = frozenset(feat.split())
43 mach_overrides = d.getVar('MACHINEOVERRIDES')
44 mach_overrides = frozenset(mach_overrides.split(':'))
45
46 v7=frozenset(['armv7a', 'armv7r', 'armv7m', 'armv7ve'])
47 if mach_overrides.isdisjoint(v7) and feat.isdisjoint(v7):
48 return False
49 else:
50 return True
51target_is_armv7[vardepvalue] = "${@target_is_armv7(d)}"
52
53# Responsible for taking Yocto triples and converting it to Rust triples
54def rust_base_triple(d, thing):
55 '''
56 Mangle bitbake's *_SYS into something that rust might support (see
57 rust/mk/cfg/* for a list)
58
59 Note that os is assumed to be some linux form
60 '''
61
62 # The llvm-target for armv7 is armv7-unknown-linux-gnueabihf
63 if thing == "TARGET" and target_is_armv7(d):
64 arch = "armv7"
65 else:
66 arch = d.getVar('{}_ARCH'.format(thing))
67
68 # All the Yocto targets are Linux and are 'unknown'
69 vendor = "-unknown"
70 os = d.getVar('{}_OS'.format(thing))
71 libc = determine_libc(d, thing)
72
73 # Prefix with a dash and convert glibc -> gnu
74 if libc == "glibc":
75 libc = "-gnu"
76 elif libc == "musl":
77 libc = "-musl"
78
79 # Don't double up musl (only appears to be the case on aarch64)
80 if os == "linux-musl":
81 if libc != "-musl":
82 bb.fatal("{}_OS was '{}' but TCLIBC was not 'musl'".format(thing, os))
83 os = "linux"
84
85 # This catches ARM targets and appends the necessary hard float bits
86 if os == "linux-gnueabi" or os == "linux-musleabi":
87 libc = bb.utils.contains('TUNE_FEATURES', 'callconvention-hard', 'hf', '', d)
88 return arch + vendor + '-' + os + libc
89
90# Naming explanation
91# Yocto
92# - BUILD_SYS - Yocto triple of the build environment
93# - HOST_SYS - What we're building for in Yocto
94# - TARGET_SYS - What we're building for in Yocto
95#
96# So when building '-native' packages BUILD_SYS == HOST_SYS == TARGET_SYS
97# When building packages for the image HOST_SYS == TARGET_SYS
98# This is a gross over simplification as there are other modes but
99# currently this is all that's supported.
100#
101# Rust
102# - TARGET - the system where the binary will run
103# - HOST - the system where the binary is being built
104#
105# Rust additionally will use two additional cases:
106# - undecorated (e.g. CC) - equivalent to TARGET
107# - triple suffix (e.g. CC:x86_64_unknown_linux_gnu) - both
108# see: https://github.com/alexcrichton/gcc-rs
109# The way that Rust's internal triples and Yocto triples are mapped together
110# its likely best to not use the triple suffix due to potential confusion.
111
112RUST_BUILD_SYS = "${@rust_base_triple(d, 'BUILD')}"
113RUST_HOST_SYS = "${@rust_base_triple(d, 'HOST')}"
114RUST_TARGET_SYS = "${@rust_base_triple(d, 'TARGET')}"
115
116# wrappers to get around the fact that Rust needs a single
117# binary but Yocto's compiler and linker commands have
118# arguments. Technically the archiver is always one command but
119# this is necessary for builds that determine the prefix and then
120# use those commands based on the prefix.
121WRAPPER_DIR = "${WORKDIR}/wrapper"
122RUST_BUILD_CC = "${WRAPPER_DIR}/build-rust-cc"
123RUST_BUILD_CXX = "${WRAPPER_DIR}/build-rust-cxx"
124RUST_BUILD_CCLD = "${WRAPPER_DIR}/build-rust-ccld"
125RUST_BUILD_AR = "${WRAPPER_DIR}/build-rust-ar"
126RUST_TARGET_CC = "${WRAPPER_DIR}/target-rust-cc"
127RUST_TARGET_CXX = "${WRAPPER_DIR}/target-rust-cxx"
128RUST_TARGET_CCLD = "${WRAPPER_DIR}/target-rust-ccld"
129RUST_TARGET_AR = "${WRAPPER_DIR}/target-rust-ar"
130
131create_wrapper () {
132 file="$1"
133 shift
134
135 cat <<- EOF > "${file}"
136 #!/bin/sh
137 exec $@ "\$@"
138 EOF
139 chmod +x "${file}"
140}
141
142export WRAPPER_TARGET_CC = "${CC}"
143export WRAPPER_TARGET_CXX = "${CXX}"
144export WRAPPER_TARGET_CCLD = "${CCLD}"
145export WRAPPER_TARGET_LDFLAGS = "${LDFLAGS}"
146export WRAPPER_TARGET_AR = "${AR}"
147
148# compiler is used by gcc-rs
149# linker is used by rustc/cargo
150# archiver is used by the build of libstd-rs
151do_rust_create_wrappers () {
152 mkdir -p "${WRAPPER_DIR}"
153
154 # Yocto Build / Rust Host C compiler
155 create_wrapper "${RUST_BUILD_CC}" "${BUILD_CC}"
156 # Yocto Build / Rust Host C++ compiler
157 create_wrapper "${RUST_BUILD_CXX}" "${BUILD_CXX}"
158 # Yocto Build / Rust Host linker
159 create_wrapper "${RUST_BUILD_CCLD}" "${BUILD_CCLD}" "${BUILD_LDFLAGS}"
160 # Yocto Build / Rust Host archiver
161 create_wrapper "${RUST_BUILD_AR}" "${BUILD_AR}"
162
163 # Yocto Target / Rust Target C compiler
164 create_wrapper "${RUST_TARGET_CC}" "${WRAPPER_TARGET_CC}" "${WRAPPER_TARGET_LDFLAGS}"
165 # Yocto Target / Rust Target C++ compiler
166 create_wrapper "${RUST_TARGET_CXX}" "${WRAPPER_TARGET_CXX}"
167 # Yocto Target / Rust Target linker
168 create_wrapper "${RUST_TARGET_CCLD}" "${WRAPPER_TARGET_CCLD}" "${WRAPPER_TARGET_LDFLAGS}"
169 # Yocto Target / Rust Target archiver
170 create_wrapper "${RUST_TARGET_AR}" "${WRAPPER_TARGET_AR}"
171
172 # Need to filter out LD_LIBRARY_PATH from the linker without using shell
173 mv ${RUST_BUILD_CCLD} ${RUST_BUILD_CCLD}.real
174 ${BUILD_CC} ${COREBASE}/meta/files/rust-ccld-wrapper.c -o ${RUST_BUILD_CCLD}
175 mv ${RUST_TARGET_CCLD} ${RUST_TARGET_CCLD}.real
176 ${BUILD_CC} ${COREBASE}/meta/files/rust-ccld-wrapper.c -o ${RUST_TARGET_CCLD}
177}
178
Andrew Geissler5199d832021-09-24 16:47:35 -0500179addtask rust_create_wrappers before do_configure after do_patch do_prepare_recipe_sysroot
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500180do_rust_create_wrappers[dirs] += "${WRAPPER_DIR}"