1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: cmake-utils.eclass
8 # Tomáš Chvátal <scarabeus@gentoo.org>
9 # Maciej Mrozowski <reavertm@gentoo.org>
10 # (undisclosed contributors)
11 # Original author: Zephyrus (zephyrus@mirach.it)
12 # @SUPPORTED_EAPIS: 5 6 7
13 # @BLURB: common ebuild functions for cmake-based packages
15 # The cmake-utils eclass makes creating ebuilds for cmake-based packages much easier.
16 # It provides all inherited features (DOCS, HTML_DOCS, PATCHES) along with out-of-source
17 # builds (default), in-source builds and an implementation of the well-known use_enable
18 # and use_with functions for CMake.
20 if [[ -z ${_CMAKE_UTILS_ECLASS} ]]; then
23 # @ECLASS-VARIABLE: BUILD_DIR
25 # Build directory where all cmake processed files should be generated.
26 # For in-source build it's fixed to ${CMAKE_USE_DIR}.
27 # For out-of-source build it can be overridden, by default it uses
28 # ${WORKDIR}/${P}_build.
30 # This variable has been called CMAKE_BUILD_DIR formerly.
31 # It is set under that name for compatibility.
33 # @ECLASS-VARIABLE: CMAKE_BINARY
35 # Eclass can use different cmake binary than the one provided in by system.
36 : ${CMAKE_BINARY:=cmake}
38 # @ECLASS-VARIABLE: CMAKE_BUILD_TYPE
40 # Set to override default CMAKE_BUILD_TYPE. Only useful for packages
41 # known to make use of "if (CMAKE_BUILD_TYPE MATCHES xxx)".
42 # If about to be set - needs to be set before invoking cmake-utils_src_configure.
43 # You usualy do *NOT* want nor need to set it as it pulls CMake default build-type
44 # specific compiler flags overriding make.conf.
45 : ${CMAKE_BUILD_TYPE:=Gentoo}
47 # @ECLASS-VARIABLE: CMAKE_IN_SOURCE_BUILD
50 # Set to enable in-source build.
52 # @ECLASS-VARIABLE: CMAKE_MAKEFILE_GENERATOR
55 # Specify a makefile generator to be used by cmake.
56 # At this point only "emake" and "ninja" are supported.
57 # In EAPI 7 and above, the default is set to "ninja",
58 # whereas in EAPIs below 7, it is set to "emake".
60 # @ECLASS-VARIABLE: CMAKE_MIN_VERSION
62 # Specify the minimum required CMake version.
63 : ${CMAKE_MIN_VERSION:=3.9.6}
65 # @ECLASS-VARIABLE: CMAKE_REMOVE_MODULES
67 # Do we want to remove anything? yes or whatever else for no
68 : ${CMAKE_REMOVE_MODULES:=yes}
70 # @ECLASS-VARIABLE: CMAKE_REMOVE_MODULES_LIST
72 # Space-separated list of CMake modules that will be removed in $S during src_prepare,
73 # in order to force packages to use the system version.
74 : ${CMAKE_REMOVE_MODULES_LIST:=FindBLAS FindLAPACK}
76 # @ECLASS-VARIABLE: CMAKE_USE_DIR
78 # Sets the directory where we are working with cmake.
79 # For example when application uses autotools and only one
80 # plugin needs to be done by cmake.
81 # By default it uses ${S}.
83 # @ECLASS-VARIABLE: CMAKE_VERBOSE
85 # Set to OFF to disable verbose messages during compilation
86 : ${CMAKE_VERBOSE:=ON}
88 # @ECLASS-VARIABLE: CMAKE_WARN_UNUSED_CLI
90 # Warn about variables that are declared on the command line
91 # but not used. Might give false-positives.
92 # "no" to disable (default) or anything else to enable.
94 # @ECLASS-VARIABLE: CMAKE_EXTRA_CACHE_FILE
97 # Specifies an extra cache file to pass to cmake. This is the analog of EXTRA_ECONF
98 # for econf and is needed to pass TRY_RUN results when cross-compiling.
99 # Should be set by user in a per-package basis in /etc/portage/package.env.
101 # @ECLASS-VARIABLE: CMAKE_UTILS_QA_SRC_DIR_READONLY
104 # After running cmake-utils_src_prepare, sets ${S} to read-only. This is
105 # a user flag and should under _no circumstances_ be set in the ebuild.
106 # Helps in improving QA of build systems that write to source tree.
109 5) : ${CMAKE_WARN_UNUSED_CLI:=no} ;;
110 6|7) : ${CMAKE_WARN_UNUSED_CLI:=yes} ;;
111 *) die "EAPI=${EAPI:-0} is not supported" ;;
114 inherit toolchain-funcs ninja-utils flag-o-matic multiprocessing xdg-utils
118 : ${CMAKE_MAKEFILE_GENERATOR:=emake}
119 inherit eutils multilib
122 : ${CMAKE_MAKEFILE_GENERATOR:=ninja}
126 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
128 if [[ ${WANT_CMAKE} ]]; then
129 if [[ ${EAPI} != [56] ]]; then
130 die "\${WANT_CMAKE} has been removed and is a no-op now"
132 eqawarn "\${WANT_CMAKE} has been removed and is a no-op now"
135 [[ ${PREFIX} ]] && die "\${PREFIX} has been removed and is a no-op now"
137 case ${CMAKE_MAKEFILE_GENERATOR} in
139 BDEPEND="sys-devel/make"
142 BDEPEND="dev-util/ninja"
145 eerror "Unknown value for \${CMAKE_MAKEFILE_GENERATOR}"
146 die "Value ${CMAKE_MAKEFILE_GENERATOR} is not supported"
150 if [[ ${PN} != cmake ]]; then
151 BDEPEND+=" >=dev-util/cmake-${CMAKE_MIN_VERSION}"
156 *) DEPEND=" ${BDEPEND}" ;;
159 # Internal functions used by cmake-utils_use_*
160 _cmake_use_me_now() {
161 debug-print-function ${FUNCNAME} "$@"
164 [[ ! -z $3 ]] && arg=$3
166 [[ ${EAPI} == 5 ]] || die "${FUNCNAME[1]} is banned in EAPI 6 and later: use -D$1<related_CMake_variable>=\"\$(usex $2)\" instead"
168 local uper capitalised x
169 [[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
170 if [[ ! -z $3 ]]; then
171 # user specified the use name so use it
172 echo "-D$1$3=$(use $2 && echo ON || echo OFF)"
174 # use all various most used combinations
175 uper=$(echo ${2} | tr '[:lower:]' '[:upper:]')
176 capitalised=$(echo ${2} | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g')
177 for x in $2 $uper $capitalised; do
178 echo "-D$1$x=$(use $2 && echo ON || echo OFF) "
182 _cmake_use_me_now_inverted() {
183 debug-print-function ${FUNCNAME} "$@"
186 [[ ! -z $3 ]] && arg=$3
188 if [[ ${EAPI} != 5 && "${FUNCNAME[1]}" != cmake-utils_use_find_package ]] ; then
189 die "${FUNCNAME[1]} is banned in EAPI 6 and later: use -D$1<related_CMake_variable>=\"\$(usex $2)\" instead"
192 local uper capitalised x
193 [[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
194 if [[ ! -z $3 ]]; then
195 # user specified the use name so use it
196 echo "-D$1$3=$(use $2 && echo OFF || echo ON)"
198 # use all various most used combinations
199 uper=$(echo ${2} | tr '[:lower:]' '[:upper:]')
200 capitalised=$(echo ${2} | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g')
201 for x in $2 $uper $capitalised; do
202 echo "-D$1$x=$(use $2 && echo OFF || echo ON) "
207 # Determine using IN or OUT source build
208 _cmake_check_build_dir() {
209 : ${CMAKE_USE_DIR:=${S}}
210 if [[ -n ${CMAKE_IN_SOURCE_BUILD} ]]; then
211 # we build in source dir
212 BUILD_DIR="${CMAKE_USE_DIR}"
214 # Respect both the old variable and the new one, depending
215 # on which one was set by the ebuild.
216 if [[ ! ${BUILD_DIR} && ${CMAKE_BUILD_DIR} ]]; then
217 if [[ ${EAPI} != [56] ]]; then
218 eerror "The CMAKE_BUILD_DIR variable has been renamed to BUILD_DIR."
219 die "The ebuild must be migrated to BUILD_DIR."
221 eqawarn "The CMAKE_BUILD_DIR variable has been renamed to BUILD_DIR."
222 eqawarn "Please migrate the ebuild to use the new one."
225 # In the next call, both variables will be set already
226 # and we'd have to know which one takes precedence.
227 _RESPECT_CMAKE_BUILD_DIR=1
230 if [[ ${_RESPECT_CMAKE_BUILD_DIR} ]]; then
231 BUILD_DIR=${CMAKE_BUILD_DIR:-${WORKDIR}/${P}_build}
233 : ${BUILD_DIR:=${WORKDIR}/${P}_build}
237 # Backwards compatibility for getting the value.
238 [[ ${EAPI} == [56] ]] && CMAKE_BUILD_DIR=${BUILD_DIR}
240 mkdir -p "${BUILD_DIR}" || die
241 echo ">>> Working in BUILD_DIR: \"$BUILD_DIR\""
244 # Determine which generator to use
245 _cmake_generator_to_use() {
248 case ${CMAKE_MAKEFILE_GENERATOR} in
250 # if ninja is enabled but not installed, the build could fail
251 # this could happen if ninja is manually enabled (eg. make.conf) but not installed
254 if ! ROOT=/ has_version dev-util/ninja; then
255 die "CMAKE_MAKEFILE_GENERATOR is set to ninja, but ninja is not installed. Please install dev-util/ninja or unset CMAKE_MAKEFILE_GENERATOR."
259 if ! has_version -b dev-util/ninja; then
260 die "CMAKE_MAKEFILE_GENERATOR is set to ninja, but ninja is not installed. Please install dev-util/ninja or unset CMAKE_MAKEFILE_GENERATOR."
264 generator_name="Ninja"
267 generator_name="Unix Makefiles"
270 eerror "Unknown value for \${CMAKE_MAKEFILE_GENERATOR}"
271 die "Value ${CMAKE_MAKEFILE_GENERATOR} is not supported"
275 echo ${generator_name}
278 # @FUNCTION: cmake_comment_add_subdirectory
279 # @USAGE: <subdirectory>
281 # Comment out one or more add_subdirectory calls in CMakeLists.txt in the current directory
282 cmake_comment_add_subdirectory() {
283 if [[ -z ${1} ]]; then
284 die "comment_add_subdirectory must be passed at least one directory name to comment"
287 if [[ -e "CMakeLists.txt" ]]; then
290 sed -e "/add_subdirectory[[:space:]]*([[:space:]]*${d//\//\\/}[[:space:]]*)/I s/^/#DONOTCOMPILE /" \
291 -i CMakeLists.txt || die "failed to comment add_subdirectory(${d})"
296 # @FUNCTION: comment_add_subdirectory
297 # @USAGE: <subdirectory>
299 # Comment out an add_subdirectory call in CMakeLists.txt in the current directory
300 # Banned in EAPI 6 and later - use cmake_comment_add_subdirectory instead.
301 comment_add_subdirectory() {
302 [[ ${EAPI} == 5 ]] || die "comment_add_subdirectory is banned in EAPI 6 and later - use cmake_comment_add_subdirectory instead"
304 cmake_comment_add_subdirectory "$@"
307 # @FUNCTION: cmake-utils_use_with
308 # @USAGE: <USE flag> [flag name]
310 # Based on use_with. See ebuild(5).
312 # `cmake-utils_use_with foo FOO` echoes -DWITH_FOO=ON if foo is enabled
313 # and -DWITH_FOO=OFF if it is disabled.
314 cmake-utils_use_with() { _cmake_use_me_now WITH_ "$@" ; }
316 # @FUNCTION: cmake-utils_use_enable
317 # @USAGE: <USE flag> [flag name]
319 # Based on use_enable. See ebuild(5).
321 # `cmake-utils_use_enable foo FOO` echoes -DENABLE_FOO=ON if foo is enabled
322 # and -DENABLE_FOO=OFF if it is disabled.
323 cmake-utils_use_enable() { _cmake_use_me_now ENABLE_ "$@" ; }
325 # @FUNCTION: cmake-utils_use_find_package
326 # @USAGE: <USE flag> <package name>
328 # Based on use_enable. See ebuild(5).
330 # `cmake-utils_use_find_package foo LibFoo` echoes -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=OFF
331 # if foo is enabled and -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=ON if it is disabled.
332 # This can be used to make find_package optional.
333 cmake-utils_use_find_package() {
334 if [[ ${EAPI} != 5 && "$#" != 2 ]] ; then
335 die "Usage: cmake-utils_use_find_package <USE flag> <package name>"
338 _cmake_use_me_now_inverted CMAKE_DISABLE_FIND_PACKAGE_ "$@" ;
341 # @FUNCTION: cmake_use_find_package
342 # @USAGE: <USE flag> <package name>
344 # Alias for cmake-utils_use_find_package.
345 cmake_use_find_package() {
346 if [[ "$#" != 2 ]] ; then
347 die "Usage: cmake_use_find_package <USE flag> <package name>"
350 cmake-utils_use_find_package "$@" ;
353 # @FUNCTION: cmake-utils_use_disable
354 # @USAGE: <USE flag> [flag name]
356 # Based on inversion of use_enable. See ebuild(5).
358 # `cmake-utils_use_enable foo FOO` echoes -DDISABLE_FOO=OFF if foo is enabled
359 # and -DDISABLE_FOO=ON if it is disabled.
360 cmake-utils_use_disable() { _cmake_use_me_now_inverted DISABLE_ "$@" ; }
362 # @FUNCTION: cmake-utils_use_no
363 # @USAGE: <USE flag> [flag name]
365 # Based on use_disable. See ebuild(5).
367 # `cmake-utils_use_no foo FOO` echoes -DNO_FOO=OFF if foo is enabled
368 # and -DNO_FOO=ON if it is disabled.
369 cmake-utils_use_no() { _cmake_use_me_now_inverted NO_ "$@" ; }
371 # @FUNCTION: cmake-utils_use_want
372 # @USAGE: <USE flag> [flag name]
374 # Based on use_enable. See ebuild(5).
376 # `cmake-utils_use_want foo FOO` echoes -DWANT_FOO=ON if foo is enabled
377 # and -DWANT_FOO=OFF if it is disabled.
378 cmake-utils_use_want() { _cmake_use_me_now WANT_ "$@" ; }
380 # @FUNCTION: cmake-utils_use_build
381 # @USAGE: <USE flag> [flag name]
383 # Based on use_enable. See ebuild(5).
385 # `cmake-utils_use_build foo FOO` echoes -DBUILD_FOO=ON if foo is enabled
386 # and -DBUILD_FOO=OFF if it is disabled.
387 cmake-utils_use_build() { _cmake_use_me_now BUILD_ "$@" ; }
389 # @FUNCTION: cmake-utils_use_has
390 # @USAGE: <USE flag> [flag name]
392 # Based on use_enable. See ebuild(5).
394 # `cmake-utils_use_has foo FOO` echoes -DHAVE_FOO=ON if foo is enabled
395 # and -DHAVE_FOO=OFF if it is disabled.
396 cmake-utils_use_has() { _cmake_use_me_now HAVE_ "$@" ; }
398 # @FUNCTION: cmake-utils_use_use
399 # @USAGE: <USE flag> [flag name]
401 # Based on use_enable. See ebuild(5).
403 # `cmake-utils_use_use foo FOO` echoes -DUSE_FOO=ON if foo is enabled
404 # and -DUSE_FOO=OFF if it is disabled.
405 cmake-utils_use_use() { _cmake_use_me_now USE_ "$@" ; }
407 # @FUNCTION: cmake-utils_use
408 # @USAGE: <USE flag> [flag name]
410 # Based on use_enable. See ebuild(5).
412 # `cmake-utils_use foo FOO` echoes -DFOO=ON if foo is enabled
413 # and -DFOO=OFF if it is disabled.
414 cmake-utils_use() { _cmake_use_me_now "" "$@" ; }
416 # @FUNCTION: cmake-utils_useno
417 # @USAGE: <USE flag> [flag name]
419 # Based on use_enable. See ebuild(5).
421 # `cmake-utils_useno foo NOFOO` echoes -DNOFOO=OFF if foo is enabled
422 # and -DNOFOO=ON if it is disabled.
423 cmake-utils_useno() { _cmake_use_me_now_inverted "" "$@" ; }
425 # Internal function for modifying hardcoded definitions.
426 # Removes dangerous definitions that override Gentoo settings.
427 _cmake_modify-cmakelists() {
428 debug-print-function ${FUNCNAME} "$@"
430 # Only edit the files once
431 grep -qs "<<< Gentoo configuration >>>" "${CMAKE_USE_DIR}"/CMakeLists.txt && return 0
433 # Comment out all set (<some_should_be_user_defined_variable> value)
434 find "${CMAKE_USE_DIR}" -name CMakeLists.txt -exec sed \
435 -e '/^[[:space:]]*set[[:space:]]*([[:space:]]*CMAKE_BUILD_TYPE\([[:space:]].*)\|)\)/I{s/^/#_cmake_modify_IGNORE /g}' \
436 -e '/^[[:space:]]*set[[:space:]]*([[:space:]]*CMAKE_COLOR_MAKEFILE[[:space:]].*)/I{s/^/#_cmake_modify_IGNORE /g}' \
437 -e '/^[[:space:]]*set[[:space:]]*([[:space:]]*CMAKE_INSTALL_PREFIX[[:space:]].*)/I{s/^/#_cmake_modify_IGNORE /g}' \
438 -e '/^[[:space:]]*set[[:space:]]*([[:space:]]*CMAKE_VERBOSE_MAKEFILE[[:space:]].*)/I{s/^/#_cmake_modify_IGNORE /g}' \
439 -i {} + || die "${LINENO}: failed to disable hardcoded settings"
441 for x in $(find "${CMAKE_USE_DIR}" -name CMakeLists.txt -exec grep -l "^#_cmake_modify_IGNORE" {} +;); do
442 einfo "Hardcoded definition(s) removed in $(echo "${x}" | cut -c $((${#CMAKE_USE_DIR}+2))-):"
443 einfo "$(grep -se '^#_cmake_modify_IGNORE' ${x} | cut -c 22-99)"
446 # NOTE Append some useful summary here
447 cat >> "${CMAKE_USE_DIR}"/CMakeLists.txt <<- _EOF_ || die
449 MESSAGE(STATUS "<<< Gentoo configuration >>>
450 Build type \${CMAKE_BUILD_TYPE}
451 Install path \${CMAKE_INSTALL_PREFIX}
454 C++ \${CMAKE_CXX_FLAGS}
456 Executable \${CMAKE_EXE_LINKER_FLAGS}
457 Module \${CMAKE_MODULE_LINKER_FLAGS}
458 Shared \${CMAKE_SHARED_LINKER_FLAGS}\n")
462 # temporary function for moving cmake cleanups from from src_configure -> src_prepare.
464 _cmake_cleanup_cmake() {
465 : ${CMAKE_USE_DIR:=${S}}
467 if [[ "${CMAKE_REMOVE_MODULES}" == "yes" ]] ; then
469 for name in ${CMAKE_REMOVE_MODULES_LIST} ; do
470 find "${S}" -name ${name}.cmake -exec rm -v {} + || die
474 # check if CMakeLists.txt exist and if no then die
475 if [[ ! -e ${CMAKE_USE_DIR}/CMakeLists.txt ]] ; then
476 eerror "Unable to locate CMakeLists.txt under:"
477 eerror "\"${CMAKE_USE_DIR}/CMakeLists.txt\""
478 eerror "Consider not inheriting the cmake eclass."
479 die "FATAL: Unable to find CMakeLists.txt"
482 # Remove dangerous things.
483 _cmake_modify-cmakelists
486 # @FUNCTION: cmake-utils_src_prepare
488 # Apply ebuild and user patches.
489 cmake-utils_src_prepare() {
490 debug-print-function ${FUNCNAME} "$@"
492 pushd "${S}" > /dev/null || die
494 if [[ ${EAPI} != 5 ]]; then
498 debug-print "$FUNCNAME: PATCHES=$PATCHES"
499 [[ ${PATCHES[@]} ]] && epatch "${PATCHES[@]}"
501 debug-print "$FUNCNAME: applying user patches"
505 popd > /dev/null || die
507 # make ${S} read-only in order to detect broken build-systems
508 if [[ ${CMAKE_UTILS_QA_SRC_DIR_READONLY} && ! ${CMAKE_IN_SOURCE_BUILD} ]]; then
512 _CMAKE_UTILS_SRC_PREPARE_HAS_RUN=1
515 # @VARIABLE: mycmakeargs
518 # Optional cmake defines as a bash array. Should be defined before calling
522 # local mycmakeargs=(
523 # $(cmake-utils_use_with openconnect)
526 # cmake-utils_src_configure
530 # @FUNCTION: cmake-utils_src_configure
532 # General function for configuring with cmake. Default behaviour is to start an
533 # out-of-source build.
534 cmake-utils_src_configure() {
535 debug-print-function ${FUNCNAME} "$@"
537 if [[ ! ${_CMAKE_UTILS_SRC_PREPARE_HAS_RUN} ]]; then
538 if [[ ${EAPI} != [56] ]]; then
539 die "FATAL: cmake-utils_src_prepare has not been run"
541 eqawarn "cmake-utils_src_prepare has not been run, please open a bug on https://bugs.gentoo.org/"
545 [[ ${EAPI} == 5 ]] && _cmake_cleanup_cmake
547 _cmake_check_build_dir
549 # Fix xdg collision with sandbox
550 xdg_environment_reset
552 # @SEE CMAKE_BUILD_TYPE
553 if [[ ${CMAKE_BUILD_TYPE} = Gentoo ]]; then
554 # Handle release builds
555 if ! has debug ${IUSE//+} || ! use debug; then
556 local CPPFLAGS=${CPPFLAGS}
557 append-cppflags -DNDEBUG
561 # Prepare Gentoo override rules (set valid compiler, append CPPFLAGS etc.)
562 local build_rules=${BUILD_DIR}/gentoo_rules.cmake
564 cat > "${build_rules}" <<- _EOF_ || die
565 SET (CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "ASM compile command" FORCE)
566 SET (CMAKE_ASM-ATT_COMPILE_OBJECT "<CMAKE_ASM-ATT_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c -x assembler <SOURCE>" CACHE STRING "ASM-ATT compile command" FORCE)
567 SET (CMAKE_ASM-ATT_LINK_FLAGS "-nostdlib" CACHE STRING "ASM-ATT link flags" FORCE)
568 SET (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C compile command" FORCE)
569 SET (CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C++ compile command" FORCE)
570 SET (CMAKE_Fortran_COMPILE_OBJECT "<CMAKE_Fortran_COMPILER> <DEFINES> <INCLUDES> ${FCFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "Fortran compile command" FORCE)
573 local myCC=$(tc-getCC) myCXX=$(tc-getCXX) myFC=$(tc-getFC)
575 # !!! IMPORTANT NOTE !!!
576 # Single slash below is intentional. CMake is weird and wants the
577 # CMAKE_*_VARIABLES split into two elements: the first one with
578 # compiler path, and the second one with all command-line options,
580 local toolchain_file=${BUILD_DIR}/gentoo_toolchain.cmake
581 cat > ${toolchain_file} <<- _EOF_ || die
582 SET (CMAKE_ASM_COMPILER "${myCC/ /;}")
583 SET (CMAKE_ASM-ATT_COMPILER "${myCC/ /;}")
584 SET (CMAKE_C_COMPILER "${myCC/ /;}")
585 SET (CMAKE_CXX_COMPILER "${myCXX/ /;}")
586 SET (CMAKE_Fortran_COMPILER "${myFC/ /;}")
587 SET (CMAKE_AR $(type -P $(tc-getAR)) CACHE FILEPATH "Archive manager" FORCE)
588 SET (CMAKE_RANLIB $(type -P $(tc-getRANLIB)) CACHE FILEPATH "Archive index generator" FORCE)
589 SET (CMAKE_SYSTEM_PROCESSOR "${CHOST%%-*}")
592 # We are using the C compiler for assembly by default.
593 local -x ASMFLAGS=${CFLAGS}
594 local -x PKG_CONFIG=$(tc-getPKG_CONFIG)
596 if tc-is-cross-compiler; then
598 case "${KERNEL:-linux}" in
599 Cygwin) sysname="CYGWIN_NT-5.1" ;;
600 HPUX) sysname="HP-UX" ;;
601 linux) sysname="Linux" ;;
604 cat >> "${toolchain_file}" <<- _EOF_ || die
605 SET (CMAKE_RC_COMPILER $(tc-getRC))
608 *) sysname="${KERNEL}" ;;
611 cat >> "${toolchain_file}" <<- _EOF_ || die
612 SET (CMAKE_SYSTEM_NAME "${sysname}")
615 if [ "${SYSROOT:-/}" != "/" ] ; then
616 # When cross-compiling with a sysroot (e.g. with crossdev's emerge wrappers)
617 # we need to tell cmake to use libs/headers from the sysroot but programs from / only.
618 cat >> "${toolchain_file}" <<- _EOF_ || die
619 SET (CMAKE_FIND_ROOT_PATH "${SYSROOT}")
620 SET (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
621 SET (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
622 SET (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
627 if use prefix-guest; then
628 cat >> "${build_rules}" <<- _EOF_ || die
629 # in Prefix we need rpath and must ensure cmake gets our default linker path
630 # right ... except for Darwin hosts
632 SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
633 SET (CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH "${EPREFIX}/usr/${CHOST}/lib/gcc;${EPREFIX}/usr/${CHOST}/lib;${EPREFIX}/usr/$(get_libdir);${EPREFIX}/$(get_libdir)"
634 CACHE STRING "" FORCE)
638 SET (CMAKE_PREFIX_PATH "${EPREFIX}/usr" CACHE STRING "" FORCE)
639 SET (CMAKE_MACOSX_RPATH ON CACHE BOOL "" FORCE)
640 SET (CMAKE_SKIP_BUILD_RPATH OFF CACHE BOOL "" FORCE)
641 SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
642 SET (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOL "" FORCE)
648 # Common configure parameters (invariants)
649 local common_config=${BUILD_DIR}/gentoo_common_config.cmake
650 local libdir=$(get_libdir)
651 cat > "${common_config}" <<- _EOF_ || die
652 SET (CMAKE_GENTOO_BUILD ON CACHE BOOL "Indicate Gentoo package build")
653 SET (LIB_SUFFIX ${libdir/lib} CACHE STRING "library path suffix" FORCE)
654 SET (CMAKE_INSTALL_LIBDIR ${libdir} CACHE PATH "Output directory for libraries")
655 SET (CMAKE_INSTALL_INFODIR "${EPREFIX}/usr/share/info" CACHE PATH "")
656 SET (CMAKE_INSTALL_MANDIR "${EPREFIX}/usr/share/man" CACHE PATH "")
657 SET (CMAKE_USER_MAKE_RULES_OVERRIDE "${build_rules}" CACHE FILEPATH "Gentoo override rules")
659 [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo 'SET (CMAKE_COLOR_MAKEFILE OFF CACHE BOOL "pretty colors during make" FORCE)' >> "${common_config}"
661 if [[ ${EAPI} != [56] ]]; then
662 cat >> "${common_config}" <<- _EOF_ || die
663 SET (CMAKE_INSTALL_DOCDIR "${EPREFIX}/usr/share/doc/${PF}" CACHE PATH "")
664 SET (BUILD_SHARED_LIBS ON CACHE BOOL "")
668 # Wipe the default optimization flags out of CMake
669 if [[ ${CMAKE_BUILD_TYPE} != Gentoo && ${EAPI} != 5 ]]; then
670 cat >> ${common_config} <<- _EOF_ || die
671 SET (CMAKE_ASM_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
672 SET (CMAKE_ASM-ATT_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
673 SET (CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
674 SET (CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
675 SET (CMAKE_Fortran_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
676 SET (CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
677 SET (CMAKE_MODULE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
678 SET (CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
679 SET (CMAKE_STATIC_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
683 # Convert mycmakeargs to an array, for backwards compatibility
684 # Make the array a local variable since <=portage-2.1.6.x does not
685 # support global arrays (see bug #297255).
686 local mycmakeargstype=$(declare -p mycmakeargs 2>&-)
687 if [[ "${mycmakeargstype}" != "declare -a mycmakeargs="* ]]; then
688 if [[ -n "${mycmakeargstype}" ]] ; then
689 if [[ ${EAPI} == 5 ]]; then
690 eqawarn "Declaring mycmakeargs as a variable is deprecated. Please use an array instead."
692 die "Declaring mycmakeargs as a variable is banned in EAPI=${EAPI}. Please use an array instead."
695 local mycmakeargs_local=(${mycmakeargs})
697 local mycmakeargs_local=("${mycmakeargs[@]}")
700 if [[ ${CMAKE_WARN_UNUSED_CLI} == no ]] ; then
701 local warn_unused_cli="--no-warn-unused-cli"
703 local warn_unused_cli=""
706 # Common configure parameters (overridable)
707 # NOTE CMAKE_BUILD_TYPE can be only overridden via CMAKE_BUILD_TYPE eclass variable
708 # No -DCMAKE_BUILD_TYPE=xxx definitions will be in effect.
711 -C "${common_config}"
712 -G "$(_cmake_generator_to_use)"
713 -DCMAKE_INSTALL_PREFIX="${EPREFIX}/usr"
714 "${mycmakeargs_local[@]}"
715 -DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
716 $([[ ${EAPI} == 5 ]] && echo -DCMAKE_INSTALL_DO_STRIP=OFF)
717 -DCMAKE_TOOLCHAIN_FILE="${toolchain_file}"
721 if [[ -n "${CMAKE_EXTRA_CACHE_FILE}" ]] ; then
722 cmakeargs+=( -C "${CMAKE_EXTRA_CACHE_FILE}" )
725 pushd "${BUILD_DIR}" > /dev/null || die
726 debug-print "${LINENO} ${ECLASS} ${FUNCNAME}: mycmakeargs is ${mycmakeargs_local[*]}"
727 echo "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}"
728 "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}" || die "cmake failed"
729 popd > /dev/null || die
732 # @FUNCTION: cmake-utils_src_compile
734 # General function for compiling with cmake.
735 # Automatically detects the build type. All arguments are passed to emake.
736 cmake-utils_src_compile() {
737 debug-print-function ${FUNCNAME} "$@"
739 cmake-utils_src_make "$@"
742 # @FUNCTION: _cmake_ninja_src_make
745 # Build the package using ninja generator
746 _cmake_ninja_src_make() {
747 debug-print-function ${FUNCNAME} "$@"
749 [[ -e build.ninja ]] || die "build.ninja not found. Error during configure stage."
754 # @FUNCTION: _cmake_emake_src_make
757 # Build the package using make generator
758 _cmake_emake_src_make() {
759 debug-print-function ${FUNCNAME} "$@"
761 [[ -e Makefile ]] || die "Makefile not found. Error during configure stage."
763 if [[ "${CMAKE_VERBOSE}" != "OFF" ]]; then
764 emake VERBOSE=1 "$@" || die
771 # @FUNCTION: cmake-utils_src_make
773 # Function for building the package. Automatically detects the build type.
774 # All arguments are passed to emake.
775 cmake-utils_src_make() {
776 debug-print-function ${FUNCNAME} "$@"
778 _cmake_check_build_dir
779 pushd "${BUILD_DIR}" > /dev/null || die
781 _cmake_${CMAKE_MAKEFILE_GENERATOR}_src_make "$@"
783 popd > /dev/null || die
786 # @FUNCTION: cmake-utils_src_test
788 # Function for testing the package. Automatically detects the build type.
789 cmake-utils_src_test() {
790 debug-print-function ${FUNCNAME} "$@"
792 _cmake_check_build_dir
793 pushd "${BUILD_DIR}" > /dev/null || die
794 [[ -e CTestTestfile.cmake ]] || { echo "No tests found. Skipping."; return 0 ; }
796 [[ -n ${TEST_VERBOSE} ]] && myctestargs+=( --extra-verbose --output-on-failure )
798 set -- ctest -j "$(makeopts_jobs)" --test-load "$(makeopts_loadavg)" "${myctestargs[@]}" "$@"
801 einfo "Tests succeeded."
802 popd > /dev/null || die
805 if [[ -n "${CMAKE_YES_I_WANT_TO_SEE_THE_TEST_LOG}" ]] ; then
806 # on request from Diego
807 eerror "Tests failed. Test log ${BUILD_DIR}/Testing/Temporary/LastTest.log follows:"
808 eerror "--START TEST LOG--------------------------------------------------------------"
809 cat "${BUILD_DIR}/Testing/Temporary/LastTest.log"
810 eerror "--END TEST LOG----------------------------------------------------------------"
813 die "Tests failed. When you file a bug, please attach the following file: \n\t${BUILD_DIR}/Testing/Temporary/LastTest.log"
816 # die might not die due to nonfatal
817 popd > /dev/null || die
822 # @FUNCTION: cmake-utils_src_install
824 # Function for installing the package. Automatically detects the build type.
825 cmake-utils_src_install() {
826 debug-print-function ${FUNCNAME} "$@"
828 _cmake_check_build_dir
829 pushd "${BUILD_DIR}" > /dev/null || die
830 DESTDIR="${D}" ${CMAKE_MAKEFILE_GENERATOR} install "$@" || die "died running ${CMAKE_MAKEFILE_GENERATOR} install"
831 popd > /dev/null || die
833 pushd "${S}" > /dev/null || die
835 popd > /dev/null || die