blob: 3338de75025ba627759636b6864aeae5b0bc4ab2 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7inherit python3native
8inherit rust-target-config
9
10# Common variables used by all Rust builds
11export rustlibdir = "${libdir}/rustlib/${RUST_HOST_SYS}/lib"
12FILES:${PN} += "${rustlibdir}/*.so"
13FILES:${PN}-dev += "${rustlibdir}/*.rlib ${rustlibdir}/*.rmeta"
14FILES:${PN}-dbg += "${rustlibdir}/.debug"
15
16RUSTLIB = "-L ${STAGING_DIR_HOST}${rustlibdir}"
17RUST_DEBUG_REMAP = "--remap-path-prefix=${WORKDIR}=/usr/src/debug/${PN}/${EXTENDPE}${PV}-${PR}"
18RUSTFLAGS += "${RUSTLIB} ${RUST_DEBUG_REMAP}"
19RUSTLIB_DEP ?= "libstd-rs"
20RUST_PANIC_STRATEGY ?= "unwind"
21
22def target_is_armv7(d):
23 '''Determine if target is armv7'''
24 # TUNE_FEATURES may include arm* even if the target is not arm
25 # in the case of *-native packages
26 if d.getVar('TARGET_ARCH') != 'arm':
27 return False
28
29 feat = d.getVar('TUNE_FEATURES')
30 feat = frozenset(feat.split())
31 mach_overrides = d.getVar('MACHINEOVERRIDES')
32 mach_overrides = frozenset(mach_overrides.split(':'))
33
34 v7=frozenset(['armv7a', 'armv7r', 'armv7m', 'armv7ve'])
35 if mach_overrides.isdisjoint(v7) and feat.isdisjoint(v7):
36 return False
37 else:
38 return True
39target_is_armv7[vardepvalue] = "${@target_is_armv7(d)}"
40
41# Responsible for taking Yocto triples and converting it to Rust triples
42def rust_base_triple(d, thing):
43 '''
44 Mangle bitbake's *_SYS into something that rust might support (see
45 rust/mk/cfg/* for a list)
46
47 Note that os is assumed to be some linux form
48 '''
49
50 # The llvm-target for armv7 is armv7-unknown-linux-gnueabihf
51 if d.getVar('{}_ARCH'.format(thing)) == d.getVar('TARGET_ARCH') and target_is_armv7(d):
52 arch = "armv7"
53 else:
54 arch = oe.rust.arch_to_rust_arch(d.getVar('{}_ARCH'.format(thing)))
55
Patrick Williams2390b1b2022-11-03 13:47:49 -050056 # Substituting "unknown" when vendor is empty will match rust's standard
57 # targets when building native recipes (including rust-native itself)
58 vendor = d.getVar('{}_VENDOR'.format(thing)) or "-unknown"
Patrick Williams92b42cb2022-09-03 06:53:57 -050059
60 # Default to glibc
61 libc = "-gnu"
62 os = d.getVar('{}_OS'.format(thing))
63 # This catches ARM targets and appends the necessary hard float bits
64 if os == "linux-gnueabi" or os == "linux-musleabi":
65 libc = bb.utils.contains('TUNE_FEATURES', 'callconvention-hard', 'hf', '', d)
66 elif "musl" in os:
67 libc = "-musl"
68 os = "linux"
69
70 return arch + vendor + '-' + os + libc
71
72
73# In some cases uname and the toolchain differ on their idea of the arch name
74RUST_BUILD_ARCH = "${@oe.rust.arch_to_rust_arch(d.getVar('BUILD_ARCH'))}"
75
76# Naming explanation
77# Yocto
78# - BUILD_SYS - Yocto triple of the build environment
79# - HOST_SYS - What we're building for in Yocto
80# - TARGET_SYS - What we're building for in Yocto
81#
82# So when building '-native' packages BUILD_SYS == HOST_SYS == TARGET_SYS
83# When building packages for the image HOST_SYS == TARGET_SYS
84# This is a gross over simplification as there are other modes but
85# currently this is all that's supported.
86#
87# Rust
88# - TARGET - the system where the binary will run
89# - HOST - the system where the binary is being built
90#
91# Rust additionally will use two additional cases:
92# - undecorated (e.g. CC) - equivalent to TARGET
93# - triple suffix (e.g. CC:x86_64_unknown_linux_gnu) - both
94# see: https://github.com/alexcrichton/gcc-rs
95# The way that Rust's internal triples and Yocto triples are mapped together
96# its likely best to not use the triple suffix due to potential confusion.
97
98RUST_BUILD_SYS = "${@rust_base_triple(d, 'BUILD')}"
99RUST_BUILD_SYS[vardepvalue] = "${RUST_BUILD_SYS}"
100RUST_HOST_SYS = "${@rust_base_triple(d, 'HOST')}"
101RUST_HOST_SYS[vardepvalue] = "${RUST_HOST_SYS}"
102RUST_TARGET_SYS = "${@rust_base_triple(d, 'TARGET')}"
103RUST_TARGET_SYS[vardepvalue] = "${RUST_TARGET_SYS}"
104
105# wrappers to get around the fact that Rust needs a single
106# binary but Yocto's compiler and linker commands have
107# arguments. Technically the archiver is always one command but
108# this is necessary for builds that determine the prefix and then
109# use those commands based on the prefix.
110WRAPPER_DIR = "${WORKDIR}/wrapper"
111RUST_BUILD_CC = "${WRAPPER_DIR}/build-rust-cc"
112RUST_BUILD_CXX = "${WRAPPER_DIR}/build-rust-cxx"
113RUST_BUILD_CCLD = "${WRAPPER_DIR}/build-rust-ccld"
114RUST_BUILD_AR = "${WRAPPER_DIR}/build-rust-ar"
115RUST_TARGET_CC = "${WRAPPER_DIR}/target-rust-cc"
116RUST_TARGET_CXX = "${WRAPPER_DIR}/target-rust-cxx"
117RUST_TARGET_CCLD = "${WRAPPER_DIR}/target-rust-ccld"
118RUST_TARGET_AR = "${WRAPPER_DIR}/target-rust-ar"
119
120create_wrapper_rust () {
121 file="$1"
122 shift
123 extras="$1"
124 shift
125
126 cat <<- EOF > "${file}"
127 #!/usr/bin/env python3
128 import os, sys
129 orig_binary = "$@"
130 extras = "${extras}"
131 binary = orig_binary.split()[0]
132 args = orig_binary.split() + sys.argv[1:]
133 if extras:
134 args.append(extras)
135 os.execvp(binary, args)
136 EOF
137 chmod +x "${file}"
138}
139
140WRAPPER_TARGET_CC = "${CC}"
141WRAPPER_TARGET_CXX = "${CXX}"
142WRAPPER_TARGET_CCLD = "${CCLD}"
143WRAPPER_TARGET_LDFLAGS = "${LDFLAGS}"
144WRAPPER_TARGET_EXTRALD = ""
145WRAPPER_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 "${RUST_BUILD_CC}" "" "${BUILD_CC}"
155 # Yocto Build / Rust Host C++ compiler
156 create_wrapper_rust "${RUST_BUILD_CXX}" "" "${BUILD_CXX}"
157 # Yocto Build / Rust Host linker
158 create_wrapper_rust "${RUST_BUILD_CCLD}" "" "${BUILD_CCLD}" "${BUILD_LDFLAGS}"
159 # Yocto Build / Rust Host archiver
160 create_wrapper_rust "${RUST_BUILD_AR}" "" "${BUILD_AR}"
161
162 # Yocto Target / Rust Target C compiler
163 create_wrapper_rust "${RUST_TARGET_CC}" "${WRAPPER_TARGET_EXTRALD}" "${WRAPPER_TARGET_CC}" "${WRAPPER_TARGET_LDFLAGS}"
164 # Yocto Target / Rust Target C++ compiler
165 create_wrapper_rust "${RUST_TARGET_CXX}" "${WRAPPER_TARGET_EXTRALD}" "${WRAPPER_TARGET_CXX}" "${CXXFLAGS}"
166 # Yocto Target / Rust Target linker
167 create_wrapper_rust "${RUST_TARGET_CCLD}" "${WRAPPER_TARGET_EXTRALD}" "${WRAPPER_TARGET_CCLD}" "${WRAPPER_TARGET_LDFLAGS}"
168 # Yocto Target / Rust Target archiver
169 create_wrapper_rust "${RUST_TARGET_AR}" "" "${WRAPPER_TARGET_AR}"
170
171}
172
173addtask rust_create_wrappers before do_configure after do_patch do_prepare_recipe_sysroot
174do_rust_create_wrappers[dirs] += "${WRAPPER_DIR}"