1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
6 : ${CMAKE_MAKEFILE_GENERATOR:=ninja}
7 # (needed due to CMAKE_BUILD_TYPE != Gentoo)
8 CMAKE_MIN_VERSION=3.7.0-r1
9 PYTHON_COMPAT=( python2_7 )
11 inherit cmake-utils eapi7-ver flag-o-matic llvm \
12 multilib-minimal multiprocessing pax-utils prefix python-single-r1 \
16 EXTRA_P=clang-tools-extra-${PV/_/}.src
17 LLVM_P=llvm-${PV/_/}.src
19 DESCRIPTION="C language family frontend for LLVM"
20 HOMEPAGE="https://llvm.org/"
21 SRC_URI="https://prereleases.llvm.org/${PV/_//}/${MY_P}.tar.xz
22 https://prereleases.llvm.org/${PV/_//}/${EXTRA_P}.tar.xz
23 test? ( https://prereleases.llvm.org/${PV/_//}/${LLVM_P}.tar.xz )"
24 # !doc? ( https://dev.gentoo.org/~mgorny/dist/llvm/llvm-${PV}-manpages.tar.bz2 )"
26 # Keep in sync with sys-devel/llvm
27 ALL_LLVM_TARGETS=( AArch64 AMDGPU ARM BPF Hexagon Lanai Mips MSP430
28 NVPTX PowerPC Sparc SystemZ X86 XCore )
29 ALL_LLVM_TARGETS=( "${ALL_LLVM_TARGETS[@]/#/llvm_targets_}" )
30 LLVM_TARGET_USEDEPS=${ALL_LLVM_TARGETS[@]/%/?}
34 KEYWORDS="~amd64 ~arm ~arm64 ~ppc64 ~x86 ~amd64-fbsd ~amd64-linux ~ppc-macos ~x64-macos ~x86-macos"
35 IUSE="debug default-compiler-rt default-libcxx doc +static-analyzer
36 test xml z3 kernel_FreeBSD ${ALL_LLVM_TARGETS[*]}"
37 RESTRICT="!test? ( test )"
40 ~sys-devel/llvm-${PV}:${SLOT}=[debug=,${LLVM_TARGET_USEDEPS// /,},${MULTILIB_USEDEP}]
43 z3? ( >=sci-mathematics/z3-4.7.1:0=[${MULTILIB_USEDEP}] )
45 xml? ( dev-libs/libxml2:2=[${MULTILIB_USEDEP}] )
47 # configparser-3.2 breaks the build (3.3 or none at all are fine)
49 doc? ( dev-python/sphinx )
50 xml? ( virtual/pkgconfig )
51 !!<dev-python/configparser-3.3.0.2
54 !<sys-devel/llvm-4.0.0_rc:0
57 sys-devel/clang-common
58 ~sys-devel/clang-runtime-${PV}
59 default-compiler-rt? ( =sys-libs/compiler-rt-${PV%_*}* )
60 default-libcxx? ( >=sys-libs/libcxx-${PV} )"
62 REQUIRED_USE="${PYTHON_REQUIRED_USE}
63 || ( ${ALL_LLVM_TARGETS[*]} )"
65 # We need extra level of indirection for CLANG_RESOURCE_DIR
66 S=${WORKDIR}/x/y/${MY_P}
68 # least intrusive of all
69 CMAKE_BUILD_TYPE=RelWithDebInfo
72 # add Prefix include paths for Darwin
73 "${FILESDIR}"/6.0.1/darwin_prefix-include-paths.patch
77 # 1. ABI_* flags control ABIs libclang* is built for only.
78 # 2. clang is always capable of compiling code for all ABIs for enabled
79 # target. However, you will need appropriate crt* files (installed
80 # e.g. by sys-devel/gcc and sys-libs/glibc).
81 # 3. ${CHOST}-clang wrappers are always installed for all ABIs included
82 # in the current profile (i.e. alike supported by sys-devel/gcc).
84 # Therefore: use sys-devel/clang[${MULTILIB_USEDEP}] only if you need
85 # multilib clang* libraries (not runtime, not wrappers).
88 LLVM_MAX_SLOT=${SLOT} llvm_pkg_setup
89 python-single-r1_pkg_setup
93 # create extra parent dir for CLANG_RESOURCE_DIR
97 einfo "Unpacking ${MY_P}.tar.xz ..."
98 tar -xf "${DISTDIR}/${MY_P}.tar.xz" || die
99 einfo "Unpacking ${EXTRA_P}.tar.xz ..."
100 tar -xf "${DISTDIR}/${EXTRA_P}.tar.xz" || die
102 mv "${EXTRA_P}" "${S}"/tools/extra || die
104 einfo "Unpacking parts of ${LLVM_P}.tar.xz ..."
105 tar -xf "${DISTDIR}/${LLVM_P}.tar.xz" \
106 "${LLVM_P}"/lib/Testing/Support \
107 "${LLVM_P}"/utils/{lit,llvm-lit,unittest} || die
108 mv "${LLVM_P}" "${WORKDIR}"/llvm || die
112 # einfo "Unpacking llvm-${PV}-manpages.tar.bz2 ..."
113 # tar -xf "${DISTDIR}/llvm-${PV}-manpages.tar.bz2" || die
118 cmake-utils_src_prepare
119 eprefixify lib/Frontend/InitHeaderSearch.cpp
122 multilib_src_configure() {
123 local llvm_version=$(llvm-config --version) || die
124 local clang_version=$(ver_cut 1-3 "${llvm_version}")
127 # ensure that the correct llvm-config is used
128 -DLLVM_CONFIG="$(type -P "${CHOST}-llvm-config")"
129 -DCMAKE_INSTALL_PREFIX="${EPREFIX}/usr/lib/llvm/${SLOT}"
130 -DCMAKE_INSTALL_MANDIR="${EPREFIX}/usr/lib/llvm/${SLOT}/share/man"
132 -DCLANG_RESOURCE_DIR="../../../../lib/clang/${clang_version}"
134 -DBUILD_SHARED_LIBS=ON
135 -DLLVM_TARGETS_TO_BUILD="${LLVM_TARGETS// /;}"
136 -DLLVM_BUILD_TESTS=$(usex test)
138 # these are not propagated reliably, so redefine them
140 -DLLVM_ENABLE_RTTI=ON
142 -DCMAKE_DISABLE_FIND_PACKAGE_LibXml2=$(usex !xml)
143 # libgomp support fails to find headers without explicit -I
144 # furthermore, it provides only syntax checking
145 -DCLANG_DEFAULT_OPENMP_RUNTIME=libomp
147 # override default stdlib and rtlib
148 -DCLANG_DEFAULT_CXX_STDLIB=$(usex default-libcxx libc++ "")
149 -DCLANG_DEFAULT_RTLIB=$(usex default-compiler-rt compiler-rt "")
151 -DCLANG_ENABLE_ARCMT=$(usex static-analyzer)
152 -DCLANG_ENABLE_STATIC_ANALYZER=$(usex static-analyzer)
153 # z3 is not multilib-friendly
154 -DCLANG_ANALYZER_ENABLE_Z3_SOLVER=$(usex z3)
156 use test && mycmakeargs+=(
157 -DLLVM_MAIN_SRC_DIR="${WORKDIR}/llvm"
158 -DLLVM_LIT_ARGS="-vv;-j;${LIT_JOBS:-$(makeopts_jobs "${MAKEOPTS}" "$(get_nproc)")}"
161 if multilib_is_native_abi; then
163 # normally copied from LLVM_INCLUDE_DOCS but the latter
164 # is lacking value in stand-alone builds
165 -DCLANG_INCLUDE_DOCS=$(usex doc)
166 -DCLANG_TOOLS_EXTRA_INCLUDE_DOCS=$(usex doc)
168 use doc && mycmakeargs+=(
170 -DLLVM_ENABLE_SPHINX=ON
171 -DCLANG_INSTALL_SPHINX_HTML_DIR="${EPREFIX}/usr/share/doc/${PF}/html"
172 -DCLANG-TOOLS_INSTALL_SPHINX_HTML_DIR="${EPREFIX}/usr/share/doc/${PF}/tools-extra"
173 -DSPHINX_WARNINGS_AS_ERRORS=OFF
175 use z3 && mycmakeargs+=(
176 -DZ3_INCLUDE_DIR="${EPREFIX}/usr/include/z3"
180 -DLLVM_TOOL_CLANG_TOOLS_EXTRA_BUILD=OFF
184 if [[ -n ${EPREFIX} ]]; then
186 -DGCC_INSTALL_PREFIX="${EPREFIX}/usr"
190 if tc-is-cross-compiler; then
191 [[ -x "/usr/bin/clang-tblgen" ]] \
192 || die "/usr/bin/clang-tblgen not found or usable"
194 -DCMAKE_CROSSCOMPILING=ON
195 -DCLANG_TABLEGEN=/usr/bin/clang-tblgen
199 # LLVM_ENABLE_ASSERTIONS=NO does not guarantee this for us, #614844
200 use debug || local -x CPPFLAGS="${CPPFLAGS} -DNDEBUG"
201 cmake-utils_src_configure
204 multilib_src_compile() {
205 cmake-utils_src_compile
207 # provide a symlink for tests
208 if [[ ! -L ${WORKDIR}/lib/clang ]]; then
209 mkdir -p "${WORKDIR}"/lib || die
210 ln -s "${BUILD_DIR}/$(get_libdir)/clang" "${WORKDIR}"/lib/clang || die
214 multilib_src_test() {
216 local -x LIT_PRESERVES_TMP=1
217 cmake-utils_src_make check-clang
218 multilib_is_native_abi && cmake-utils_src_make check-clang-tools
222 MULTILIB_WRAPPED_HEADERS=(
223 /usr/include/clang/Config/config.h
226 multilib-minimal_src_install
228 # Move runtime headers to /usr/lib/clang, where they belong
229 mv "${ED%/}"/usr/include/clangrt "${ED%/}"/usr/lib/clang || die
230 # move (remaining) wrapped headers back
231 mv "${ED%/}"/usr/include "${ED%/}"/usr/lib/llvm/${SLOT}/include || die
233 # Apply CHOST and version suffix to clang tools
234 # note: we use two version components here (vs 3 in runtime path)
235 local llvm_version=$(llvm-config --version) || die
236 local clang_version=$(ver_cut 1 "${llvm_version}")
237 local clang_full_version=$(ver_cut 1-3 "${llvm_version}")
238 local clang_tools=( clang clang++ clang-cl clang-cpp )
244 # - clang++, clang-cl, clang-cpp -> clang
247 # - clang++-X, clang-cl-X, clang-cpp-X -> clang-X
248 # - clang, clang++, clang-cl, clang-cpp -> clang*-X
249 # also in CHOST variant
250 for i in "${clang_tools[@]:1}"; do
251 rm "${ED%/}/usr/lib/llvm/${SLOT}/bin/${i}" || die
252 dosym "clang-${clang_version}" "/usr/lib/llvm/${SLOT}/bin/${i}-${clang_version}"
253 dosym "${i}-${clang_version}" "/usr/lib/llvm/${SLOT}/bin/${i}"
256 # now create target symlinks for all supported ABIs
257 for abi in $(get_all_abis); do
258 local abi_chost=$(get_abi_CHOST "${abi}")
259 for i in "${clang_tools[@]}"; do
260 dosym "${i}-${clang_version}" \
261 "/usr/lib/llvm/${SLOT}/bin/${abi_chost}-${i}-${clang_version}"
262 dosym "${abi_chost}-${i}-${clang_version}" \
263 "/usr/lib/llvm/${SLOT}/bin/${abi_chost}-${i}"
267 # Remove unnecessary headers on FreeBSD, bug #417171
268 if use kernel_FreeBSD; then
269 rm "${ED}"usr/lib/clang/${clang_full_version}/include/{std,float,iso,limits,tgmath,varargs}*.h || die
273 multilib_src_install() {
274 cmake-utils_src_install
276 # move headers to /usr/include for wrapping & ABI mismatch checks
277 # (also drop the version suffix from runtime headers)
278 rm -rf "${ED%/}"/usr/include || die
279 mv "${ED%/}"/usr/lib/llvm/${SLOT}/include "${ED%/}"/usr/include || die
280 mv "${ED%/}"/usr/lib/llvm/${SLOT}/$(get_libdir)/clang "${ED%/}"/usr/include/clangrt || die
283 multilib_src_install_all() {
284 python_fix_shebang "${ED}"
285 if use static-analyzer; then
286 python_optimize "${ED}"usr/lib/llvm/${SLOT}/share/scan-view
289 # # install pre-generated manpages
291 # insinto "/usr/lib/llvm/${SLOT}/share/man/man1"
292 # doins "${WORKDIR}/x/y/llvm-${PV}-manpages/clang"/*.1
295 docompress "/usr/lib/llvm/${SLOT}/share/man"
296 # match 'html' non-compression
297 use doc && docompress -x "/usr/share/doc/${PF}/tools-extra"
298 # +x for some reason; TODO: investigate
299 use static-analyzer && fperms a-x "/usr/lib/llvm/${SLOT}/share/man/man1/scan-build.1"
303 if [[ ${ROOT} == / && -f ${EPREFIX}/usr/share/eselect/modules/compiler-shadow.eselect ]] ; then
304 eselect compiler-shadow update all
307 elog "You can find additional utility scripts in:"
308 elog " ${EROOT}/usr/lib/llvm/${SLOT}/share/clang"
309 elog "To use these scripts, you will need Python 2.7. Some of them are vim"
310 elog "integration scripts (with instructions inside). The run-clang-tidy.py"
311 elog "scripts requires the following additional package:"
312 elog " dev-python/pyyaml"
316 if [[ ${ROOT} == / && -f ${EPREFIX}/usr/share/eselect/modules/compiler-shadow.eselect ]] ; then
317 eselect compiler-shadow clean all