summaryrefslogtreecommitdiffstats
path: root/meta/classes/rust-common.bbclass
diff options
context:
space:
mode:
authorRandy MacLeod <Randy.MacLeod@windriver.com>2021-08-10 17:52:19 (GMT)
committerRichard Purdie <richard.purdie@linuxfoundation.org>2021-08-26 21:09:43 (GMT)
commit61e1570c6a09c1984e919e8c0a82a74c1a08d821 (patch)
treeaccab4b08aa3c62f098c9bb19399efb46906256a /meta/classes/rust-common.bbclass
parent705b1d757fa221614f4f72cabf0fac5884cb6bfd (diff)
downloadpoky-61e1570c6a09c1984e919e8c0a82a74c1a08d821.tar.gz
rust: initial merge of most of meta-rust
In the meta-rust repo at commit: 448047c Upgrade to 1.54.0 (#359) Make the required directories: mkdir ../oe-core/meta/recipes-devtools/rust mkdir ../oe-core/meta/recipes-devtools/cargo mkdir ../oe-core/meta/recipes-example and then: cp recipes-devtools/rust/* ../oe-core/meta/recipes-devtools/rust cp recipes-devtools/cargo/* ../oe-core/meta/recipes-devtools/cargo cp lib/crate.py ../oe-core/meta/lib cp recipes-example/* ../oe-core/meta/recipes-example cp conf/distro/include/rust_* ../oe-core/meta/conf/distro/include/ cp classes/* ../oe-core/meta/classes/ cp recipes-core/packagegroups/packagegroup-rust-cross-canadian.bb ../oe-core/meta/recipes-core/packagegroups (From OE-Core rev: 3ed57578cca93ff1ba4e0bf3f25566e10659a2f9) Signed-off-by: Randy MacLeod <Randy.MacLeod@windriver.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/classes/rust-common.bbclass')
-rw-r--r--meta/classes/rust-common.bbclass174
1 files changed, 174 insertions, 0 deletions
diff --git a/meta/classes/rust-common.bbclass b/meta/classes/rust-common.bbclass
new file mode 100644
index 0000000..2093654
--- /dev/null
+++ b/meta/classes/rust-common.bbclass
@@ -0,0 +1,174 @@
1# 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"
11RUST_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
51
52# Responsible for taking Yocto triples and converting it to Rust triples
53def rust_base_triple(d, thing):
54 '''
55 Mangle bitbake's *_SYS into something that rust might support (see
56 rust/mk/cfg/* for a list)
57
58 Note that os is assumed to be some linux form
59 '''
60
61 # The llvm-target for armv7 is armv7-unknown-linux-gnueabihf
62 if thing == "TARGET" and target_is_armv7(d):
63 arch = "armv7"
64 else:
65 arch = d.getVar('{}_ARCH'.format(thing))
66
67 # All the Yocto targets are Linux and are 'unknown'
68 vendor = "-unknown"
69 os = d.getVar('{}_OS'.format(thing))
70 libc = determine_libc(d, thing)
71
72 # Prefix with a dash and convert glibc -> gnu
73 if libc == "glibc":
74 libc = "-gnu"
75 elif libc == "musl":
76 libc = "-musl"
77
78 # Don't double up musl (only appears to be the case on aarch64)
79 if os == "linux-musl":
80 if libc != "-musl":
81 bb.fatal("{}_OS was '{}' but TCLIBC was not 'musl'".format(thing, os))
82 os = "linux"
83
84 # This catches ARM targets and appends the necessary hard float bits
85 if os == "linux-gnueabi" or os == "linux-musleabi":
86 libc = bb.utils.contains('TUNE_FEATURES', 'callconvention-hard', 'hf', '', d)
87 return arch + vendor + '-' + os + libc
88
89# Naming explanation
90# Yocto
91# - BUILD_SYS - Yocto triple of the build environment
92# - HOST_SYS - What we're building for in Yocto
93# - TARGET_SYS - What we're building for in Yocto
94#
95# So when building '-native' packages BUILD_SYS == HOST_SYS == TARGET_SYS
96# When building packages for the image HOST_SYS == TARGET_SYS
97# This is a gross over simplification as there are other modes but
98# currently this is all that's supported.
99#
100# Rust
101# - TARGET - the system where the binary will run
102# - HOST - the system where the binary is being built
103#
104# Rust additionally will use two additional cases:
105# - undecorated (e.g. CC) - equivalent to TARGET
106# - triple suffix (e.g. CC:x86_64_unknown_linux_gnu) - both
107# see: https://github.com/alexcrichton/gcc-rs
108# The way that Rust's internal triples and Yocto triples are mapped together
109# its likely best to not use the triple suffix due to potential confusion.
110
111RUST_BUILD_SYS = "${@rust_base_triple(d, 'BUILD')}"
112RUST_HOST_SYS = "${@rust_base_triple(d, 'HOST')}"
113RUST_TARGET_SYS = "${@rust_base_triple(d, 'TARGET')}"
114
115# wrappers to get around the fact that Rust needs a single
116# binary but Yocto's compiler and linker commands have
117# arguments. Technically the archiver is always one command but
118# this is necessary for builds that determine the prefix and then
119# use those commands based on the prefix.
120WRAPPER_DIR = "${WORKDIR}/wrapper"
121RUST_BUILD_CC = "${WRAPPER_DIR}/build-rust-cc"
122RUST_BUILD_CXX = "${WRAPPER_DIR}/build-rust-cxx"
123RUST_BUILD_CCLD = "${WRAPPER_DIR}/build-rust-ccld"
124RUST_BUILD_AR = "${WRAPPER_DIR}/build-rust-ar"
125RUST_TARGET_CC = "${WRAPPER_DIR}/target-rust-cc"
126RUST_TARGET_CXX = "${WRAPPER_DIR}/target-rust-cxx"
127RUST_TARGET_CCLD = "${WRAPPER_DIR}/target-rust-ccld"
128RUST_TARGET_AR = "${WRAPPER_DIR}/target-rust-ar"
129
130create_wrapper () {
131 file="$1"
132 shift
133
134 cat <<- EOF > "${file}"
135 #!/bin/sh
136 exec $@ "\$@"
137 EOF
138 chmod +x "${file}"
139}
140
141export WRAPPER_TARGET_CC = "${CC}"
142export WRAPPER_TARGET_CXX = "${CXX}"
143export WRAPPER_TARGET_CCLD = "${CCLD}"
144export WRAPPER_TARGET_LDFLAGS = "${LDFLAGS}"
145export WRAPPER_TARGET_AR = "${AR}"
146
147# compiler is used by gcc-rs
148# linker is used by rustc/cargo
149# archiver is used by the build of libstd-rs
150do_rust_create_wrappers () {
151 mkdir -p "${WRAPPER_DIR}"
152
153 # Yocto Build / Rust Host C compiler
154 create_wrapper "${RUST_BUILD_CC}" "${BUILD_CC}"
155 # Yocto Build / Rust Host C++ compiler
156 create_wrapper "${RUST_BUILD_CXX}" "${BUILD_CXX}"
157 # Yocto Build / Rust Host linker
158 create_wrapper "${RUST_BUILD_CCLD}" "${BUILD_CCLD}" "${BUILD_LDFLAGS}"
159 # Yocto Build / Rust Host archiver
160 create_wrapper "${RUST_BUILD_AR}" "${BUILD_AR}"
161
162 # Yocto Target / Rust Target C compiler
163 create_wrapper "${RUST_TARGET_CC}" "${WRAPPER_TARGET_CC}"
164 # Yocto Target / Rust Target C++ compiler
165 create_wrapper "${RUST_TARGET_CXX}" "${WRAPPER_TARGET_CXX}"
166 # Yocto Target / Rust Target linker
167 create_wrapper "${RUST_TARGET_CCLD}" "${WRAPPER_TARGET_CCLD}" "${WRAPPER_TARGET_LDFLAGS}"
168 # Yocto Target / Rust Target archiver
169 create_wrapper "${RUST_TARGET_AR}" "${WRAPPER_TARGET_AR}"
170
171}
172
173addtask rust_create_wrappers before do_configure after do_patch
174do_rust_create_wrappers[dirs] += "${WRAPPER_DIR}"