cmake.eclass: Fix fatal typo
[gentoo.git] / eclass / cmake.eclass
1 # Copyright 1999-2019 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         pushd "${S}" > /dev/null || die
298
299         default_src_prepare
300         _cmake_check_build_dir
301
302         # check if CMakeLists.txt exist and if no then die
303         if [[ ! -e ${CMAKE_USE_DIR}/CMakeLists.txt ]] ; then
304                 eerror "Unable to locate CMakeLists.txt under:"
305                 eerror "\"${CMAKE_USE_DIR}/CMakeLists.txt\""
306                 eerror "Consider not inheriting the cmake eclass."
307                 die "FATAL: Unable to find CMakeLists.txt"
308         fi
309
310         # if ninja is enabled but not installed, the build could fail
311         # this could happen if ninja is manually enabled (eg. make.conf) but not installed
312         if [[ ${CMAKE_MAKEFILE_GENERATOR} == ninja ]] && ! has_version -b dev-util/ninja; then
313                 eerror "CMAKE_MAKEFILE_GENERATOR is set to ninja, but ninja is not installed."
314                 die "Please install dev-util/ninja or unset CMAKE_MAKEFILE_GENERATOR."
315         fi
316
317         local modules_list
318         if [[ $(declare -p CMAKE_REMOVE_MODULES_LIST) == "declare -a"* ]]; then
319                 modules_list=( "${CMAKE_REMOVE_MODULES_LIST[@]}" )
320         else
321                 modules_list=( ${CMAKE_REMOVE_MODULES_LIST} )
322         fi
323
324         local name
325         for name in "${modules_list[@]}" ; do
326                 find "${S}" -name ${name}.cmake -exec rm -v {} + || die
327         done
328
329         # Remove dangerous things.
330         _cmake_modify-cmakelists
331
332         popd > /dev/null || die
333
334         # make ${S} read-only in order to detect broken build-systems
335         if [[ ${CMAKE_QA_SRC_DIR_READONLY} && ! ${CMAKE_IN_SOURCE_BUILD} ]]; then
336                 chmod -R a-w "${S}"
337         fi
338
339         _CMAKE_SRC_PREPARE_HAS_RUN=1
340 }
341
342 # @VARIABLE: mycmakeargs
343 # @DEFAULT_UNSET
344 # @DESCRIPTION:
345 # Optional cmake defines as a bash array. Should be defined before calling
346 # src_configure.
347 # @CODE
348 # src_configure() {
349 #       local mycmakeargs=(
350 #               $(cmake_use_with openconnect)
351 #       )
352 #
353 #       cmake_src_configure
354 # }
355 # @CODE
356
357 # @FUNCTION: cmake_src_configure
358 # @DESCRIPTION:
359 # General function for configuring with cmake. Default behaviour is to start an
360 # out-of-source build.
361 cmake_src_configure() {
362         debug-print-function ${FUNCNAME} "$@"
363
364         [[ ${_CMAKE_SRC_PREPARE_HAS_RUN} ]] || \
365                 die "FATAL: cmake_src_prepare has not been run"
366
367         _cmake_check_build_dir
368
369         # Fix xdg collision with sandbox
370         xdg_environment_reset
371
372         # @SEE CMAKE_BUILD_TYPE
373         if [[ ${CMAKE_BUILD_TYPE} = Gentoo ]]; then
374                 # Handle release builds
375                 if ! in_iuse debug || ! use debug; then
376                         local CPPFLAGS=${CPPFLAGS}
377                         append-cppflags -DNDEBUG
378                 fi
379         fi
380
381         # Prepare Gentoo override rules (set valid compiler, append CPPFLAGS etc.)
382         local build_rules=${BUILD_DIR}/gentoo_rules.cmake
383
384         cat > "${build_rules}" <<- _EOF_ || die
385                 SET (CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "ASM compile command" FORCE)
386                 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)
387                 SET (CMAKE_ASM-ATT_LINK_FLAGS "-nostdlib" CACHE STRING "ASM-ATT link flags" FORCE)
388                 SET (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C compile command" FORCE)
389                 SET (CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C++ compile command" FORCE)
390                 SET (CMAKE_Fortran_COMPILE_OBJECT "<CMAKE_Fortran_COMPILER> <DEFINES> <INCLUDES> ${FCFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "Fortran compile command" FORCE)
391         _EOF_
392
393         local myCC=$(tc-getCC) myCXX=$(tc-getCXX) myFC=$(tc-getFC)
394
395         # !!! IMPORTANT NOTE !!!
396         # Single slash below is intentional. CMake is weird and wants the
397         # CMAKE_*_VARIABLES split into two elements: the first one with
398         # compiler path, and the second one with all command-line options,
399         # space separated.
400         local toolchain_file=${BUILD_DIR}/gentoo_toolchain.cmake
401         cat > ${toolchain_file} <<- _EOF_ || die
402                 SET (CMAKE_ASM_COMPILER "${myCC/ /;}")
403                 SET (CMAKE_ASM-ATT_COMPILER "${myCC/ /;}")
404                 SET (CMAKE_C_COMPILER "${myCC/ /;}")
405                 SET (CMAKE_CXX_COMPILER "${myCXX/ /;}")
406                 SET (CMAKE_Fortran_COMPILER "${myFC/ /;}")
407                 SET (CMAKE_AR $(type -P $(tc-getAR)) CACHE FILEPATH "Archive manager" FORCE)
408                 SET (CMAKE_RANLIB $(type -P $(tc-getRANLIB)) CACHE FILEPATH "Archive index generator" FORCE)
409                 SET (CMAKE_SYSTEM_PROCESSOR "${CHOST%%-*}")
410         _EOF_
411
412         # We are using the C compiler for assembly by default.
413         local -x ASMFLAGS=${CFLAGS}
414         local -x PKG_CONFIG=$(tc-getPKG_CONFIG)
415
416         if tc-is-cross-compiler; then
417                 local sysname
418                 case "${KERNEL:-linux}" in
419                         Cygwin) sysname="CYGWIN_NT-5.1" ;;
420                         HPUX) sysname="HP-UX" ;;
421                         linux) sysname="Linux" ;;
422                         Winnt)
423                                 sysname="Windows"
424                                 cat >> "${toolchain_file}" <<- _EOF_ || die
425                                         SET (CMAKE_RC_COMPILER $(tc-getRC))
426                                 _EOF_
427                                 ;;
428                         *) sysname="${KERNEL}" ;;
429                 esac
430
431                 cat >> "${toolchain_file}" <<- _EOF_ || die
432                         SET (CMAKE_SYSTEM_NAME "${sysname}")
433                 _EOF_
434
435                 if [ "${SYSROOT:-/}" != "/" ] ; then
436                         # When cross-compiling with a sysroot (e.g. with crossdev's emerge wrappers)
437                         # we need to tell cmake to use libs/headers from the sysroot but programs from / only.
438                         cat >> "${toolchain_file}" <<- _EOF_ || die
439                                 SET (CMAKE_FIND_ROOT_PATH "${SYSROOT}")
440                                 SET (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
441                                 SET (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
442                                 SET (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
443                         _EOF_
444                 fi
445         fi
446
447         if use prefix-guest; then
448                 cat >> "${build_rules}" <<- _EOF_ || die
449                         # in Prefix we need rpath and must ensure cmake gets our default linker path
450                         # right ... except for Darwin hosts
451                         IF (NOT APPLE)
452                         SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
453                         SET (CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH "${EPREFIX}/usr/${CHOST}/lib/gcc;${EPREFIX}/usr/${CHOST}/lib;${EPREFIX}/usr/$(get_libdir);${EPREFIX}/$(get_libdir)"
454                         CACHE STRING "" FORCE)
455
456                         ELSE ()
457
458                         SET (CMAKE_PREFIX_PATH "${EPREFIX}/usr" CACHE STRING "" FORCE)
459                         SET (CMAKE_MACOSX_RPATH ON CACHE BOOL "" FORCE)
460                         SET (CMAKE_SKIP_BUILD_RPATH OFF CACHE BOOL "" FORCE)
461                         SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
462                         SET (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOL "" FORCE)
463
464                         ENDIF (NOT APPLE)
465                 _EOF_
466         fi
467
468         # Common configure parameters (invariants)
469         local common_config=${BUILD_DIR}/gentoo_common_config.cmake
470         local libdir=$(get_libdir)
471         cat > "${common_config}" <<- _EOF_ || die
472                 SET (CMAKE_GENTOO_BUILD ON CACHE BOOL "Indicate Gentoo package build")
473                 SET (LIB_SUFFIX ${libdir/lib} CACHE STRING "library path suffix" FORCE)
474                 SET (CMAKE_INSTALL_LIBDIR ${libdir} CACHE PATH "Output directory for libraries")
475                 SET (CMAKE_INSTALL_INFODIR "${EPREFIX}/usr/share/info" CACHE PATH "")
476                 SET (CMAKE_INSTALL_MANDIR "${EPREFIX}/usr/share/man" CACHE PATH "")
477                 SET (CMAKE_USER_MAKE_RULES_OVERRIDE "${build_rules}" CACHE FILEPATH "Gentoo override rules")
478                 SET (CMAKE_INSTALL_DOCDIR "${EPREFIX}/usr/share/doc/${PF}" CACHE PATH "")
479                 SET (BUILD_SHARED_LIBS ON CACHE BOOL "")
480         _EOF_
481         if [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]]; then
482                 echo 'SET (CMAKE_COLOR_MAKEFILE OFF CACHE BOOL "pretty colors during make" FORCE)' >> "${common_config}" || die
483         fi
484
485         # Wipe the default optimization flags out of CMake
486         if [[ ${CMAKE_BUILD_TYPE} != Gentoo ]]; then
487                 cat >> ${common_config} <<- _EOF_ || die
488                         SET (CMAKE_ASM_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
489                         SET (CMAKE_ASM-ATT_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
490                         SET (CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
491                         SET (CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
492                         SET (CMAKE_Fortran_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
493                         SET (CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
494                         SET (CMAKE_MODULE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
495                         SET (CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
496                         SET (CMAKE_STATIC_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
497                 _EOF_
498         fi
499
500         # Make the array a local variable since <=portage-2.1.6.x does not support
501         # global arrays (see bug #297255). But first make sure it is initialised.
502         [[ -z ${mycmakeargs} ]] && declare -a mycmakeargs=()
503         local mycmakeargstype=$(declare -p mycmakeargs 2>&-)
504         if [[ "${mycmakeargstype}" != "declare -a mycmakeargs="* ]]; then
505                 die "mycmakeargs must be declared as array"
506         fi
507
508         local mycmakeargs_local=( "${mycmakeargs[@]}" )
509
510         local warn_unused_cli=""
511         if [[ ${CMAKE_WARN_UNUSED_CLI} == no ]] ; then
512                 warn_unused_cli="--no-warn-unused-cli"
513         fi
514
515         local generator_name
516         case ${CMAKE_MAKEFILE_GENERATOR} in
517                 ninja) generator_name="Ninja" ;;
518                 emake) generator_name="Unix Makefiles" ;;
519         esac
520
521         # Common configure parameters (overridable)
522         # NOTE CMAKE_BUILD_TYPE can be only overridden via CMAKE_BUILD_TYPE eclass variable
523         # No -DCMAKE_BUILD_TYPE=xxx definitions will be in effect.
524         local cmakeargs=(
525                 ${warn_unused_cli}
526                 -C "${common_config}"
527                 -G "${generator_name}"
528                 -DCMAKE_INSTALL_PREFIX="${EPREFIX}/usr"
529                 "${mycmakeargs_local[@]}"
530                 -DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
531                 -DCMAKE_TOOLCHAIN_FILE="${toolchain_file}"
532                 "${MYCMAKEARGS}"
533         )
534
535         if [[ -n "${CMAKE_EXTRA_CACHE_FILE}" ]] ; then
536                 cmakeargs+=( -C "${CMAKE_EXTRA_CACHE_FILE}" )
537         fi
538
539         pushd "${BUILD_DIR}" > /dev/null || die
540         debug-print "${LINENO} ${ECLASS} ${FUNCNAME}: mycmakeargs is ${mycmakeargs_local[*]}"
541         echo "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}"
542         "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}" || die "cmake failed"
543         popd > /dev/null || die
544 }
545
546 # @FUNCTION: cmake_src_compile
547 # @DESCRIPTION:
548 # General function for compiling with cmake.
549 # Automatically detects the build type. All arguments are passed to emake.
550 cmake_src_compile() {
551         debug-print-function ${FUNCNAME} "$@"
552
553         cmake_build "$@"
554 }
555
556 # @FUNCTION: cmake_build
557 # @DESCRIPTION:
558 # Function for building the package. Automatically detects the build type.
559 # All arguments are passed to emake.
560 cmake_build() {
561         debug-print-function ${FUNCNAME} "$@"
562
563         _cmake_check_build_dir
564         pushd "${BUILD_DIR}" > /dev/null || die
565
566         case ${CMAKE_MAKEFILE_GENERATOR} in
567                 emake)
568                         [[ -e Makefile ]] || die "Makefile not found. Error during configure stage."
569                         [[ "${CMAKE_VERBOSE}" != "OFF" ]] && local verbosity="VERBOSE=1"
570                         emake "${verbosity} "$@"
571                         ;;
572                 ninja)
573                         [[ -e build.ninja ]] || die "build.ninja not found. Error during configure stage."
574                         eninja "$@"
575                         ;;
576         esac
577
578         popd > /dev/null || die
579 }
580
581 # @FUNCTION: cmake-utils_src_make
582 # @INTERNAL
583 # @DESCRIPTION:
584 # Banned. Use cmake_build instead.
585 cmake-utils_src_make() {
586         die "cmake_src_make is banned. Use cmake_build instead"
587 }
588
589 # @FUNCTION: cmake_src_test
590 # @DESCRIPTION:
591 # Function for testing the package. Automatically detects the build type.
592 cmake_src_test() {
593         debug-print-function ${FUNCNAME} "$@"
594
595         _cmake_check_build_dir
596         pushd "${BUILD_DIR}" > /dev/null || die
597         [[ -e CTestTestfile.cmake ]] || { echo "No tests found. Skipping."; return 0 ; }
598
599         [[ -n ${TEST_VERBOSE} ]] && myctestargs+=( --extra-verbose --output-on-failure )
600
601         set -- ctest -j "$(makeopts_jobs)" --test-load "$(makeopts_loadavg)" "${myctestargs[@]}" "$@"
602         echo "$@" >&2
603         if "$@" ; then
604                 einfo "Tests succeeded."
605                 popd > /dev/null || die
606                 return 0
607         else
608                 if [[ -n "${CMAKE_YES_I_WANT_TO_SEE_THE_TEST_LOG}" ]] ; then
609                         # on request from Diego
610                         eerror "Tests failed. Test log ${BUILD_DIR}/Testing/Temporary/LastTest.log follows:"
611                         eerror "--START TEST LOG--------------------------------------------------------------"
612                         cat "${BUILD_DIR}/Testing/Temporary/LastTest.log"
613                         eerror "--END TEST LOG----------------------------------------------------------------"
614                         die "Tests failed."
615                 else
616                         die "Tests failed. When you file a bug, please attach the following file: \n\t${BUILD_DIR}/Testing/Temporary/LastTest.log"
617                 fi
618
619                 # die might not die due to nonfatal
620                 popd > /dev/null || die
621                 return 1
622         fi
623 }
624
625 # @FUNCTION: cmake_src_install
626 # @DESCRIPTION:
627 # Function for installing the package. Automatically detects the build type.
628 cmake_src_install() {
629         debug-print-function ${FUNCNAME} "$@"
630
631         _cmake_check_build_dir
632         pushd "${BUILD_DIR}" > /dev/null || die
633         DESTDIR="${D}" ${CMAKE_MAKEFILE_GENERATOR} install "$@" ||
634                 die "died running ${CMAKE_MAKEFILE_GENERATOR} install"
635         popd > /dev/null || die
636
637         pushd "${S}" > /dev/null || die
638         einstalldocs
639         popd > /dev/null || die
640 }
641
642 fi