media-fonts/noto-emoji: add ~ppc64 keyword
[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         # Prepare Gentoo override rules (set valid compiler, append CPPFLAGS etc.)
375         local build_rules=${BUILD_DIR}/gentoo_rules.cmake
376
377         cat > "${build_rules}" <<- _EOF_ || die
378                 SET (CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "ASM compile command" FORCE)
379                 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)
380                 SET (CMAKE_ASM-ATT_LINK_FLAGS "-nostdlib" CACHE STRING "ASM-ATT link flags" FORCE)
381                 SET (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C compile command" FORCE)
382                 SET (CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C++ compile command" FORCE)
383                 SET (CMAKE_Fortran_COMPILE_OBJECT "<CMAKE_Fortran_COMPILER> <DEFINES> <INCLUDES> ${FCFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "Fortran compile command" FORCE)
384         _EOF_
385
386         local myCC=$(tc-getCC) myCXX=$(tc-getCXX) myFC=$(tc-getFC)
387
388         # !!! IMPORTANT NOTE !!!
389         # Single slash below is intentional. CMake is weird and wants the
390         # CMAKE_*_VARIABLES split into two elements: the first one with
391         # compiler path, and the second one with all command-line options,
392         # space separated.
393         local toolchain_file=${BUILD_DIR}/gentoo_toolchain.cmake
394         cat > ${toolchain_file} <<- _EOF_ || die
395                 SET (CMAKE_ASM_COMPILER "${myCC/ /;}")
396                 SET (CMAKE_ASM-ATT_COMPILER "${myCC/ /;}")
397                 SET (CMAKE_C_COMPILER "${myCC/ /;}")
398                 SET (CMAKE_CXX_COMPILER "${myCXX/ /;}")
399                 SET (CMAKE_Fortran_COMPILER "${myFC/ /;}")
400                 SET (CMAKE_AR $(type -P $(tc-getAR)) CACHE FILEPATH "Archive manager" FORCE)
401                 SET (CMAKE_RANLIB $(type -P $(tc-getRANLIB)) CACHE FILEPATH "Archive index generator" FORCE)
402                 SET (CMAKE_SYSTEM_PROCESSOR "${CHOST%%-*}")
403         _EOF_
404
405         # We are using the C compiler for assembly by default.
406         local -x ASMFLAGS=${CFLAGS}
407         local -x PKG_CONFIG=$(tc-getPKG_CONFIG)
408
409         if tc-is-cross-compiler; then
410                 local sysname
411                 case "${KERNEL:-linux}" in
412                         Cygwin) sysname="CYGWIN_NT-5.1" ;;
413                         HPUX) sysname="HP-UX" ;;
414                         linux) sysname="Linux" ;;
415                         Winnt)
416                                 sysname="Windows"
417                                 cat >> "${toolchain_file}" <<- _EOF_ || die
418                                         SET (CMAKE_RC_COMPILER $(tc-getRC))
419                                 _EOF_
420                                 ;;
421                         *) sysname="${KERNEL}" ;;
422                 esac
423
424                 cat >> "${toolchain_file}" <<- _EOF_ || die
425                         SET (CMAKE_SYSTEM_NAME "${sysname}")
426                 _EOF_
427
428                 if [ "${SYSROOT:-/}" != "/" ] ; then
429                         # When cross-compiling with a sysroot (e.g. with crossdev's emerge wrappers)
430                         # we need to tell cmake to use libs/headers from the sysroot but programs from / only.
431                         cat >> "${toolchain_file}" <<- _EOF_ || die
432                                 SET (CMAKE_FIND_ROOT_PATH "${SYSROOT}")
433                                 SET (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
434                                 SET (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
435                                 SET (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
436                         _EOF_
437                 fi
438         fi
439
440         if use prefix-guest; then
441                 cat >> "${build_rules}" <<- _EOF_ || die
442                         # in Prefix we need rpath and must ensure cmake gets our default linker path
443                         # right ... except for Darwin hosts
444                         IF (NOT APPLE)
445                         SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
446                         SET (CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH "${EPREFIX}/usr/${CHOST}/lib/gcc;${EPREFIX}/usr/${CHOST}/lib;${EPREFIX}/usr/$(get_libdir);${EPREFIX}/$(get_libdir)"
447                         CACHE STRING "" FORCE)
448
449                         ELSE ()
450
451                         SET (CMAKE_PREFIX_PATH "${EPREFIX}/usr" CACHE STRING "" FORCE)
452                         SET (CMAKE_MACOSX_RPATH ON CACHE BOOL "" FORCE)
453                         SET (CMAKE_SKIP_BUILD_RPATH OFF CACHE BOOL "" FORCE)
454                         SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
455                         SET (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOL "" FORCE)
456
457                         ENDIF (NOT APPLE)
458                 _EOF_
459         fi
460
461         # Common configure parameters (invariants)
462         local common_config=${BUILD_DIR}/gentoo_common_config.cmake
463         local libdir=$(get_libdir)
464         cat > "${common_config}" <<- _EOF_ || die
465                 SET (CMAKE_GENTOO_BUILD ON CACHE BOOL "Indicate Gentoo package build")
466                 SET (LIB_SUFFIX ${libdir/lib} CACHE STRING "library path suffix" FORCE)
467                 SET (CMAKE_INSTALL_LIBDIR ${libdir} CACHE PATH "Output directory for libraries")
468                 SET (CMAKE_INSTALL_INFODIR "${EPREFIX}/usr/share/info" CACHE PATH "")
469                 SET (CMAKE_INSTALL_MANDIR "${EPREFIX}/usr/share/man" CACHE PATH "")
470                 SET (CMAKE_USER_MAKE_RULES_OVERRIDE "${build_rules}" CACHE FILEPATH "Gentoo override rules")
471                 SET (CMAKE_INSTALL_DOCDIR "${EPREFIX}/usr/share/doc/${PF}" CACHE PATH "")
472                 SET (BUILD_SHARED_LIBS ON CACHE BOOL "")
473         _EOF_
474         if [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]]; then
475                 echo 'SET (CMAKE_COLOR_MAKEFILE OFF CACHE BOOL "pretty colors during make" FORCE)' >> "${common_config}" || die
476         fi
477
478         # Wipe the default optimization flags out of CMake
479         if [[ ${CMAKE_BUILD_TYPE} != Gentoo ]]; then
480                 cat >> ${common_config} <<- _EOF_ || die
481                         SET (CMAKE_ASM_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
482                         SET (CMAKE_ASM-ATT_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
483                         SET (CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
484                         SET (CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
485                         SET (CMAKE_Fortran_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
486                         SET (CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
487                         SET (CMAKE_MODULE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
488                         SET (CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
489                         SET (CMAKE_STATIC_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
490                 _EOF_
491         fi
492
493         # Make the array a local variable since <=portage-2.1.6.x does not support
494         # global arrays (see bug #297255). But first make sure it is initialised.
495         [[ -z ${mycmakeargs} ]] && declare -a mycmakeargs=()
496         local mycmakeargstype=$(declare -p mycmakeargs 2>&-)
497         if [[ "${mycmakeargstype}" != "declare -a mycmakeargs="* ]]; then
498                 die "mycmakeargs must be declared as array"
499         fi
500
501         local mycmakeargs_local=( "${mycmakeargs[@]}" )
502
503         local warn_unused_cli=""
504         if [[ ${CMAKE_WARN_UNUSED_CLI} == no ]] ; then
505                 warn_unused_cli="--no-warn-unused-cli"
506         fi
507
508         local generator_name
509         case ${CMAKE_MAKEFILE_GENERATOR} in
510                 ninja) generator_name="Ninja" ;;
511                 emake) generator_name="Unix Makefiles" ;;
512         esac
513
514         # Common configure parameters (overridable)
515         # NOTE CMAKE_BUILD_TYPE can be only overridden via CMAKE_BUILD_TYPE eclass variable
516         # No -DCMAKE_BUILD_TYPE=xxx definitions will be in effect.
517         local cmakeargs=(
518                 ${warn_unused_cli}
519                 -C "${common_config}"
520                 -G "${generator_name}"
521                 -DCMAKE_INSTALL_PREFIX="${EPREFIX}/usr"
522                 "${mycmakeargs_local[@]}"
523                 -DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
524                 -DCMAKE_TOOLCHAIN_FILE="${toolchain_file}"
525                 "${MYCMAKEARGS}"
526         )
527
528         if [[ -n "${CMAKE_EXTRA_CACHE_FILE}" ]] ; then
529                 cmakeargs+=( -C "${CMAKE_EXTRA_CACHE_FILE}" )
530         fi
531
532         pushd "${BUILD_DIR}" > /dev/null || die
533         debug-print "${LINENO} ${ECLASS} ${FUNCNAME}: mycmakeargs is ${mycmakeargs_local[*]}"
534         echo "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}"
535         "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}" || die "cmake failed"
536         popd > /dev/null || die
537 }
538
539 # @FUNCTION: cmake_src_compile
540 # @DESCRIPTION:
541 # General function for compiling with cmake.
542 # Automatically detects the build type. All arguments are passed to emake.
543 cmake_src_compile() {
544         debug-print-function ${FUNCNAME} "$@"
545
546         cmake_build "$@"
547 }
548
549 # @FUNCTION: cmake_build
550 # @DESCRIPTION:
551 # Function for building the package. Automatically detects the build type.
552 # All arguments are passed to emake.
553 cmake_build() {
554         debug-print-function ${FUNCNAME} "$@"
555
556         _cmake_check_build_dir
557         pushd "${BUILD_DIR}" > /dev/null || die
558
559         case ${CMAKE_MAKEFILE_GENERATOR} in
560                 emake)
561                         [[ -e Makefile ]] || die "Makefile not found. Error during configure stage."
562                         case ${CMAKE_VERBOSE} in
563                                 OFF) emake "$@" ;;
564                                 *) emake VERBOSE=1 "$@" ;;
565                         esac
566                         ;;
567                 ninja)
568                         [[ -e build.ninja ]] || die "build.ninja not found. Error during configure stage."
569                         eninja "$@"
570                         ;;
571         esac
572
573         popd > /dev/null || die
574 }
575
576 # @FUNCTION: cmake-utils_src_make
577 # @INTERNAL
578 # @DESCRIPTION:
579 # Banned. Use cmake_build instead.
580 cmake-utils_src_make() {
581         die "cmake-utils_src_make is banned. Use cmake_build instead"
582 }
583
584 # @FUNCTION: cmake_src_test
585 # @DESCRIPTION:
586 # Function for testing the package. Automatically detects the build type.
587 cmake_src_test() {
588         debug-print-function ${FUNCNAME} "$@"
589
590         _cmake_check_build_dir
591         pushd "${BUILD_DIR}" > /dev/null || die
592         [[ -e CTestTestfile.cmake ]] || { echo "No tests found. Skipping."; return 0 ; }
593
594         [[ -n ${TEST_VERBOSE} ]] && myctestargs+=( --extra-verbose --output-on-failure )
595
596         set -- ctest -j "$(makeopts_jobs)" --test-load "$(makeopts_loadavg)" "${myctestargs[@]}" "$@"
597         echo "$@" >&2
598         if "$@" ; then
599                 einfo "Tests succeeded."
600                 popd > /dev/null || die
601                 return 0
602         else
603                 if [[ -n "${CMAKE_YES_I_WANT_TO_SEE_THE_TEST_LOG}" ]] ; then
604                         # on request from Diego
605                         eerror "Tests failed. Test log ${BUILD_DIR}/Testing/Temporary/LastTest.log follows:"
606                         eerror "--START TEST LOG--------------------------------------------------------------"
607                         cat "${BUILD_DIR}/Testing/Temporary/LastTest.log"
608                         eerror "--END TEST LOG----------------------------------------------------------------"
609                         die "Tests failed."
610                 else
611                         die "Tests failed. When you file a bug, please attach the following file: \n\t${BUILD_DIR}/Testing/Temporary/LastTest.log"
612                 fi
613
614                 # die might not die due to nonfatal
615                 popd > /dev/null || die
616                 return 1
617         fi
618 }
619
620 # @FUNCTION: cmake_src_install
621 # @DESCRIPTION:
622 # Function for installing the package. Automatically detects the build type.
623 cmake_src_install() {
624         debug-print-function ${FUNCNAME} "$@"
625
626         _cmake_check_build_dir
627         pushd "${BUILD_DIR}" > /dev/null || die
628         DESTDIR="${D}" ${CMAKE_MAKEFILE_GENERATOR} install "$@" ||
629                 die "died running ${CMAKE_MAKEFILE_GENERATOR} install"
630         popd > /dev/null || die
631
632         pushd "${S}" > /dev/null || die
633         einstalldocs
634         popd > /dev/null || die
635 }
636
637 fi