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