rust-target-config.bbclass 14 KB

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