blob: 21a56ede3e5e5b1927613af96ea77d2152c42012 [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7# Right now this is focused on arm-specific tune features.
8# We get away with this for now as one can only use x86-64 as the build host
9# (not arm).
10# Note that TUNE_FEATURES is _always_ refering to the target, so we really
11# don't want to use this for the host/build.
12def llvm_features_from_tune(d):
13 f = []
14 feat = d.getVar('TUNE_FEATURES')
15 if not feat:
16 return []
17 feat = frozenset(feat.split())
18
19 mach_overrides = d.getVar('MACHINEOVERRIDES')
20 mach_overrides = frozenset(mach_overrides.split(':'))
21
22 if 'vfpv4' in feat:
23 f.append("+vfp4")
24 if 'vfpv3' in feat:
25 f.append("+vfp3")
26 if 'vfpv3d16' in feat:
27 f.append("+d16")
28
29 if 'vfpv2' in feat or 'vfp' in feat:
30 f.append("+vfp2")
31
32 if 'neon' in feat:
33 f.append("+neon")
34
35 if 'mips32' in feat:
36 f.append("+mips32")
37
38 if 'mips32r2' in feat:
39 f.append("+mips32r2")
40
41 if target_is_armv7(d):
42 f.append('+v7')
43
44 if ('armv6' in mach_overrides) or ('armv6' in feat):
45 f.append("+v6")
46 if 'armv5te' in feat:
47 f.append("+strict-align")
48 f.append("+v5te")
49 elif 'armv5' in feat:
50 f.append("+strict-align")
51 f.append("+v5")
52
53 if ('armv4' in mach_overrides) or ('armv4' in feat):
54 f.append("+strict-align")
55
56 if 'dsp' in feat:
57 f.append("+dsp")
58
59 if 'thumb' in feat:
60 if d.getVar('ARM_THUMB_OPT') == "thumb":
61 if target_is_armv7(d):
62 f.append('+thumb2')
63 f.append("+thumb-mode")
64
65 if 'cortexa5' in feat:
66 f.append("+a5")
67 if 'cortexa7' in feat:
68 f.append("+a7")
69 if 'cortexa9' in feat:
70 f.append("+a9")
71 if 'cortexa15' in feat:
72 f.append("+a15")
73 if 'cortexa17' in feat:
74 f.append("+a17")
75 if ('riscv64' in feat) or ('riscv32' in feat):
76 f.append("+a,+c,+d,+f,+m")
77 return f
78llvm_features_from_tune[vardepvalue] = "${@llvm_features_from_tune(d)}"
79
80# TARGET_CC_ARCH changes from build/cross/target so it'll do the right thing
81# this should go away when https://github.com/rust-lang/rust/pull/31709 is
82# stable (1.9.0?)
83def llvm_features_from_cc_arch(d):
84 f = []
85 feat = d.getVar('TARGET_CC_ARCH')
86 if not feat:
87 return []
88 feat = frozenset(feat.split())
89
90 if '-mmmx' in feat:
91 f.append("+mmx")
92 if '-msse' in feat:
93 f.append("+sse")
94 if '-msse2' in feat:
95 f.append("+sse2")
96 if '-msse3' in feat:
97 f.append("+sse3")
98 if '-mssse3' in feat:
99 f.append("+ssse3")
100 if '-msse4.1' in feat:
101 f.append("+sse4.1")
102 if '-msse4.2' in feat:
103 f.append("+sse4.2")
104 if '-msse4a' in feat:
105 f.append("+sse4a")
106 if '-mavx' in feat:
107 f.append("+avx")
108 if '-mavx2' in feat:
109 f.append("+avx2")
110
111 return f
112
113def llvm_features_from_target_fpu(d):
114 # TARGET_FPU can be hard or soft. +soft-float tell llvm to use soft float
115 # ABI. There is no option for hard.
116
Patrick Williams864cc432023-02-09 14:54:44 -0600117 fpu = d.getVar('TARGET_FPU')
Patrick Williams92b42cb2022-09-03 06:53:57 -0500118 return ["+soft-float"] if fpu == "soft" else []
119
120def llvm_features(d):
121 return ','.join(llvm_features_from_tune(d) +
122 llvm_features_from_cc_arch(d) +
123 llvm_features_from_target_fpu(d))
124
125llvm_features[vardepvalue] = "${@llvm_features(d)}"
126
127## arm-unknown-linux-gnueabihf
128DATA_LAYOUT[arm-eabi] = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
129TARGET_ENDIAN[arm-eabi] = "little"
130TARGET_POINTER_WIDTH[arm-eabi] = "32"
131TARGET_C_INT_WIDTH[arm-eabi] = "32"
132MAX_ATOMIC_WIDTH[arm-eabi] = "64"
133FEATURES[arm-eabi] = "+v6,+vfp2"
134
135## armv7-unknown-linux-gnueabihf
136DATA_LAYOUT[armv7-eabi] = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
137TARGET_ENDIAN[armv7-eabi] = "little"
138TARGET_POINTER_WIDTH[armv7-eabi] = "32"
139TARGET_C_INT_WIDTH[armv7-eabi] = "32"
140MAX_ATOMIC_WIDTH[armv7-eabi] = "64"
141FEATURES[armv7-eabi] = "+v7,+vfp2,+thumb2"
142
143## aarch64-unknown-linux-{gnu, musl}
144DATA_LAYOUT[aarch64] = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
145TARGET_ENDIAN[aarch64] = "little"
146TARGET_POINTER_WIDTH[aarch64] = "64"
147TARGET_C_INT_WIDTH[aarch64] = "32"
148MAX_ATOMIC_WIDTH[aarch64] = "128"
149
150## x86_64-unknown-linux-{gnu, musl}
151DATA_LAYOUT[x86_64] = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
152TARGET_ENDIAN[x86_64] = "little"
153TARGET_POINTER_WIDTH[x86_64] = "64"
154TARGET_C_INT_WIDTH[x86_64] = "32"
155MAX_ATOMIC_WIDTH[x86_64] = "64"
156
157## x86_64-unknown-linux-gnux32
158DATA_LAYOUT[x86_64-x32] = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
159TARGET_ENDIAN[x86_64-x32] = "little"
160TARGET_POINTER_WIDTH[x86_64-x32] = "32"
161TARGET_C_INT_WIDTH[x86_64-x32] = "32"
162MAX_ATOMIC_WIDTH[x86_64-x32] = "64"
163
164## i686-unknown-linux-{gnu, musl}
165DATA_LAYOUT[i686] = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128"
166TARGET_ENDIAN[i686] = "little"
167TARGET_POINTER_WIDTH[i686] = "32"
168TARGET_C_INT_WIDTH[i686] = "32"
169MAX_ATOMIC_WIDTH[i686] = "64"
170
171## XXX: a bit of a hack so qemux86 builds, clone of i686-unknown-linux-{gnu, musl} above
172DATA_LAYOUT[i586] = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128"
173TARGET_ENDIAN[i586] = "little"
174TARGET_POINTER_WIDTH[i586] = "32"
175TARGET_C_INT_WIDTH[i586] = "32"
176MAX_ATOMIC_WIDTH[i586] = "64"
177
178## mips-unknown-linux-{gnu, musl}
179DATA_LAYOUT[mips] = "E-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64"
180TARGET_ENDIAN[mips] = "big"
181TARGET_POINTER_WIDTH[mips] = "32"
182TARGET_C_INT_WIDTH[mips] = "32"
183MAX_ATOMIC_WIDTH[mips] = "32"
184
185## mipsel-unknown-linux-{gnu, musl}
186DATA_LAYOUT[mipsel] = "e-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64"
187TARGET_ENDIAN[mipsel] = "little"
188TARGET_POINTER_WIDTH[mipsel] = "32"
189TARGET_C_INT_WIDTH[mipsel] = "32"
190MAX_ATOMIC_WIDTH[mipsel] = "32"
191
192## mips64-unknown-linux-{gnu, musl}
193DATA_LAYOUT[mips64] = "E-m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128"
194TARGET_ENDIAN[mips64] = "big"
195TARGET_POINTER_WIDTH[mips64] = "64"
196TARGET_C_INT_WIDTH[mips64] = "64"
197MAX_ATOMIC_WIDTH[mips64] = "64"
198
199## mips64-n32-unknown-linux-{gnu, musl}
200DATA_LAYOUT[mips64-n32] = "E-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-n32:64-S128"
201TARGET_ENDIAN[mips64-n32] = "big"
202TARGET_POINTER_WIDTH[mips64-n32] = "32"
203TARGET_C_INT_WIDTH[mips64-n32] = "32"
204MAX_ATOMIC_WIDTH[mips64-n32] = "64"
205
206## mips64el-unknown-linux-{gnu, musl}
207DATA_LAYOUT[mips64el] = "e-m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128"
208TARGET_ENDIAN[mips64el] = "little"
209TARGET_POINTER_WIDTH[mips64el] = "64"
210TARGET_C_INT_WIDTH[mips64el] = "64"
211MAX_ATOMIC_WIDTH[mips64el] = "64"
212
213## powerpc-unknown-linux-{gnu, musl}
214DATA_LAYOUT[powerpc] = "E-m:e-p:32:32-i64:64-n32"
215TARGET_ENDIAN[powerpc] = "big"
216TARGET_POINTER_WIDTH[powerpc] = "32"
217TARGET_C_INT_WIDTH[powerpc] = "32"
218MAX_ATOMIC_WIDTH[powerpc] = "32"
219
220## powerpc64-unknown-linux-{gnu, musl}
221DATA_LAYOUT[powerpc64] = "E-m:e-i64:64-n32:64-S128-v256:256:256-v512:512:512"
222TARGET_ENDIAN[powerpc64] = "big"
223TARGET_POINTER_WIDTH[powerpc64] = "64"
224TARGET_C_INT_WIDTH[powerpc64] = "64"
225MAX_ATOMIC_WIDTH[powerpc64] = "64"
226
227## powerpc64le-unknown-linux-{gnu, musl}
228DATA_LAYOUT[powerpc64le] = "e-m:e-i64:64-n32:64-v256:256:256-v512:512:512"
229TARGET_ENDIAN[powerpc64le] = "little"
230TARGET_POINTER_WIDTH[powerpc64le] = "64"
231TARGET_C_INT_WIDTH[powerpc64le] = "64"
232MAX_ATOMIC_WIDTH[powerpc64le] = "64"
233
Patrick Williams2390b1b2022-11-03 13:47:49 -0500234## riscv32gc-unknown-linux-{gnu, musl}
235DATA_LAYOUT[riscv32gc] = "e-m:e-p:32:32-i64:64-n32-S128"
236TARGET_ENDIAN[riscv32gc] = "little"
237TARGET_POINTER_WIDTH[riscv32gc] = "32"
238TARGET_C_INT_WIDTH[riscv32gc] = "32"
239MAX_ATOMIC_WIDTH[riscv32gc] = "32"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500240
Patrick Williams2390b1b2022-11-03 13:47:49 -0500241## riscv64gc-unknown-linux-{gnu, musl}
242DATA_LAYOUT[riscv64gc] = "e-m:e-p:64:64-i64:64-i128:128-n64-S128"
243TARGET_ENDIAN[riscv64gc] = "little"
244TARGET_POINTER_WIDTH[riscv64gc] = "64"
245TARGET_C_INT_WIDTH[riscv64gc] = "64"
246MAX_ATOMIC_WIDTH[riscv64gc] = "64"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500247
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500248## loongarch64-unknown-linux-{gnu, musl}
249DATA_LAYOUT[loongarch64] = "e-m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128"
250TARGET_ENDIAN[loongarch64] = "little"
251TARGET_POINTER_WIDTH[loongarch64] = "64"
252TARGET_C_INT_WIDTH[loongarch64] = "32"
253MAX_ATOMIC_WIDTH[loongarch64] = "64"
254FEATURES[loongarch64] = "+d"
255
Patrick Williams92b42cb2022-09-03 06:53:57 -0500256# Convert a normal arch (HOST_ARCH, TARGET_ARCH, BUILD_ARCH, etc) to something
257# rust's internals won't choke on.
258def arch_to_rust_target_arch(arch):
259 if arch == "i586" or arch == "i686":
260 return "x86"
261 elif arch == "mipsel":
262 return "mips"
263 elif arch == "mip64sel":
264 return "mips64"
265 elif arch == "armv7":
266 return "arm"
267 elif arch == "powerpc64le":
268 return "powerpc64"
Patrick Williams2390b1b2022-11-03 13:47:49 -0500269 elif arch == "riscv32gc":
270 return "riscv32"
271 elif arch == "riscv64gc":
272 return "riscv64"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500273 else:
274 return arch
275
Patrick Williams2390b1b2022-11-03 13:47:49 -0500276# Convert a rust target string to a llvm-compatible triplet
277def rust_sys_to_llvm_target(sys):
278 if sys.startswith('riscv32gc-'):
279 return sys.replace('riscv32gc-', 'riscv32-', 1)
280 if sys.startswith('riscv64gc-'):
281 return sys.replace('riscv64gc-', 'riscv64-', 1)
282 return sys
283
Patrick Williams92b42cb2022-09-03 06:53:57 -0500284# generates our target CPU value
285def llvm_cpu(d):
286 cpu = d.getVar('PACKAGE_ARCH')
287 target = d.getVar('TRANSLATED_TARGET_ARCH')
288
289 trans = {}
290 trans['corei7-64'] = "corei7"
291 trans['core2-32'] = "core2"
292 trans['x86-64'] = "x86-64"
293 trans['i686'] = "i686"
294 trans['i586'] = "i586"
295 trans['mips64'] = "mips64"
296 trans['mips64el'] = "mips64"
297 trans['riscv64'] = "generic-rv64"
298 trans['riscv32'] = "generic-rv32"
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500299 trans['loongarch64'] = "la464"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500300
301 if target in ["mips", "mipsel", "powerpc"]:
302 feat = frozenset(d.getVar('TUNE_FEATURES').split())
303 if "mips32r2" in feat:
304 trans['mipsel'] = "mips32r2"
305 trans['mips'] = "mips32r2"
306 elif "mips32" in feat:
307 trans['mipsel'] = "mips32"
308 trans['mips'] = "mips32"
309 elif "ppc7400" in feat:
310 trans['powerpc'] = "7400"
311
312 try:
313 return trans[cpu]
314 except:
315 return trans.get(target, "generic")
316
317llvm_cpu[vardepvalue] = "${@llvm_cpu(d)}"
318
319def rust_gen_target(d, thing, wd, arch):
320 import json
321
322 build_sys = d.getVar('BUILD_SYS')
323 target_sys = d.getVar('TARGET_SYS')
324
325 sys = d.getVar('{}_SYS'.format(thing))
326 prefix = d.getVar('{}_PREFIX'.format(thing))
327 rustsys = d.getVar('RUST_{}_SYS'.format(thing))
328
329 abi = None
330 cpu = "generic"
331 features = ""
332
333 # Need to apply the target tuning consitently, only if the triplet applies to the target
334 # and not in the native case
335 if sys == target_sys and sys != build_sys:
336 abi = d.getVar('ABIEXTENSION')
337 cpu = llvm_cpu(d)
338 if bb.data.inherits_class('native', d):
339 features = ','.join(llvm_features_from_cc_arch(d))
340 else:
341 features = llvm_features(d) or ""
342 # arm and armv7 have different targets in llvm
343 if arch == "arm" and target_is_armv7(d):
344 arch = 'armv7'
345
346 rust_arch = oe.rust.arch_to_rust_arch(arch)
347
348 if abi:
349 arch_abi = "{}-{}".format(rust_arch, abi)
350 else:
351 arch_abi = rust_arch
352
353 features = features or d.getVarFlag('FEATURES', arch_abi) or ""
354 features = features.strip()
355
356 # build tspec
357 tspec = {}
Patrick Williams2390b1b2022-11-03 13:47:49 -0500358 tspec['llvm-target'] = rust_sys_to_llvm_target(rustsys)
Patrick Williams92b42cb2022-09-03 06:53:57 -0500359 tspec['data-layout'] = d.getVarFlag('DATA_LAYOUT', arch_abi)
360 if tspec['data-layout'] is None:
361 bb.fatal("No rust target defined for %s" % arch_abi)
362 tspec['max-atomic-width'] = int(d.getVarFlag('MAX_ATOMIC_WIDTH', arch_abi))
363 tspec['target-pointer-width'] = d.getVarFlag('TARGET_POINTER_WIDTH', arch_abi)
364 tspec['target-c-int-width'] = d.getVarFlag('TARGET_C_INT_WIDTH', arch_abi)
365 tspec['target-endian'] = d.getVarFlag('TARGET_ENDIAN', arch_abi)
366 tspec['arch'] = arch_to_rust_target_arch(rust_arch)
Andrew Geissler517393d2023-01-13 08:55:19 -0600367 if "baremetal" in d.getVar('TCLIBC'):
368 tspec['os'] = "none"
369 else:
370 tspec['os'] = "linux"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500371 if "musl" in tspec['llvm-target']:
372 tspec['env'] = "musl"
373 else:
374 tspec['env'] = "gnu"
375 if "riscv64" in tspec['llvm-target']:
376 tspec['llvm-abiname'] = "lp64d"
377 if "riscv32" in tspec['llvm-target']:
378 tspec['llvm-abiname'] = "ilp32d"
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500379 if "loongarch64" in tspec['llvm-target']:
380 tspec['llvm-abiname'] = "lp64d"
Patrick Williams92b42cb2022-09-03 06:53:57 -0500381 tspec['vendor'] = "unknown"
382 tspec['target-family'] = "unix"
383 tspec['linker'] = "{}{}gcc".format(d.getVar('CCACHE'), prefix)
384 tspec['cpu'] = cpu
385 if features != "":
386 tspec['features'] = features
387 tspec['dynamic-linking'] = True
388 tspec['executables'] = True
389 tspec['linker-is-gnu'] = True
390 tspec['linker-flavor'] = "gcc"
391 tspec['has-rpath'] = True
392 tspec['position-independent-executables'] = True
393 tspec['panic-strategy'] = d.getVar("RUST_PANIC_STRATEGY")
394
395 # write out the target spec json file
396 with open(wd + rustsys + '.json', 'w') as f:
397 json.dump(tspec, f, indent=4)
398
399# These are accounted for in tmpdir path names so don't need to be in the task sig
400rust_gen_target[vardepsexclude] += "ABIEXTENSION llvm_cpu"
401
402do_rust_gen_targets[vardeps] += "DATA_LAYOUT TARGET_ENDIAN TARGET_POINTER_WIDTH TARGET_C_INT_WIDTH MAX_ATOMIC_WIDTH FEATURES"
403
404RUST_TARGETS_DIR = "${WORKDIR}/rust-targets/"
405export RUST_TARGET_PATH = "${RUST_TARGETS_DIR}"
406
407python do_rust_gen_targets () {
408 wd = d.getVar('RUST_TARGETS_DIR')
409 # Order of BUILD, HOST, TARGET is important in case the files overwrite, most specific last
410 rust_gen_target(d, 'BUILD', wd, d.getVar('BUILD_ARCH'))
411 rust_gen_target(d, 'HOST', wd, d.getVar('HOST_ARCH'))
412 rust_gen_target(d, 'TARGET', wd, d.getVar('TARGET_ARCH'))
413}
414
415addtask rust_gen_targets after do_patch before do_compile
416do_rust_gen_targets[dirs] += "${RUST_TARGETS_DIR}"
417
Andrew Geissler517393d2023-01-13 08:55:19 -0600418# For building target C dependecies use only compiler parameters defined in OE
419# and ignore the CC crate defaults which conflicts with OE ones in some cases.
420# https://github.com/rust-lang/cc-rs#external-configuration-via-environment-variables
421# Some CC crate compiler flags are still required.
422# We apply them conditionally in rust wrappers.
423
424CRATE_CC_FLAGS:class-native = ""
425CRATE_CC_FLAGS:class-nativesdk = ""
426CRATE_CC_FLAGS:class-target = " -ffunction-sections -fdata-sections -fPIC"
427
428do_compile:prepend:class-target() {
429 export CRATE_CC_NO_DEFAULTS=1
430}
431do_install:prepend:class-target() {
432 export CRATE_CC_NO_DEFAULTS=1
433}