cmake-utils.eclass: EAPI future-proof inherit-stop
[gentoo.git] / eclass / cmake-utils.eclass
1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: cmake-utils.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: 5 6
13 # @BLURB: common ebuild functions for cmake-based packages
14 # @DESCRIPTION:
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.
19
20 if [[ -z ${_CMAKE_UTILS_ECLASS} ]]; then
21 _CMAKE_UTILS_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 #
30 # This variable has been called CMAKE_BUILD_DIR formerly.
31 # It is set under that name for compatibility.
32
33 # @ECLASS-VARIABLE: CMAKE_BINARY
34 # @DESCRIPTION:
35 # Eclass can use different cmake binary than the one provided in by system.
36 : ${CMAKE_BINARY:=cmake}
37
38 # @ECLASS-VARIABLE: CMAKE_BUILD_TYPE
39 # @DESCRIPTION:
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}
46
47 # @ECLASS-VARIABLE: CMAKE_IN_SOURCE_BUILD
48 # @DEFAULT_UNSET
49 # @DESCRIPTION:
50 # Set to enable in-source build.
51
52 # @ECLASS-VARIABLE: CMAKE_MAKEFILE_GENERATOR
53 # @DESCRIPTION:
54 # Specify a makefile generator to be used by cmake.
55 # At this point only "emake" and "ninja" are supported.
56 : ${CMAKE_MAKEFILE_GENERATOR:=emake}
57
58 # @ECLASS-VARIABLE: CMAKE_MIN_VERSION
59 # @DESCRIPTION:
60 # Specify the minimum required CMake version.
61 : ${CMAKE_MIN_VERSION:=3.9.6}
62
63 # @ECLASS-VARIABLE: CMAKE_REMOVE_MODULES
64 # @DESCRIPTION:
65 # Do we want to remove anything? yes or whatever else for no
66 : ${CMAKE_REMOVE_MODULES:=yes}
67
68 # @ECLASS-VARIABLE: CMAKE_REMOVE_MODULES_LIST
69 # @DESCRIPTION:
70 # Space-separated list of CMake modules that will be removed in $S during src_prepare,
71 # in order to force packages to use the system version.
72 : ${CMAKE_REMOVE_MODULES_LIST:=FindBLAS FindLAPACK}
73
74 # @ECLASS-VARIABLE: CMAKE_USE_DIR
75 # @DESCRIPTION:
76 # Sets the directory where we are working with cmake.
77 # For example when application uses autotools and only one
78 # plugin needs to be done by cmake.
79 # By default it uses ${S}.
80
81 # @ECLASS-VARIABLE: CMAKE_VERBOSE
82 # @DESCRIPTION:
83 # Set to OFF to disable verbose messages during compilation
84 : ${CMAKE_VERBOSE:=ON}
85
86 # @ECLASS-VARIABLE: CMAKE_WARN_UNUSED_CLI
87 # @DESCRIPTION:
88 # Warn about variables that are declared on the command line
89 # but not used. Might give false-positives.
90 # "no" to disable (default) or anything else to enable.
91
92 # @ECLASS-VARIABLE: CMAKE_EXTRA_CACHE_FILE
93 # @DEFAULT_UNSET
94 # @DESCRIPTION:
95 # Specifies an extra cache file to pass to cmake. This is the analog of EXTRA_ECONF
96 # for econf and is needed to pass TRY_RUN results when cross-compiling.
97 # Should be set by user in a per-package basis in /etc/portage/package.env.
98
99 # @ECLASS-VARIABLE: CMAKE_UTILS_QA_SRC_DIR_READONLY
100 # @DEFAULT_UNSET
101 # @DESCRIPTION:
102 # After running cmake-utils_src_prepare, sets ${S} to read-only. This is
103 # a user flag and should under _no circumstances_ be set in the ebuild.
104 # Helps in improving QA of build systems that write to source tree.
105
106 case ${EAPI} in
107         5) : ${CMAKE_WARN_UNUSED_CLI:=no} ;;
108         6) : ${CMAKE_WARN_UNUSED_CLI:=yes} ;;
109         *) die "EAPI=${EAPI:-0} is not supported" ;;
110 esac
111
112 inherit toolchain-funcs ninja-utils flag-o-matic multiprocessing xdg-utils
113
114 case ${EAPI} in
115         5|6) inherit eutils multilib ;;
116 esac
117
118 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
119
120 [[ ${WANT_CMAKE} ]] && eqawarn "\${WANT_CMAKE} has been removed and is a no-op now"
121 [[ ${PREFIX} ]] && die "\${PREFIX} has been removed and is a no-op now"
122
123 case ${CMAKE_MAKEFILE_GENERATOR} in
124         emake)
125                 DEPEND="sys-devel/make"
126                 ;;
127         ninja)
128                 DEPEND="dev-util/ninja"
129                 ;;
130         *)
131                 eerror "Unknown value for \${CMAKE_MAKEFILE_GENERATOR}"
132                 die "Value ${CMAKE_MAKEFILE_GENERATOR} is not supported"
133                 ;;
134 esac
135
136 if [[ ${PN} != cmake ]]; then
137         DEPEND+=" >=dev-util/cmake-${CMAKE_MIN_VERSION}"
138 fi
139
140 # Internal functions used by cmake-utils_use_*
141 _cmake_use_me_now() {
142         debug-print-function ${FUNCNAME} "$@"
143
144         local arg=$2
145         [[ ! -z $3 ]] && arg=$3
146
147         [[ ${EAPI} == 5 ]] || die "${FUNCNAME[1]} is banned in EAPI 6 and later: use -D$1<related_CMake_variable>=\"\$(usex $2)\" instead"
148
149         local uper capitalised x
150         [[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
151         if [[ ! -z $3 ]]; then
152                 # user specified the use name so use it
153                 echo "-D$1$3=$(use $2 && echo ON || echo OFF)"
154         else
155                 # use all various most used combinations
156                 uper=$(echo ${2} | tr '[:lower:]' '[:upper:]')
157                 capitalised=$(echo ${2} | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g')
158                 for x in $2 $uper $capitalised; do
159                         echo "-D$1$x=$(use $2 && echo ON || echo OFF) "
160                 done
161         fi
162 }
163 _cmake_use_me_now_inverted() {
164         debug-print-function ${FUNCNAME} "$@"
165
166         local arg=$2
167         [[ ! -z $3 ]] && arg=$3
168
169         if [[ ${EAPI} != 5 && "${FUNCNAME[1]}" != cmake-utils_use_find_package ]] ; then
170                 die "${FUNCNAME[1]} is banned in EAPI 6 and later: use -D$1<related_CMake_variable>=\"\$(usex $2)\" instead"
171         fi
172
173         local uper capitalised x
174         [[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
175         if [[ ! -z $3 ]]; then
176                 # user specified the use name so use it
177                 echo "-D$1$3=$(use $2 && echo OFF || echo ON)"
178         else
179                 # use all various most used combinations
180                 uper=$(echo ${2} | tr '[:lower:]' '[:upper:]')
181                 capitalised=$(echo ${2} | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g')
182                 for x in $2 $uper $capitalised; do
183                         echo "-D$1$x=$(use $2 && echo OFF || echo ON) "
184                 done
185         fi
186 }
187
188 # Determine using IN or OUT source build
189 _cmake_check_build_dir() {
190         : ${CMAKE_USE_DIR:=${S}}
191         if [[ -n ${CMAKE_IN_SOURCE_BUILD} ]]; then
192                 # we build in source dir
193                 BUILD_DIR="${CMAKE_USE_DIR}"
194         else
195                 # Respect both the old variable and the new one, depending
196                 # on which one was set by the ebuild.
197                 if [[ ! ${BUILD_DIR} && ${CMAKE_BUILD_DIR} ]]; then
198                         eqawarn "The CMAKE_BUILD_DIR variable has been renamed to BUILD_DIR."
199                         eqawarn "Please migrate the ebuild to use the new one."
200
201                         # In the next call, both variables will be set already
202                         # and we'd have to know which one takes precedence.
203                         _RESPECT_CMAKE_BUILD_DIR=1
204                 fi
205
206                 if [[ ${_RESPECT_CMAKE_BUILD_DIR} ]]; then
207                         BUILD_DIR=${CMAKE_BUILD_DIR:-${WORKDIR}/${P}_build}
208                 else
209                         : ${BUILD_DIR:=${WORKDIR}/${P}_build}
210                 fi
211         fi
212
213         # Backwards compatibility for getting the value.
214         CMAKE_BUILD_DIR=${BUILD_DIR}
215
216         mkdir -p "${BUILD_DIR}" || die
217         echo ">>> Working in BUILD_DIR: \"$BUILD_DIR\""
218 }
219
220 # Determine which generator to use
221 _cmake_generator_to_use() {
222         local generator_name
223
224         case ${CMAKE_MAKEFILE_GENERATOR} in
225                 ninja)
226                         # if ninja is enabled but not installed, the build could fail
227                         # this could happen if ninja is manually enabled (eg. make.conf) but not installed
228                         case ${EAPI} in
229                                 5|6)
230                                         if ! ROOT=/ has_version dev-util/ninja; then
231                                                 die "CMAKE_MAKEFILE_GENERATOR is set to ninja, but ninja is not installed. Please install dev-util/ninja or unset CMAKE_MAKEFILE_GENERATOR."
232                                         fi
233                                 ;;
234                                 *)
235                                         if ! has_version -b dev-util/ninja; then
236                                                 die "CMAKE_MAKEFILE_GENERATOR is set to ninja, but ninja is not installed. Please install dev-util/ninja or unset CMAKE_MAKEFILE_GENERATOR."
237                                         fi
238                                 ;;
239                         esac
240                         generator_name="Ninja"
241                         ;;
242                 emake)
243                         generator_name="Unix Makefiles"
244                         ;;
245                 *)
246                         eerror "Unknown value for \${CMAKE_MAKEFILE_GENERATOR}"
247                         die "Value ${CMAKE_MAKEFILE_GENERATOR} is not supported"
248                         ;;
249         esac
250
251         echo ${generator_name}
252 }
253
254 # @FUNCTION: cmake_comment_add_subdirectory
255 # @USAGE: <subdirectory>
256 # @DESCRIPTION:
257 # Comment out one or more add_subdirectory calls in CMakeLists.txt in the current directory
258 cmake_comment_add_subdirectory() {
259         if [[ -z ${1} ]]; then
260                 die "comment_add_subdirectory must be passed at least one directory name to comment"
261         fi
262
263         if [[ -e "CMakeLists.txt" ]]; then
264                 local d
265                 for d in $@; do
266                         sed -e "/add_subdirectory[[:space:]]*([[:space:]]*${d//\//\\/}[[:space:]]*)/I s/^/#DONOTCOMPILE /" \
267                                 -i CMakeLists.txt || die "failed to comment add_subdirectory(${d})"
268                 done
269         fi
270 }
271
272 # @FUNCTION: comment_add_subdirectory
273 # @USAGE: <subdirectory>
274 # @DESCRIPTION:
275 # Comment out an add_subdirectory call in CMakeLists.txt in the current directory
276 # Banned in EAPI 6 and later - use cmake_comment_add_subdirectory instead.
277 comment_add_subdirectory() {
278         [[ ${EAPI} == 5 ]] || die "comment_add_subdirectory is banned in EAPI 6 and later - use cmake_comment_add_subdirectory instead"
279
280         cmake_comment_add_subdirectory "$@"
281 }
282
283 # @FUNCTION: cmake-utils_use_with
284 # @USAGE: <USE flag> [flag name]
285 # @DESCRIPTION:
286 # Based on use_with. See ebuild(5).
287 #
288 # `cmake-utils_use_with foo FOO` echoes -DWITH_FOO=ON if foo is enabled
289 # and -DWITH_FOO=OFF if it is disabled.
290 cmake-utils_use_with() { _cmake_use_me_now WITH_ "$@" ; }
291
292 # @FUNCTION: cmake-utils_use_enable
293 # @USAGE: <USE flag> [flag name]
294 # @DESCRIPTION:
295 # Based on use_enable. See ebuild(5).
296 #
297 # `cmake-utils_use_enable foo FOO` echoes -DENABLE_FOO=ON if foo is enabled
298 # and -DENABLE_FOO=OFF if it is disabled.
299 cmake-utils_use_enable() { _cmake_use_me_now ENABLE_ "$@" ; }
300
301 # @FUNCTION: cmake-utils_use_find_package
302 # @USAGE: <USE flag> <package name>
303 # @DESCRIPTION:
304 # Based on use_enable. See ebuild(5).
305 #
306 # `cmake-utils_use_find_package foo LibFoo` echoes -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=OFF
307 # if foo is enabled and -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=ON if it is disabled.
308 # This can be used to make find_package optional.
309 cmake-utils_use_find_package() {
310         if [[ ${EAPI} != 5 && "$#" != 2 ]] ; then
311                 die "Usage: cmake-utils_use_find_package <USE flag> <package name>"
312         fi
313
314         _cmake_use_me_now_inverted CMAKE_DISABLE_FIND_PACKAGE_ "$@" ;
315 }
316
317 # @FUNCTION: cmake-utils_use_disable
318 # @USAGE: <USE flag> [flag name]
319 # @DESCRIPTION:
320 # Based on inversion of use_enable. See ebuild(5).
321 #
322 # `cmake-utils_use_enable foo FOO` echoes -DDISABLE_FOO=OFF if foo is enabled
323 # and -DDISABLE_FOO=ON if it is disabled.
324 cmake-utils_use_disable() { _cmake_use_me_now_inverted DISABLE_ "$@" ; }
325
326 # @FUNCTION: cmake-utils_use_no
327 # @USAGE: <USE flag> [flag name]
328 # @DESCRIPTION:
329 # Based on use_disable. See ebuild(5).
330 #
331 # `cmake-utils_use_no foo FOO` echoes -DNO_FOO=OFF if foo is enabled
332 # and -DNO_FOO=ON if it is disabled.
333 cmake-utils_use_no() { _cmake_use_me_now_inverted NO_ "$@" ; }
334
335 # @FUNCTION: cmake-utils_use_want
336 # @USAGE: <USE flag> [flag name]
337 # @DESCRIPTION:
338 # Based on use_enable. See ebuild(5).
339 #
340 # `cmake-utils_use_want foo FOO` echoes -DWANT_FOO=ON if foo is enabled
341 # and -DWANT_FOO=OFF if it is disabled.
342 cmake-utils_use_want() { _cmake_use_me_now WANT_ "$@" ; }
343
344 # @FUNCTION: cmake-utils_use_build
345 # @USAGE: <USE flag> [flag name]
346 # @DESCRIPTION:
347 # Based on use_enable. See ebuild(5).
348 #
349 # `cmake-utils_use_build foo FOO` echoes -DBUILD_FOO=ON if foo is enabled
350 # and -DBUILD_FOO=OFF if it is disabled.
351 cmake-utils_use_build() { _cmake_use_me_now BUILD_ "$@" ; }
352
353 # @FUNCTION: cmake-utils_use_has
354 # @USAGE: <USE flag> [flag name]
355 # @DESCRIPTION:
356 # Based on use_enable. See ebuild(5).
357 #
358 # `cmake-utils_use_has foo FOO` echoes -DHAVE_FOO=ON if foo is enabled
359 # and -DHAVE_FOO=OFF if it is disabled.
360 cmake-utils_use_has() { _cmake_use_me_now HAVE_ "$@" ; }
361
362 # @FUNCTION: cmake-utils_use_use
363 # @USAGE: <USE flag> [flag name]
364 # @DESCRIPTION:
365 # Based on use_enable. See ebuild(5).
366 #
367 # `cmake-utils_use_use foo FOO` echoes -DUSE_FOO=ON if foo is enabled
368 # and -DUSE_FOO=OFF if it is disabled.
369 cmake-utils_use_use() { _cmake_use_me_now USE_ "$@" ; }
370
371 # @FUNCTION: cmake-utils_use
372 # @USAGE: <USE flag> [flag name]
373 # @DESCRIPTION:
374 # Based on use_enable. See ebuild(5).
375 #
376 # `cmake-utils_use foo FOO` echoes -DFOO=ON if foo is enabled
377 # and -DFOO=OFF if it is disabled.
378 cmake-utils_use() { _cmake_use_me_now "" "$@" ; }
379
380 # @FUNCTION: cmake-utils_useno
381 # @USAGE: <USE flag> [flag name]
382 # @DESCRIPTION:
383 # Based on use_enable. See ebuild(5).
384 #
385 # `cmake-utils_useno foo NOFOO` echoes -DNOFOO=OFF if foo is enabled
386 # and -DNOFOO=ON if it is disabled.
387 cmake-utils_useno() { _cmake_use_me_now_inverted "" "$@" ; }
388
389 # Internal function for modifying hardcoded definitions.
390 # Removes dangerous definitions that override Gentoo settings.
391 _cmake_modify-cmakelists() {
392         debug-print-function ${FUNCNAME} "$@"
393
394         # Only edit the files once
395         grep -qs "<<< Gentoo configuration >>>" "${CMAKE_USE_DIR}"/CMakeLists.txt && return 0
396
397         # Comment out all set (<some_should_be_user_defined_variable> value)
398         # TODO Add QA checker - inform when variable being checked for below is set in CMakeLists.txt
399         find "${CMAKE_USE_DIR}" -name CMakeLists.txt \
400                 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_BUILD_TYPE.*)/{s/^/#IGNORE /g}' {} + \
401                 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_COLOR_MAKEFILE.*)/{s/^/#IGNORE /g}' {} + \
402                 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_INSTALL_PREFIX.*)/{s/^/#IGNORE /g}' {} + \
403                 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_VERBOSE_MAKEFILE.*)/{s/^/#IGNORE /g}' {} + \
404                 || die "${LINENO}: failed to disable hardcoded settings"
405
406         # NOTE Append some useful summary here
407         cat >> "${CMAKE_USE_DIR}"/CMakeLists.txt <<- _EOF_ || die
408
409                 MESSAGE(STATUS "<<< Gentoo configuration >>>
410                 Build type      \${CMAKE_BUILD_TYPE}
411                 Install path    \${CMAKE_INSTALL_PREFIX}
412                 Compiler flags:
413                 C               \${CMAKE_C_FLAGS}
414                 C++             \${CMAKE_CXX_FLAGS}
415                 Linker flags:
416                 Executable      \${CMAKE_EXE_LINKER_FLAGS}
417                 Module          \${CMAKE_MODULE_LINKER_FLAGS}
418                 Shared          \${CMAKE_SHARED_LINKER_FLAGS}\n")
419         _EOF_
420 }
421
422 # temporary function for moving cmake cleanups from from src_configure -> src_prepare.
423 # bug #378850
424 _cmake_cleanup_cmake() {
425         : ${CMAKE_USE_DIR:=${S}}
426
427         if [[ "${CMAKE_REMOVE_MODULES}" == "yes" ]] ; then
428                 local name
429                 for name in ${CMAKE_REMOVE_MODULES_LIST} ; do
430                         find "${S}" -name ${name}.cmake -exec rm -v {} + || die
431                 done
432         fi
433
434         # check if CMakeLists.txt exist and if no then die
435         if [[ ! -e ${CMAKE_USE_DIR}/CMakeLists.txt ]] ; then
436                 eerror "Unable to locate CMakeLists.txt under:"
437                 eerror "\"${CMAKE_USE_DIR}/CMakeLists.txt\""
438                 eerror "Consider not inheriting the cmake eclass."
439                 die "FATAL: Unable to find CMakeLists.txt"
440         fi
441
442         # Remove dangerous things.
443         _cmake_modify-cmakelists
444 }
445
446 # @FUNCTION: cmake-utils_src_prepare
447 # @DESCRIPTION:
448 # Apply ebuild and user patches.
449 cmake-utils_src_prepare() {
450         debug-print-function ${FUNCNAME} "$@"
451
452         pushd "${S}" > /dev/null || die
453
454         if [[ ${EAPI} != 5 ]]; then
455                 default_src_prepare
456                 _cmake_cleanup_cmake
457         else
458                 debug-print "$FUNCNAME: PATCHES=$PATCHES"
459                 [[ ${PATCHES[@]} ]] && epatch "${PATCHES[@]}"
460
461                 debug-print "$FUNCNAME: applying user patches"
462                 epatch_user
463         fi
464
465         popd > /dev/null || die
466
467         # make ${S} read-only in order to detect broken build-systems
468         if [[ ${CMAKE_UTILS_QA_SRC_DIR_READONLY} && ! ${CMAKE_IN_SOURCE_BUILD} ]]; then
469                 chmod -R a-w "${S}"
470         fi
471
472         _CMAKE_UTILS_SRC_PREPARE_HAS_RUN=1
473 }
474
475 # @VARIABLE: mycmakeargs
476 # @DEFAULT_UNSET
477 # @DESCRIPTION:
478 # Optional cmake defines as a bash array. Should be defined before calling
479 # src_configure.
480 # @CODE
481 # src_configure() {
482 #       local mycmakeargs=(
483 #               $(cmake-utils_use_with openconnect)
484 #       )
485 #
486 #       cmake-utils_src_configure
487 # }
488 # @CODE
489
490 # @FUNCTION: cmake-utils_src_configure
491 # @DESCRIPTION:
492 # General function for configuring with cmake. Default behaviour is to start an
493 # out-of-source build.
494 cmake-utils_src_configure() {
495         debug-print-function ${FUNCNAME} "$@"
496
497         if [[ ! ${_CMAKE_UTILS_SRC_PREPARE_HAS_RUN} ]]; then
498                 eqawarn "cmake-utils_src_prepare has not been run, please open a bug on https://bugs.gentoo.org/"
499         fi
500
501         [[ ${EAPI} == 5 ]] && _cmake_cleanup_cmake
502
503         _cmake_check_build_dir
504
505         # Fix xdg collision with sandbox
506         xdg_environment_reset
507
508         # @SEE CMAKE_BUILD_TYPE
509         if [[ ${CMAKE_BUILD_TYPE} = Gentoo ]]; then
510                 # Handle release builds
511                 if ! has debug ${IUSE//+} || ! use debug; then
512                         local CPPFLAGS=${CPPFLAGS}
513                         append-cppflags -DNDEBUG
514                 fi
515         fi
516
517         # Prepare Gentoo override rules (set valid compiler, append CPPFLAGS etc.)
518         local build_rules=${BUILD_DIR}/gentoo_rules.cmake
519
520         cat > "${build_rules}" <<- _EOF_ || die
521                 SET (CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "ASM compile command" FORCE)
522                 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)
523                 SET (CMAKE_ASM-ATT_LINK_FLAGS "-nostdlib" CACHE STRING "ASM-ATT link flags" FORCE)
524                 SET (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C compile command" FORCE)
525                 SET (CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C++ compile command" FORCE)
526                 SET (CMAKE_Fortran_COMPILE_OBJECT "<CMAKE_Fortran_COMPILER> <DEFINES> <INCLUDES> ${FCFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "Fortran compile command" FORCE)
527         _EOF_
528
529         local myCC=$(tc-getCC) myCXX=$(tc-getCXX) myFC=$(tc-getFC)
530
531         # !!! IMPORTANT NOTE !!!
532         # Single slash below is intentional. CMake is weird and wants the
533         # CMAKE_*_VARIABLES split into two elements: the first one with
534         # compiler path, and the second one with all command-line options,
535         # space separated.
536         local toolchain_file=${BUILD_DIR}/gentoo_toolchain.cmake
537         cat > ${toolchain_file} <<- _EOF_ || die
538                 SET (CMAKE_ASM_COMPILER "${myCC/ /;}")
539                 SET (CMAKE_ASM-ATT_COMPILER "${myCC/ /;}")
540                 SET (CMAKE_C_COMPILER "${myCC/ /;}")
541                 SET (CMAKE_CXX_COMPILER "${myCXX/ /;}")
542                 SET (CMAKE_Fortran_COMPILER "${myFC/ /;}")
543                 SET (CMAKE_AR $(type -P $(tc-getAR)) CACHE FILEPATH "Archive manager" FORCE)
544                 SET (CMAKE_RANLIB $(type -P $(tc-getRANLIB)) CACHE FILEPATH "Archive index generator" FORCE)
545                 SET (CMAKE_SYSTEM_PROCESSOR "${CHOST%%-*}")
546         _EOF_
547
548         # We are using the C compiler for assembly by default.
549         local -x ASMFLAGS=${CFLAGS}
550         local -x PKG_CONFIG=$(tc-getPKG_CONFIG)
551
552         if tc-is-cross-compiler; then
553                 local sysname
554                 case "${KERNEL:-linux}" in
555                         Cygwin) sysname="CYGWIN_NT-5.1" ;;
556                         HPUX) sysname="HP-UX" ;;
557                         linux) sysname="Linux" ;;
558                         Winnt)
559                                 sysname="Windows"
560                                 cat >> "${toolchain_file}" <<- _EOF_ || die
561                                         SET (CMAKE_RC_COMPILER $(tc-getRC))
562                                 _EOF_
563                                 ;;
564                         *) sysname="${KERNEL}" ;;
565                 esac
566
567                 cat >> "${toolchain_file}" <<- _EOF_ || die
568                         SET (CMAKE_SYSTEM_NAME "${sysname}")
569                 _EOF_
570
571                 if [ "${SYSROOT:-/}" != "/" ] ; then
572                         # When cross-compiling with a sysroot (e.g. with crossdev's emerge wrappers)
573                         # we need to tell cmake to use libs/headers from the sysroot but programs from / only.
574                         cat >> "${toolchain_file}" <<- _EOF_ || die
575                                 SET (CMAKE_FIND_ROOT_PATH "${SYSROOT}")
576                                 SET (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
577                                 SET (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
578                                 SET (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
579                         _EOF_
580                 fi
581         fi
582
583         if [[ ${EPREFIX} ]]; then
584                 cat >> "${build_rules}" <<- _EOF_ || die
585                         # in Prefix we need rpath and must ensure cmake gets our default linker path
586                         # right ... except for Darwin hosts
587                         IF (NOT APPLE)
588                         SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
589                         SET (CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH "${EPREFIX}/usr/${CHOST}/lib/gcc;${EPREFIX}/usr/${CHOST}/lib;${EPREFIX}/usr/$(get_libdir);${EPREFIX}/$(get_libdir)"
590                         CACHE STRING "" FORCE)
591
592                         ELSE ()
593
594                         SET (CMAKE_PREFIX_PATH "${EPREFIX}/usr" CACHE STRING "" FORCE)
595                         SET (CMAKE_MACOSX_RPATH ON CACHE BOOL "" FORCE)
596                         SET (CMAKE_SKIP_BUILD_RPATH OFF CACHE BOOL "" FORCE)
597                         SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
598                         SET (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOL "" FORCE)
599
600                         ENDIF (NOT APPLE)
601                 _EOF_
602         fi
603
604         # Common configure parameters (invariants)
605         local common_config=${BUILD_DIR}/gentoo_common_config.cmake
606         local libdir=$(get_libdir)
607         cat > "${common_config}" <<- _EOF_ || die
608                 SET (CMAKE_GENTOO_BUILD ON CACHE BOOL "Indicate Gentoo package build")
609                 SET (LIB_SUFFIX ${libdir/lib} CACHE STRING "library path suffix" FORCE)
610                 SET (CMAKE_INSTALL_LIBDIR ${libdir} CACHE PATH "Output directory for libraries")
611                 SET (CMAKE_INSTALL_INFODIR "${EPREFIX}/usr/share/info" CACHE PATH "")
612                 SET (CMAKE_INSTALL_MANDIR "${EPREFIX}/usr/share/man" CACHE PATH "")
613         _EOF_
614         [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo 'SET (CMAKE_COLOR_MAKEFILE OFF CACHE BOOL "pretty colors during make" FORCE)' >> "${common_config}"
615
616         if [[ ${EAPI} != [56] ]]; then
617                 cat >> "${common_config}" <<- _EOF_ || die
618                         SET (CMAKE_INSTALL_DOCDIR "${EPREFIX}/usr/share/doc/${PF}" CACHE PATH "")
619                 _EOF_
620         fi
621
622         # Wipe the default optimization flags out of CMake
623         if [[ ${CMAKE_BUILD_TYPE} != Gentoo && ${EAPI} != 5 ]]; then
624                 cat >> ${common_config} <<- _EOF_ || die
625                         SET (CMAKE_ASM_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
626                         SET (CMAKE_ASM-ATT_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
627                         SET (CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
628                         SET (CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
629                         SET (CMAKE_Fortran_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
630                         SET (CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
631                         SET (CMAKE_MODULE_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
632                         SET (CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
633                         SET (CMAKE_STATIC_LINKER_FLAGS_${CMAKE_BUILD_TYPE^^} "" CACHE STRING "")
634                 _EOF_
635         fi
636
637         # Convert mycmakeargs to an array, for backwards compatibility
638         # Make the array a local variable since <=portage-2.1.6.x does not
639         # support global arrays (see bug #297255).
640         local mycmakeargstype=$(declare -p mycmakeargs 2>&-)
641         if [[ "${mycmakeargstype}" != "declare -a mycmakeargs="* ]]; then
642                 if [[ -n "${mycmakeargstype}" ]] ; then
643                         if [[ ${EAPI} == 5 ]]; then
644                                 eqawarn "Declaring mycmakeargs as a variable is deprecated. Please use an array instead."
645                         else
646                                 die "Declaring mycmakeargs as a variable is banned in EAPI=${EAPI}. Please use an array instead."
647                         fi
648                 fi
649                 local mycmakeargs_local=(${mycmakeargs})
650         else
651                 local mycmakeargs_local=("${mycmakeargs[@]}")
652         fi
653
654         if [[ ${CMAKE_WARN_UNUSED_CLI} == no ]] ; then
655                 local warn_unused_cli="--no-warn-unused-cli"
656         else
657                 local warn_unused_cli=""
658         fi
659
660         # Common configure parameters (overridable)
661         # NOTE CMAKE_BUILD_TYPE can be only overridden via CMAKE_BUILD_TYPE eclass variable
662         # No -DCMAKE_BUILD_TYPE=xxx definitions will be in effect.
663         local cmakeargs=(
664                 ${warn_unused_cli}
665                 -C "${common_config}"
666                 -G "$(_cmake_generator_to_use)"
667                 -DCMAKE_INSTALL_PREFIX="${EPREFIX}/usr"
668                 "${mycmakeargs_local[@]}"
669                 -DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
670                 $([[ ${EAPI} == 5 ]] && echo -DCMAKE_INSTALL_DO_STRIP=OFF)
671                 -DCMAKE_USER_MAKE_RULES_OVERRIDE="${build_rules}"
672                 -DCMAKE_TOOLCHAIN_FILE="${toolchain_file}"
673                 "${MYCMAKEARGS}"
674         )
675
676         if [[ -n "${CMAKE_EXTRA_CACHE_FILE}" ]] ; then
677                 cmakeargs+=( -C "${CMAKE_EXTRA_CACHE_FILE}" )
678         fi
679
680         pushd "${BUILD_DIR}" > /dev/null || die
681         debug-print "${LINENO} ${ECLASS} ${FUNCNAME}: mycmakeargs is ${mycmakeargs_local[*]}"
682         echo "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}"
683         "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}" || die "cmake failed"
684         popd > /dev/null || die
685 }
686
687 # @FUNCTION: cmake-utils_src_compile
688 # @DESCRIPTION:
689 # General function for compiling with cmake.
690 # Automatically detects the build type. All arguments are passed to emake.
691 cmake-utils_src_compile() {
692         debug-print-function ${FUNCNAME} "$@"
693
694         cmake-utils_src_make "$@"
695 }
696
697 # @FUNCTION: _cmake_ninja_src_make
698 # @INTERNAL
699 # @DESCRIPTION:
700 # Build the package using ninja generator
701 _cmake_ninja_src_make() {
702         debug-print-function ${FUNCNAME} "$@"
703
704         [[ -e build.ninja ]] || die "build.ninja not found. Error during configure stage."
705
706         eninja "$@"
707 }
708
709 # @FUNCTION: _cmake_emake_src_make
710 # @INTERNAL
711 # @DESCRIPTION:
712 # Build the package using make generator
713 _cmake_emake_src_make() {
714         debug-print-function ${FUNCNAME} "$@"
715
716         [[ -e Makefile ]] || die "Makefile not found. Error during configure stage."
717
718         if [[ "${CMAKE_VERBOSE}" != "OFF" ]]; then
719                 emake VERBOSE=1 "$@" || die
720         else
721                 emake "$@" || die
722         fi
723
724 }
725
726 # @FUNCTION: cmake-utils_src_make
727 # @DESCRIPTION:
728 # Function for building the package. Automatically detects the build type.
729 # All arguments are passed to emake.
730 cmake-utils_src_make() {
731         debug-print-function ${FUNCNAME} "$@"
732
733         _cmake_check_build_dir
734         pushd "${BUILD_DIR}" > /dev/null || die
735
736         _cmake_${CMAKE_MAKEFILE_GENERATOR}_src_make "$@"
737
738         popd > /dev/null || die
739 }
740
741 # @FUNCTION: cmake-utils_src_test
742 # @DESCRIPTION:
743 # Function for testing the package. Automatically detects the build type.
744 cmake-utils_src_test() {
745         debug-print-function ${FUNCNAME} "$@"
746
747         _cmake_check_build_dir
748         pushd "${BUILD_DIR}" > /dev/null || die
749         [[ -e CTestTestfile.cmake ]] || { echo "No tests found. Skipping."; return 0 ; }
750
751         [[ -n ${TEST_VERBOSE} ]] && myctestargs+=( --extra-verbose --output-on-failure )
752
753         set -- ctest -j "$(makeopts_jobs)" --test-load "$(makeopts_loadavg)" "${myctestargs[@]}" "$@"
754         echo "$@" >&2
755         if "$@" ; then
756                 einfo "Tests succeeded."
757                 popd > /dev/null || die
758                 return 0
759         else
760                 if [[ -n "${CMAKE_YES_I_WANT_TO_SEE_THE_TEST_LOG}" ]] ; then
761                         # on request from Diego
762                         eerror "Tests failed. Test log ${BUILD_DIR}/Testing/Temporary/LastTest.log follows:"
763                         eerror "--START TEST LOG--------------------------------------------------------------"
764                         cat "${BUILD_DIR}/Testing/Temporary/LastTest.log"
765                         eerror "--END TEST LOG----------------------------------------------------------------"
766                         die "Tests failed."
767                 else
768                         die "Tests failed. When you file a bug, please attach the following file: \n\t${BUILD_DIR}/Testing/Temporary/LastTest.log"
769                 fi
770
771                 # die might not die due to nonfatal
772                 popd > /dev/null || die
773                 return 1
774         fi
775 }
776
777 # @FUNCTION: cmake-utils_src_install
778 # @DESCRIPTION:
779 # Function for installing the package. Automatically detects the build type.
780 cmake-utils_src_install() {
781         debug-print-function ${FUNCNAME} "$@"
782
783         _cmake_check_build_dir
784         pushd "${BUILD_DIR}" > /dev/null || die
785         DESTDIR="${D}" ${CMAKE_MAKEFILE_GENERATOR} install "$@" || die "died running ${CMAKE_MAKEFILE_GENERATOR} install"
786         popd > /dev/null || die
787
788         pushd "${S}" > /dev/null || die
789         einstalldocs
790         popd > /dev/null || die
791 }
792
793 fi