rust-target-config.bbclass 15 KB

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