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 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-utils_use_disable
342 # @USAGE: <USE flag> [flag name]
344 # Based on inversion of use_enable. See ebuild(5).
346 # `cmake-utils_use_enable foo FOO` echoes -DDISABLE_FOO=OFF if foo is enabled
347 # and -DDISABLE_FOO=ON if it is disabled.
348 cmake-utils_use_disable() { _cmake_use_me_now_inverted DISABLE_ "$@" ; }
350 # @FUNCTION: cmake-utils_use_no
351 # @USAGE: <USE flag> [flag name]
353 # Based on use_disable. See ebuild(5).
355 # `cmake-utils_use_no foo FOO` echoes -DNO_FOO=OFF if foo is enabled
356 # and -DNO_FOO=ON if it is disabled.
357 cmake-utils_use_no() { _cmake_use_me_now_inverted NO_ "$@" ; }
359 # @FUNCTION: cmake-utils_use_want
360 # @USAGE: <USE flag> [flag name]
362 # Based on use_enable. See ebuild(5).
364 # `cmake-utils_use_want foo FOO` echoes -DWANT_FOO=ON if foo is enabled
365 # and -DWANT_FOO=OFF if it is disabled.
366 cmake-utils_use_want() { _cmake_use_me_now WANT_ "$@" ; }
368 # @FUNCTION: cmake-utils_use_build
369 # @USAGE: <USE flag> [flag name]
371 # Based on use_enable. See ebuild(5).
373 # `cmake-utils_use_build foo FOO` echoes -DBUILD_FOO=ON if foo is enabled
374 # and -DBUILD_FOO=OFF if it is disabled.
375 cmake-utils_use_build() { _cmake_use_me_now BUILD_ "$@" ; }
377 # @FUNCTION: cmake-utils_use_has
378 # @USAGE: <USE flag> [flag name]
380 # Based on use_enable. See ebuild(5).
382 # `cmake-utils_use_has foo FOO` echoes -DHAVE_FOO=ON if foo is enabled
383 # and -DHAVE_FOO=OFF if it is disabled.
384 cmake-utils_use_has() { _cmake_use_me_now HAVE_ "$@" ; }
386 # @FUNCTION: cmake-utils_use_use
387 # @USAGE: <USE flag> [flag name]
389 # Based on use_enable. See ebuild(5).
391 # `cmake-utils_use_use foo FOO` echoes -DUSE_FOO=ON if foo is enabled
392 # and -DUSE_FOO=OFF if it is disabled.
393 cmake-utils_use_use() { _cmake_use_me_now USE_ "$@" ; }
395 # @FUNCTION: cmake-utils_use
396 # @USAGE: <USE flag> [flag name]
398 # Based on use_enable. See ebuild(5).
400 # `cmake-utils_use foo FOO` echoes -DFOO=ON if foo is enabled
401 # and -DFOO=OFF if it is disabled.
402 cmake-utils_use() { _cmake_use_me_now "" "$@" ; }
404 # @FUNCTION: cmake-utils_useno
405 # @USAGE: <USE flag> [flag name]
407 # Based on use_enable. See ebuild(5).
409 # `cmake-utils_useno foo NOFOO` echoes -DNOFOO=OFF if foo is enabled
410 # and -DNOFOO=ON if it is disabled.
411 cmake-utils_useno() { _cmake_use_me_now_inverted "" "$@" ; }
413 # Internal function for modifying hardcoded definitions.
414 # Removes dangerous definitions that override Gentoo settings.
415 _cmake_modify-cmakelists() {
416 debug-print-function ${FUNCNAME} "$@"
418 # Only edit the files once
419 grep -qs "<<< Gentoo configuration >>>" "${CMAKE_USE_DIR}"/CMakeLists.txt && return 0
421 # Comment out all set (<some_should_be_user_defined_variable> value)
422 # TODO Add QA checker - inform when variable being checked for below is set in CMakeLists.txt
423 find "${CMAKE_USE_DIR}" -name CMakeLists.txt \
424 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_BUILD_TYPE.*)/{s/^/#IGNORE /g}' {} + \
425 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_COLOR_MAKEFILE.*)/{s/^/#IGNORE /g}' {} + \
426 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_INSTALL_PREFIX.*)/{s/^/#IGNORE /g}' {} + \
427 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_VERBOSE_MAKEFILE.*)/{s/^/#IGNORE /g}' {} + \
428 || die "${LINENO}: failed to disable hardcoded settings"
430 # NOTE Append some useful summary here
431 cat >> "${CMAKE_USE_DIR}"/CMakeLists.txt <<- _EOF_ || die
433 MESSAGE(STATUS "<<< Gentoo configuration >>>
434 Build type \${CMAKE_BUILD_TYPE}
435 Install path \${CMAKE_INSTALL_PREFIX}
438 C++ \${CMAKE_CXX_FLAGS}
440 Executable \${CMAKE_EXE_LINKER_FLAGS}
441 Module \${CMAKE_MODULE_LINKER_FLAGS}
442 Shared \${CMAKE_SHARED_LINKER_FLAGS}\n")
446 # temporary function for moving cmake cleanups from from src_configure -> src_prepare.
448 _cmake_cleanup_cmake() {
449 : ${CMAKE_USE_DIR:=${S}}
451 if [[ "${CMAKE_REMOVE_MODULES}" == "yes" ]] ; then
453 for name in ${CMAKE_REMOVE_MODULES_LIST} ; do
454 find "${S}" -name ${name}.cmake -exec rm -v {} + || die
458 # check if CMakeLists.txt exist and if no then die
459 if [[ ! -e ${CMAKE_USE_DIR}/CMakeLists.txt ]] ; then
460 eerror "Unable to locate CMakeLists.txt under:"
461 eerror "\"${CMAKE_USE_DIR}/CMakeLists.txt\""
462 eerror "Consider not inheriting the cmake eclass."
463 die "FATAL: Unable to find CMakeLists.txt"
466 # Remove dangerous things.
467 _cmake_modify-cmakelists
470 # @FUNCTION: cmake-utils_src_prepare
472 # Apply ebuild and user patches.
473 cmake-utils_src_prepare() {
474 debug-print-function ${FUNCNAME} "$@"
476 pushd "${S}" > /dev/null || die
478 if [[ ${EAPI} != 5 ]]; then
482 debug-print "$FUNCNAME: PATCHES=$PATCHES"
483 [[ ${PATCHES[@]} ]] && epatch "${PATCHES[@]}"
485 debug-print "$FUNCNAME: applying user patches"
489 popd > /dev/null || die
491 # make ${S} read-only in order to detect broken build-systems
492 if [[ ${CMAKE_UTILS_QA_SRC_DIR_READONLY} && ! ${CMAKE_IN_SOURCE_BUILD} ]]; then
496 _CMAKE_UTILS_SRC_PREPARE_HAS_RUN=1
499 # @VARIABLE: mycmakeargs
502 # Optional cmake defines as a bash array. Should be defined before calling
506 # local mycmakeargs=(
507 # $(cmake-utils_use_with openconnect)
510 # cmake-utils_src_configure
514 # @FUNCTION: cmake-utils_src_configure
516 # General function for configuring with cmake. Default behaviour is to start an
517 # out-of-source build.
518 cmake-utils_src_configure() {
519 debug-print-function ${FUNCNAME} "$@"
521 if [[ ! ${_CMAKE_UTILS_SRC_PREPARE_HAS_RUN} ]]; then
522 if [[ ${EAPI} != [56] ]]; then
523 die "FATAL: cmake-utils_src_prepare has not been run"
525 eqawarn "cmake-utils_src_prepare has not been run, please open a bug on https://bugs.gentoo.org/"
529 [[ ${EAPI} == 5 ]] && _cmake_cleanup_cmake
531 _cmake_check_build_dir
533 # Fix xdg collision with sandbox
534 xdg_environment_reset
536 # @SEE CMAKE_BUILD_TYPE
537 if [[ ${CMAKE_BUILD_TYPE} = Gentoo ]]; then
538 # Handle release builds
539 if ! has debug ${IUSE//+} || ! use debug; then
540 local CPPFLAGS=${CPPFLAGS}
541 append-cppflags -DNDEBUG
545 # Prepare Gentoo override rules (set valid compiler, append CPPFLAGS etc.)
546 local build_rules=${BUILD_DIR}/gentoo_rules.cmake
548 cat > "${build_rules}" <<- _EOF_ || die
549 SET (CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "ASM compile command" FORCE)
550 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)
551 SET (CMAKE_ASM-ATT_LINK_FLAGS "-nostdlib" CACHE STRING "ASM-ATT link flags" FORCE)
552 SET (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C compile command" FORCE)
553 SET (CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C++ compile command" FORCE)
554 SET (CMAKE_Fortran_COMPILE_OBJECT "<CMAKE_Fortran_COMPILER> <DEFINES> <INCLUDES> ${FCFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "Fortran compile command" FORCE)
557 local myCC=$(tc-getCC) myCXX=$(tc-getCXX) myFC=$(tc-getFC)
559 # !!! IMPORTANT NOTE !!!
560 # Single slash below is intentional. CMake is weird and wants the
561 # CMAKE_*_VARIABLES split into two elements: the first one with
562 # compiler path, and the second one with all command-line options,
564 local toolchain_file=${BUILD_DIR}/gentoo_toolchain.cmake
565 cat > ${toolchain_file} <<- _EOF_ || die
566 SET (CMAKE_ASM_COMPILER "${myCC/ /;}")
567 SET (CMAKE_ASM-ATT_COMPILER "${myCC/ /;}")
568 SET (CMAKE_C_COMPILER "${myCC/ /;}")
569 SET (CMAKE_CXX_COMPILER "${myCXX/ /;}")
570 SET (CMAKE_Fortran_COMPILER "${myFC/ /;}")
571 SET (CMAKE_AR $(type -P $(tc-getAR)) CACHE FILEPATH "Archive manager" FORCE)
572 SET (CMAKE_RANLIB $(type -P $(tc-getRANLIB)) CACHE FILEPATH "Archive index generator" FORCE)
573 SET (CMAKE_SYSTEM_PROCESSOR "${CHOST%%-*}")
576 # We are using the C compiler for assembly by default.
577 local -x ASMFLAGS=${CFLAGS}
578 local -x PKG_CONFIG=$(tc-getPKG_CONFIG)
580 if tc-is-cross-compiler; then
582 case "${KERNEL:-linux}" in
583 Cygwin) sysname="CYGWIN_NT-5.1" ;;
584 HPUX) sysname="HP-UX" ;;
585 linux) sysname="Linux" ;;
588 cat >> "${toolchain_file}" <<- _EOF_ || die
589 SET (CMAKE_RC_COMPILER $(tc-getRC))
592 *) sysname="${KERNEL}" ;;
595 cat >> "${toolchain_file}" <<- _EOF_ || die
596 SET (CMAKE_SYSTEM_NAME "${sysname}")
599 if [ "${SYSROOT:-/}" != "/" ] ; then
600 # When cross-compiling with a sysroot (e.g. with crossdev's emerge wrappers)
601 # we need to tell cmake to use libs/headers from the sysroot but programs from / only.
602 cat >> "${toolchain_file}" <<- _EOF_ || die
603 SET (CMAKE_FIND_ROOT_PATH "${SYSROOT}")
604 SET (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
605 SET (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
606 SET (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
611 if [[ ${EPREFIX} ]]; then
612 cat >> "${build_rules}" <<- _EOF_ || die
613 # in Prefix we need rpath and must ensure cmake gets our default linker path
614 # right ... except for Darwin hosts
616 SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
617 SET (CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH "${EPREFIX}/usr/${CHOST}/lib/gcc;${EPREFIX}/usr/${CHOST}/lib;${EPREFIX}/usr/$(get_libdir);${EPREFIX}/$(get_libdir)"
618 CACHE STRING "" FORCE)
622 SET (CMAKE_PREFIX_PATH "${EPREFIX}/usr" CACHE STRING "" FORCE)
623 SET (CMAKE_MACOSX_RPATH ON CACHE BOOL "" FORCE)
624 SET (CMAKE_SKIP_BUILD_RPATH OFF CACHE BOOL "" FORCE)
625 SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
626 SET (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOL "" FORCE)
632 # Common configure parameters (invariants)
633 local common_config=${BUILD_DIR}/gentoo_common_config.cmake
634 local libdir=$(get_libdir)
635 cat > "${common_config}" <<- _EOF_ || die
636 SET (CMAKE_GENTOO_BUILD ON CACHE BOOL "Indicate Gentoo package build")
637 SET (LIB_SUFFIX ${libdir/lib} CACHE STRING "library path suffix" FORCE)
638 SET (CMAKE_INSTALL_LIBDIR ${libdir} CACHE PATH "Output directory for libraries")
639 SET (CMAKE_INSTALL_INFODIR "${EPREFIX}/usr/share/info" CACHE PATH "")
640 SET (CMAKE_INSTALL_MANDIR "${EPREFIX}/usr/share/man" CACHE PATH "")
641 SET (CMAKE_USER_MAKE_RULES_OVERRIDE "${build_rules}" CACHE FILEPATH "Gentoo override rules")
643 [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo 'SET (CMAKE_COLOR_MAKEFILE OFF CACHE BOOL "pretty colors during make" FORCE)' >> "${common_config}"
645 if [[ ${EAPI} != [56] ]]; then
646 cat >> "${common_config}" <<- _EOF_ || die
647 SET (CMAKE_INSTALL_DOCDIR "${EPREFIX}/usr/share/doc/${PF}" CACHE PATH "")
648 SET (BUILD_SHARED_LIBS ON CACHE BOOLEAN "")
652 # Wipe the default optimization flags out of CMake
653 if [[ ${CMAKE_BUILD_TYPE} != Gentoo && ${EAPI} != 5 ]]; then
654 cat >> ${common_config} <<- _EOF_ || die
655 SET (CMAKE_ASM_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
656 SET (CMAKE_ASM-ATT_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
657 SET (CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
658 SET (CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
659 SET (CMAKE_Fortran_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
660 SET (CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
661 SET (CMAKE_MODULE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
662 SET (CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
663 SET (CMAKE_STATIC_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
667 # Convert mycmakeargs to an array, for backwards compatibility
668 # Make the array a local variable since <=portage-2.1.6.x does not
669 # support global arrays (see bug #297255).
670 local mycmakeargstype=$(declare -p mycmakeargs 2>&-)
671 if [[ "${mycmakeargstype}" != "declare -a mycmakeargs="* ]]; then
672 if [[ -n "${mycmakeargstype}" ]] ; then
673 if [[ ${EAPI} == 5 ]]; then
674 eqawarn "Declaring mycmakeargs as a variable is deprecated. Please use an array instead."
676 die "Declaring mycmakeargs as a variable is banned in EAPI=${EAPI}. Please use an array instead."
679 local mycmakeargs_local=(${mycmakeargs})
681 local mycmakeargs_local=("${mycmakeargs[@]}")
684 if [[ ${CMAKE_WARN_UNUSED_CLI} == no ]] ; then
685 local warn_unused_cli="--no-warn-unused-cli"
687 local warn_unused_cli=""
690 # Common configure parameters (overridable)
691 # NOTE CMAKE_BUILD_TYPE can be only overridden via CMAKE_BUILD_TYPE eclass variable
692 # No -DCMAKE_BUILD_TYPE=xxx definitions will be in effect.
695 -C "${common_config}"
696 -G "$(_cmake_generator_to_use)"
697 -DCMAKE_INSTALL_PREFIX="${EPREFIX}/usr"
698 "${mycmakeargs_local[@]}"
699 -DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
700 $([[ ${EAPI} == 5 ]] && echo -DCMAKE_INSTALL_DO_STRIP=OFF)
701 -DCMAKE_TOOLCHAIN_FILE="${toolchain_file}"
705 if [[ -n "${CMAKE_EXTRA_CACHE_FILE}" ]] ; then
706 cmakeargs+=( -C "${CMAKE_EXTRA_CACHE_FILE}" )
709 pushd "${BUILD_DIR}" > /dev/null || die
710 debug-print "${LINENO} ${ECLASS} ${FUNCNAME}: mycmakeargs is ${mycmakeargs_local[*]}"
711 echo "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}"
712 "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}" || die "cmake failed"
713 popd > /dev/null || die
716 # @FUNCTION: cmake-utils_src_compile
718 # General function for compiling with cmake.
719 # Automatically detects the build type. All arguments are passed to emake.
720 cmake-utils_src_compile() {
721 debug-print-function ${FUNCNAME} "$@"
723 cmake-utils_src_make "$@"
726 # @FUNCTION: _cmake_ninja_src_make
729 # Build the package using ninja generator
730 _cmake_ninja_src_make() {
731 debug-print-function ${FUNCNAME} "$@"
733 [[ -e build.ninja ]] || die "build.ninja not found. Error during configure stage."
738 # @FUNCTION: _cmake_emake_src_make
741 # Build the package using make generator
742 _cmake_emake_src_make() {
743 debug-print-function ${FUNCNAME} "$@"
745 [[ -e Makefile ]] || die "Makefile not found. Error during configure stage."
747 if [[ "${CMAKE_VERBOSE}" != "OFF" ]]; then
748 emake VERBOSE=1 "$@" || die
755 # @FUNCTION: cmake-utils_src_make
757 # Function for building the package. Automatically detects the build type.
758 # All arguments are passed to emake.
759 cmake-utils_src_make() {
760 debug-print-function ${FUNCNAME} "$@"
762 _cmake_check_build_dir
763 pushd "${BUILD_DIR}" > /dev/null || die
765 _cmake_${CMAKE_MAKEFILE_GENERATOR}_src_make "$@"
767 popd > /dev/null || die
770 # @FUNCTION: cmake-utils_src_test
772 # Function for testing the package. Automatically detects the build type.
773 cmake-utils_src_test() {
774 debug-print-function ${FUNCNAME} "$@"
776 _cmake_check_build_dir
777 pushd "${BUILD_DIR}" > /dev/null || die
778 [[ -e CTestTestfile.cmake ]] || { echo "No tests found. Skipping."; return 0 ; }
780 [[ -n ${TEST_VERBOSE} ]] && myctestargs+=( --extra-verbose --output-on-failure )
782 set -- ctest -j "$(makeopts_jobs)" --test-load "$(makeopts_loadavg)" "${myctestargs[@]}" "$@"
785 einfo "Tests succeeded."
786 popd > /dev/null || die
789 if [[ -n "${CMAKE_YES_I_WANT_TO_SEE_THE_TEST_LOG}" ]] ; then
790 # on request from Diego
791 eerror "Tests failed. Test log ${BUILD_DIR}/Testing/Temporary/LastTest.log follows:"
792 eerror "--START TEST LOG--------------------------------------------------------------"
793 cat "${BUILD_DIR}/Testing/Temporary/LastTest.log"
794 eerror "--END TEST LOG----------------------------------------------------------------"
797 die "Tests failed. When you file a bug, please attach the following file: \n\t${BUILD_DIR}/Testing/Temporary/LastTest.log"
800 # die might not die due to nonfatal
801 popd > /dev/null || die
806 # @FUNCTION: cmake-utils_src_install
808 # Function for installing the package. Automatically detects the build type.
809 cmake-utils_src_install() {
810 debug-print-function ${FUNCNAME} "$@"
812 _cmake_check_build_dir
813 pushd "${BUILD_DIR}" > /dev/null || die
814 DESTDIR="${D}" ${CMAKE_MAKEFILE_GENERATOR} install "$@" || die "died running ${CMAKE_MAKEFILE_GENERATOR} install"
815 popd > /dev/null || die
817 pushd "${S}" > /dev/null || die
819 popd > /dev/null || die