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