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