1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: cmake-utils.eclass
9 # Tomáš Chvátal <scarabeus@gentoo.org>
10 # Maciej Mrozowski <reavertm@gentoo.org>
11 # (undisclosed contributors)
12 # Original author: Zephyrus (zephyrus@mirach.it)
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
49 # Set to enable in-source build.
51 # @ECLASS-VARIABLE: CMAKE_MAKEFILE_GENERATOR
53 # Specify a makefile generator to be used by cmake.
54 # At this point only "emake" and "ninja" are supported.
55 : ${CMAKE_MAKEFILE_GENERATOR:=emake}
57 # @ECLASS-VARIABLE: CMAKE_MIN_VERSION
59 # Specify the minimum required CMake version.
60 : ${CMAKE_MIN_VERSION:=2.8.12}
62 # @ECLASS-VARIABLE: CMAKE_REMOVE_MODULES
64 # Do we want to remove anything? yes or whatever else for no
65 : ${CMAKE_REMOVE_MODULES:=yes}
67 # @ECLASS-VARIABLE: CMAKE_REMOVE_MODULES_LIST
69 # Space-separated list of CMake modules that will be removed in $S during src_prepare,
70 # in order to force packages to use the system version.
71 : ${CMAKE_REMOVE_MODULES_LIST:=FindBLAS FindLAPACK}
73 # @ECLASS-VARIABLE: CMAKE_USE_DIR
75 # Sets the directory where we are working with cmake.
76 # For example when application uses autotools and only one
77 # plugin needs to be done by cmake.
78 # By default it uses ${S}.
80 # @ECLASS-VARIABLE: CMAKE_VERBOSE
82 # Set to OFF to disable verbose messages during compilation
83 : ${CMAKE_VERBOSE:=ON}
85 # @ECLASS-VARIABLE: CMAKE_WARN_UNUSED_CLI
87 # Warn about variables that are declared on the command line
88 # but not used. Might give false-positives.
89 # "no" to disable (default) or anything else to enable.
90 : ${CMAKE_WARN_UNUSED_CLI:=no}
92 # @ECLASS-VARIABLE: PREFIX
94 # Eclass respects PREFIX variable, though it's not recommended way to set
95 # install/lib/bin prefixes.
96 # Use -DCMAKE_INSTALL_PREFIX=... CMake variable instead.
99 # @ECLASS-VARIABLE: WANT_CMAKE
101 # Specify if cmake-utils eclass should depend on cmake optionally or not.
102 # This is useful when only part of application is using cmake build system.
103 # Valid values are: always [default], optional (where the value is the useflag
104 # used for optionality)
106 # This is banned in EAPI 6 and later.
107 : ${WANT_CMAKE:=always}
109 # @ECLASS-VARIABLE: CMAKE_EXTRA_CACHE_FILE
111 # Specifies an extra cache file to pass to cmake. This is the analog of EXTRA_ECONF
112 # for econf and is needed to pass TRY_RUN results when cross-compiling.
113 # Should be set by user in a per-package basis in /etc/portage/package.env.
117 *) die "EAPI=${EAPI:-0} is not supported" ;;
120 inherit toolchain-funcs multilib flag-o-matic eutils versionator
122 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
125 case ${WANT_CMAKE} in
129 [[ ${EAPI} == [2345] ]] || die "WANT_CMAKE is banned in EAPI 6 and later"
130 IUSE+=" ${WANT_CMAKE}"
131 CMAKEDEPEND+="${WANT_CMAKE}? ( "
135 case ${CMAKE_MAKEFILE_GENERATOR} in
137 CMAKEDEPEND+=" sys-devel/make"
140 CMAKEDEPEND+=" dev-util/ninja"
143 eerror "Unknown value for \${CMAKE_MAKEFILE_GENERATOR}"
144 die "Value ${CMAKE_MAKEFILE_GENERATOR} is not supported"
148 if [[ ${PN} != cmake ]]; then
149 CMAKEDEPEND+=" >=dev-util/cmake-${CMAKE_MIN_VERSION}"
152 [[ ${WANT_CMAKE} = always ]] || CMAKEDEPEND+=" )"
154 DEPEND="${CMAKEDEPEND}"
157 # Internal functions used by cmake-utils_use_*
158 _cmake_use_me_now() {
159 debug-print-function ${FUNCNAME} "$@"
162 [[ ! -z $3 ]] && arg=$3
164 [[ ${EAPI} == [2345] ]] || die "${FUNCNAME[1]} is banned in EAPI 6 and later: use -D$1${arg}=\"\$(usex $2)\" instead"
166 local uper capitalised x
167 [[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
168 if [[ ! -z $3 ]]; then
169 # user specified the use name so use it
170 echo "-D$1$3=$(use $2 && echo ON || echo OFF)"
172 # use all various most used combinations
173 uper=$(echo ${2} | tr '[:lower:]' '[:upper:]')
174 capitalised=$(echo ${2} | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g')
175 for x in $2 $uper $capitalised; do
176 echo "-D$1$x=$(use $2 && echo ON || echo OFF) "
180 _cmake_use_me_now_inverted() {
181 debug-print-function ${FUNCNAME} "$@"
184 [[ ! -z $3 ]] && arg=$3
186 if [[ ${EAPI} != [2345] && "${FUNCNAME[1]}" != cmake-utils_use_find_package ]] ; then
187 die "${FUNCNAME[1]} is banned in EAPI 6 and later: use -D$1${arg}=\"\$(usex $2)\" insteadss"
190 local uper capitalised x
191 [[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
192 if [[ ! -z $3 ]]; then
193 # user specified the use name so use it
194 echo "-D$1$3=$(use $2 && echo OFF || echo ON)"
196 # use all various most used combinations
197 uper=$(echo ${2} | tr '[:lower:]' '[:upper:]')
198 capitalised=$(echo ${2} | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g')
199 for x in $2 $uper $capitalised; do
200 echo "-D$1$x=$(use $2 && echo OFF || echo ON) "
205 # Determine using IN or OUT source build
206 _cmake_check_build_dir() {
207 : ${CMAKE_USE_DIR:=${S}}
208 if [[ -n ${CMAKE_IN_SOURCE_BUILD} ]]; then
209 # we build in source dir
210 BUILD_DIR="${CMAKE_USE_DIR}"
212 # Respect both the old variable and the new one, depending
213 # on which one was set by the ebuild.
214 if [[ ! ${BUILD_DIR} && ${CMAKE_BUILD_DIR} ]]; then
215 eqawarn "The CMAKE_BUILD_DIR variable has been renamed to BUILD_DIR."
216 eqawarn "Please migrate the ebuild to use the new one."
218 # In the next call, both variables will be set already
219 # and we'd have to know which one takes precedence.
220 _RESPECT_CMAKE_BUILD_DIR=1
223 if [[ ${_RESPECT_CMAKE_BUILD_DIR} ]]; then
224 BUILD_DIR=${CMAKE_BUILD_DIR:-${WORKDIR}/${P}_build}
226 : ${BUILD_DIR:=${WORKDIR}/${P}_build}
230 # Backwards compatibility for getting the value.
231 CMAKE_BUILD_DIR=${BUILD_DIR}
233 mkdir -p "${BUILD_DIR}" || die
234 echo ">>> Working in BUILD_DIR: \"$BUILD_DIR\""
237 # Determine which generator to use
238 _cmake_generator_to_use() {
241 case ${CMAKE_MAKEFILE_GENERATOR} in
243 # if ninja is enabled but not installed, the build could fail
244 # this could happen if ninja is manually enabled (eg. make.conf) but not installed
245 if ! has_version dev-util/ninja; then
246 die "CMAKE_MAKEFILE_GENERATOR is set to ninja, but ninja is not installed. Please install dev-util/ninja or unset CMAKE_MAKEFILE_GENERATOR."
248 generator_name="Ninja"
251 generator_name="Unix Makefiles"
254 eerror "Unknown value for \${CMAKE_MAKEFILE_GENERATOR}"
255 die "Value ${CMAKE_MAKEFILE_GENERATOR} is not supported"
259 echo ${generator_name}
262 # @FUNCTION: cmake_comment_add_subdirectory
263 # @USAGE: <subdirectory>
265 # Comment out an add_subdirectory call in CMakeLists.txt in the current directory
266 cmake_comment_add_subdirectory() {
267 if [[ -z ${1} ]]; then
268 die "comment_add_subdirectory must be passed the directory name to comment"
271 if [[ -e "CMakeLists.txt" ]]; then
272 sed -e "/add_subdirectory[[:space:]]*([[:space:]]*${1//\//\\/}[[:space:]]*)/I s/^/#DONOTCOMPILE /" \
273 -i CMakeLists.txt || die "failed to comment add_subdirectory(${1})"
277 # @FUNCTION: comment_add_subdirectory
278 # @USAGE: <subdirectory>
280 # Comment out an add_subdirectory call in CMakeLists.txt in the current directory
281 # Banned in EAPI 6 and later - use cmake_comment_add_subdirectory instead.
282 comment_add_subdirectory() {
283 [[ ${EAPI} == [2345] ]] || die "comment_add_subdirectory is banned in EAPI 6 and later - use cmake_comment_add_subdirectory instead"
285 cmake_comment_add_subdirectory "$@"
288 # @FUNCTION: cmake-utils_use_with
289 # @USAGE: <USE flag> [flag name]
291 # Based on use_with. See ebuild(5).
293 # `cmake-utils_use_with foo FOO` echoes -DWITH_FOO=ON if foo is enabled
294 # and -DWITH_FOO=OFF if it is disabled.
295 cmake-utils_use_with() { _cmake_use_me_now WITH_ "$@" ; }
297 # @FUNCTION: cmake-utils_use_enable
298 # @USAGE: <USE flag> [flag name]
300 # Based on use_enable. See ebuild(5).
302 # `cmake-utils_use_enable foo FOO` echoes -DENABLE_FOO=ON if foo is enabled
303 # and -DENABLE_FOO=OFF if it is disabled.
304 cmake-utils_use_enable() { _cmake_use_me_now ENABLE_ "$@" ; }
306 # @FUNCTION: cmake-utils_use_find_package
307 # @USAGE: <USE flag> <package name>
309 # Based on use_enable. See ebuild(5).
311 # `cmake-utils_use_find_package foo LibFoo` echoes -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=OFF
312 # if foo is enabled and -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=ON if it is disabled.
313 # This can be used to make find_package optional.
314 cmake-utils_use_find_package() {
315 if [[ ${EAPI} != [2345] && "$#" != 2 ]] ; then
316 die "Usage: cmake-utils_use_find_package <USE flag> <package name>"
319 _cmake_use_me_now_inverted CMAKE_DISABLE_FIND_PACKAGE_ "$@" ;
322 # @FUNCTION: cmake-utils_use_disable
323 # @USAGE: <USE flag> [flag name]
325 # Based on inversion of use_enable. See ebuild(5).
327 # `cmake-utils_use_enable foo FOO` echoes -DDISABLE_FOO=OFF if foo is enabled
328 # and -DDISABLE_FOO=ON if it is disabled.
329 cmake-utils_use_disable() { _cmake_use_me_now_inverted DISABLE_ "$@" ; }
331 # @FUNCTION: cmake-utils_use_no
332 # @USAGE: <USE flag> [flag name]
334 # Based on use_disable. See ebuild(5).
336 # `cmake-utils_use_no foo FOO` echoes -DNO_FOO=OFF if foo is enabled
337 # and -DNO_FOO=ON if it is disabled.
338 cmake-utils_use_no() { _cmake_use_me_now_inverted NO_ "$@" ; }
340 # @FUNCTION: cmake-utils_use_want
341 # @USAGE: <USE flag> [flag name]
343 # Based on use_enable. See ebuild(5).
345 # `cmake-utils_use_want foo FOO` echoes -DWANT_FOO=ON if foo is enabled
346 # and -DWANT_FOO=OFF if it is disabled.
347 cmake-utils_use_want() { _cmake_use_me_now WANT_ "$@" ; }
349 # @FUNCTION: cmake-utils_use_build
350 # @USAGE: <USE flag> [flag name]
352 # Based on use_enable. See ebuild(5).
354 # `cmake-utils_use_build foo FOO` echoes -DBUILD_FOO=ON if foo is enabled
355 # and -DBUILD_FOO=OFF if it is disabled.
356 cmake-utils_use_build() { _cmake_use_me_now BUILD_ "$@" ; }
358 # @FUNCTION: cmake-utils_use_has
359 # @USAGE: <USE flag> [flag name]
361 # Based on use_enable. See ebuild(5).
363 # `cmake-utils_use_has foo FOO` echoes -DHAVE_FOO=ON if foo is enabled
364 # and -DHAVE_FOO=OFF if it is disabled.
365 cmake-utils_use_has() { _cmake_use_me_now HAVE_ "$@" ; }
367 # @FUNCTION: cmake-utils_use_use
368 # @USAGE: <USE flag> [flag name]
370 # Based on use_enable. See ebuild(5).
372 # `cmake-utils_use_use foo FOO` echoes -DUSE_FOO=ON if foo is enabled
373 # and -DUSE_FOO=OFF if it is disabled.
374 cmake-utils_use_use() { _cmake_use_me_now USE_ "$@" ; }
376 # @FUNCTION: cmake-utils_use
377 # @USAGE: <USE flag> [flag name]
379 # Based on use_enable. See ebuild(5).
381 # `cmake-utils_use foo FOO` echoes -DFOO=ON if foo is enabled
382 # and -DFOO=OFF if it is disabled.
383 cmake-utils_use() { _cmake_use_me_now "" "$@" ; }
385 # @FUNCTION: cmake-utils_useno
386 # @USAGE: <USE flag> [flag name]
388 # Based on use_enable. See ebuild(5).
390 # `cmake-utils_useno foo NOFOO` echoes -DNOFOO=OFF if foo is enabled
391 # and -DNOFOO=ON if it is disabled.
392 cmake-utils_useno() { _cmake_use_me_now_inverted "" "$@" ; }
394 # Internal function for modifying hardcoded definitions.
395 # Removes dangerous definitions that override Gentoo settings.
396 _cmake_modify-cmakelists() {
397 debug-print-function ${FUNCNAME} "$@"
399 # Only edit the files once
400 grep -qs "<<< Gentoo configuration >>>" "${CMAKE_USE_DIR}"/CMakeLists.txt && return 0
402 # Comment out all set (<some_should_be_user_defined_variable> value)
403 # TODO Add QA checker - inform when variable being checked for below is set in CMakeLists.txt
404 find "${CMAKE_USE_DIR}" -name CMakeLists.txt \
405 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_BUILD_TYPE.*)/{s/^/#IGNORE /g}' {} + \
406 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_COLOR_MAKEFILE.*)/{s/^/#IGNORE /g}' {} + \
407 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_INSTALL_PREFIX.*)/{s/^/#IGNORE /g}' {} + \
408 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_VERBOSE_MAKEFILE.*)/{s/^/#IGNORE /g}' {} + \
409 || die "${LINENO}: failed to disable hardcoded settings"
411 # NOTE Append some useful summary here
412 cat >> "${CMAKE_USE_DIR}"/CMakeLists.txt <<- _EOF_ || die
414 MESSAGE(STATUS "<<< Gentoo configuration >>>
415 Build type \${CMAKE_BUILD_TYPE}
416 Install path \${CMAKE_INSTALL_PREFIX}
419 C++ \${CMAKE_CXX_FLAGS}
421 Executable \${CMAKE_EXE_LINKER_FLAGS}
422 Module \${CMAKE_MODULE_LINKER_FLAGS}
423 Shared \${CMAKE_SHARED_LINKER_FLAGS}\n")
427 # temporary function for moving cmake cleanups from from src_configure -> src_prepare.
429 _cmake_cleanup_cmake() {
430 : ${CMAKE_USE_DIR:=${S}}
432 if [[ "${CMAKE_REMOVE_MODULES}" == "yes" ]] ; then
434 for name in ${CMAKE_REMOVE_MODULES_LIST} ; do
435 find "${S}" -name ${name}.cmake -exec rm -v {} + || die
439 # check if CMakeLists.txt exist and if no then die
440 if [[ ! -e ${CMAKE_USE_DIR}/CMakeLists.txt ]] ; then
441 eerror "Unable to locate CMakeLists.txt under:"
442 eerror "\"${CMAKE_USE_DIR}/CMakeLists.txt\""
443 eerror "Consider not inheriting the cmake eclass."
444 die "FATAL: Unable to find CMakeLists.txt"
447 # Remove dangerous things.
448 _cmake_modify-cmakelists
451 enable_cmake-utils_src_prepare() {
452 debug-print-function ${FUNCNAME} "$@"
454 pushd "${S}" > /dev/null || die
456 if [[ ${EAPI} != [2345] ]]; then
460 debug-print "$FUNCNAME: PATCHES=$PATCHES"
461 [[ ${PATCHES[@]} ]] && epatch "${PATCHES[@]}"
463 debug-print "$FUNCNAME: applying user patches"
467 popd > /dev/null || die
470 # @VARIABLE: mycmakeargs
473 # Optional cmake defines as a bash array. Should be defined before calling
477 # local mycmakeargs=(
478 # $(cmake-utils_use_with openconnect)
481 # cmake-utils_src_configure
485 enable_cmake-utils_src_configure() {
486 debug-print-function ${FUNCNAME} "$@"
488 [[ ${EAPI} == [2345] ]] && _cmake_cleanup_cmake
490 _cmake_check_build_dir
492 # Fix xdg collision with sandbox
493 local -x XDG_CONFIG_HOME="${T}"
495 # @SEE CMAKE_BUILD_TYPE
496 if [[ ${CMAKE_BUILD_TYPE} = Gentoo ]]; then
497 # Handle release builds
498 if ! has debug ${IUSE//+} || ! use debug; then
499 local CPPFLAGS=${CPPFLAGS}
500 append-cppflags -DNDEBUG
504 # Prepare Gentoo override rules (set valid compiler, append CPPFLAGS etc.)
505 local build_rules=${BUILD_DIR}/gentoo_rules.cmake
506 # Since cmake-3.4.0_rc1 "<FLAGS>" no longer contains includes and thus
507 # we need to add "<INCLUDES>"
509 if [[ ${PN} == cmake ]] ; then
510 if $(version_is_at_least 3.4.0 $(get_version_component_range 1-3 ${PV})) ; then
511 includes="<INCLUDES>"
513 elif ROOT=/ has_version \>=dev-util/cmake-3.4.0_rc1 ; then
514 includes="<INCLUDES>"
516 cat > "${build_rules}" <<- _EOF_ || die
517 SET (CMAKE_AR $(type -P $(tc-getAR)) CACHE FILEPATH "Archive manager" FORCE)
518 SET (CMAKE_ASM_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> ${includes} ${CFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "ASM compile command" FORCE)
519 SET (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> ${includes} ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C compile command" FORCE)
520 SET (CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> ${includes} ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C++ compile command" FORCE)
521 SET (CMAKE_Fortran_COMPILE_OBJECT "<CMAKE_Fortran_COMPILER> <DEFINES> ${includes} ${FCFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "Fortran compile command" FORCE)
522 SET (CMAKE_RANLIB $(type -P $(tc-getRANLIB)) CACHE FILEPATH "Archive index generator" FORCE)
523 SET (PKG_CONFIG_EXECUTABLE $(type -P $(tc-getPKG_CONFIG)) CACHE FILEPATH "pkg-config executable" FORCE)
526 local toolchain_file=${BUILD_DIR}/gentoo_toolchain.cmake
527 cat > ${toolchain_file} <<- _EOF_ || die
528 SET (CMAKE_C_COMPILER $(tc-getCC))
529 SET (CMAKE_CXX_COMPILER $(tc-getCXX))
530 SET (CMAKE_Fortran_COMPILER $(tc-getFC))
533 if tc-is-cross-compiler; then
535 case "${KERNEL:-linux}" in
536 Cygwin) sysname="CYGWIN_NT-5.1" ;;
537 HPUX) sysname="HP-UX" ;;
538 linux) sysname="Linux" ;;
541 cat >> "${toolchain_file}" <<- _EOF_ || die
542 SET (CMAKE_RC_COMPILER $(tc-getRC))
545 *) sysname="${KERNEL}" ;;
548 cat >> "${toolchain_file}" <<- _EOF_ || die
549 SET (CMAKE_SYSTEM_NAME "${sysname}")
552 if [ "${SYSROOT:-/}" != "/" ] ; then
553 # When cross-compiling with a sysroot (e.g. with crossdev's emerge wrappers)
554 # we need to tell cmake to use libs/headers from the sysroot but programs from / only.
555 cat >> "${toolchain_file}" <<- _EOF_ || die
556 set(CMAKE_FIND_ROOT_PATH "${SYSROOT}")
557 set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
558 set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
559 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
564 [[ ${EAPI} == 2 ]] && ! use prefix && local EPREFIX=
566 if [[ ${EPREFIX} ]]; then
567 cat >> "${build_rules}" <<- _EOF_ || die
568 # in Prefix we need rpath and must ensure cmake gets our default linker path
569 # right ... except for Darwin hosts
571 SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
572 SET (CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH "${EPREFIX}/usr/${CHOST}/lib/gcc;${EPREFIX}/usr/${CHOST}/lib;${EPREFIX}/usr/$(get_libdir);${EPREFIX}/$(get_libdir)"
573 CACHE STRING "" FORCE)
577 SET(CMAKE_PREFIX_PATH "${EPREFIX}${PREFIX}" CACHE STRING "" FORCE)
578 SET(CMAKE_SKIP_BUILD_RPATH OFF CACHE BOOL "" FORCE)
579 SET(CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
580 SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE CACHE BOOL "")
581 SET(CMAKE_INSTALL_RPATH "${EPREFIX}${PREFIX}/lib;${EPREFIX}/usr/${CHOST}/lib/gcc;${EPREFIX}/usr/${CHOST}/lib;${EPREFIX}/usr/$(get_libdir);${EPREFIX}/$(get_libdir)" CACHE STRING "" FORCE)
582 SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOL "" FORCE)
583 SET(CMAKE_INSTALL_NAME_DIR "${EPREFIX}${PREFIX}/lib" CACHE STRING "" FORCE)
589 # Common configure parameters (invariants)
590 local common_config=${BUILD_DIR}/gentoo_common_config.cmake
591 local libdir=$(get_libdir)
592 cat > "${common_config}" <<- _EOF_ || die
593 SET (LIB_SUFFIX ${libdir/lib} CACHE STRING "library path suffix" FORCE)
594 SET (CMAKE_INSTALL_LIBDIR ${libdir} CACHE PATH "Output directory for libraries")
596 [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo 'SET (CMAKE_COLOR_MAKEFILE OFF CACHE BOOL "pretty colors during make" FORCE)' >> "${common_config}"
598 # Convert mycmakeargs to an array, for backwards compatibility
599 # Make the array a local variable since <=portage-2.1.6.x does not
600 # support global arrays (see bug #297255).
601 local mycmakeargstype=$(declare -p mycmakeargs 2>&-)
602 if [[ "${mycmakeargstype}" != "declare -a mycmakeargs="* ]]; then
603 if [[ -n "${mycmakeargstype}" ]] ; then
604 if [[ ${EAPI} == [2345] ]]; then
605 eqawarn "Declaring mycmakeargs as a variable is deprecated. Please use an array instead."
607 die "Declaring mycmakeargs as a variable is banned in EAPI=${EAPI}. Please use an array instead."
610 local mycmakeargs_local=(${mycmakeargs})
612 local mycmakeargs_local=("${mycmakeargs[@]}")
615 if [[ ${CMAKE_WARN_UNUSED_CLI} == no ]] ; then
616 local warn_unused_cli="--no-warn-unused-cli"
618 local warn_unused_cli=""
621 # Common configure parameters (overridable)
622 # NOTE CMAKE_BUILD_TYPE can be only overriden via CMAKE_BUILD_TYPE eclass variable
623 # No -DCMAKE_BUILD_TYPE=xxx definitions will be in effect.
626 -C "${common_config}"
627 -G "$(_cmake_generator_to_use)"
628 -DCMAKE_INSTALL_PREFIX="${EPREFIX}${PREFIX}"
629 "${mycmakeargs_local[@]}"
630 -DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
631 -DCMAKE_INSTALL_DO_STRIP=OFF
632 -DCMAKE_USER_MAKE_RULES_OVERRIDE="${build_rules}"
633 -DCMAKE_TOOLCHAIN_FILE="${toolchain_file}"
637 if [[ -n "${CMAKE_EXTRA_CACHE_FILE}" ]] ; then
638 cmakeargs+=( -C "${CMAKE_EXTRA_CACHE_FILE}" )
641 pushd "${BUILD_DIR}" > /dev/null || die
642 debug-print "${LINENO} ${ECLASS} ${FUNCNAME}: mycmakeargs is ${mycmakeargs_local[*]}"
643 echo "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}"
644 "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}" || die "cmake failed"
645 popd > /dev/null || die
648 enable_cmake-utils_src_compile() {
649 debug-print-function ${FUNCNAME} "$@"
651 cmake-utils_src_make "$@"
654 _ninjaopts_from_makeopts() {
655 if [[ ${NINJAOPTS+set} == set ]]; then
673 export NINJAOPTS="${ninjaopts[*]}"
676 # @FUNCTION: _cmake_ninja_src_make
679 # Build the package using ninja generator
680 _cmake_ninja_src_make() {
681 debug-print-function ${FUNCNAME} "$@"
683 [[ -e build.ninja ]] || die "build.ninja not found. Error during configure stage."
685 _ninjaopts_from_makeopts
687 if [[ "${CMAKE_VERBOSE}" != "OFF" ]]; then
688 set -- ninja ${NINJAOPTS} -v "$@"
690 set -- ninja ${NINJAOPTS} "$@"
697 # @FUNCTION: _cmake_emake_src_make
700 # Build the package using make generator
701 _cmake_emake_src_make() {
702 debug-print-function ${FUNCNAME} "$@"
704 [[ -e Makefile ]] || die "Makefile not found. Error during configure stage."
706 if [[ "${CMAKE_VERBOSE}" != "OFF" ]]; then
707 emake VERBOSE=1 "$@" || die
714 # @FUNCTION: cmake-utils_src_make
716 # Function for building the package. Automatically detects the build type.
717 # All arguments are passed to emake.
718 cmake-utils_src_make() {
719 debug-print-function ${FUNCNAME} "$@"
721 _cmake_check_build_dir
722 pushd "${BUILD_DIR}" > /dev/null || die
724 _cmake_${CMAKE_MAKEFILE_GENERATOR}_src_make "$@"
726 popd > /dev/null || die
729 enable_cmake-utils_src_test() {
730 debug-print-function ${FUNCNAME} "$@"
732 _cmake_check_build_dir
733 pushd "${BUILD_DIR}" > /dev/null || die
734 [[ -e CTestTestfile.cmake ]] || { echo "No tests found. Skipping."; return 0 ; }
736 [[ -n ${TEST_VERBOSE} ]] && myctestargs+=( --extra-verbose --output-on-failure )
738 if ctest "${myctestargs[@]}" "$@" ; then
739 einfo "Tests succeeded."
740 popd > /dev/null || die
743 if [[ -n "${CMAKE_YES_I_WANT_TO_SEE_THE_TEST_LOG}" ]] ; then
744 # on request from Diego
745 eerror "Tests failed. Test log ${BUILD_DIR}/Testing/Temporary/LastTest.log follows:"
746 eerror "--START TEST LOG--------------------------------------------------------------"
747 cat "${BUILD_DIR}/Testing/Temporary/LastTest.log"
748 eerror "--END TEST LOG----------------------------------------------------------------"
751 die "Tests failed. When you file a bug, please attach the following file: \n\t${BUILD_DIR}/Testing/Temporary/LastTest.log"
754 # die might not die due to nonfatal
755 popd > /dev/null || die
760 enable_cmake-utils_src_install() {
761 debug-print-function ${FUNCNAME} "$@"
763 _cmake_check_build_dir
764 pushd "${BUILD_DIR}" > /dev/null || die
765 DESTDIR="${D}" ${CMAKE_MAKEFILE_GENERATOR} install "$@" || die "died running ${CMAKE_MAKEFILE_GENERATOR} install"
766 popd > /dev/null || die
768 pushd "${S}" > /dev/null || die
770 popd > /dev/null || die
773 # @FUNCTION: cmake-utils_src_prepare
775 # Apply ebuild and user patches.
776 cmake-utils_src_prepare() {
777 _cmake_execute_optionally "src_prepare" "$@"
780 # @FUNCTION: cmake-utils_src_configure
782 # General function for configuring with cmake. Default behaviour is to start an
783 # out-of-source build.
784 cmake-utils_src_configure() {
785 _cmake_execute_optionally "src_configure" "$@"
788 # @FUNCTION: cmake-utils_src_compile
790 # General function for compiling with cmake.
791 # Automatically detects the build type. All arguments are passed to emake.
792 cmake-utils_src_compile() {
793 _cmake_execute_optionally "src_compile" "$@"
796 # @FUNCTION: cmake-utils_src_test
798 # Function for testing the package. Automatically detects the build type.
799 cmake-utils_src_test() {
800 _cmake_execute_optionally "src_test" "$@"
803 # @FUNCTION: cmake-utils_src_install
805 # Function for installing the package. Automatically detects the build type.
806 cmake-utils_src_install() {
807 _cmake_execute_optionally "src_install" "$@"
810 # Optionally executes phases based on WANT_CMAKE variable/USE flag.
811 _cmake_execute_optionally() {
812 local phase="$1" ; shift
813 if [[ ${WANT_CMAKE} = always ]]; then
814 enable_cmake-utils_${phase} "$@"
816 use ${WANT_CMAKE} && enable_cmake-utils_${phase} "$@"