1 # Copyright 1999-2018 Gentoo Foundation
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
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
54 # Specify a makefile generator to be used by cmake.
55 # At this point only "emake" and "ninja" are supported.
56 : ${CMAKE_MAKEFILE_GENERATOR:=emake}
58 # @ECLASS-VARIABLE: CMAKE_MIN_VERSION
60 # Specify the minimum required CMake version.
61 : ${CMAKE_MIN_VERSION:=3.9.6}
63 # @ECLASS-VARIABLE: CMAKE_REMOVE_MODULES
65 # Do we want to remove anything? yes or whatever else for no
66 : ${CMAKE_REMOVE_MODULES:=yes}
68 # @ECLASS-VARIABLE: CMAKE_REMOVE_MODULES_LIST
70 # Space-separated list of CMake modules that will be removed in $S during src_prepare,
71 # in order to force packages to use the system version.
72 : ${CMAKE_REMOVE_MODULES_LIST:=FindBLAS FindLAPACK}
74 # @ECLASS-VARIABLE: CMAKE_USE_DIR
76 # Sets the directory where we are working with cmake.
77 # For example when application uses autotools and only one
78 # plugin needs to be done by cmake.
79 # By default it uses ${S}.
81 # @ECLASS-VARIABLE: CMAKE_VERBOSE
83 # Set to OFF to disable verbose messages during compilation
84 : ${CMAKE_VERBOSE:=ON}
86 # @ECLASS-VARIABLE: CMAKE_WARN_UNUSED_CLI
88 # Warn about variables that are declared on the command line
89 # but not used. Might give false-positives.
90 # "no" to disable (default) or anything else to enable.
92 # @ECLASS-VARIABLE: CMAKE_EXTRA_CACHE_FILE
95 # Specifies an extra cache file to pass to cmake. This is the analog of EXTRA_ECONF
96 # for econf and is needed to pass TRY_RUN results when cross-compiling.
97 # Should be set by user in a per-package basis in /etc/portage/package.env.
99 # @ECLASS-VARIABLE: CMAKE_UTILS_QA_SRC_DIR_READONLY
102 # After running cmake-utils_src_prepare, sets ${S} to read-only. This is
103 # a user flag and should under _no circumstances_ be set in the ebuild.
104 # Helps in improving QA of build systems that write to source tree.
107 5) : ${CMAKE_WARN_UNUSED_CLI:=no} ;;
108 6) : ${CMAKE_WARN_UNUSED_CLI:=yes} ;;
109 *) die "EAPI=${EAPI:-0} is not supported" ;;
112 inherit toolchain-funcs ninja-utils flag-o-matic multiprocessing xdg-utils
115 5|6) inherit eutils multilib ;;
118 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
120 [[ ${WANT_CMAKE} ]] && eqawarn "\${WANT_CMAKE} has been removed and is a no-op now"
121 [[ ${PREFIX} ]] && die "\${PREFIX} has been removed and is a no-op now"
123 case ${CMAKE_MAKEFILE_GENERATOR} in
125 DEPEND="sys-devel/make"
128 DEPEND="dev-util/ninja"
131 eerror "Unknown value for \${CMAKE_MAKEFILE_GENERATOR}"
132 die "Value ${CMAKE_MAKEFILE_GENERATOR} is not supported"
136 if [[ ${PN} != cmake ]]; then
137 DEPEND+=" >=dev-util/cmake-${CMAKE_MIN_VERSION}"
140 # Internal functions used by cmake-utils_use_*
141 _cmake_use_me_now() {
142 debug-print-function ${FUNCNAME} "$@"
145 [[ ! -z $3 ]] && arg=$3
147 [[ ${EAPI} == 5 ]] || die "${FUNCNAME[1]} is banned in EAPI 6 and later: use -D$1<related_CMake_variable>=\"\$(usex $2)\" instead"
149 local uper capitalised x
150 [[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
151 if [[ ! -z $3 ]]; then
152 # user specified the use name so use it
153 echo "-D$1$3=$(use $2 && echo ON || echo OFF)"
155 # use all various most used combinations
156 uper=$(echo ${2} | tr '[:lower:]' '[:upper:]')
157 capitalised=$(echo ${2} | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g')
158 for x in $2 $uper $capitalised; do
159 echo "-D$1$x=$(use $2 && echo ON || echo OFF) "
163 _cmake_use_me_now_inverted() {
164 debug-print-function ${FUNCNAME} "$@"
167 [[ ! -z $3 ]] && arg=$3
169 if [[ ${EAPI} != 5 && "${FUNCNAME[1]}" != cmake-utils_use_find_package ]] ; then
170 die "${FUNCNAME[1]} is banned in EAPI 6 and later: use -D$1<related_CMake_variable>=\"\$(usex $2)\" instead"
173 local uper capitalised x
174 [[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
175 if [[ ! -z $3 ]]; then
176 # user specified the use name so use it
177 echo "-D$1$3=$(use $2 && echo OFF || echo ON)"
179 # use all various most used combinations
180 uper=$(echo ${2} | tr '[:lower:]' '[:upper:]')
181 capitalised=$(echo ${2} | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g')
182 for x in $2 $uper $capitalised; do
183 echo "-D$1$x=$(use $2 && echo OFF || echo ON) "
188 # Determine using IN or OUT source build
189 _cmake_check_build_dir() {
190 : ${CMAKE_USE_DIR:=${S}}
191 if [[ -n ${CMAKE_IN_SOURCE_BUILD} ]]; then
192 # we build in source dir
193 BUILD_DIR="${CMAKE_USE_DIR}"
195 # Respect both the old variable and the new one, depending
196 # on which one was set by the ebuild.
197 if [[ ! ${BUILD_DIR} && ${CMAKE_BUILD_DIR} ]]; then
198 eqawarn "The CMAKE_BUILD_DIR variable has been renamed to BUILD_DIR."
199 eqawarn "Please migrate the ebuild to use the new one."
201 # In the next call, both variables will be set already
202 # and we'd have to know which one takes precedence.
203 _RESPECT_CMAKE_BUILD_DIR=1
206 if [[ ${_RESPECT_CMAKE_BUILD_DIR} ]]; then
207 BUILD_DIR=${CMAKE_BUILD_DIR:-${WORKDIR}/${P}_build}
209 : ${BUILD_DIR:=${WORKDIR}/${P}_build}
213 # Backwards compatibility for getting the value.
214 CMAKE_BUILD_DIR=${BUILD_DIR}
216 mkdir -p "${BUILD_DIR}" || die
217 echo ">>> Working in BUILD_DIR: \"$BUILD_DIR\""
220 # Determine which generator to use
221 _cmake_generator_to_use() {
224 case ${CMAKE_MAKEFILE_GENERATOR} in
226 # if ninja is enabled but not installed, the build could fail
227 # this could happen if ninja is manually enabled (eg. make.conf) but not installed
230 if ! ROOT=/ has_version dev-util/ninja; then
231 die "CMAKE_MAKEFILE_GENERATOR is set to ninja, but ninja is not installed. Please install dev-util/ninja or unset CMAKE_MAKEFILE_GENERATOR."
235 if ! has_version -b dev-util/ninja; then
236 die "CMAKE_MAKEFILE_GENERATOR is set to ninja, but ninja is not installed. Please install dev-util/ninja or unset CMAKE_MAKEFILE_GENERATOR."
240 generator_name="Ninja"
243 generator_name="Unix Makefiles"
246 eerror "Unknown value for \${CMAKE_MAKEFILE_GENERATOR}"
247 die "Value ${CMAKE_MAKEFILE_GENERATOR} is not supported"
251 echo ${generator_name}
254 # @FUNCTION: cmake_comment_add_subdirectory
255 # @USAGE: <subdirectory>
257 # Comment out one or more add_subdirectory calls in CMakeLists.txt in the current directory
258 cmake_comment_add_subdirectory() {
259 if [[ -z ${1} ]]; then
260 die "comment_add_subdirectory must be passed at least one directory name to comment"
263 if [[ -e "CMakeLists.txt" ]]; then
266 sed -e "/add_subdirectory[[:space:]]*([[:space:]]*${d//\//\\/}[[:space:]]*)/I s/^/#DONOTCOMPILE /" \
267 -i CMakeLists.txt || die "failed to comment add_subdirectory(${d})"
272 # @FUNCTION: comment_add_subdirectory
273 # @USAGE: <subdirectory>
275 # Comment out an add_subdirectory call in CMakeLists.txt in the current directory
276 # Banned in EAPI 6 and later - use cmake_comment_add_subdirectory instead.
277 comment_add_subdirectory() {
278 [[ ${EAPI} == 5 ]] || die "comment_add_subdirectory is banned in EAPI 6 and later - use cmake_comment_add_subdirectory instead"
280 cmake_comment_add_subdirectory "$@"
283 # @FUNCTION: cmake-utils_use_with
284 # @USAGE: <USE flag> [flag name]
286 # Based on use_with. See ebuild(5).
288 # `cmake-utils_use_with foo FOO` echoes -DWITH_FOO=ON if foo is enabled
289 # and -DWITH_FOO=OFF if it is disabled.
290 cmake-utils_use_with() { _cmake_use_me_now WITH_ "$@" ; }
292 # @FUNCTION: cmake-utils_use_enable
293 # @USAGE: <USE flag> [flag name]
295 # Based on use_enable. See ebuild(5).
297 # `cmake-utils_use_enable foo FOO` echoes -DENABLE_FOO=ON if foo is enabled
298 # and -DENABLE_FOO=OFF if it is disabled.
299 cmake-utils_use_enable() { _cmake_use_me_now ENABLE_ "$@" ; }
301 # @FUNCTION: cmake-utils_use_find_package
302 # @USAGE: <USE flag> <package name>
304 # Based on use_enable. See ebuild(5).
306 # `cmake-utils_use_find_package foo LibFoo` echoes -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=OFF
307 # if foo is enabled and -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=ON if it is disabled.
308 # This can be used to make find_package optional.
309 cmake-utils_use_find_package() {
310 if [[ ${EAPI} != 5 && "$#" != 2 ]] ; then
311 die "Usage: cmake-utils_use_find_package <USE flag> <package name>"
314 _cmake_use_me_now_inverted CMAKE_DISABLE_FIND_PACKAGE_ "$@" ;
317 # @FUNCTION: cmake-utils_use_disable
318 # @USAGE: <USE flag> [flag name]
320 # Based on inversion of use_enable. See ebuild(5).
322 # `cmake-utils_use_enable foo FOO` echoes -DDISABLE_FOO=OFF if foo is enabled
323 # and -DDISABLE_FOO=ON if it is disabled.
324 cmake-utils_use_disable() { _cmake_use_me_now_inverted DISABLE_ "$@" ; }
326 # @FUNCTION: cmake-utils_use_no
327 # @USAGE: <USE flag> [flag name]
329 # Based on use_disable. See ebuild(5).
331 # `cmake-utils_use_no foo FOO` echoes -DNO_FOO=OFF if foo is enabled
332 # and -DNO_FOO=ON if it is disabled.
333 cmake-utils_use_no() { _cmake_use_me_now_inverted NO_ "$@" ; }
335 # @FUNCTION: cmake-utils_use_want
336 # @USAGE: <USE flag> [flag name]
338 # Based on use_enable. See ebuild(5).
340 # `cmake-utils_use_want foo FOO` echoes -DWANT_FOO=ON if foo is enabled
341 # and -DWANT_FOO=OFF if it is disabled.
342 cmake-utils_use_want() { _cmake_use_me_now WANT_ "$@" ; }
344 # @FUNCTION: cmake-utils_use_build
345 # @USAGE: <USE flag> [flag name]
347 # Based on use_enable. See ebuild(5).
349 # `cmake-utils_use_build foo FOO` echoes -DBUILD_FOO=ON if foo is enabled
350 # and -DBUILD_FOO=OFF if it is disabled.
351 cmake-utils_use_build() { _cmake_use_me_now BUILD_ "$@" ; }
353 # @FUNCTION: cmake-utils_use_has
354 # @USAGE: <USE flag> [flag name]
356 # Based on use_enable. See ebuild(5).
358 # `cmake-utils_use_has foo FOO` echoes -DHAVE_FOO=ON if foo is enabled
359 # and -DHAVE_FOO=OFF if it is disabled.
360 cmake-utils_use_has() { _cmake_use_me_now HAVE_ "$@" ; }
362 # @FUNCTION: cmake-utils_use_use
363 # @USAGE: <USE flag> [flag name]
365 # Based on use_enable. See ebuild(5).
367 # `cmake-utils_use_use foo FOO` echoes -DUSE_FOO=ON if foo is enabled
368 # and -DUSE_FOO=OFF if it is disabled.
369 cmake-utils_use_use() { _cmake_use_me_now USE_ "$@" ; }
371 # @FUNCTION: cmake-utils_use
372 # @USAGE: <USE flag> [flag name]
374 # Based on use_enable. See ebuild(5).
376 # `cmake-utils_use foo FOO` echoes -DFOO=ON if foo is enabled
377 # and -DFOO=OFF if it is disabled.
378 cmake-utils_use() { _cmake_use_me_now "" "$@" ; }
380 # @FUNCTION: cmake-utils_useno
381 # @USAGE: <USE flag> [flag name]
383 # Based on use_enable. See ebuild(5).
385 # `cmake-utils_useno foo NOFOO` echoes -DNOFOO=OFF if foo is enabled
386 # and -DNOFOO=ON if it is disabled.
387 cmake-utils_useno() { _cmake_use_me_now_inverted "" "$@" ; }
389 # Internal function for modifying hardcoded definitions.
390 # Removes dangerous definitions that override Gentoo settings.
391 _cmake_modify-cmakelists() {
392 debug-print-function ${FUNCNAME} "$@"
394 # Only edit the files once
395 grep -qs "<<< Gentoo configuration >>>" "${CMAKE_USE_DIR}"/CMakeLists.txt && return 0
397 # Comment out all set (<some_should_be_user_defined_variable> value)
398 # TODO Add QA checker - inform when variable being checked for below is set in CMakeLists.txt
399 find "${CMAKE_USE_DIR}" -name CMakeLists.txt \
400 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_BUILD_TYPE.*)/{s/^/#IGNORE /g}' {} + \
401 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_COLOR_MAKEFILE.*)/{s/^/#IGNORE /g}' {} + \
402 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_INSTALL_PREFIX.*)/{s/^/#IGNORE /g}' {} + \
403 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_VERBOSE_MAKEFILE.*)/{s/^/#IGNORE /g}' {} + \
404 || die "${LINENO}: failed to disable hardcoded settings"
406 # NOTE Append some useful summary here
407 cat >> "${CMAKE_USE_DIR}"/CMakeLists.txt <<- _EOF_ || die
409 MESSAGE(STATUS "<<< Gentoo configuration >>>
410 Build type \${CMAKE_BUILD_TYPE}
411 Install path \${CMAKE_INSTALL_PREFIX}
414 C++ \${CMAKE_CXX_FLAGS}
416 Executable \${CMAKE_EXE_LINKER_FLAGS}
417 Module \${CMAKE_MODULE_LINKER_FLAGS}
418 Shared \${CMAKE_SHARED_LINKER_FLAGS}\n")
422 # temporary function for moving cmake cleanups from from src_configure -> src_prepare.
424 _cmake_cleanup_cmake() {
425 : ${CMAKE_USE_DIR:=${S}}
427 if [[ "${CMAKE_REMOVE_MODULES}" == "yes" ]] ; then
429 for name in ${CMAKE_REMOVE_MODULES_LIST} ; do
430 find "${S}" -name ${name}.cmake -exec rm -v {} + || die
434 # check if CMakeLists.txt exist and if no then die
435 if [[ ! -e ${CMAKE_USE_DIR}/CMakeLists.txt ]] ; then
436 eerror "Unable to locate CMakeLists.txt under:"
437 eerror "\"${CMAKE_USE_DIR}/CMakeLists.txt\""
438 eerror "Consider not inheriting the cmake eclass."
439 die "FATAL: Unable to find CMakeLists.txt"
442 # Remove dangerous things.
443 _cmake_modify-cmakelists
446 # @FUNCTION: cmake-utils_src_prepare
448 # Apply ebuild and user patches.
449 cmake-utils_src_prepare() {
450 debug-print-function ${FUNCNAME} "$@"
452 pushd "${S}" > /dev/null || die
454 if [[ ${EAPI} != 5 ]]; then
458 debug-print "$FUNCNAME: PATCHES=$PATCHES"
459 [[ ${PATCHES[@]} ]] && epatch "${PATCHES[@]}"
461 debug-print "$FUNCNAME: applying user patches"
465 popd > /dev/null || die
467 # make ${S} read-only in order to detect broken build-systems
468 if [[ ${CMAKE_UTILS_QA_SRC_DIR_READONLY} && ! ${CMAKE_IN_SOURCE_BUILD} ]]; then
472 _CMAKE_UTILS_SRC_PREPARE_HAS_RUN=1
475 # @VARIABLE: mycmakeargs
478 # Optional cmake defines as a bash array. Should be defined before calling
482 # local mycmakeargs=(
483 # $(cmake-utils_use_with openconnect)
486 # cmake-utils_src_configure
490 # @FUNCTION: cmake-utils_src_configure
492 # General function for configuring with cmake. Default behaviour is to start an
493 # out-of-source build.
494 cmake-utils_src_configure() {
495 debug-print-function ${FUNCNAME} "$@"
497 if [[ ! ${_CMAKE_UTILS_SRC_PREPARE_HAS_RUN} ]]; then
498 eqawarn "cmake-utils_src_prepare has not been run, please open a bug on https://bugs.gentoo.org/"
501 [[ ${EAPI} == 5 ]] && _cmake_cleanup_cmake
503 _cmake_check_build_dir
505 # Fix xdg collision with sandbox
506 xdg_environment_reset
508 # @SEE CMAKE_BUILD_TYPE
509 if [[ ${CMAKE_BUILD_TYPE} = Gentoo ]]; then
510 # Handle release builds
511 if ! has debug ${IUSE//+} || ! use debug; then
512 local CPPFLAGS=${CPPFLAGS}
513 append-cppflags -DNDEBUG
517 # Prepare Gentoo override rules (set valid compiler, append CPPFLAGS etc.)
518 local build_rules=${BUILD_DIR}/gentoo_rules.cmake
520 cat > "${build_rules}" <<- _EOF_ || die
521 SET (CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "ASM compile command" FORCE)
522 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)
523 SET (CMAKE_ASM-ATT_LINK_FLAGS "-nostdlib" CACHE STRING "ASM-ATT link flags" FORCE)
524 SET (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C compile command" FORCE)
525 SET (CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C++ compile command" FORCE)
526 SET (CMAKE_Fortran_COMPILE_OBJECT "<CMAKE_Fortran_COMPILER> <DEFINES> <INCLUDES> ${FCFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "Fortran compile command" FORCE)
529 local myCC=$(tc-getCC) myCXX=$(tc-getCXX) myFC=$(tc-getFC)
531 # !!! IMPORTANT NOTE !!!
532 # Single slash below is intentional. CMake is weird and wants the
533 # CMAKE_*_VARIABLES split into two elements: the first one with
534 # compiler path, and the second one with all command-line options,
536 local toolchain_file=${BUILD_DIR}/gentoo_toolchain.cmake
537 cat > ${toolchain_file} <<- _EOF_ || die
538 SET (CMAKE_ASM_COMPILER "${myCC/ /;}")
539 SET (CMAKE_ASM-ATT_COMPILER "${myCC/ /;}")
540 SET (CMAKE_C_COMPILER "${myCC/ /;}")
541 SET (CMAKE_CXX_COMPILER "${myCXX/ /;}")
542 SET (CMAKE_Fortran_COMPILER "${myFC/ /;}")
543 SET (CMAKE_AR $(type -P $(tc-getAR)) CACHE FILEPATH "Archive manager" FORCE)
544 SET (CMAKE_RANLIB $(type -P $(tc-getRANLIB)) CACHE FILEPATH "Archive index generator" FORCE)
545 SET (CMAKE_SYSTEM_PROCESSOR "${CHOST%%-*}")
548 # We are using the C compiler for assembly by default.
549 local -x ASMFLAGS=${CFLAGS}
550 local -x PKG_CONFIG=$(tc-getPKG_CONFIG)
552 if tc-is-cross-compiler; then
554 case "${KERNEL:-linux}" in
555 Cygwin) sysname="CYGWIN_NT-5.1" ;;
556 HPUX) sysname="HP-UX" ;;
557 linux) sysname="Linux" ;;
560 cat >> "${toolchain_file}" <<- _EOF_ || die
561 SET (CMAKE_RC_COMPILER $(tc-getRC))
564 *) sysname="${KERNEL}" ;;
567 cat >> "${toolchain_file}" <<- _EOF_ || die
568 SET (CMAKE_SYSTEM_NAME "${sysname}")
571 if [ "${SYSROOT:-/}" != "/" ] ; then
572 # When cross-compiling with a sysroot (e.g. with crossdev's emerge wrappers)
573 # we need to tell cmake to use libs/headers from the sysroot but programs from / only.
574 cat >> "${toolchain_file}" <<- _EOF_ || die
575 SET (CMAKE_FIND_ROOT_PATH "${SYSROOT}")
576 SET (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
577 SET (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
578 SET (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
583 if [[ ${EPREFIX} ]]; then
584 cat >> "${build_rules}" <<- _EOF_ || die
585 # in Prefix we need rpath and must ensure cmake gets our default linker path
586 # right ... except for Darwin hosts
588 SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
589 SET (CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH "${EPREFIX}/usr/${CHOST}/lib/gcc;${EPREFIX}/usr/${CHOST}/lib;${EPREFIX}/usr/$(get_libdir);${EPREFIX}/$(get_libdir)"
590 CACHE STRING "" FORCE)
594 SET (CMAKE_PREFIX_PATH "${EPREFIX}/usr" CACHE STRING "" FORCE)
595 SET (CMAKE_MACOSX_RPATH ON CACHE BOOL "" FORCE)
596 SET (CMAKE_SKIP_BUILD_RPATH OFF CACHE BOOL "" FORCE)
597 SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
598 SET (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOL "" FORCE)
604 # Common configure parameters (invariants)
605 local common_config=${BUILD_DIR}/gentoo_common_config.cmake
606 local libdir=$(get_libdir)
607 cat > "${common_config}" <<- _EOF_ || die
608 SET (CMAKE_GENTOO_BUILD ON CACHE BOOL "Indicate Gentoo package build")
609 SET (LIB_SUFFIX ${libdir/lib} CACHE STRING "library path suffix" FORCE)
610 SET (CMAKE_INSTALL_LIBDIR ${libdir} CACHE PATH "Output directory for libraries")
611 SET (CMAKE_INSTALL_INFODIR "${EPREFIX}/usr/share/info" CACHE PATH "")
612 SET (CMAKE_INSTALL_MANDIR "${EPREFIX}/usr/share/man" CACHE PATH "")
614 [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo 'SET (CMAKE_COLOR_MAKEFILE OFF CACHE BOOL "pretty colors during make" FORCE)' >> "${common_config}"
616 if [[ ${EAPI} != [56] ]]; then
617 cat >> "${common_config}" <<- _EOF_ || die
618 SET (CMAKE_INSTALL_DOCDIR "${EPREFIX}/usr/share/doc/${PF}" CACHE PATH "")
622 # Wipe the default optimization flags out of CMake
623 if [[ ${CMAKE_BUILD_TYPE} != Gentoo && ${EAPI} != 5 ]]; then
624 cat >> ${common_config} <<- _EOF_ || die
625 SET (CMAKE_ASM_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
626 SET (CMAKE_ASM-ATT_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
627 SET (CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
628 SET (CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
629 SET (CMAKE_Fortran_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
630 SET (CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
631 SET (CMAKE_MODULE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
632 SET (CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
633 SET (CMAKE_STATIC_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
637 # Convert mycmakeargs to an array, for backwards compatibility
638 # Make the array a local variable since <=portage-2.1.6.x does not
639 # support global arrays (see bug #297255).
640 local mycmakeargstype=$(declare -p mycmakeargs 2>&-)
641 if [[ "${mycmakeargstype}" != "declare -a mycmakeargs="* ]]; then
642 if [[ -n "${mycmakeargstype}" ]] ; then
643 if [[ ${EAPI} == 5 ]]; then
644 eqawarn "Declaring mycmakeargs as a variable is deprecated. Please use an array instead."
646 die "Declaring mycmakeargs as a variable is banned in EAPI=${EAPI}. Please use an array instead."
649 local mycmakeargs_local=(${mycmakeargs})
651 local mycmakeargs_local=("${mycmakeargs[@]}")
654 if [[ ${CMAKE_WARN_UNUSED_CLI} == no ]] ; then
655 local warn_unused_cli="--no-warn-unused-cli"
657 local warn_unused_cli=""
660 # Common configure parameters (overridable)
661 # NOTE CMAKE_BUILD_TYPE can be only overridden via CMAKE_BUILD_TYPE eclass variable
662 # No -DCMAKE_BUILD_TYPE=xxx definitions will be in effect.
665 -C "${common_config}"
666 -G "$(_cmake_generator_to_use)"
667 -DCMAKE_INSTALL_PREFIX="${EPREFIX}/usr"
668 "${mycmakeargs_local[@]}"
669 -DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
670 $([[ ${EAPI} == 5 ]] && echo -DCMAKE_INSTALL_DO_STRIP=OFF)
671 -DCMAKE_USER_MAKE_RULES_OVERRIDE="${build_rules}"
672 -DCMAKE_TOOLCHAIN_FILE="${toolchain_file}"
676 if [[ -n "${CMAKE_EXTRA_CACHE_FILE}" ]] ; then
677 cmakeargs+=( -C "${CMAKE_EXTRA_CACHE_FILE}" )
680 pushd "${BUILD_DIR}" > /dev/null || die
681 debug-print "${LINENO} ${ECLASS} ${FUNCNAME}: mycmakeargs is ${mycmakeargs_local[*]}"
682 echo "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}"
683 "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}" || die "cmake failed"
684 popd > /dev/null || die
687 # @FUNCTION: cmake-utils_src_compile
689 # General function for compiling with cmake.
690 # Automatically detects the build type. All arguments are passed to emake.
691 cmake-utils_src_compile() {
692 debug-print-function ${FUNCNAME} "$@"
694 cmake-utils_src_make "$@"
697 # @FUNCTION: _cmake_ninja_src_make
700 # Build the package using ninja generator
701 _cmake_ninja_src_make() {
702 debug-print-function ${FUNCNAME} "$@"
704 [[ -e build.ninja ]] || die "build.ninja not found. Error during configure stage."
709 # @FUNCTION: _cmake_emake_src_make
712 # Build the package using make generator
713 _cmake_emake_src_make() {
714 debug-print-function ${FUNCNAME} "$@"
716 [[ -e Makefile ]] || die "Makefile not found. Error during configure stage."
718 if [[ "${CMAKE_VERBOSE}" != "OFF" ]]; then
719 emake VERBOSE=1 "$@" || die
726 # @FUNCTION: cmake-utils_src_make
728 # Function for building the package. Automatically detects the build type.
729 # All arguments are passed to emake.
730 cmake-utils_src_make() {
731 debug-print-function ${FUNCNAME} "$@"
733 _cmake_check_build_dir
734 pushd "${BUILD_DIR}" > /dev/null || die
736 _cmake_${CMAKE_MAKEFILE_GENERATOR}_src_make "$@"
738 popd > /dev/null || die
741 # @FUNCTION: cmake-utils_src_test
743 # Function for testing the package. Automatically detects the build type.
744 cmake-utils_src_test() {
745 debug-print-function ${FUNCNAME} "$@"
747 _cmake_check_build_dir
748 pushd "${BUILD_DIR}" > /dev/null || die
749 [[ -e CTestTestfile.cmake ]] || { echo "No tests found. Skipping."; return 0 ; }
751 [[ -n ${TEST_VERBOSE} ]] && myctestargs+=( --extra-verbose --output-on-failure )
753 set -- ctest -j "$(makeopts_jobs)" --test-load "$(makeopts_loadavg)" "${myctestargs[@]}" "$@"
756 einfo "Tests succeeded."
757 popd > /dev/null || die
760 if [[ -n "${CMAKE_YES_I_WANT_TO_SEE_THE_TEST_LOG}" ]] ; then
761 # on request from Diego
762 eerror "Tests failed. Test log ${BUILD_DIR}/Testing/Temporary/LastTest.log follows:"
763 eerror "--START TEST LOG--------------------------------------------------------------"
764 cat "${BUILD_DIR}/Testing/Temporary/LastTest.log"
765 eerror "--END TEST LOG----------------------------------------------------------------"
768 die "Tests failed. When you file a bug, please attach the following file: \n\t${BUILD_DIR}/Testing/Temporary/LastTest.log"
771 # die might not die due to nonfatal
772 popd > /dev/null || die
777 # @FUNCTION: cmake-utils_src_install
779 # Function for installing the package. Automatically detects the build type.
780 cmake-utils_src_install() {
781 debug-print-function ${FUNCNAME} "$@"
783 _cmake_check_build_dir
784 pushd "${BUILD_DIR}" > /dev/null || die
785 DESTDIR="${D}" ${CMAKE_MAKEFILE_GENERATOR} install "$@" || die "died running ${CMAKE_MAKEFILE_GENERATOR} install"
786 popd > /dev/null || die
788 pushd "${S}" > /dev/null || die
790 popd > /dev/null || die