clang-19.1.7.ebuild 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. # Copyright 1999-2025 Gentoo Authors
  2. # Distributed under the terms of the GNU General Public License v2
  3. EAPI=8
  4. PYTHON_COMPAT=( python3_{10..13} )
  5. inherit cmake llvm.org llvm-utils multilib multilib-minimal
  6. inherit prefix python-single-r1 toolchain-funcs
  7. inherit flag-o-matic ninja-utils
  8. DESCRIPTION="C language family frontend for LLVM"
  9. HOMEPAGE="https://llvm.org/"
  10. # MSVCSetupApi.h: MIT
  11. # sorttable.js: MIT
  12. LICENSE="Apache-2.0-with-LLVM-exceptions UoI-NCSA MIT"
  13. SLOT="${LLVM_MAJOR}/${LLVM_SOABI}"
  14. KEYWORDS="~amd64 ~arm ~arm64 ~loong ~ppc ~ppc64 ~riscv ~sparc ~x86 ~amd64-linux ~x64-macos"
  15. IUSE="debug doc +extra ieee-long-double +pie static-analyzer test xml \
  16. default-fortify-source-2 default-fortify-source-3 default-full-relro \
  17. default-partial-relro default-ssp-buffer-size-4 \
  18. default-stack-clash-protection cet hardened hardened-compat rocm ssp"
  19. REQUIRED_USE="${PYTHON_REQUIRED_USE}
  20. amd64? (
  21. llvm_targets_X86
  22. )
  23. arm? (
  24. llvm_targets_ARM
  25. )
  26. arm64? (
  27. llvm_targets_AArch64
  28. )
  29. loong? (
  30. llvm_targets_LoongArch
  31. )
  32. m68k? (
  33. llvm_targets_M68k
  34. )
  35. mips? (
  36. llvm_targets_Mips
  37. )
  38. ppc? (
  39. llvm_targets_PowerPC
  40. )
  41. ppc64? (
  42. llvm_targets_PowerPC
  43. )
  44. riscv? (
  45. llvm_targets_RISCV
  46. )
  47. sparc? (
  48. llvm_targets_Sparc
  49. )
  50. x86? (
  51. llvm_targets_X86
  52. )
  53. default-fortify-source-2? (
  54. !default-fortify-source-3
  55. !test
  56. )
  57. default-fortify-source-3? (
  58. !default-fortify-source-2
  59. !test
  60. )
  61. default-full-relro? (
  62. !default-partial-relro
  63. !test
  64. )
  65. default-partial-relro? (
  66. !default-full-relro
  67. !test
  68. )
  69. default-stack-clash-protection? (
  70. !test
  71. )
  72. hardened? (
  73. !test
  74. default-fortify-source-3
  75. default-full-relro
  76. default-ssp-buffer-size-4
  77. default-stack-clash-protection
  78. pie
  79. ssp
  80. )
  81. ssp? (
  82. !test
  83. )
  84. "
  85. RDEPEND+="
  86. ${PYTHON_DEPS}
  87. >=llvm-core/clang-common-${PV}
  88. rocm? (
  89. dev-libs/rocm-device-libs:0/5.7
  90. )
  91. static-analyzer? (
  92. dev-lang/perl:*
  93. )
  94. xml? (
  95. dev-libs/libxml2:2=[${MULTILIB_USEDEP}]
  96. )
  97. ~llvm-core/llvm-${PV}:${LLVM_MAJOR}=[${MULTILIB_USEDEP},debug=]
  98. "
  99. DEPEND="
  100. ${RDEPEND}
  101. "
  102. BDEPEND="
  103. ${PYTHON_DEPS}
  104. >=dev-build/cmake-3.16
  105. doc? (
  106. $(python_gen_cond_dep '
  107. dev-python/recommonmark[${PYTHON_USEDEP}]
  108. dev-python/sphinx[${PYTHON_USEDEP}]
  109. ')
  110. )
  111. test? ( ~llvm-core/lld-${PV} )
  112. xml? ( virtual/pkgconfig )
  113. "
  114. PDEPEND="
  115. ~llvm-core/clang-runtime-${PV}
  116. llvm-core/clang-toolchain-symlinks:${LLVM_MAJOR}
  117. "
  118. RESTRICT="
  119. !test? ( test )
  120. "
  121. LLVM_COMPONENTS=(
  122. "clang"
  123. "clang-tools-extra"
  124. "cmake"
  125. )
  126. LLVM_MANPAGES=0
  127. #LLVM_PATCHSET=${PV}-r6
  128. LLVM_TEST_COMPONENTS=(
  129. "llvm/utils"
  130. )
  131. LLVM_USE_TARGETS="llvm"
  132. llvm.org_set_globals
  133. # Multilib notes:
  134. # 1. ABI_* flags control ABIs libclang* is built for only.
  135. # 2. clang is always capable of compiling code for all ABIs for enabled
  136. # target. However, you will need appropriate crt* files (installed
  137. # e.g. by sys-devel/gcc and sys-libs/glibc).
  138. # 3. ${CHOST}-clang wrappers are always installed for all ABIs included
  139. # in the current profile (i.e. alike supported by sys-devel/gcc).
  140. #
  141. # Therefore: use llvm-core/clang[${MULTILIB_USEDEP}] only if you need
  142. # multilib clang* libraries (not runtime, not wrappers).
  143. pkg_setup() {
  144. LLVM_MAX_SLOT=${LLVM_MAJOR}
  145. python-single-r1_pkg_setup
  146. if tc-is-gcc ; then
  147. local gcc_slot=$(best_version "sys-devel/gcc" \
  148. | sed -e "s|sys-devel/gcc-||g")
  149. gcc_slot=$(ver_cut 1-3 ${gcc_slot})
  150. # gcc-major-version is broken with gcc hardened 11.2.1_p20220115
  151. if (( $(ver_cut 1 ${gcc_slot}) != $(ver_cut 1 $(_gcc_fullversion)) )) ; then
  152. # Prevent: undefined reference to `std::__throw_bad_array_new_length()'
  153. ewarn
  154. ewarn "Detected not using latest gcc."
  155. ewarn
  156. ewarn "Build may break if highest gcc version not chosen and profile not"
  157. ewarn "sourced. To fix do the following:"
  158. ewarn
  159. ewarn " gcc-config -l"
  160. ewarn " gcc-config ${CHOST}-${gcc_slot} # It must match at least one row from \ "
  161. ewarn " # the above list."
  162. ewarn " source /etc/profile"
  163. ewarn
  164. fi
  165. fi
  166. if [[ -n "${MAKEOPTS}" ]] ; then
  167. local nmakeopts=$(echo "${MAKEOPTS}" \
  168. | grep -o -E -e "-j[ ]*[0-9]+( |$)" \
  169. | sed -e "s|-j||g" -e "s|[ ]*||" \
  170. | tail -n 1)
  171. if [[ -n "${nmakeopts}" ]] && (( ${nmakeopts} > 1 )) ; then
  172. ewarn
  173. ewarn "MAKEOPTS=-jN should be -j1 if linking with BFD or <= 4 GiB RAM or"
  174. ewarn "<= 3 GiB per core."
  175. ewarn "Adjust your per-package package.env to avoid very long linking times."
  176. ewarn
  177. fi
  178. fi
  179. ewarn
  180. ewarn "If you encounter the following during the build:"
  181. ewarn
  182. ewarn "FAILED: lib/Tooling/ASTNodeAPI.json"
  183. ewarn
  184. ewarn "Build ~clang-${PV} with only gcc and ~llvm-${PV} without LTO."
  185. ewarn
  186. ewarn
  187. ewarn "To avoid missing symbols, both clang-${PV} and llvm-${PV} should be"
  188. ewarn "built with the same commit."
  189. ewarn
  190. if [[ "${CC}" == "clang" ]] ; then
  191. local clang_path="clang-${LLVM_MAJOR}"
  192. if which "${clang_path}" 2>/dev/null 1>/dev/null && "${clang_path}" --help \
  193. | grep "symbol lookup error" ; then
  194. eerror
  195. eerror "The entire slot should be uninstalled and set CC=gcc and CXX=g++"
  196. eerror
  197. die
  198. fi
  199. fi
  200. ewarn
  201. ewarn "To avoid long linking delays, close programs that produce unexpectedly"
  202. ewarn "high disk activity (web browsers) and possibly switch to -j1."
  203. ewarn
  204. # See https://bugs.gentoo.org/767700
  205. einfo
  206. einfo "To remove the hard USE mask for llvm_targets_*, do:"
  207. einfo
  208. local t
  209. for t in ${ALL_LLVM_TARGET_FLAGS[@]} ; do
  210. echo "echo \"${CATEGORY}/${PN} -${t}\" >> ${EROOT}/etc/portage/profile/package.use.force"
  211. done
  212. for t in ${ALL_LLVM_EXPERIMENTAL_TARGETS[@]/#/llvm_targets_} ; do
  213. echo "echo \"${CATEGORY}/${PN} -${t}\" >> ${EROOT}/etc/portage/profile/package.use.mask"
  214. done
  215. einfo
  216. einfo "However, some packages still need some or all of these. Some are"
  217. einfo "mentioned in bug #767700."
  218. einfo
  219. }
  220. src_unpack() {
  221. llvm.org_src_unpack
  222. }
  223. eapply_hardened() {
  224. ewarn
  225. ewarn "The hardened USE flag and associated patches via default_* are still in"
  226. ewarn "testing."
  227. ewarn
  228. local hardened_features=""
  229. local patches_hardened=()
  230. if use pie ; then
  231. hardened_features+="PIE, "
  232. fi
  233. if use ssp ; then
  234. patches_hardened+=(
  235. "${FILESDIR}/clang-12.0.1-enable-SSP-by-default.patch"
  236. )
  237. hardened_features+="SSP, "
  238. fi
  239. if use default-ssp-buffer-size-4 ; then
  240. patches_hardened+=(
  241. "${FILESDIR}/clang-16.0.0.9999-change-SSP-buffer-size-to-4.patch"
  242. )
  243. fi
  244. if use default-fortify-source-2 ; then
  245. patches_hardened+=(
  246. "${FILESDIR}/clang-14.0.0.9999-set-_FORTIFY_SOURCE-to-2-by-default.patch"
  247. )
  248. hardened_features+="_FORITIFY_SOURCE=2, "
  249. fi
  250. if use default-fortify-source-3 ; then
  251. patches_hardened+=(
  252. "${FILESDIR}/clang-14.0.0.9999-set-_FORTIFY_SOURCE-to-3-by-default.patch"
  253. )
  254. hardened_features+="_FORITIFY_SOURCE=3, "
  255. ewarn
  256. ewarn "The _FORITIFY_SOURCE=3 is in testing."
  257. ewarn
  258. fi
  259. if use default-full-relro ; then
  260. patches_hardened+=(
  261. "${FILESDIR}/clang-12.0.1-enable-full-relro-by-default.patch"
  262. )
  263. hardened_features+="Full RELRO, "
  264. ewarn
  265. ewarn "The Full RELRO is in testing."
  266. ewarn
  267. fi
  268. if use default-partial-relro ; then
  269. patches_hardened+=(
  270. "${FILESDIR}/clang-12.0.1-enable-partial-relro-by-default.patch"
  271. )
  272. hardened_features+="Partial RELRO, "
  273. ewarn
  274. ewarn "The Partial RELRO is in testing."
  275. ewarn
  276. fi
  277. if use default-stack-clash-protection ; then
  278. if use x86 || use amd64 ; then
  279. patches_hardened+=(
  280. "${FILESDIR}/clang-18.0.0.9999-2b033a3-enable-SCP-by-default.patch"
  281. )
  282. hardened_features+="SCP, "
  283. elif use arm64 ; then
  284. ewarn
  285. ewarn "arm64 -fstack-clash-protection is not default ON. The feature is still"
  286. ewarn "in development."
  287. ewarn
  288. fi
  289. fi
  290. if use hardened || use hardened-compat ; then
  291. patches_hardened+=(
  292. "${FILESDIR}/clang-12.0.1-version-info.patch"
  293. )
  294. fi
  295. if use cet ; then
  296. patches_hardened+=(
  297. "${FILESDIR}/clang-17.0.0.9999-enable-cf-protection-full-by-default.patch"
  298. )
  299. hardened_features+="CET, "
  300. ewarn
  301. ewarn "The CET as default is in testing."
  302. ewarn
  303. fi
  304. patches_hardened+=(
  305. "${FILESDIR}/clang-18.0.0.9999-cross-dso-cfi-link-with-shared.patch"
  306. )
  307. eapply ${patches_hardened[@]}
  308. if use hardened || use hardened-compat ; then
  309. hardened_features=$(echo "${hardened_features}" \
  310. | sed -e "s|, $||g")
  311. sed -i -e "s|__HARDENED_FEATURES__|${hardened_features}|g" \
  312. lib/Driver/Driver.cpp || die
  313. fi
  314. }
  315. src_prepare() {
  316. # Create an extra parent dir for relative CLANG_RESOURCE_DIR access.
  317. mkdir -p "${WORKDIR}/x/y" || die
  318. BUILD_DIR="${WORKDIR}/x/y/clang"
  319. llvm.org_src_prepare
  320. eapply -p2 "${FILESDIR}/${PN}-17.0.0.9999-stdatomic-force.patch"
  321. eapply "${FILESDIR}/clang-17.0.4-fix-glibc-limits.h-relative-path.patch"
  322. #use pgo && \
  323. eapply "${FILESDIR}/clang-16.0.0.9999-add-include-path.patch"
  324. eapply_hardened
  325. # Add Gentoo Portage Prefix for Darwin. See prefix-dirs.patch.
  326. eprefixify \
  327. lib/Lex/InitHeaderSearch.cpp \
  328. lib/Driver/ToolChains/Darwin.cpp || die
  329. if ! use prefix-guest && [[ -n ${EPREFIX} ]]; then
  330. sed -i "/LibDir.*Loader/s@return \"\/\"@return \"${EPREFIX}/\"@" \
  331. lib/Driver/ToolChains/Linux.cpp \
  332. || die
  333. fi
  334. }
  335. check_distribution_components() {
  336. if [[ ${CMAKE_MAKEFILE_GENERATOR} == ninja ]]; then
  337. local all_targets=() my_targets=() l
  338. cd "${BUILD_DIR}" || die
  339. while read -r l; do
  340. if [[ ${l} == install-*-stripped:* ]]; then
  341. l=${l#install-}
  342. l=${l%%-stripped*}
  343. case ${l} in
  344. # meta-targets
  345. clang-libraries|distribution)
  346. continue
  347. ;;
  348. # tools
  349. clang|clangd|clang-*)
  350. ;;
  351. # static libraries
  352. clang*|findAllSymbols)
  353. continue
  354. ;;
  355. # conditional to USE=doc
  356. docs-clang-html|docs-clang-tools-html)
  357. use doc || continue
  358. ;;
  359. esac
  360. all_targets+=( "${l}" )
  361. fi
  362. done < <(${NINJA} -t targets all)
  363. while read -r l; do
  364. my_targets+=( "${l}" )
  365. done < <(get_distribution_components $"\n")
  366. local add=() remove=()
  367. for l in "${all_targets[@]}"; do
  368. if ! has "${l}" "${my_targets[@]}"; then
  369. add+=( "${l}" )
  370. fi
  371. done
  372. for l in "${my_targets[@]}"; do
  373. if ! has "${l}" "${all_targets[@]}"; then
  374. remove+=( "${l}" )
  375. fi
  376. done
  377. if [[ ${#add[@]} -gt 0 || ${#remove[@]} -gt 0 ]]; then
  378. eqawarn "get_distribution_components() is outdated!"
  379. eqawarn " Add: ${add[*]}"
  380. eqawarn "Remove: ${remove[*]}"
  381. fi
  382. cd - >/dev/null || die
  383. fi
  384. }
  385. get_distribution_components() {
  386. local sep=${1-;}
  387. local out=(
  388. # common stuff
  389. clang-cmake-exports
  390. clang-headers
  391. clang-resource-headers
  392. libclang-headers
  393. aarch64-resource-headers
  394. arm-common-resource-headers
  395. arm-resource-headers
  396. core-resource-headers
  397. cuda-resource-headers
  398. hexagon-resource-headers
  399. hip-resource-headers
  400. hlsl-resource-headers
  401. mips-resource-headers
  402. opencl-resource-headers
  403. openmp-resource-headers
  404. ppc-htm-resource-headers
  405. ppc-resource-headers
  406. riscv-resource-headers
  407. systemz-resource-headers
  408. utility-resource-headers
  409. ve-resource-headers
  410. webassembly-resource-headers
  411. windows-resource-headers
  412. x86-resource-headers
  413. # libs
  414. clang-cpp
  415. libclang
  416. )
  417. if multilib_is_native_abi; then
  418. out+=(
  419. # common stuff
  420. bash-autocomplete
  421. libclang-python-bindings
  422. # tools
  423. amdgpu-arch
  424. c-index-test
  425. clang
  426. clang-format
  427. clang-installapi
  428. clang-linker-wrapper
  429. clang-nvlink-wrapper
  430. clang-offload-bundler
  431. clang-offload-packager
  432. clang-refactor
  433. clang-repl
  434. clang-rename
  435. clang-scan-deps
  436. diagtool
  437. hmaptool
  438. nvptx-arch
  439. # needed for cross-compiling Clang
  440. clang-tblgen
  441. )
  442. if use extra; then
  443. out+=(
  444. # extra tools
  445. clang-apply-replacements
  446. clang-change-namespace
  447. clang-doc
  448. clang-include-cleaner
  449. clang-include-fixer
  450. clang-move
  451. clang-pseudo
  452. clang-query
  453. clang-reorder-fields
  454. clang-tidy
  455. clang-tidy-headers
  456. clangd
  457. find-all-symbols
  458. modularize
  459. pp-trace
  460. )
  461. fi
  462. if llvm_are_manpages_built; then
  463. out+=( docs-clang-man )
  464. use extra && out+=( docs-clang-tools-man )
  465. fi
  466. if use doc; then
  467. out+=( docs-clang-html )
  468. use extra && out+=( docs-clang-tools-html )
  469. fi
  470. use static-analyzer && out+=(
  471. clang-check
  472. clang-extdef-mapping
  473. scan-build
  474. scan-build-py
  475. scan-view
  476. )
  477. fi
  478. printf "%s${sep}" "${out[@]}"
  479. }
  480. _gcc_fullversion() {
  481. gcc --version | head -n 1 | grep -o -E -e "[0-9_p.]+" | head -n 1
  482. }
  483. multilib_src_configure() {
  484. llvm_prepend_path "${LLVM_MAJOR}"
  485. # TODO: Add GCC-10 and below checks to add exceptions to -O* flag downgrading.
  486. # Leave a note if you know the commit that fixes the internal compiler error below.
  487. if tc-is-gcc && ( \
  488. ( ver_test $(_gcc_fullversion) -lt 11.2.1_p20220112 ) \
  489. )
  490. then
  491. # Build time bug with gcc 10.3.0, 11.2.0:
  492. # internal compiler error: maximum number of LRA assignment passes is achieved (30)
  493. # Apply if using GCC
  494. ewarn
  495. ewarn "Detected <=sys-devel/gcc-11.2.1_p20220112. Downgrading to -Os to avoid"
  496. ewarn "bug. Re-emerge >=sys-devel/gcc-11.2.1_p20220112 for a more optimized"
  497. ewarn "build with >= -O2."
  498. ewarn
  499. replace-flags '-O3' '-Os'
  500. replace-flags '-O2' '-Os'
  501. fi
  502. # LLVM can have very high memory consumption while linking,
  503. # exhausting the limit on 32-bit linker executable
  504. use x86 && local -x LDFLAGS="${LDFLAGS} -Wl,--no-keep-memory"
  505. # LLVM_ENABLE_ASSERTIONS=NO does not guarantee this for us, #614844
  506. use debug || local -x CPPFLAGS="${CPPFLAGS} -DNDEBUG"
  507. # Fix longer than usual build times when building webkit-gtk.
  508. # Bump to next fastest build setting.
  509. replace-flags -O0 -O1
  510. # Fix longer than usual build times when building rocm ebuilds in sci-libs.
  511. # -O3 may cause random segfaults during build like in rocSPARSE.
  512. replace-flags -O1 -O2
  513. replace-flags -Oz -O2
  514. replace-flags -Os -O2
  515. replace-flags -O3 -O2
  516. replace-flags -Ofast -O2
  517. replace-flags -O4 -O2
  518. # For PGO
  519. if tc-is-gcc ; then
  520. # error: number of counters in profile data for function '...' does not match its profile data (counter 'arcs', expected 7 and have 13) [-Werror=coverage-mismatch]
  521. # The PGO profiles are isolated. The Code is the same.
  522. append-flags -Wno-error=coverage-mismatch
  523. fi
  524. filter-flags -m32 -m64 -mx32 -m31 '-mabi=*'
  525. [[ ${CHOST} =~ "risc" ]] && filter-flags '-march=*'
  526. export CFLAGS="$(get_abi_CFLAGS ${ABI}) ${CFLAGS}"
  527. export CXXFLAGS="$(get_abi_CFLAGS ${ABI}) ${CXXFLAGS}"
  528. # [Err 8]: control flow integrity check for type '.*' failed during non-virtual call (vtable address 0x[0-9a-z]+)
  529. # [Err 5]: runtime error: control flow integrity check for type '.*' failed during cast to unrelated type (vtable address 0x[0-9a-z]+)
  530. # llvm-core/clang no-cfi-nvcall.conf no-cfi-cast.conf # Build time failures: [Err 8] with llvm header, [Err 5] with gcc header
  531. if tc-is-clang ; then
  532. if is-flagq "-fsanitize=*cfi" ; then
  533. ewarn
  534. ewarn "Using -fsanitize=cfi without"
  535. ewarn "-fno-sanitize=cfi-nvcall,cfi-derived-cast,cfi-unrelated-cast"
  536. ewarn "may break build."
  537. ewarn
  538. fi
  539. if is-flagq "-fsanitize=*cfi-nvcall" ; then
  540. ewarn
  541. ewarn "Using -fsanitize=cfi-nvcall may break build."
  542. ewarn
  543. fi
  544. if is-flagq "-fsanitize=*cfi-derived-cast" ; then
  545. ewarn
  546. ewarn "Using -fsanitize=cfi-derived-cast may break build."
  547. ewarn
  548. fi
  549. if is-flagq "-fsanitize=*cfi-unrelated-cast" ; then
  550. ewarn
  551. ewarn "Using -fsanitize=cfi-unrelated-cast may break build."
  552. ewarn
  553. fi
  554. fi
  555. einfo
  556. einfo "*FLAGS for ${ABI}:"
  557. einfo
  558. einfo " CFLAGS=${CFLAGS}"
  559. einfo " CXXFLAGS=${CXXFLAGS}"
  560. einfo " LDFLAGS=${LDFLAGS}"
  561. einfo " PATH=${PATH}"
  562. if tc-is-cross-compiler ; then
  563. einfo " IS_CROSS_COMPILE=True"
  564. else
  565. einfo " IS_CROSS_COMPILE=False"
  566. fi
  567. einfo
  568. use debug && CMAKE_BUILD_TYPE="Debug" || CMAKE_BUILD_TYPE="Release"
  569. local mycmakeargs=(
  570. -DDEFAULT_SYSROOT=$(usex prefix-guest "" "${EPREFIX}")
  571. -DCMAKE_INSTALL_PREFIX="${EPREFIX}/usr/lib/llvm/${LLVM_MAJOR}"
  572. -DCMAKE_INSTALL_MANDIR="${EPREFIX}/usr/lib/llvm/${LLVM_MAJOR}/share/man"
  573. -DLLVM_ROOT="${EPREFIX}/usr/lib/llvm/${LLVM_MAJOR}"
  574. -DCLANG_CONFIG_FILE_SYSTEM_DIR="${EPREFIX}/etc/clang"
  575. -DCLANG_CONFIG_FILE_USER_DIR="~/.config/clang"
  576. # relative to bindir
  577. -DCLANG_RESOURCE_DIR="../../../../lib/clang/${LLVM_MAJOR}"
  578. -DBUILD_SHARED_LIBS=OFF
  579. -DCLANG_LINK_CLANG_DYLIB=ON
  580. -DLLVM_DISTRIBUTION_COMPONENTS=$(get_distribution_components)
  581. -DCLANG_INCLUDE_TESTS=$(usex test)
  582. -DLLVM_INCLUDE_TESTS=$(usex test)
  583. -DLLVM_TARGETS_TO_BUILD="${LLVM_TARGETS// /;}"
  584. # These are not propagated reliably, so redefine them.
  585. -DLLVM_ENABLE_EH=ON
  586. -DLLVM_ENABLE_RTTI=ON
  587. # libgomp support fails to find headers without explicit -I
  588. # furthermore, it provides only syntax checking
  589. -DCLANG_DEFAULT_OPENMP_RUNTIME=libomp
  590. # Disable CUDA to autodetect GPU, so build for all.
  591. -DCMAKE_DISABLE_FIND_PACKAGE_CUDAToolkit=ON
  592. # Disable linking to HSA to avoid automagic dep.
  593. # Load it dynamically instead.
  594. -DCMAKE_DISABLE_FIND_PACKAGE_hsa-runtime64=ON
  595. -DCLANG_DEFAULT_PIE_ON_LINUX=$(usex pie)
  596. -DCLANG_ENABLE_LIBXML2=$(usex xml)
  597. -DCLANG_ENABLE_ARCMT=$(usex static-analyzer)
  598. -DCLANG_ENABLE_STATIC_ANALYZER=$(usex static-analyzer)
  599. # TODO: CLANG_ENABLE_HLSL?
  600. -DPython3_EXECUTABLE="${PYTHON}"
  601. )
  602. if ! use elibc_musl; then
  603. mycmakeargs+=(
  604. -DPPC_LINUX_DEFAULT_IEEELONGDOUBLE=$(usex ieee-long-double)
  605. )
  606. fi
  607. use test && mycmakeargs+=(
  608. -DLLVM_BUILD_TESTS=ON
  609. -DLLVM_LIT_ARGS="$(get_lit_flags)"
  610. )
  611. if multilib_is_native_abi; then
  612. local build_docs=OFF
  613. if llvm_are_manpages_built; then
  614. build_docs=ON
  615. mycmakeargs+=(
  616. -DLLVM_BUILD_DOCS=ON
  617. -DLLVM_ENABLE_SPHINX=ON
  618. -DCLANG_INSTALL_SPHINX_HTML_DIR="${EPREFIX}/usr/share/doc/${PF}/html"
  619. -DSPHINX_WARNINGS_AS_ERRORS=OFF
  620. )
  621. if use extra; then
  622. mycmakeargs+=(
  623. -DCLANG-TOOLS_INSTALL_SPHINX_HTML_DIR="${EPREFIX}/usr/share/doc/${PF}/tools-extra"
  624. )
  625. fi
  626. fi
  627. mycmakeargs+=(
  628. -DCLANG_INCLUDE_DOCS=${build_docs}
  629. )
  630. fi
  631. if multilib_native_use extra; then
  632. mycmakeargs+=(
  633. -DLLVM_EXTERNAL_CLANG_TOOLS_EXTRA_SOURCE_DIR="${WORKDIR}"/clang-tools-extra
  634. -DCLANG_TOOLS_EXTRA_INCLUDE_DOCS=${build_docs}
  635. )
  636. else
  637. mycmakeargs+=(
  638. -DLLVM_TOOL_CLANG_TOOLS_EXTRA_BUILD=OFF
  639. )
  640. fi
  641. if [[ -n "${EPREFIX}" ]]; then
  642. mycmakeargs+=(
  643. -DGCC_INSTALL_PREFIX="${EPREFIX}/usr"
  644. )
  645. fi
  646. if tc-is-cross-compiler; then
  647. has_version -b llvm-core/clang:${LLVM_MAJOR} ||
  648. die "llvm-core/clang:${LLVM_MAJOR} is required on the build host."
  649. local tools_bin=${BROOT}/usr/lib/llvm/${LLVM_MAJOR}/bin
  650. mycmakeargs+=(
  651. -DLLVM_TOOLS_BINARY_DIR="${tools_bin}"
  652. -DCLANG_TABLEGEN="${tools_bin}"/clang-tblgen
  653. )
  654. fi
  655. # LLVM can have very high memory consumption while linking,
  656. # exhausting the limit on 32-bit linker executable
  657. use x86 && local -x LDFLAGS="${LDFLAGS} -Wl,--no-keep-memory"
  658. # LLVM_ENABLE_ASSERTIONS=NO does not guarantee this for us, #614844
  659. use debug || local -x CPPFLAGS="${CPPFLAGS} -DNDEBUG"
  660. mycmakeargs+=(
  661. -DCMAKE_C_COMPILER="${CC}"
  662. -DCMAKE_CXX_COMPILER="${CXX}"
  663. -DCMAKE_ASM_COMPILER="${CC}"
  664. )
  665. cmake_src_configure
  666. multilib_is_native_abi && check_distribution_components
  667. }
  668. multilib_src_compile() {
  669. cmake_build distribution
  670. }
  671. multilib_src_test() {
  672. if use hardened ; then
  673. ewarn
  674. ewarn "Tests are broken with the enable-SSP-and-PIE-by-default.patch"
  675. ewarn
  676. fi
  677. # respect TMPDIR!
  678. local -x LIT_PRESERVES_TMP=1
  679. local test_targets=( check-clang )
  680. if multilib_native_use extra; then
  681. test_targets+=(
  682. check-clang-tools
  683. check-clangd
  684. )
  685. fi
  686. cmake_build "${test_targets[@]}"
  687. }
  688. src_install() {
  689. MULTILIB_WRAPPED_HEADERS=(
  690. /usr/include/clang/Config/config.h
  691. )
  692. multilib-minimal_src_install
  693. # Move runtime headers to /usr/lib/clang, where they belong
  694. mv "${ED}"/usr/include/clangrt "${ED}"/usr/lib/clang || die
  695. # move (remaining) wrapped headers back
  696. if use extra; then
  697. mv "${T}"/clang-tidy "${ED}"/usr/include/ || die
  698. fi
  699. mv "${ED}"/usr/include "${ED}"/usr/lib/llvm/${LLVM_MAJOR}/include || die
  700. # Apply CHOST and version suffix to clang tools
  701. local clang_tools=( clang clang++ clang-cl clang-cpp )
  702. local abi i
  703. # cmake gives us:
  704. # - clang-X
  705. # - clang -> clang-X
  706. # - clang++, clang-cl, clang-cpp -> clang
  707. # we want to have:
  708. # - clang-X
  709. # - clang++-X, clang-cl-X, clang-cpp-X -> clang-X
  710. # - clang, clang++, clang-cl, clang-cpp -> clang*-X
  711. # also in CHOST variant
  712. for i in "${clang_tools[@]:1}"; do
  713. rm "${ED}/usr/lib/llvm/${LLVM_MAJOR}/bin/${i}" || die
  714. dosym "clang-${LLVM_MAJOR}" "/usr/lib/llvm/${LLVM_MAJOR}/bin/${i}-${LLVM_MAJOR}"
  715. dosym "${i}-${LLVM_MAJOR}" "/usr/lib/llvm/${LLVM_MAJOR}/bin/${i}"
  716. done
  717. # now create target symlinks for all supported ABIs
  718. for abi in $(get_all_abis); do
  719. local abi_chost=$(get_abi_CHOST "${abi}")
  720. for i in "${clang_tools[@]}"; do
  721. dosym "${i}-${LLVM_MAJOR}" \
  722. "/usr/lib/llvm/${LLVM_MAJOR}/bin/${abi_chost}-${i}-${LLVM_MAJOR}"
  723. dosym "${abi_chost}-${i}-${LLVM_MAJOR}" \
  724. "/usr/lib/llvm/${LLVM_MAJOR}/bin/${abi_chost}-${i}"
  725. done
  726. done
  727. }
  728. multilib_src_install() {
  729. DESTDIR=${D} cmake_build install-distribution
  730. # Move headers to /usr/include for wrapping & ABI mismatch checks.
  731. # (Also, drop the version suffix from runtime headers.)
  732. rm -rf "${ED}"/usr/include || die
  733. if [[ -e "${ED}"/usr/lib/llvm/${LLVM_MAJOR}/include ]] ; then
  734. mv \
  735. "${ED}"/usr/lib/llvm/${LLVM_MAJOR}/include \
  736. "${ED}"/usr/include \
  737. || die
  738. fi
  739. if [[ -e "${ED}"/usr/lib/clang ]] ; then
  740. mv \
  741. "${ED}"/usr/lib/clang \
  742. "${ED}"/usr/include/clangrt \
  743. || die
  744. fi
  745. if multilib_native_use extra && [[ -e "${ED}"/usr/include/clang-tidy ]] ; then
  746. # Don't wrap clang-tidy headers; the list is too long.
  747. # (They're fine for non-native ABI but enabling the targets is problematic.)
  748. mkdir -p "${T}/clang-tidy" || die
  749. cp -aT "${ED}"/usr/include/clang-tidy "${T}/" || die
  750. rm -rf "${ED}"/usr/include/clang-tidy || die
  751. fi
  752. }
  753. multilib_src_install_all() {
  754. python_fix_shebang "${ED}"
  755. if use static-analyzer; then
  756. python_optimize "${ED}"/usr/lib/llvm/${LLVM_MAJOR}/share/scan-view
  757. fi
  758. docompress "/usr/lib/llvm/${LLVM_MAJOR}/share/man"
  759. llvm_install_manpages
  760. # match 'html' non-compression
  761. use doc && docompress -x "/usr/share/doc/${PF}/tools-extra"
  762. # +x for some reason; TODO: investigate
  763. use static-analyzer && fperms a-x "/usr/lib/llvm/${LLVM_MAJOR}/share/man/man1/scan-build.1"
  764. }
  765. pkg_postinst() {
  766. if [[ -z "${ROOT}" && -f "${EPREFIX}"/usr/share/eselect/modules/compiler-shadow.eselect ]] ; then
  767. eselect compiler-shadow update all
  768. fi
  769. elog "You can find additional utility scripts in:"
  770. elog " ${EROOT}/usr/lib/llvm/${LLVM_MAJOR}/share/clang"
  771. if use extra; then
  772. elog "Some of them are vim integration scripts (with instructions inside)."
  773. elog "The run-clang-tidy.py script requires the following additional package:"
  774. elog " dev-python/pyyaml"
  775. fi
  776. }
  777. pkg_postrm() {
  778. if [[ -z "${ROOT}" && -f "${EPREFIX}"/usr/share/eselect/modules/compiler-shadow.eselect ]] ; then
  779. eselect compiler-shadow clean all
  780. fi
  781. }