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