blob: 87b7dee3edc4b657b14bf7c7c4cac3661da47275 [file] [log] [blame]
Andrew Geisslerd159c7f2021-09-02 21:05:58 -05001# Right now this is focused on arm-specific tune features.
2# We get away with this for now as one can only use x86-64 as the build host
3# (not arm).
4# Note that TUNE_FEATURES is _always_ refering to the target, so we really
5# don't want to use this for the host/build.
6def llvm_features_from_tune(d):
7 f = []
8 feat = d.getVar('TUNE_FEATURES')
9 if not feat:
10 return []
11 feat = frozenset(feat.split())
12
13 mach_overrides = d.getVar('MACHINEOVERRIDES')
14 mach_overrides = frozenset(mach_overrides.split(':'))
15
16 if 'vfpv4' in feat:
17 f.append("+vfp4")
18 if 'vfpv3' in feat:
19 f.append("+vfp3")
20 if 'vfpv3d16' in feat:
21 f.append("+d16")
22
23 if 'vfpv2' in feat or 'vfp' in feat:
24 f.append("+vfp2")
25
26 if 'neon' in feat:
27 f.append("+neon")
28
29 if 'mips32' in feat:
30 f.append("+mips32")
31
32 if 'mips32r2' in feat:
33 f.append("+mips32r2")
34
35 if target_is_armv7(d):
36 f.append('+v7')
37
38 if ('armv6' in mach_overrides) or ('armv6' in feat):
39 f.append("+v6")
40 if 'armv5te' in feat:
41 f.append("+strict-align")
42 f.append("+v5te")
43 elif 'armv5' in feat:
44 f.append("+strict-align")
45 f.append("+v5")
46
47 if ('armv4' in mach_overrides) or ('armv4' in feat):
48 f.append("+strict-align")
49
50 if 'dsp' in feat:
51 f.append("+dsp")
52
53 if 'thumb' in feat:
54 if d.getVar('ARM_THUMB_OPT') == "thumb":
55 if target_is_armv7(d):
56 f.append('+thumb2')
57 f.append("+thumb-mode")
58
59 if 'cortexa5' in feat:
60 f.append("+a5")
61 if 'cortexa7' in feat:
62 f.append("+a7")
63 if 'cortexa9' in feat:
64 f.append("+a9")
65 if 'cortexa15' in feat:
66 f.append("+a15")
67 if 'cortexa17' in feat:
68 f.append("+a17")
69 if ('riscv64' in feat) or ('riscv32' in feat):
70 f.append("+a,+c,+d,+f,+m")
71 return f
72llvm_features_from_tune[vardepvalue] = "${@llvm_features_from_tune(d)}"
73
74# TARGET_CC_ARCH changes from build/cross/target so it'll do the right thing
75# this should go away when https://github.com/rust-lang/rust/pull/31709 is
76# stable (1.9.0?)
77def llvm_features_from_cc_arch(d):
78 f = []
79 feat = d.getVar('TARGET_CC_ARCH')
80 if not feat:
81 return []
82 feat = frozenset(feat.split())
83
84 if '-mmmx' in feat:
85 f.append("+mmx")
86 if '-msse' in feat:
87 f.append("+sse")
88 if '-msse2' in feat:
89 f.append("+sse2")
90 if '-msse3' in feat:
91 f.append("+sse3")
92 if '-mssse3' in feat:
93 f.append("+ssse3")
94 if '-msse4.1' in feat:
95 f.append("+sse4.1")
96 if '-msse4.2' in feat:
97 f.append("+sse4.2")
98 if '-msse4a' in feat:
99 f.append("+sse4a")
100 if '-mavx' in feat:
101 f.append("+avx")
102 if '-mavx2' in feat:
103 f.append("+avx2")
104
105 return f
106
107def llvm_features_from_target_fpu(d):
108 # TARGET_FPU can be hard or soft. +soft-float tell llvm to use soft float
109 # ABI. There is no option for hard.
110
111 fpu = d.getVar('TARGET_FPU', True)
112 return ["+soft-float"] if fpu == "soft" else []
113
114def llvm_features(d):
115 return ','.join(llvm_features_from_tune(d) +
116 llvm_features_from_cc_arch(d) +
117 llvm_features_from_target_fpu(d))
118
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500119llvm_features[vardepvalue] = "${@llvm_features(d)}"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500120
121## arm-unknown-linux-gnueabihf
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500122DATA_LAYOUT[arm-eabi] = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
123TARGET_ENDIAN[arm-eabi] = "little"
124TARGET_POINTER_WIDTH[arm-eabi] = "32"
125TARGET_C_INT_WIDTH[arm-eabi] = "32"
126MAX_ATOMIC_WIDTH[arm-eabi] = "64"
127FEATURES[arm-eabi] = "+v6,+vfp2"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500128
129## armv7-unknown-linux-gnueabihf
Andrew Geisslereff27472021-10-29 15:35:00 -0500130DATA_LAYOUT[armv7-eabi] = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
Andrew Geisslereff27472021-10-29 15:35:00 -0500131TARGET_ENDIAN[armv7-eabi] = "little"
132TARGET_POINTER_WIDTH[armv7-eabi] = "32"
133TARGET_C_INT_WIDTH[armv7-eabi] = "32"
134MAX_ATOMIC_WIDTH[armv7-eabi] = "64"
135FEATURES[armv7-eabi] = "+v7,+vfp2,+thumb2"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500136
137## aarch64-unknown-linux-{gnu, musl}
138DATA_LAYOUT[aarch64] = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500139TARGET_ENDIAN[aarch64] = "little"
140TARGET_POINTER_WIDTH[aarch64] = "64"
141TARGET_C_INT_WIDTH[aarch64] = "32"
142MAX_ATOMIC_WIDTH[aarch64] = "128"
143
144## x86_64-unknown-linux-{gnu, musl}
145DATA_LAYOUT[x86_64] = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500146TARGET_ENDIAN[x86_64] = "little"
147TARGET_POINTER_WIDTH[x86_64] = "64"
148TARGET_C_INT_WIDTH[x86_64] = "32"
149MAX_ATOMIC_WIDTH[x86_64] = "64"
150
Andrew Geisslereff27472021-10-29 15:35:00 -0500151## x86_64-unknown-linux-gnux32
152DATA_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"
Andrew Geisslereff27472021-10-29 15:35:00 -0500153TARGET_ENDIAN[x86_64-x32] = "little"
154TARGET_POINTER_WIDTH[x86_64-x32] = "32"
155TARGET_C_INT_WIDTH[x86_64-x32] = "32"
156MAX_ATOMIC_WIDTH[x86_64-x32] = "64"
157
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500158## i686-unknown-linux-{gnu, musl}
159DATA_LAYOUT[i686] = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500160TARGET_ENDIAN[i686] = "little"
161TARGET_POINTER_WIDTH[i686] = "32"
162TARGET_C_INT_WIDTH[i686] = "32"
163MAX_ATOMIC_WIDTH[i686] = "64"
164
165## XXX: a bit of a hack so qemux86 builds, clone of i686-unknown-linux-{gnu, musl} above
166DATA_LAYOUT[i586] = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500167TARGET_ENDIAN[i586] = "little"
168TARGET_POINTER_WIDTH[i586] = "32"
169TARGET_C_INT_WIDTH[i586] = "32"
170MAX_ATOMIC_WIDTH[i586] = "64"
171
172## mips-unknown-linux-{gnu, musl}
173DATA_LAYOUT[mips] = "E-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500174TARGET_ENDIAN[mips] = "big"
175TARGET_POINTER_WIDTH[mips] = "32"
176TARGET_C_INT_WIDTH[mips] = "32"
177MAX_ATOMIC_WIDTH[mips] = "32"
178
179## mipsel-unknown-linux-{gnu, musl}
180DATA_LAYOUT[mipsel] = "e-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500181TARGET_ENDIAN[mipsel] = "little"
182TARGET_POINTER_WIDTH[mipsel] = "32"
183TARGET_C_INT_WIDTH[mipsel] = "32"
184MAX_ATOMIC_WIDTH[mipsel] = "32"
185
186## mips64-unknown-linux-{gnu, musl}
187DATA_LAYOUT[mips64] = "E-m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500188TARGET_ENDIAN[mips64] = "big"
189TARGET_POINTER_WIDTH[mips64] = "64"
190TARGET_C_INT_WIDTH[mips64] = "64"
191MAX_ATOMIC_WIDTH[mips64] = "64"
192
193## mips64el-unknown-linux-{gnu, musl}
194DATA_LAYOUT[mips64el] = "e-m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500195TARGET_ENDIAN[mips64el] = "little"
196TARGET_POINTER_WIDTH[mips64el] = "64"
197TARGET_C_INT_WIDTH[mips64el] = "64"
198MAX_ATOMIC_WIDTH[mips64el] = "64"
199
200## powerpc-unknown-linux-{gnu, musl}
201DATA_LAYOUT[powerpc] = "E-m:e-p:32:32-i64:64-n32"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500202TARGET_ENDIAN[powerpc] = "big"
203TARGET_POINTER_WIDTH[powerpc] = "32"
204TARGET_C_INT_WIDTH[powerpc] = "32"
205MAX_ATOMIC_WIDTH[powerpc] = "32"
206
Andrew Geisslereff27472021-10-29 15:35:00 -0500207## powerpc64-unknown-linux-{gnu, musl}
208DATA_LAYOUT[powerpc64] = "E-m:e-i64:64-n32:64-S128-v256:256:256-v512:512:512"
Andrew Geisslereff27472021-10-29 15:35:00 -0500209TARGET_ENDIAN[powerpc64] = "big"
210TARGET_POINTER_WIDTH[powerpc64] = "64"
211TARGET_C_INT_WIDTH[powerpc64] = "64"
212MAX_ATOMIC_WIDTH[powerpc64] = "64"
213
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500214## powerpc64le-unknown-linux-{gnu, musl}
215DATA_LAYOUT[powerpc64le] = "e-m:e-i64:64-n32:64-v256:256:256-v512:512:512"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500216TARGET_ENDIAN[powerpc64le] = "little"
217TARGET_POINTER_WIDTH[powerpc64le] = "64"
218TARGET_C_INT_WIDTH[powerpc64le] = "64"
219MAX_ATOMIC_WIDTH[powerpc64le] = "64"
220
221## riscv32-unknown-linux-{gnu, musl}
222DATA_LAYOUT[riscv32] = "e-m:e-p:32:32-i64:64-n32-S128"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500223TARGET_ENDIAN[riscv32] = "little"
224TARGET_POINTER_WIDTH[riscv32] = "32"
225TARGET_C_INT_WIDTH[riscv32] = "32"
226MAX_ATOMIC_WIDTH[riscv32] = "32"
227
228## riscv64-unknown-linux-{gnu, musl}
229DATA_LAYOUT[riscv64] = "e-m:e-p:64:64-i64:64-i128:128-n64-S128"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500230TARGET_ENDIAN[riscv64] = "little"
231TARGET_POINTER_WIDTH[riscv64] = "64"
232TARGET_C_INT_WIDTH[riscv64] = "64"
233MAX_ATOMIC_WIDTH[riscv64] = "64"
234
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500235# Convert a normal arch (HOST_ARCH, TARGET_ARCH, BUILD_ARCH, etc) to something
236# rust's internals won't choke on.
237def arch_to_rust_target_arch(arch):
238 if arch == "i586" or arch == "i686":
239 return "x86"
240 elif arch == "mipsel":
241 return "mips"
242 elif arch == "mip64sel":
243 return "mips64"
244 elif arch == "armv7":
245 return "arm"
246 elif arch == "powerpc64le":
247 return "powerpc64"
248 else:
249 return arch
250
251# generates our target CPU value
252def llvm_cpu(d):
253 cpu = d.getVar('PACKAGE_ARCH')
254 target = d.getVar('TRANSLATED_TARGET_ARCH')
255
256 trans = {}
257 trans['corei7-64'] = "corei7"
258 trans['core2-32'] = "core2"
259 trans['x86-64'] = "x86-64"
260 trans['i686'] = "i686"
261 trans['i586'] = "i586"
262 trans['powerpc'] = "powerpc"
263 trans['mips64'] = "mips64"
264 trans['mips64el'] = "mips64"
265 trans['riscv64'] = "generic-rv64"
266 trans['riscv32'] = "generic-rv32"
267
268 if target in ["mips", "mipsel"]:
269 feat = frozenset(d.getVar('TUNE_FEATURES').split())
270 if "mips32r2" in feat:
271 trans['mipsel'] = "mips32r2"
272 trans['mips'] = "mips32r2"
273 elif "mips32" in feat:
274 trans['mipsel'] = "mips32"
275 trans['mips'] = "mips32"
276
277 try:
278 return trans[cpu]
279 except:
280 return trans.get(target, "generic")
281
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500282llvm_cpu[vardepvalue] = "${@llvm_cpu(d)}"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500283
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500284def rust_gen_target(d, thing, wd, arch):
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500285 import json
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500286 sys = d.getVar('{}_SYS'.format(thing))
287 prefix = d.getVar('{}_PREFIX'.format(thing))
288
289 abi = None
290 cpu = "generic"
291 features = ""
292
293 if thing == "TARGET":
294 abi = d.getVar('ABIEXTENSION')
295 cpu = llvm_cpu(d)
296 if bb.data.inherits_class('native', d):
297 features = ','.join(llvm_features_from_cc_arch(d))
298 else:
299 features = llvm_features(d) or ""
300 # arm and armv7 have different targets in llvm
301 if arch == "arm" and target_is_armv7(d):
302 arch = 'armv7'
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500303
Andrew Jefferyecdf5f12022-03-01 01:09:46 +1030304 rust_arch = oe.rust.arch_to_rust_arch(arch)
305
Andrew Geisslereff27472021-10-29 15:35:00 -0500306 if abi:
Andrew Jefferyecdf5f12022-03-01 01:09:46 +1030307 arch_abi = "{}-{}".format(rust_arch, abi)
Andrew Geisslereff27472021-10-29 15:35:00 -0500308 else:
Andrew Jefferyecdf5f12022-03-01 01:09:46 +1030309 arch_abi = rust_arch
Andrew Geisslereff27472021-10-29 15:35:00 -0500310
311 features = features or d.getVarFlag('FEATURES', arch_abi) or ""
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500312 features = features.strip()
313
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500314 llvm_target = d.getVar('RUST_TARGET_SYS')
315 if thing == "BUILD":
316 llvm_target = d.getVar('RUST_HOST_SYS')
317
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500318 # build tspec
319 tspec = {}
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500320 tspec['llvm-target'] = llvm_target
Andrew Geisslereff27472021-10-29 15:35:00 -0500321 tspec['data-layout'] = d.getVarFlag('DATA_LAYOUT', arch_abi)
322 tspec['max-atomic-width'] = int(d.getVarFlag('MAX_ATOMIC_WIDTH', arch_abi))
323 tspec['target-pointer-width'] = d.getVarFlag('TARGET_POINTER_WIDTH', arch_abi)
324 tspec['target-c-int-width'] = d.getVarFlag('TARGET_C_INT_WIDTH', arch_abi)
325 tspec['target-endian'] = d.getVarFlag('TARGET_ENDIAN', arch_abi)
Andrew Jefferyecdf5f12022-03-01 01:09:46 +1030326 tspec['arch'] = arch_to_rust_target_arch(rust_arch)
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500327 tspec['os'] = "linux"
328 if "musl" in tspec['llvm-target']:
329 tspec['env'] = "musl"
330 else:
331 tspec['env'] = "gnu"
332 if "riscv64" in tspec['llvm-target']:
333 tspec['llvm-abiname'] = "lp64d"
334 if "riscv32" in tspec['llvm-target']:
335 tspec['llvm-abiname'] = "ilp32d"
336 tspec['vendor'] = "unknown"
337 tspec['target-family'] = "unix"
338 tspec['linker'] = "{}{}gcc".format(d.getVar('CCACHE'), prefix)
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500339 tspec['cpu'] = cpu
340 if features != "":
341 tspec['features'] = features
342 tspec['dynamic-linking'] = True
343 tspec['executables'] = True
344 tspec['linker-is-gnu'] = True
345 tspec['linker-flavor'] = "gcc"
346 tspec['has-rpath'] = True
347 tspec['has-elf-tls'] = True
348 tspec['position-independent-executables'] = True
349 tspec['panic-strategy'] = d.getVar("RUST_PANIC_STRATEGY")
350
351 # write out the target spec json file
352 with open(wd + sys + '.json', 'w') as f:
353 json.dump(tspec, f, indent=4)
354
Andrew Geisslerd5838332022-05-27 11:33:10 -0500355# These are accounted for in tmpdir path names so don't need to be in the task sig
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500356rust_gen_target[vardepsexclude] += "RUST_HOST_SYS RUST_TARGET_SYS ABIEXTENSION llvm_cpu"
Andrew Geisslerd5838332022-05-27 11:33:10 -0500357
358do_rust_gen_targets[vardeps] += "DATA_LAYOUT TARGET_ENDIAN TARGET_POINTER_WIDTH TARGET_C_INT_WIDTH MAX_ATOMIC_WIDTH FEATURES"
359
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500360RUST_TARGETGENS = "BUILD"
361
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500362python do_rust_gen_targets () {
363 wd = d.getVar('WORKDIR') + '/targets/'
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500364 # Order of BUILD, HOST, TARGET is important in case the files overwrite, most specific last
365 rust_gen_target(d, 'BUILD', wd, d.getVar('BUILD_ARCH'))
366 if "HOST" in d.getVar("RUST_TARGETGENS"):
367 rust_gen_target(d, 'HOST', wd, d.getVar('HOST_ARCH'))
368 if "TARGET" in d.getVar("RUST_TARGETGENS"):
369 rust_gen_target(d, 'TARGET', wd, d.getVar('TARGET_ARCH'))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500370}
371
372addtask rust_gen_targets after do_patch before do_compile
373do_rust_gen_targets[dirs] += "${WORKDIR}/targets"
374