rust-target-config.bbclass 13 KB

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