*/*: Bump copyright on files touched this year
[gentoo.git] / eclass / cmake.eclass
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: cmake.eclass
5 # @MAINTAINER:
6 # kde@gentoo.org
7 # @AUTHOR:
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: 7
13 # @BLURB: common ebuild functions for cmake-based packages
14 # @DESCRIPTION:
15 # The cmake eclass makes creating ebuilds for cmake-based packages much easier.
16 # It provides all inherited features (DOCS, HTML_DOCS, PATCHES) along with
17 # out-of-source builds (default), in-source builds and an implementation of the
18 # well-known use_enable function for CMake.
19
20 if [[ -z ${_CMAKE_ECLASS} ]]; then
21 _CMAKE_ECLASS=1
22
23 # @ECLASS-VARIABLE: BUILD_DIR
24 # @DESCRIPTION:
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.
29 : ${BUILD_DIR:=${WORKDIR}/${P}_build}
30
31 # @ECLASS-VARIABLE: CMAKE_BINARY
32 # @DESCRIPTION:
33 # Eclass can use different cmake binary than the one provided in by system.
34 : ${CMAKE_BINARY:=cmake}
35
36 # @ECLASS-VARIABLE: CMAKE_BUILD_TYPE
37 # @DESCRIPTION:
38 # Set to override default CMAKE_BUILD_TYPE. Only useful for packages
39 # known to make use of "if (CMAKE_BUILD_TYPE MATCHES xxx)".
40 # If about to be set - needs to be set before invoking cmake_src_configure.
41 # You usually do *NOT* want nor need to set it as it pulls CMake default
42 # build-type specific compiler flags overriding make.conf.
43 : ${CMAKE_BUILD_TYPE:=Gentoo}
44
45 # @ECLASS-VARIABLE: CMAKE_IN_SOURCE_BUILD
46 # @DEFAULT_UNSET
47 # @DESCRIPTION:
48 # Set to enable in-source build.
49
50 # @ECLASS-VARIABLE: CMAKE_MAKEFILE_GENERATOR
51 # @DEFAULT_UNSET
52 # @DESCRIPTION:
53 # Specify a makefile generator to be used by cmake.
54 # At this point only "emake" and "ninja" are supported.
55 # The default is set to "ninja".
56 : ${CMAKE_MAKEFILE_GENERATOR:=ninja}
57
58 # @ECLASS-VARIABLE: CMAKE_REMOVE_MODULES_LIST
59 # @DESCRIPTION:
60 # Array of CMake modules that will be removed in $S during src_prepare,
61 # in order to force packages to use the system version.
62 # Set to "none" to disable removing modules entirely.
63 : ${CMAKE_REMOVE_MODULES_LIST:=FindBLAS FindLAPACK}
64
65 # @ECLASS-VARIABLE: CMAKE_USE_DIR
66 # @DESCRIPTION:
67 # Sets the directory where we are working with cmake, for example when
68 # application uses autotools and only one plugin needs to be done by cmake.
69 # By default it uses ${S}.
70
71 # @ECLASS-VARIABLE: CMAKE_VERBOSE
72 # @DESCRIPTION:
73 # Set to OFF to disable verbose messages during compilation
74 : ${CMAKE_VERBOSE:=ON}
75
76 # @ECLASS-VARIABLE: CMAKE_WARN_UNUSED_CLI
77 # @DESCRIPTION:
78 # Warn about variables that are declared on the command line
79 # but not used. Might give false-positives.
80 # "no" to disable (default) or anything else to enable.
81 : ${CMAKE_WARN_UNUSED_CLI:=yes}
82
83 # @ECLASS-VARIABLE: CMAKE_EXTRA_CACHE_FILE
84 # @DEFAULT_UNSET
85 # @DESCRIPTION:
86 # Specifies an extra cache file to pass to cmake. This is the analog of EXTRA_ECONF
87 # for econf and is needed to pass TRY_RUN results when cross-compiling.
88 # Should be set by user in a per-package basis in /etc/portage/package.env.
89
90 # @ECLASS-VARIABLE: CMAKE_QA_SRC_DIR_READONLY
91 # @DEFAULT_UNSET
92 # @DESCRIPTION:
93 # After running cmake_src_prepare, sets ${S} to read-only. This is
94 # a user flag and should under _no circumstances_ be set in the ebuild.
95 # Helps in improving QA of build systems that write to source tree.
96
97 case ${EAPI} in
98         7) ;;
99         *) die "EAPI=${EAPI:-0} is not supported" ;;
100 esac
101
102 inherit toolchain-funcs ninja-utils flag-o-matic multiprocessing xdg-utils
103
104 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
105
106 [[ ${CMAKE_MIN_VERSION} ]] && die "CMAKE_MIN_VERSION is banned; if necessary, set BDEPEND=\">=dev-util/cmake-${CMAKE_MIN_VERSION}\" directly"
107 [[ ${CMAKE_BUILD_DIR} ]] && die "The ebuild must be migrated to BUILD_DIR"
108 [[ ${CMAKE_REMOVE_MODULES} ]] && die "CMAKE_REMOVE_MODULES is banned, set CMAKE_REMOVE_MODULES_LIST=\"\" instead"
109 [[ ${CMAKE_UTILS_QA_SRC_DIR_READONLY} ]] && die "Use CMAKE_QA_SRC_DIR_READONLY instead"
110 [[ ${WANT_CMAKE} ]] && die "WANT_CMAKE has been removed and is a no-op"
111 [[ ${PREFIX} ]] && die "PREFIX has been removed and is a no-op"
112
113 case ${CMAKE_MAKEFILE_GENERATOR} in
114         emake)
115                 BDEPEND="sys-devel/make"
116                 ;;
117         ninja)
118                 BDEPEND="dev-util/ninja"
119                 ;;
120         *)
121                 eerror "Unknown value for \${CMAKE_MAKEFILE_GENERATOR}"
122                 die "Value ${CMAKE_MAKEFILE_GENERATOR} is not supported"
123                 ;;
124 esac
125
126 if [[ ${PN} != cmake ]]; then
127         BDEPEND+=" dev-util/cmake"
128 fi
129
130 # @FUNCTION: _cmake_banned_func
131 # @INTERNAL
132 # @DESCRIPTION:
133 # Banned functions are banned.
134 _cmake_banned_func() {
135         die "${FUNCNAME[1]} is banned. use -D$1<related_CMake_variable>=\"\$(usex $2)\" instead"
136 }
137
138 # Determine using IN or OUT source build
139 _cmake_check_build_dir() {
140         : ${CMAKE_USE_DIR:=${S}}
141         if [[ -n ${CMAKE_IN_SOURCE_BUILD} ]]; then
142                 # we build in source dir
143                 BUILD_DIR="${CMAKE_USE_DIR}"
144         fi
145
146         mkdir -p "${BUILD_DIR}" || die
147         einfo "Working in BUILD_DIR: \"$BUILD_DIR\""
148 }
149
150 # @FUNCTION: cmake_comment_add_subdirectory
151 # @USAGE: <subdirectory>
152 # @DESCRIPTION:
153 # Comment out one or more add_subdirectory calls in CMakeLists.txt in the current directory
154 cmake_comment_add_subdirectory() {
155         if [[ -z ${1} ]]; then
156                 die "comment_add_subdirectory must be passed at least one directory name to comment"
157         fi
158
159         if [[ -e "CMakeLists.txt" ]]; then
160                 local d
161                 for d in $@; do
162                         sed -e "/add_subdirectory[[:space:]]*([[:space:]]*${d//\//\\/}[[:space:]]*)/I s/^/#DONOTCOMPILE /" \
163                                 -i CMakeLists.txt || die "failed to comment add_subdirectory(${d})"
164                 done
165         fi
166 }
167
168 # @FUNCTION: comment_add_subdirectory
169 # @INTERNAL
170 # @DESCRIPTION:
171 # Banned. Use cmake_comment_add_subdirectory instead.
172 comment_add_subdirectory() {
173         die "comment_add_subdirectory is banned. Use cmake_comment_add_subdirectory instead"
174 }
175
176 # @FUNCTION: cmake-utils_use_with
177 # @INTERNAL
178 # @DESCRIPTION:
179 # Banned. Use -DWITH_FOO=$(usex foo) instead.
180 cmake-utils_use_with() { _cmake_banned_func WITH_ "$@" ; }
181
182 # @FUNCTION: cmake-utils_use_enable
183 # @INTERNAL
184 # @DESCRIPTION:
185 # Banned. Use -DENABLE_FOO=$(usex foo) instead.
186 cmake-utils_use_enable() { _cmake_banned_func ENABLE_ "$@" ; }
187
188 # @FUNCTION: cmake_use_find_package
189 # @USAGE: <USE flag> <package name>
190 # @DESCRIPTION:
191 # Based on use_enable. See ebuild(5).
192 #
193 # `cmake_use_find_package foo LibFoo` echoes -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=OFF
194 # if foo is enabled and -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=ON if it is disabled.
195 # This can be used to make find_package optional.
196 cmake_use_find_package() {
197         debug-print-function ${FUNCNAME} "$@"
198
199         if [[ "$#" != 2 || -z $1 ]] ; then
200                 die "Usage: cmake_use_find_package <USE flag> <package name>"
201         fi
202
203         echo "-DCMAKE_DISABLE_FIND_PACKAGE_$2=$(use $1 && echo OFF || echo ON)"
204 }
205
206 # @FUNCTION: cmake-utils_use_disable
207 # @INTERNAL
208 # @DESCRIPTION:
209 # Banned. Use -DDISABLE_FOO=$(usex !foo) instead.
210 cmake-utils_use_disable() { _cmake_banned_func DISABLE_ "$@" ; }
211
212 # @FUNCTION: cmake-utils_use_no
213 # @INTERNAL
214 # @DESCRIPTION:
215 # Banned. Use -DNO_FOO=$(usex !foo) instead.
216 cmake-utils_use_no() { _cmake_banned_func NO_ "$@" ; }
217
218 # @FUNCTION: cmake-utils_use_want
219 # @INTERNAL
220 # @DESCRIPTION:
221 # Banned. Use -DWANT_FOO=$(usex foo) instead.
222 cmake-utils_use_want() { _cmake_banned_func WANT_ "$@" ; }
223
224 # @FUNCTION: cmake-utils_use_build
225 # @INTERNAL
226 # @DESCRIPTION:
227 # Banned. Use -DBUILD_FOO=$(usex foo) instead.
228 cmake-utils_use_build() { _cmake_banned_func BUILD_ "$@" ; }
229
230 # @FUNCTION: cmake-utils_use_has
231 # @INTERNAL
232 # @DESCRIPTION:
233 # Banned. Use -DHAVE_FOO=$(usex foo) instead.
234 cmake-utils_use_has() { _cmake_banned_func HAVE_ "$@" ; }
235
236 # @FUNCTION: cmake-utils_use_use
237 # @INTERNAL
238 # @DESCRIPTION:
239 # Banned. Use -DUSE_FOO=$(usex foo) instead.
240 cmake-utils_use_use() { _cmake_banned_func USE_ "$@" ; }
241
242 # @FUNCTION: cmake-utils_use
243 # @INTERNAL
244 # @DESCRIPTION:
245 # Banned. Use -DFOO=$(usex foo) instead.
246 cmake-utils_use() { _cmake_banned_func "" "$@" ; }
247
248 # @FUNCTION: cmake-utils_useno
249 # @INTERNAL
250 # @DESCRIPTION:
251 # Banned. Use -DNOFOO=$(usex !foo) instead.
252 cmake-utils_useno() { _cmake_banned_func "" "$@" ; }
253
254 # Internal function for modifying hardcoded definitions.
255 # Removes dangerous definitions that override Gentoo settings.
256 _cmake_modify-cmakelists() {
257         debug-print-function ${FUNCNAME} "$@"
258
259         # Only edit the files once
260         grep -qs "<<< Gentoo configuration >>>" "${CMAKE_USE_DIR}"/CMakeLists.txt && return 0
261
262         # Comment out all set (<some_should_be_user_defined_variable> value)
263         find "${CMAKE_USE_DIR}" -name CMakeLists.txt -exec sed \
264                 -e '/^[[:space:]]*set[[:space:]]*([[:space:]]*CMAKE_BUILD_TYPE\([[:space:]].*)\|)\)/I{s/^/#_cmake_modify_IGNORE /g}' \
265                 -e '/^[[:space:]]*set[[:space:]]*([[:space:]]*CMAKE_COLOR_MAKEFILE[[:space:]].*)/I{s/^/#_cmake_modify_IGNORE /g}' \
266                 -e '/^[[:space:]]*set[[:space:]]*([[:space:]]*CMAKE_INSTALL_PREFIX[[:space:]].*)/I{s/^/#_cmake_modify_IGNORE /g}' \
267                 -e '/^[[:space:]]*set[[:space:]]*([[:space:]]*CMAKE_VERBOSE_MAKEFILE[[:space:]].*)/I{s/^/#_cmake_modify_IGNORE /g}' \
268                 -i {} + || die "${LINENO}: failed to disable hardcoded settings"
269         local x
270         for x in $(find "${CMAKE_USE_DIR}" -name CMakeLists.txt -exec grep -l "^#_cmake_modify_IGNORE" {} +;); do
271                 einfo "Hardcoded definition(s) removed in $(echo "${x}" | cut -c $((${#CMAKE_USE_DIR}+2))-):"
272                 einfo "$(grep -se '^#_cmake_modify_IGNORE' ${x} | cut -c 22-99)"
273         done
274
275         # NOTE Append some useful summary here
276         cat >> "${CMAKE_USE_DIR}"/CMakeLists.txt <<- _EOF_ || die
277
278                 MESSAGE(STATUS "<<< Gentoo configuration >>>
279                 Build type      \${CMAKE_BUILD_TYPE}
280                 Install path    \${CMAKE_INSTALL_PREFIX}
281                 Compiler flags:
282                 C               \${CMAKE_C_FLAGS}
283                 C++             \${CMAKE_CXX_FLAGS}
284                 Linker flags:
285                 Executable      \${CMAKE_EXE_LINKER_FLAGS}
286                 Module          \${CMAKE_MODULE_LINKER_FLAGS}
287                 Shared          \${CMAKE_SHARED_LINKER_FLAGS}\n")
288         _EOF_
289 }
290
291 # @FUNCTION: cmake_src_prepare
292 # @DESCRIPTION:
293 # Apply ebuild and user patches.
294 cmake_src_prepare() {
295         debug-print-function ${FUNCNAME} "$@"
296
297         # FIXME: workaround from cmake-utils; use current working directory instead, bug #704524
298         # esp. test with 'special' pkgs like: app-arch/brotli, media-gfx/gmic, net-libs/quiche
299         pushd "${S}" > /dev/null || die
300
301         default_src_prepare
302         _cmake_check_build_dir
303
304         # check if CMakeLists.txt exist and if no then die
305         if [[ ! -e ${CMAKE_USE_DIR}/CMakeLists.txt ]] ; then
306                 eerror "Unable to locate CMakeLists.txt under:"
307                 eerror "\"${CMAKE_USE_DIR}/CMakeLists.txt\""
308                 eerror "Consider not inheriting the cmake eclass."
309                 die "FATAL: Unable to find CMakeLists.txt"
310         fi
311
312         # if ninja is enabled but not installed, the build could fail
313         # this could happen if ninja is manually enabled (eg. make.conf) but not installed
314         if [[ ${CMAKE_MAKEFILE_GENERATOR} == ninja ]] && ! has_version -b dev-util/ninja; then
315                 eerror "CMAKE_MAKEFILE_GENERATOR is set to ninja, but ninja is not installed."
316                 die "Please install dev-util/ninja or unset CMAKE_MAKEFILE_GENERATOR."
317         fi
318
319         local modules_list
320         if [[ $(declare -p CMAKE_REMOVE_MODULES_LIST) == "declare -a"* ]]; then
321                 modules_list=( "${CMAKE_REMOVE_MODULES_LIST[@]}" )
322         else
323                 modules_list=( ${CMAKE_REMOVE_MODULES_LIST} )
324         fi
325
326         local name
327         for name in "${modules_list[@]}" ; do
328                 find "${S}" -name ${name}.cmake -exec rm -v {} + || die
329         done
330
331         # Remove dangerous things.
332         _cmake_modify-cmakelists
333
334         popd > /dev/null || die
335
336         # make ${S} read-only in order to detect broken build-systems
337         if [[ ${CMAKE_QA_SRC_DIR_READONLY} && ! ${CMAKE_IN_SOURCE_BUILD} ]]; then
338                 chmod -R a-w "${S}"
339         fi
340
341         _CMAKE_SRC_PREPARE_HAS_RUN=1
342 }
343
344 # @VARIABLE: mycmakeargs
345 # @DEFAULT_UNSET
346 # @DESCRIPTION:
347 # Optional cmake defines as a bash array. Should be defined before calling
348 # src_configure.
349 # @CODE
350 # src_configure() {
351 #       local mycmakeargs=(
352 #               $(cmake_use_with openconnect)
353 #       )
354 #
355 #       cmake_src_configure
356 # }
357 # @CODE
358
359 # @FUNCTION: cmake_src_configure
360 # @DESCRIPTION:
361 # General function for configuring with cmake. Default behaviour is to start an
362 # out-of-source build.
363 cmake_src_configure() {
364         debug-print-function ${FUNCNAME} "$@"
365
366         [[ ${_CMAKE_SRC_PREPARE_HAS_RUN} ]] || \
367                 die "FATAL: cmake_src_prepare has not been run"
368
369         _cmake_check_build_dir
370
371         # Fix xdg collision with sandbox
372         xdg_environment_reset
373
374         # @SEE CMAKE_BUILD_TYPE
375         if [[ ${CMAKE_BUILD_TYPE} = Gentoo ]]; then
376                 # Handle release builds
377                 if ! in_iuse debug || ! use debug; then
378                         local CPPFLAGS=${CPPFLAGS}
379                         append-cppflags -DNDEBUG
380                 fi
381         fi
382
383         # Prepare Gentoo override rules (set valid compiler, append CPPFLAGS etc.)
384         local build_rules=${BUILD_DIR}/gentoo_rules.cmake
385
386         cat > "${build_rules}" <<- _EOF_ || die
387                 SET (CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "ASM compile command" FORCE)
388                 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)
389                 SET (CMAKE_ASM-ATT_LINK_FLAGS "-nostdlib" CACHE STRING "ASM-ATT link flags" FORCE)
390                 SET (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C compile command" FORCE)
391                 SET (CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C++ compile command" FORCE)
392                 SET (CMAKE_Fortran_COMPILE_OBJECT "<CMAKE_Fortran_COMPILER> <DEFINES> <INCLUDES> ${FCFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "Fortran compile command" FORCE)
393         _EOF_
394
395         local myCC=$(tc-getCC) myCXX=$(tc-getCXX) myFC=$(tc-getFC)
396
397         # !!! IMPORTANT NOTE !!!
398         # Single slash below is intentional. CMake is weird and wants the
399         # CMAKE_*_VARIABLES split into two elements: the first one with
400         # compiler path, and the second one with all command-line options,
401         # space separated.
402         local toolchain_file=${BUILD_DIR}/gentoo_toolchain.cmake
403         cat > ${toolchain_file} <<- _EOF_ || die
404                 SET (CMAKE_ASM_COMPILER "${myCC/ /;}")
405                 SET (CMAKE_ASM-ATT_COMPILER "${myCC/ /;}")
406                 SET (CMAKE_C_COMPILER "${myCC/ /;}")
407                 SET (CMAKE_CXX_COMPILER "${myCXX/ /;}")
408                 SET (CMAKE_Fortran_COMPILER "${myFC/ /;}")
409                 SET (CMAKE_AR $(type -P $(tc-getAR)) CACHE FILEPATH "Archive manager" FORCE)
410                 SET (CMAKE_RANLIB $(type -P $(tc-getRANLIB)) CACHE FILEPATH "Archive index generator" FORCE)
411                 SET (CMAKE_SYSTEM_PROCESSOR "${CHOST%%-*}")
412         _EOF_
413
414         # We are using the C compiler for assembly by default.
415         local -x ASMFLAGS=${CFLAGS}
416         local -x PKG_CONFIG=$(tc-getPKG_CONFIG)
417
418         if tc-is-cross-compiler; then
419                 local sysname
420                 case "${KERNEL:-linux}" in
421                         Cygwin) sysname="CYGWIN_NT-5.1" ;;
422                         HPUX) sysname="HP-UX" ;;
423                         linux) sysname="Linux" ;;
424                         Winnt)
425                                 sysname="Windows"
426                                 cat >> "${toolchain_file}" <<- _EOF_ || die
427                                         SET (CMAKE_RC_COMPILER $(tc-getRC))
428                                 _EOF_
429                                 ;;
430                         *) sysname="${KERNEL}" ;;
431                 esac
432
433                 cat >> "${toolchain_file}" <<- _EOF_ || die
434                         SET (CMAKE_SYSTEM_NAME "${sysname}")
435                 _EOF_
436
437                 if [ "${SYSROOT:-/}" != "/" ] ; then
438                         # When cross-compiling with a sysroot (e.g. with crossdev's emerge wrappers)
439                         # we need to tell cmake to use libs/headers from the sysroot but programs from / only.
440                         cat >> "${toolchain_file}" <<- _EOF_ || die
441                                 SET (CMAKE_FIND_ROOT_PATH "${SYSROOT}")
442                                 SET (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
443                                 SET (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
444                                 SET (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
445                         _EOF_
446                 fi
447         fi
448
449         if use prefix-guest; then
450                 cat >> "${build_rules}" <<- _EOF_ || die
451                         # in Prefix we need rpath and must ensure cmake gets our default linker path
452                         # right ... except for Darwin hosts
453                         IF (NOT APPLE)
454                         SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
455                         SET (CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH "${EPREFIX}/usr/${CHOST}/lib/gcc;${EPREFIX}/usr/${CHOST}/lib;${EPREFIX}/usr/$(get_libdir);${EPREFIX}/$(get_libdir)"
456                         CACHE STRING "" FORCE)
457
458                         ELSE ()
459
460                         SET (CMAKE_PREFIX_PATH "${EPREFIX}/usr" CACHE STRING "" FORCE)
461                         SET (CMAKE_MACOSX_RPATH ON CACHE BOOL "" FORCE)
462                         SET (CMAKE_SKIP_BUILD_RPATH OFF CACHE BOOL "" FORCE)
463                         SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
464                         SET (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOL "" FORCE)
465
466                         ENDIF (NOT APPLE)
467                 _EOF_
468         fi
469
470         # Common configure parameters (invariants)
471         local common_config=${BUILD_DIR}/gentoo_common_config.cmake
472         local libdir=$(get_libdir)
473         cat > "${common_config}" <<- _EOF_ || die
474                 SET (CMAKE_GENTOO_BUILD ON CACHE BOOL "Indicate Gentoo package build")
475                 SET (LIB_SUFFIX ${libdir/lib} CACHE STRING "library path suffix" FORCE)
476                 SET (CMAKE_INSTALL_LIBDIR ${libdir} CACHE PATH "Output directory for libraries")
477                 SET (CMAKE_INSTALL_INFODIR "${EPREFIX}/usr/share/info" CACHE PATH "")
478                 SET (CMAKE_INSTALL_MANDIR "${EPREFIX}/usr/share/man" CACHE PATH "")
479                 SET (CMAKE_USER_MAKE_RULES_OVERRIDE "${build_rules}" CACHE FILEPATH "Gentoo override rules")
480                 SET (CMAKE_INSTALL_DOCDIR "${EPREFIX}/usr/share/doc/${PF}" CACHE PATH "")
481                 SET (BUILD_SHARED_LIBS ON CACHE BOOL "")
482         _EOF_
483         if [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]]; then
484                 echo 'SET (CMAKE_COLOR_MAKEFILE OFF CACHE BOOL "pretty colors during make" FORCE)' >> "${common_config}" || die
485         fi
486
487         # Wipe the default optimization flags out of CMake
488         if [[ ${CMAKE_BUILD_TYPE} != Gentoo ]]; then
489                 cat >> ${common_config} <<- _EOF_ || die
490                         SET (CMAKE_ASM_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
491                         SET (CMAKE_ASM-ATT_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
492                         SET (CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
493                         SET (CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
494                         SET (CMAKE_Fortran_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
495                         SET (CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
496                         SET (CMAKE_MODULE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
497                         SET (CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
498                         SET (CMAKE_STATIC_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
499                 _EOF_
500         fi
501
502         # Make the array a local variable since <=portage-2.1.6.x does not support
503         # global arrays (see bug #297255). But first make sure it is initialised.
504         [[ -z ${mycmakeargs} ]] && declare -a mycmakeargs=()
505         local mycmakeargstype=$(declare -p mycmakeargs 2>&-)
506         if [[ "${mycmakeargstype}" != "declare -a mycmakeargs="* ]]; then
507                 die "mycmakeargs must be declared as array"
508         fi
509
510         local mycmakeargs_local=( "${mycmakeargs[@]}" )
511
512         local warn_unused_cli=""
513         if [[ ${CMAKE_WARN_UNUSED_CLI} == no ]] ; then
514                 warn_unused_cli="--no-warn-unused-cli"
515         fi
516
517         local generator_name
518         case ${CMAKE_MAKEFILE_GENERATOR} in
519                 ninja) generator_name="Ninja" ;;
520                 emake) generator_name="Unix Makefiles" ;;
521         esac
522
523         # Common configure parameters (overridable)
524         # NOTE CMAKE_BUILD_TYPE can be only overridden via CMAKE_BUILD_TYPE eclass variable
525         # No -DCMAKE_BUILD_TYPE=xxx definitions will be in effect.
526         local cmakeargs=(
527                 ${warn_unused_cli}
528                 -C "${common_config}"
529                 -G "${generator_name}"
530                 -DCMAKE_INSTALL_PREFIX="${EPREFIX}/usr"
531                 "${mycmakeargs_local[@]}"
532                 -DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
533                 -DCMAKE_TOOLCHAIN_FILE="${toolchain_file}"
534                 "${MYCMAKEARGS}"
535         )
536
537         if [[ -n "${CMAKE_EXTRA_CACHE_FILE}" ]] ; then
538                 cmakeargs+=( -C "${CMAKE_EXTRA_CACHE_FILE}" )
539         fi
540
541         pushd "${BUILD_DIR}" > /dev/null || die
542         debug-print "${LINENO} ${ECLASS} ${FUNCNAME}: mycmakeargs is ${mycmakeargs_local[*]}"
543         echo "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}"
544         "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}" || die "cmake failed"
545         popd > /dev/null || die
546 }
547
548 # @FUNCTION: cmake_src_compile
549 # @DESCRIPTION:
550 # General function for compiling with cmake.
551 # Automatically detects the build type. All arguments are passed to emake.
552 cmake_src_compile() {
553         debug-print-function ${FUNCNAME} "$@"
554
555         cmake_build "$@"
556 }
557
558 # @FUNCTION: cmake_build
559 # @DESCRIPTION:
560 # Function for building the package. Automatically detects the build type.
561 # All arguments are passed to emake.
562 cmake_build() {
563         debug-print-function ${FUNCNAME} "$@"
564
565         _cmake_check_build_dir
566         pushd "${BUILD_DIR}" > /dev/null || die
567
568         case ${CMAKE_MAKEFILE_GENERATOR} in
569                 emake)
570                         [[ -e Makefile ]] || die "Makefile not found. Error during configure stage."
571                         case ${CMAKE_VERBOSE} in
572                                 OFF) emake "$@" ;;
573                                 *) emake VERBOSE=1 "$@" ;;
574                         esac
575                         ;;
576                 ninja)
577                         [[ -e build.ninja ]] || die "build.ninja not found. Error during configure stage."
578                         eninja "$@"
579                         ;;
580         esac
581
582         popd > /dev/null || die
583 }
584
585 # @FUNCTION: cmake-utils_src_make
586 # @INTERNAL
587 # @DESCRIPTION:
588 # Banned. Use cmake_build instead.
589 cmake-utils_src_make() {
590         die "cmake-utils_src_make is banned. Use cmake_build instead"
591 }
592
593 # @FUNCTION: cmake_src_test
594 # @DESCRIPTION:
595 # Function for testing the package. Automatically detects the build type.
596 cmake_src_test() {
597         debug-print-function ${FUNCNAME} "$@"
598
599         _cmake_check_build_dir
600         pushd "${BUILD_DIR}" > /dev/null || die
601         [[ -e CTestTestfile.cmake ]] || { echo "No tests found. Skipping."; return 0 ; }
602
603         [[ -n ${TEST_VERBOSE} ]] && myctestargs+=( --extra-verbose --output-on-failure )
604
605         set -- ctest -j "$(makeopts_jobs)" --test-load "$(makeopts_loadavg)" "${myctestargs[@]}" "$@"
606         echo "$@" >&2
607         if "$@" ; then
608                 einfo "Tests succeeded."
609                 popd > /dev/null || die
610                 return 0
611         else
612                 if [[ -n "${CMAKE_YES_I_WANT_TO_SEE_THE_TEST_LOG}" ]] ; then
613                         # on request from Diego
614                         eerror "Tests failed. Test log ${BUILD_DIR}/Testing/Temporary/LastTest.log follows:"
615                         eerror "--START TEST LOG--------------------------------------------------------------"
616                         cat "${BUILD_DIR}/Testing/Temporary/LastTest.log"
617                         eerror "--END TEST LOG----------------------------------------------------------------"
618                         die "Tests failed."
619                 else
620                         die "Tests failed. When you file a bug, please attach the following file: \n\t${BUILD_DIR}/Testing/Temporary/LastTest.log"
621                 fi
622
623                 # die might not die due to nonfatal
624                 popd > /dev/null || die
625                 return 1
626         fi
627 }
628
629 # @FUNCTION: cmake_src_install
630 # @DESCRIPTION:
631 # Function for installing the package. Automatically detects the build type.
632 cmake_src_install() {
633         debug-print-function ${FUNCNAME} "$@"
634
635         _cmake_check_build_dir
636         pushd "${BUILD_DIR}" > /dev/null || die
637         DESTDIR="${D}" ${CMAKE_MAKEFILE_GENERATOR} install "$@" ||
638                 die "died running ${CMAKE_MAKEFILE_GENERATOR} install"
639         popd > /dev/null || die
640
641         pushd "${S}" > /dev/null || die
642         einstalldocs
643         popd > /dev/null || die
644 }
645
646 fi