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
24 # @ECLASS-VARIABLE: BUILD_DIR
26 # Build directory where all cmake processed files should be generated.
27 # For in-source build it's fixed to ${CMAKE_USE_DIR}.
28 # For out-of-source build it can be overridden, by default it uses
29 # ${WORKDIR}/${P}_build.
31 # This variable has been called CMAKE_BUILD_DIR formerly.
32 # It is set under that name for compatibility.
34 # @ECLASS-VARIABLE: CMAKE_BINARY
36 # Eclass can use different cmake binary than the one provided in by system.
37 : ${CMAKE_BINARY:=cmake}
39 # @ECLASS-VARIABLE: CMAKE_BUILD_TYPE
41 # Set to override default CMAKE_BUILD_TYPE. Only useful for packages
42 # known to make use of "if (CMAKE_BUILD_TYPE MATCHES xxx)".
43 # If about to be set - needs to be set before invoking cmake-utils_src_configure.
44 # You usualy do *NOT* want nor need to set it as it pulls CMake default build-type
45 # specific compiler flags overriding make.conf.
46 : ${CMAKE_BUILD_TYPE:=Gentoo}
48 # @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:=2.8.12}
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.
91 : ${CMAKE_WARN_UNUSED_CLI:=no}
93 # @ECLASS-VARIABLE: PREFIX
95 # Eclass respects PREFIX variable, though it's not recommended way to set
96 # install/lib/bin prefixes.
97 # Use -DCMAKE_INSTALL_PREFIX=... CMake variable instead.
100 # @ECLASS-VARIABLE: WANT_CMAKE
102 # Specify if cmake-utils eclass should depend on cmake optionally or not.
103 # This is useful when only part of application is using cmake build system.
104 # Valid values are: always [default], optional (where the value is the useflag
105 # used for optionality)
107 # This is banned in EAPI 6 and later.
108 : ${WANT_CMAKE:=always}
110 # @ECLASS-VARIABLE: CMAKE_EXTRA_CACHE_FILE
112 # Specifies an extra cache file to pass to cmake. This is the analog of EXTRA_ECONF
113 # for econf and is needed to pass TRY_RUN results when cross-compiling.
114 # Should be set by user in a per-package basis in /etc/portage/package.env.
118 *) die "EAPI=${EAPI:-0} is not supported" ;;
121 inherit toolchain-funcs multilib flag-o-matic eutils versionator
123 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
126 case ${WANT_CMAKE} in
130 [[ ${EAPI} == [2345] ]] || die "WANT_CMAKE is banned in EAPI 6 and later"
131 IUSE+=" ${WANT_CMAKE}"
132 CMAKEDEPEND+="${WANT_CMAKE}? ( "
136 case ${CMAKE_MAKEFILE_GENERATOR} in
138 CMAKEDEPEND+=" sys-devel/make"
141 CMAKEDEPEND+=" dev-util/ninja"
144 eerror "Unknown value for \${CMAKE_MAKEFILE_GENERATOR}"
145 die "Value ${CMAKE_MAKEFILE_GENERATOR} is not supported"
149 if [[ ${PN} != cmake ]]; then
150 CMAKEDEPEND+=" >=dev-util/cmake-${CMAKE_MIN_VERSION}"
153 CMAKEDEPEND+=" userland_GNU? ( >=sys-apps/findutils-4.4.0 )"
155 [[ ${WANT_CMAKE} = always ]] || CMAKEDEPEND+=" )"
157 DEPEND="${CMAKEDEPEND}"
160 # Internal functions used by cmake-utils_use_*
161 _cmake_use_me_now() {
162 debug-print-function ${FUNCNAME} "$@"
165 [[ ! -z $3 ]] && arg=$3
167 [[ ${EAPI} == [2345] ]] || die "${FUNCNAME[1]} is banned in EAPI 6 and later: use -D$1${arg}=\"\$(usex $2)\" instead"
169 local uper capitalised x
170 [[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
171 if [[ ! -z $3 ]]; then
172 # user specified the use name so use it
173 echo "-D$1$3=$(use $2 && echo ON || echo OFF)"
175 # use all various most used combinations
176 uper=$(echo ${2} | tr '[:lower:]' '[:upper:]')
177 capitalised=$(echo ${2} | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g')
178 for x in $2 $uper $capitalised; do
179 echo "-D$1$x=$(use $2 && echo ON || echo OFF) "
183 _cmake_use_me_now_inverted() {
184 debug-print-function ${FUNCNAME} "$@"
187 [[ ! -z $3 ]] && arg=$3
189 if [[ ${EAPI} != [2345] ]] && [[ "${FUNCNAME[1]}" != cmake-utils_use_find_package ]] ; then
190 die "${FUNCNAME[1]} is banned in EAPI 6 and later: use -D$1${arg}=\"\$(usex $2)\" insteadss"
193 local uper capitalised x
194 [[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
195 if [[ ! -z $3 ]]; then
196 # user specified the use name so use it
197 echo "-D$1$3=$(use $2 && echo OFF || echo ON)"
199 # use all various most used combinations
200 uper=$(echo ${2} | tr '[:lower:]' '[:upper:]')
201 capitalised=$(echo ${2} | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g')
202 for x in $2 $uper $capitalised; do
203 echo "-D$1$x=$(use $2 && echo OFF || echo ON) "
208 # Determine using IN or OUT source build
209 _cmake_check_build_dir() {
210 : ${CMAKE_USE_DIR:=${S}}
211 if [[ -n ${CMAKE_IN_SOURCE_BUILD} ]]; then
212 # we build in source dir
213 BUILD_DIR="${CMAKE_USE_DIR}"
215 # Respect both the old variable and the new one, depending
216 # on which one was set by the ebuild.
217 if [[ ! ${BUILD_DIR} && ${CMAKE_BUILD_DIR} ]]; then
218 eqawarn "The CMAKE_BUILD_DIR variable has been renamed to BUILD_DIR."
219 eqawarn "Please migrate the ebuild to use the new one."
221 # In the next call, both variables will be set already
222 # and we'd have to know which one takes precedence.
223 _RESPECT_CMAKE_BUILD_DIR=1
226 if [[ ${_RESPECT_CMAKE_BUILD_DIR} ]]; then
227 BUILD_DIR=${CMAKE_BUILD_DIR:-${WORKDIR}/${P}_build}
229 : ${BUILD_DIR:=${WORKDIR}/${P}_build}
233 # Backwards compatibility for getting the value.
234 CMAKE_BUILD_DIR=${BUILD_DIR}
236 mkdir -p "${BUILD_DIR}" || die
237 echo ">>> Working in BUILD_DIR: \"$BUILD_DIR\""
240 # Determine which generator to use
241 _cmake_generator_to_use() {
244 case ${CMAKE_MAKEFILE_GENERATOR} in
246 # if ninja is enabled but not installed, the build could fail
247 # this could happen if ninja is manually enabled (eg. make.conf) but not installed
248 if ! has_version dev-util/ninja; then
249 die "CMAKE_MAKEFILE_GENERATOR is set to ninja, but ninja is not installed. Please install dev-util/ninja or unset CMAKE_MAKEFILE_GENERATOR."
251 generator_name="Ninja"
254 generator_name="Unix Makefiles"
257 eerror "Unknown value for \${CMAKE_MAKEFILE_GENERATOR}"
258 die "Value ${CMAKE_MAKEFILE_GENERATOR} is not supported"
262 echo ${generator_name}
265 # @FUNCTION: cmake_comment_add_subdirectory
266 # @USAGE: <subdirectory>
268 # Comment out an add_subdirectory call in CMakeLists.txt in the current directory
269 cmake_comment_add_subdirectory() {
270 if [[ -z ${1} ]]; then
271 die "comment_add_subdirectory must be passed the directory name to comment"
274 if [[ -e "CMakeLists.txt" ]]; then
275 sed -e "/add_subdirectory[[:space:]]*([[:space:]]*${1//\//\\/}[[:space:]]*)/I s/^/#DONOTCOMPILE /" \
276 -i CMakeLists.txt || die "failed to comment add_subdirectory(${1})"
280 # @FUNCTION: comment_add_subdirectory
281 # @USAGE: <subdirectory>
283 # Comment out an add_subdirectory call in CMakeLists.txt in the current directory
284 # Banned in EAPI 6 and later - use cmake_comment_add_subdirectory instead.
285 comment_add_subdirectory() {
286 [[ ${EAPI} == [2345] ]] || die "comment_add_subdirectory is banned in EAPI 6 and later - use cmake_comment_add_subdirectory instead"
288 cmake_comment_add_subdirectory "$@"
291 # @FUNCTION: cmake-utils_use_with
292 # @USAGE: <USE flag> [flag name]
294 # Based on use_with. See ebuild(5).
296 # `cmake-utils_use_with foo FOO` echoes -DWITH_FOO=ON if foo is enabled
297 # and -DWITH_FOO=OFF if it is disabled.
298 cmake-utils_use_with() { _cmake_use_me_now WITH_ "$@" ; }
300 # @FUNCTION: cmake-utils_use_enable
301 # @USAGE: <USE flag> [flag name]
303 # Based on use_enable. See ebuild(5).
305 # `cmake-utils_use_enable foo FOO` echoes -DENABLE_FOO=ON if foo is enabled
306 # and -DENABLE_FOO=OFF if it is disabled.
307 cmake-utils_use_enable() { _cmake_use_me_now ENABLE_ "$@" ; }
309 # @FUNCTION: cmake-utils_use_find_package
310 # @USAGE: <USE flag> <package name>
312 # Based on use_enable. See ebuild(5).
314 # `cmake-utils_use_find_package foo LibFoo` echoes -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=OFF
315 # if foo is enabled and -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=ON if it is disabled.
316 # This can be used to make find_package optional.
317 cmake-utils_use_find_package() {
318 if [[ ${EAPI} != [2345] ]] && [[ "$#" != 2 ]] ; then
319 die "Usage: cmake-utils_use_find_package <USE flag> <package name>"
322 _cmake_use_me_now_inverted CMAKE_DISABLE_FIND_PACKAGE_ "$@" ;
325 # @FUNCTION: cmake-utils_use_disable
326 # @USAGE: <USE flag> [flag name]
328 # Based on inversion of use_enable. See ebuild(5).
330 # `cmake-utils_use_enable foo FOO` echoes -DDISABLE_FOO=OFF if foo is enabled
331 # and -DDISABLE_FOO=ON if it is disabled.
332 cmake-utils_use_disable() { _cmake_use_me_now_inverted DISABLE_ "$@" ; }
334 # @FUNCTION: cmake-utils_use_no
335 # @USAGE: <USE flag> [flag name]
337 # Based on use_disable. See ebuild(5).
339 # `cmake-utils_use_no foo FOO` echoes -DNO_FOO=OFF if foo is enabled
340 # and -DNO_FOO=ON if it is disabled.
341 cmake-utils_use_no() { _cmake_use_me_now_inverted NO_ "$@" ; }
343 # @FUNCTION: cmake-utils_use_want
344 # @USAGE: <USE flag> [flag name]
346 # Based on use_enable. See ebuild(5).
348 # `cmake-utils_use_want foo FOO` echoes -DWANT_FOO=ON if foo is enabled
349 # and -DWANT_FOO=OFF if it is disabled.
350 cmake-utils_use_want() { _cmake_use_me_now WANT_ "$@" ; }
352 # @FUNCTION: cmake-utils_use_build
353 # @USAGE: <USE flag> [flag name]
355 # Based on use_enable. See ebuild(5).
357 # `cmake-utils_use_build foo FOO` echoes -DBUILD_FOO=ON if foo is enabled
358 # and -DBUILD_FOO=OFF if it is disabled.
359 cmake-utils_use_build() { _cmake_use_me_now BUILD_ "$@" ; }
361 # @FUNCTION: cmake-utils_use_has
362 # @USAGE: <USE flag> [flag name]
364 # Based on use_enable. See ebuild(5).
366 # `cmake-utils_use_has foo FOO` echoes -DHAVE_FOO=ON if foo is enabled
367 # and -DHAVE_FOO=OFF if it is disabled.
368 cmake-utils_use_has() { _cmake_use_me_now HAVE_ "$@" ; }
370 # @FUNCTION: cmake-utils_use_use
371 # @USAGE: <USE flag> [flag name]
373 # Based on use_enable. See ebuild(5).
375 # `cmake-utils_use_use foo FOO` echoes -DUSE_FOO=ON if foo is enabled
376 # and -DUSE_FOO=OFF if it is disabled.
377 cmake-utils_use_use() { _cmake_use_me_now USE_ "$@" ; }
379 # @FUNCTION: cmake-utils_use
380 # @USAGE: <USE flag> [flag name]
382 # Based on use_enable. See ebuild(5).
384 # `cmake-utils_use foo FOO` echoes -DFOO=ON if foo is enabled
385 # and -DFOO=OFF if it is disabled.
386 cmake-utils_use() { _cmake_use_me_now "" "$@" ; }
388 # @FUNCTION: cmake-utils_useno
389 # @USAGE: <USE flag> [flag name]
391 # Based on use_enable. See ebuild(5).
393 # `cmake-utils_useno foo NOFOO` echoes -DNOFOO=OFF if foo is enabled
394 # and -DNOFOO=ON if it is disabled.
395 cmake-utils_useno() { _cmake_use_me_now_inverted "" "$@" ; }
397 # Internal function for modifying hardcoded definitions.
398 # Removes dangerous definitions that override Gentoo settings.
399 _cmake_modify-cmakelists() {
400 debug-print-function ${FUNCNAME} "$@"
402 # Only edit the files once
403 grep -qs "<<< Gentoo configuration >>>" "${CMAKE_USE_DIR}"/CMakeLists.txt && return 0
405 # Comment out all set (<some_should_be_user_defined_variable> value)
406 # TODO Add QA checker - inform when variable being checked for below is set in CMakeLists.txt
407 find "${CMAKE_USE_DIR}" -name CMakeLists.txt \
408 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_BUILD_TYPE.*)/{s/^/#IGNORE /g}' {} + \
409 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_COLOR_MAKEFILE.*)/{s/^/#IGNORE /g}' {} + \
410 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_INSTALL_PREFIX.*)/{s/^/#IGNORE /g}' {} + \
411 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_VERBOSE_MAKEFILE.*)/{s/^/#IGNORE /g}' {} + \
412 || die "${LINENO}: failed to disable hardcoded settings"
414 # NOTE Append some useful summary here
415 cat >> "${CMAKE_USE_DIR}"/CMakeLists.txt <<- _EOF_ || die
417 MESSAGE(STATUS "<<< Gentoo configuration >>>
418 Build type \${CMAKE_BUILD_TYPE}
419 Install path \${CMAKE_INSTALL_PREFIX}
422 C++ \${CMAKE_CXX_FLAGS}
424 Executable \${CMAKE_EXE_LINKER_FLAGS}
425 Module \${CMAKE_MODULE_LINKER_FLAGS}
426 Shared \${CMAKE_SHARED_LINKER_FLAGS}\n")
430 # temporary function for moving cmake cleanups from from src_configure -> src_prepare.
432 _cmake_cleanup_cmake() {
433 : ${CMAKE_USE_DIR:=${S}}
435 if [[ "${CMAKE_REMOVE_MODULES}" == "yes" ]] ; then
437 for name in ${CMAKE_REMOVE_MODULES_LIST} ; do
438 find "${S}" -name ${name}.cmake -exec rm -v {} + || die
442 # check if CMakeLists.txt exist and if no then die
443 if [[ ! -e ${CMAKE_USE_DIR}/CMakeLists.txt ]] ; then
444 eerror "Unable to locate CMakeLists.txt under:"
445 eerror "\"${CMAKE_USE_DIR}/CMakeLists.txt\""
446 eerror "Consider not inheriting the cmake eclass."
447 die "FATAL: Unable to find CMakeLists.txt"
450 # Remove dangerous things.
451 _cmake_modify-cmakelists
454 enable_cmake-utils_src_prepare() {
455 debug-print-function ${FUNCNAME} "$@"
457 pushd "${S}" > /dev/null || die
459 if [[ ${EAPI} != [2345] ]]; then
463 debug-print "$FUNCNAME: PATCHES=$PATCHES"
464 [[ ${PATCHES[@]} ]] && epatch "${PATCHES[@]}"
466 debug-print "$FUNCNAME: applying user patches"
470 popd > /dev/null || die
473 # @VARIABLE: mycmakeargs
476 # Optional cmake defines as a bash array. Should be defined before calling
480 # local mycmakeargs=(
481 # $(cmake-utils_use_with openconnect)
484 # cmake-utils_src_configure
488 enable_cmake-utils_src_configure() {
489 debug-print-function ${FUNCNAME} "$@"
491 [[ ${EAPI} == [2345] ]] && _cmake_cleanup_cmake
493 _cmake_check_build_dir
495 # Fix xdg collision with sandbox
496 local -x XDG_CONFIG_HOME="${T}"
498 # @SEE CMAKE_BUILD_TYPE
499 if [[ ${CMAKE_BUILD_TYPE} = Gentoo ]]; then
500 # Handle release builds
501 if ! has debug ${IUSE//+} || ! use debug; then
502 local CPPFLAGS=${CPPFLAGS}
503 append-cppflags -DNDEBUG
507 # Prepare Gentoo override rules (set valid compiler, append CPPFLAGS etc.)
508 local build_rules=${BUILD_DIR}/gentoo_rules.cmake
509 # Since cmake-3.4.0_rc1 "<FLAGS>" no longer contains includes and thus
510 # we need to add "<INCLUDES>"
512 if [[ ${PN} == cmake ]] ; then
513 if $(version_is_at_least 3.4.0 $(get_version_component_range 1-3 ${PV})) ; then
514 includes="<INCLUDES>"
516 elif ROOT=/ has_version \>=dev-util/cmake-3.4.0_rc1 ; then
517 includes="<INCLUDES>"
519 cat > "${build_rules}" <<- _EOF_ || die
520 SET (CMAKE_AR $(type -P $(tc-getAR)) CACHE FILEPATH "Archive manager" FORCE)
521 SET (CMAKE_ASM_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> ${includes} ${CFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "ASM compile command" FORCE)
522 SET (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> ${includes} ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C compile command" FORCE)
523 SET (CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> ${includes} ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C++ compile command" FORCE)
524 SET (CMAKE_Fortran_COMPILE_OBJECT "<CMAKE_Fortran_COMPILER> <DEFINES> ${includes} ${FCFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "Fortran compile command" FORCE)
525 SET (CMAKE_RANLIB $(type -P $(tc-getRANLIB)) CACHE FILEPATH "Archive index generator" FORCE)
526 SET (PKG_CONFIG_EXECUTABLE $(type -P $(tc-getPKG_CONFIG)) CACHE FILEPATH "pkg-config executable" FORCE)
529 local toolchain_file=${BUILD_DIR}/gentoo_toolchain.cmake
530 cat > ${toolchain_file} <<- _EOF_ || die
531 SET (CMAKE_C_COMPILER $(tc-getCC))
532 SET (CMAKE_CXX_COMPILER $(tc-getCXX))
533 SET (CMAKE_Fortran_COMPILER $(tc-getFC))
536 if tc-is-cross-compiler; then
538 case "${KERNEL:-linux}" in
539 Cygwin) sysname="CYGWIN_NT-5.1" ;;
540 HPUX) sysname="HP-UX" ;;
541 linux) sysname="Linux" ;;
544 cat >> "${toolchain_file}" <<- _EOF_ || die
545 SET (CMAKE_RC_COMPILER $(tc-getRC))
548 *) sysname="${KERNEL}" ;;
551 cat >> "${toolchain_file}" <<- _EOF_ || die
552 SET (CMAKE_SYSTEM_NAME "${sysname}")
555 if [ "${SYSROOT:-/}" != "/" ] ; then
556 # When cross-compiling with a sysroot (e.g. with crossdev's emerge wrappers)
557 # we need to tell cmake to use libs/headers from the sysroot but programs from / only.
558 cat >> "${toolchain_file}" <<- _EOF_ || die
559 set(CMAKE_FIND_ROOT_PATH "${SYSROOT}")
560 set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
561 set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
562 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
567 [[ ${EAPI} == 2 ]] && ! use prefix && local EPREFIX=
569 if [[ ${EPREFIX} ]]; then
570 cat >> "${build_rules}" <<- _EOF_ || die
571 # in Prefix we need rpath and must ensure cmake gets our default linker path
572 # right ... except for Darwin hosts
574 SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
575 SET (CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH "${EPREFIX}/usr/${CHOST}/lib/gcc;${EPREFIX}/usr/${CHOST}/lib;${EPREFIX}/usr/$(get_libdir);${EPREFIX}/$(get_libdir)"
576 CACHE STRING "" FORCE)
580 SET(CMAKE_PREFIX_PATH "${EPREFIX}${PREFIX}" CACHE STRING "" FORCE)
581 SET(CMAKE_SKIP_BUILD_RPATH OFF CACHE BOOL "" FORCE)
582 SET(CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
583 SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE CACHE BOOL "")
584 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)
585 SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOL "" FORCE)
586 SET(CMAKE_INSTALL_NAME_DIR "${EPREFIX}${PREFIX}/lib" CACHE STRING "" FORCE)
592 # Common configure parameters (invariants)
593 local common_config=${BUILD_DIR}/gentoo_common_config.cmake
594 local libdir=$(get_libdir)
595 cat > "${common_config}" <<- _EOF_ || die
596 SET (LIB_SUFFIX ${libdir/lib} CACHE STRING "library path suffix" FORCE)
597 SET (CMAKE_INSTALL_LIBDIR ${libdir} CACHE PATH "Output directory for libraries")
599 [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo 'SET (CMAKE_COLOR_MAKEFILE OFF CACHE BOOL "pretty colors during make" FORCE)' >> "${common_config}"
601 # Convert mycmakeargs to an array, for backwards compatibility
602 # Make the array a local variable since <=portage-2.1.6.x does not
603 # support global arrays (see bug #297255).
604 local mycmakeargstype=$(declare -p mycmakeargs 2>&-)
605 if [[ "${mycmakeargstype}" != "declare -a mycmakeargs="* ]]; then
606 if [[ -n "${mycmakeargstype}" ]] ; then
607 if [[ ${EAPI} != [2345] ]]; then
608 eqawarn "Declaring mycmakeargs as a variable is deprecated. Please use an array instead."
610 die "Declaring mycmakeargs as a variable is banned in EAPI=${EAPI}. Please use an array instead."
613 local mycmakeargs_local=(${mycmakeargs})
615 local mycmakeargs_local=("${mycmakeargs[@]}")
618 if [[ ${CMAKE_WARN_UNUSED_CLI} == no ]] ; then
619 local warn_unused_cli="--no-warn-unused-cli"
621 local warn_unused_cli=""
624 # Common configure parameters (overridable)
625 # NOTE CMAKE_BUILD_TYPE can be only overriden via CMAKE_BUILD_TYPE eclass variable
626 # No -DCMAKE_BUILD_TYPE=xxx definitions will be in effect.
629 -C "${common_config}"
630 -G "$(_cmake_generator_to_use)"
631 -DCMAKE_INSTALL_PREFIX="${EPREFIX}${PREFIX}"
632 "${mycmakeargs_local[@]}"
633 -DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
634 -DCMAKE_INSTALL_DO_STRIP=OFF
635 -DCMAKE_USER_MAKE_RULES_OVERRIDE="${build_rules}"
636 -DCMAKE_TOOLCHAIN_FILE="${toolchain_file}"
640 if [[ -n "${CMAKE_EXTRA_CACHE_FILE}" ]] ; then
641 cmakeargs+=( -C "${CMAKE_EXTRA_CACHE_FILE}" )
644 pushd "${BUILD_DIR}" > /dev/null || die
645 debug-print "${LINENO} ${ECLASS} ${FUNCNAME}: mycmakeargs is ${mycmakeargs_local[*]}"
646 echo "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}"
647 "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}" || die "cmake failed"
648 popd > /dev/null || die
651 enable_cmake-utils_src_compile() {
652 debug-print-function ${FUNCNAME} "$@"
654 cmake-utils_src_make "$@"
657 _ninjaopts_from_makeopts() {
658 if [[ ${NINJAOPTS+set} == set ]]; then
676 export NINJAOPTS="${ninjaopts[*]}"
679 # @FUNCTION: _cmake_ninja_src_make
682 # Build the package using ninja generator
683 _cmake_ninja_src_make() {
684 debug-print-function ${FUNCNAME} "$@"
686 [[ -e build.ninja ]] || die "build.ninja not found. Error during configure stage."
688 _ninjaopts_from_makeopts
690 if [[ "${CMAKE_VERBOSE}" != "OFF" ]]; then
691 set -- ninja ${NINJAOPTS} -v "$@"
693 set -- ninja ${NINJAOPTS} "$@"
700 # @FUNCTION: _cmake_emake_src_make
703 # Build the package using make generator
704 _cmake_emake_src_make() {
705 debug-print-function ${FUNCNAME} "$@"
707 [[ -e Makefile ]] || die "Makefile not found. Error during configure stage."
709 if [[ "${CMAKE_VERBOSE}" != "OFF" ]]; then
710 emake VERBOSE=1 "$@" || die
717 # @FUNCTION: cmake-utils_src_make
719 # Function for building the package. Automatically detects the build type.
720 # All arguments are passed to emake.
721 cmake-utils_src_make() {
722 debug-print-function ${FUNCNAME} "$@"
724 _cmake_check_build_dir
725 pushd "${BUILD_DIR}" > /dev/null || die
727 _cmake_${CMAKE_MAKEFILE_GENERATOR}_src_make "$@"
729 popd > /dev/null || die
732 enable_cmake-utils_src_test() {
733 debug-print-function ${FUNCNAME} "$@"
735 _cmake_check_build_dir
736 pushd "${BUILD_DIR}" > /dev/null || die
737 [[ -e CTestTestfile.cmake ]] || { echo "No tests found. Skipping."; return 0 ; }
739 [[ -n ${TEST_VERBOSE} ]] && myctestargs+=( --extra-verbose --output-on-failure )
741 if ctest "${myctestargs[@]}" "$@" ; then
742 einfo "Tests succeeded."
743 popd > /dev/null || die
746 if [[ -n "${CMAKE_YES_I_WANT_TO_SEE_THE_TEST_LOG}" ]] ; then
747 # on request from Diego
748 eerror "Tests failed. Test log ${BUILD_DIR}/Testing/Temporary/LastTest.log follows:"
749 eerror "--START TEST LOG--------------------------------------------------------------"
750 cat "${BUILD_DIR}/Testing/Temporary/LastTest.log"
751 eerror "--END TEST LOG----------------------------------------------------------------"
754 die "Tests failed. When you file a bug, please attach the following file: \n\t${BUILD_DIR}/Testing/Temporary/LastTest.log"
757 # die might not die due to nonfatal
758 popd > /dev/null || die
763 enable_cmake-utils_src_install() {
764 debug-print-function ${FUNCNAME} "$@"
766 _cmake_check_build_dir
767 pushd "${BUILD_DIR}" > /dev/null || die
768 DESTDIR="${D}" ${CMAKE_MAKEFILE_GENERATOR} install "$@" || die "died running ${CMAKE_MAKEFILE_GENERATOR} install"
769 popd > /dev/null || die
771 pushd "${S}" > /dev/null || die
773 popd > /dev/null || die
776 # @FUNCTION: cmake-utils_src_prepare
778 # Apply ebuild and user patches.
779 cmake-utils_src_prepare() {
780 _cmake_execute_optionally "src_prepare" "$@"
783 # @FUNCTION: cmake-utils_src_configure
785 # General function for configuring with cmake. Default behaviour is to start an
786 # out-of-source build.
787 cmake-utils_src_configure() {
788 _cmake_execute_optionally "src_configure" "$@"
791 # @FUNCTION: cmake-utils_src_compile
793 # General function for compiling with cmake.
794 # Automatically detects the build type. All arguments are passed to emake.
795 cmake-utils_src_compile() {
796 _cmake_execute_optionally "src_compile" "$@"
799 # @FUNCTION: cmake-utils_src_test
801 # Function for testing the package. Automatically detects the build type.
802 cmake-utils_src_test() {
803 _cmake_execute_optionally "src_test" "$@"
806 # @FUNCTION: cmake-utils_src_install
808 # Function for installing the package. Automatically detects the build type.
809 cmake-utils_src_install() {
810 _cmake_execute_optionally "src_install" "$@"
813 # Optionally executes phases based on WANT_CMAKE variable/USE flag.
814 _cmake_execute_optionally() {
815 local phase="$1" ; shift
816 if [[ ${WANT_CMAKE} = always ]]; then
817 enable_cmake-utils_${phase} "$@"
819 use ${WANT_CMAKE} && enable_cmake-utils_${phase} "$@"