/[gentoo-x86]/eclass/cmake-utils.eclass
Gentoo

Contents of /eclass/cmake-utils.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.69 - (show annotations) (download)
Mon Jun 27 21:11:57 2011 UTC (3 years, 2 months ago) by abcd
Branch: MAIN
Changes since 1.68: +5 -2 lines
Only touch CMakeLists.txt once

1 # Copyright 1999-2010 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-x86/eclass/cmake-utils.eclass,v 1.68 2011/04/27 16:54:44 scarabeus Exp $
4
5 # @ECLASS: cmake-utils.eclass
6 # @MAINTAINER:
7 # kde@gentoo.org
8 #
9 # @CODE
10 # Tomáš Chvátal <scarabeus@gentoo.org>
11 # Maciej Mrozowski <reavertm@gentoo.org>
12 # (undisclosed contributors)
13 # Original author: Zephyrus (zephyrus@mirach.it)
14 # @CODE
15 # @BLURB: common ebuild functions for cmake-based packages
16 # @DESCRIPTION:
17 # The cmake-utils eclass is base.eclass(5) wrapper that makes creating ebuilds for
18 # cmake-based packages much easier.
19 # It provides all inherited features (DOCS, HTML_DOCS, PATCHES) along with out-of-source
20 # builds (default), in-source builds and an implementation of the well-known use_enable
21 # and use_with functions for CMake.
22
23 # @ECLASS-VARIABLE: WANT_CMAKE
24 # @DESCRIPTION:
25 # Specify if cmake-utils eclass should depend on cmake optionaly or not.
26 # This is usefull when only part of aplication is using cmake build system.
27 # Valid values are: always [default], optional (where the value is the useflag
28 # used for optionality)
29 WANT_CMAKE="${WANT_CMAKE:-always}"
30
31 # @ECLASS-VARIABLE: CMAKE_MIN_VERSION
32 # @DESCRIPTION:
33 # Specify the minimum required CMake version. Default is 2.8.1
34 CMAKE_MIN_VERSION="${CMAKE_MIN_VERSION:-2.8.1}"
35
36 CMAKEDEPEND=""
37 case ${WANT_CMAKE} in
38 always)
39 ;;
40 *)
41 IUSE+=" ${WANT_CMAKE}"
42 CMAKEDEPEND+="${WANT_CMAKE}? ( "
43 ;;
44 esac
45 inherit toolchain-funcs multilib flag-o-matic base
46
47 CMAKE_EXPF="src_compile src_test src_install"
48 case ${EAPI:-0} in
49 4|3|2) CMAKE_EXPF+=" src_configure" ;;
50 1|0) ;;
51 *) die "Unknown EAPI, Bug eclass maintainers." ;;
52 esac
53 EXPORT_FUNCTIONS ${CMAKE_EXPF}
54
55 : ${DESCRIPTION:="Based on the ${ECLASS} eclass"}
56
57 if [[ ${PN} != cmake ]]; then
58 CMAKEDEPEND+=">=dev-util/cmake-${CMAKE_MIN_VERSION}"
59 fi
60
61 CMAKEDEPEND+=" userland_GNU? ( >=sys-apps/findutils-4.4.0 )"
62
63 [[ ${WANT_CMAKE} = always ]] || CMAKEDEPEND+=" )"
64
65 DEPEND="${CMAKEDEPEND}"
66 unset CMAKEDEPEND
67
68 # Internal functions used by cmake-utils_use_*
69 _use_me_now() {
70 debug-print-function ${FUNCNAME} "$@"
71
72 local uper capitalised x
73 [[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
74 if [[ ! -z $3 ]]; then
75 # user specified the use name so use it
76 echo "-D$1$3=$(use $2 && echo ON || echo OFF)"
77 else
78 # use all various most used combinations
79 uper=$(echo ${2} | tr '[:lower:]' '[:upper:]')
80 capitalised=$(echo ${2} | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g')
81 for x in $2 $uper $capitalised; do
82 echo "-D$1$x=$(use $2 && echo ON || echo OFF) "
83 done
84 fi
85 }
86 _use_me_now_inverted() {
87 debug-print-function ${FUNCNAME} "$@"
88
89 local uper capitalised x
90 [[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
91 if [[ ! -z $3 ]]; then
92 # user specified the use name so use it
93 echo "-D$1$3=$(use $2 && echo OFF || echo ON)"
94 else
95 # use all various most used combinations
96 uper=$(echo ${2} | tr '[:lower:]' '[:upper:]')
97 capitalised=$(echo ${2} | sed 's/\<\(.\)\([^ ]*\)/\u\1\L\2/g')
98 for x in $2 $uper $capitalised; do
99 echo "-D$1$x=$(use $2 && echo OFF || echo ON) "
100 done
101 fi
102 }
103
104 # @ECLASS-VARIABLE: CMAKE_BUILD_DIR
105 # @DESCRIPTION:
106 # Build directory where all cmake processed files should be generated.
107 # For in-source build it's fixed to ${CMAKE_USE_DIR}.
108 # For out-of-source build it can be overriden, by default it uses
109 # ${WORKDIR}/${P}_build.
110
111 # @ECLASS-VARIABLE: CMAKE_BUILD_TYPE
112 # @DESCRIPTION:
113 # Set to override default CMAKE_BUILD_TYPE. Only useful for packages
114 # known to make use of "if (CMAKE_BUILD_TYPE MATCHES xxx)".
115 # If about to be set - needs to be set before invoking cmake-utils_src_configure.
116 # You usualy do *NOT* want nor need to set it as it pulls CMake default build-type
117 # specific compiler flags overriding make.conf.
118 : ${CMAKE_BUILD_TYPE:=Gentoo}
119
120 # @ECLASS-VARIABLE: CMAKE_IN_SOURCE_BUILD
121 # @DESCRIPTION:
122 # Set to enable in-source build.
123
124 # @ECLASS-VARIABLE: CMAKE_USE_DIR
125 # @DESCRIPTION:
126 # Sets the directory where we are working with cmake.
127 # For example when application uses autotools and only one
128 # plugin needs to be done by cmake.
129 # By default it uses ${S}.
130
131 # @ECLASS-VARIABLE: CMAKE_VERBOSE
132 # @DESCRIPTION:
133 # Set to enable verbose messages during compilation.
134
135 # @ECLASS-VARIABLE: PREFIX
136 # @DESCRIPTION:
137 # Eclass respects PREFIX variable, though it's not recommended way to set
138 # install/lib/bin prefixes.
139 # Use -DCMAKE_INSTALL_PREFIX=... CMake variable instead.
140
141 # @ECLASS-VARIABLE: CMAKE_BINARY
142 # @DESCRIPTION:
143 # Eclass can use different cmake binary than the one provided in by system.
144 : ${CMAKE_BINARY:=cmake}
145
146 # Determine using IN or OUT source build
147 _check_build_dir() {
148 : ${CMAKE_USE_DIR:=${S}}
149 if [[ -n ${CMAKE_IN_SOURCE_BUILD} ]]; then
150 # we build in source dir
151 CMAKE_BUILD_DIR="${CMAKE_USE_DIR}"
152 else
153 : ${CMAKE_BUILD_DIR:=${WORKDIR}/${P}_build}
154 fi
155 echo ">>> Working in BUILD_DIR: \"$CMAKE_BUILD_DIR\""
156 }
157 # @FUNCTION: cmake-utils_use_with
158 # @USAGE: <USE flag> [flag name]
159 # @DESCRIPTION:
160 # Based on use_with. See ebuild(5).
161 #
162 # `cmake-utils_use_with foo FOO` echoes -DWITH_FOO=ON if foo is enabled
163 # and -DWITH_FOO=OFF if it is disabled.
164 cmake-utils_use_with() { _use_me_now WITH_ "$@" ; }
165
166 # @FUNCTION: cmake-utils_use_enable
167 # @USAGE: <USE flag> [flag name]
168 # @DESCRIPTION:
169 # Based on use_enable. See ebuild(5).
170 #
171 # `cmake-utils_use_enable foo FOO` echoes -DENABLE_FOO=ON if foo is enabled
172 # and -DENABLE_FOO=OFF if it is disabled.
173 cmake-utils_use_enable() { _use_me_now ENABLE_ "$@" ; }
174
175 # @FUNCTION: cmake-utils_use_disable
176 # @USAGE: <USE flag> [flag name]
177 # @DESCRIPTION:
178 # Based on inversion of use_enable. See ebuild(5).
179 #
180 # `cmake-utils_use_enable foo FOO` echoes -DDISABLE_FOO=OFF if foo is enabled
181 # and -DDISABLE_FOO=ON if it is disabled.
182 cmake-utils_use_disable() { _use_me_now_inverted DISABLE_ "$@" ; }
183
184 # @FUNCTION: cmake-utils_use_no
185 # @USAGE: <USE flag> [flag name]
186 # @DESCRIPTION:
187 # Based on use_disable. See ebuild(5).
188 #
189 # `cmake-utils_use_no foo FOO` echoes -DNO_FOO=OFF if foo is enabled
190 # and -DNO_FOO=ON if it is disabled.
191 cmake-utils_use_no() { _use_me_now_inverted NO_ "$@" ; }
192
193 # @FUNCTION: cmake-utils_use_want
194 # @USAGE: <USE flag> [flag name]
195 # @DESCRIPTION:
196 # Based on use_enable. See ebuild(5).
197 #
198 # `cmake-utils_use_want foo FOO` echoes -DWANT_FOO=ON if foo is enabled
199 # and -DWANT_FOO=OFF if it is disabled.
200 cmake-utils_use_want() { _use_me_now WANT_ "$@" ; }
201
202 # @FUNCTION: cmake-utils_use_build
203 # @USAGE: <USE flag> [flag name]
204 # @DESCRIPTION:
205 # Based on use_enable. See ebuild(5).
206 #
207 # `cmake-utils_use_build foo FOO` echoes -DBUILD_FOO=ON if foo is enabled
208 # and -DBUILD_FOO=OFF if it is disabled.
209 cmake-utils_use_build() { _use_me_now BUILD_ "$@" ; }
210
211 # @FUNCTION: cmake-utils_use_has
212 # @USAGE: <USE flag> [flag name]
213 # @DESCRIPTION:
214 # Based on use_enable. See ebuild(5).
215 #
216 # `cmake-utils_use_has foo FOO` echoes -DHAVE_FOO=ON if foo is enabled
217 # and -DHAVE_FOO=OFF if it is disabled.
218 cmake-utils_use_has() { _use_me_now HAVE_ "$@" ; }
219
220 # @FUNCTION: cmake-utils_use_use
221 # @USAGE: <USE flag> [flag name]
222 # @DESCRIPTION:
223 # Based on use_enable. See ebuild(5).
224 #
225 # `cmake-utils_use_use foo FOO` echoes -DUSE_FOO=ON if foo is enabled
226 # and -DUSE_FOO=OFF if it is disabled.
227 cmake-utils_use_use() { _use_me_now USE_ "$@" ; }
228
229 # @FUNCTION: cmake-utils_use
230 # @USAGE: <USE flag> [flag name]
231 # @DESCRIPTION:
232 # Based on use_enable. See ebuild(5).
233 #
234 # `cmake-utils_use foo FOO` echoes -DFOO=ON if foo is enabled
235 # and -DFOO=OFF if it is disabled.
236 cmake-utils_use() { _use_me_now "" "$@" ; }
237
238 # Internal function for modifying hardcoded definitions.
239 # Removes dangerous definitions that override Gentoo settings.
240 _modify-cmakelists() {
241 debug-print-function ${FUNCNAME} "$@"
242
243 # Only edit the files once
244 grep -qs "<<< Gentoo configuration >>>" CMakeLists.txt && return 0
245
246 # Comment out all set (<some_should_be_user_defined_variable> value)
247 # TODO Add QA checker - inform when variable being checked for below is set in CMakeLists.txt
248 find "${CMAKE_USE_DIR}" -name CMakeLists.txt \
249 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_BUILD_TYPE.*)/{s/^/#IGNORE /g}' {} + \
250 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_COLOR_MAKEFILE.*)/{s/^/#IGNORE /g}' {} + \
251 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_INSTALL_PREFIX.*)/{s/^/#IGNORE /g}' {} + \
252 -exec sed -i -e '/^[[:space:]]*[sS][eE][tT][[:space:]]*([[:space:]]*CMAKE_VERBOSE_MAKEFILE.*)/{s/^/#IGNORE /g}' {} + \
253 || die "${LINENO}: failed to disable hardcoded settings"
254
255 # NOTE Append some useful summary here
256 cat >> "${CMAKE_USE_DIR}"/CMakeLists.txt <<- _EOF_
257
258 MESSAGE(STATUS "<<< Gentoo configuration >>>
259 Build type \${CMAKE_BUILD_TYPE}
260 Install path \${CMAKE_INSTALL_PREFIX}
261 Compiler flags:
262 C \${CMAKE_C_FLAGS}
263 C++ \${CMAKE_CXX_FLAGS}
264 Linker flags:
265 Executable \${CMAKE_EXE_LINKER_FLAGS}
266 Module \${CMAKE_MODULE_LINKER_FLAGS}
267 Shared \${CMAKE_SHARED_LINKER_FLAGS}\n")
268 _EOF_
269 }
270
271 enable_cmake-utils_src_configure() {
272 debug-print-function ${FUNCNAME} "$@"
273
274 _check_build_dir
275
276 # check if CMakeLists.txt exist and if no then die
277 if [[ ! -e ${CMAKE_USE_DIR}/CMakeLists.txt ]] ; then
278 eerror "Unable to locate CMakeLists.txt under:"
279 eerror "\"${CMAKE_USE_DIR}/CMakeLists.txt\""
280 eerror "Consider not inheriting the cmake eclass."
281 die "FATAL: Unable to find CMakeLists.txt"
282 fi
283
284 # Remove dangerous things.
285 _modify-cmakelists
286
287 # Fix xdg collision with sandbox
288 export XDG_CONFIG_HOME="${T}"
289
290 # @SEE CMAKE_BUILD_TYPE
291 if [[ ${CMAKE_BUILD_TYPE} = Gentoo ]]; then
292 # Handle release builds
293 if ! has debug ${IUSE//+} || ! use debug; then
294 append-cppflags -DNDEBUG
295 fi
296 fi
297
298 # Prepare Gentoo override rules (set valid compiler, append CPPFLAGS)
299 local build_rules=${T}/gentoo_rules.cmake
300 cat > "${build_rules}" <<- _EOF_
301 SET (CMAKE_C_COMPILER $(type -P $(tc-getCC)) CACHE FILEPATH "C compiler" FORCE)
302 SET (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C compile command" FORCE)
303 SET (CMAKE_CXX_COMPILER $(type -P $(tc-getCXX)) CACHE FILEPATH "C++ compiler" FORCE)
304 SET (CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C++ compile command" FORCE)
305 _EOF_
306
307 if use prefix; then
308 cat >> "${build_rules}" <<- _EOF_
309 # in Prefix we need rpath and must ensure cmake gets our default linker path
310 # right ... except for Darwin hosts
311 IF (NOT APPLE)
312 SET (CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
313 SET (CMAKE_PLATFORM_REQUIRED_RUNTIME_PATH "${EPREFIX}/usr/${CHOST}/lib/gcc;${EPREFIX}/usr/${CHOST}/lib;${EPREFIX}/usr/$(get_libdir);${EPREFIX}/$(get_libdir)"
314 CACHE STRING "" FORCE)
315
316 ELSE ()
317
318 SET(CMAKE_PREFIX_PATH "${EPREFIX}${PREFIX:-/usr}" CACHE STRING ""FORCE)
319 SET(CMAKE_SKIP_BUILD_RPATH OFF CACHE BOOL "" FORCE)
320 SET(CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
321 SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE CACHE BOOL "" FORCE)
322 SET(CMAKE_INSTALL_RPATH "${EPREFIX}${PREFIX:-/usr}/lib;${EPREFIX}/usr/${CHOST}/lib/gcc;${EPREFIX}/usr/${CHOST}/lib;${EPREFIX}/usr/$(get_libdir);${EPREFIX}/$(get_libdir)" CACHE STRING "" FORCE)
323 SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE BOOL "" FORCE)
324 SET(CMAKE_INSTALL_NAME_DIR "${EPREFIX}${PREFIX:-/usr}/lib" CACHE STRING "" FORCE)
325
326 ENDIF (NOT APPLE)
327 _EOF_
328 fi
329
330 # Common configure parameters (invariants)
331 local common_config=${T}/gentoo_common_config.cmake
332 local libdir=$(get_libdir)
333 cat > "${common_config}" <<- _EOF_
334 SET (LIB_SUFFIX ${libdir/lib} CACHE STRING "library path suffix" FORCE)
335 SET (CMAKE_INSTALL_LIBDIR ${libdir} CACHE PATH "Output directory for libraries")
336 _EOF_
337 [[ "${NOCOLOR}" = true || "${NOCOLOR}" = yes ]] && echo 'SET (CMAKE_COLOR_MAKEFILE OFF CACHE BOOL "pretty colors during make" FORCE)' >> "${common_config}"
338
339 # Convert mycmakeargs to an array, for backwards compatibility
340 # Make the array a local variable since <=portage-2.1.6.x does not
341 # support global arrays (see bug #297255).
342 if [[ $(declare -p mycmakeargs 2>&-) != "declare -a mycmakeargs="* ]]; then
343 local mycmakeargs_local=(${mycmakeargs})
344 else
345 local mycmakeargs_local=("${mycmakeargs[@]}")
346 fi
347
348 has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
349
350 # Common configure parameters (overridable)
351 # NOTE CMAKE_BUILD_TYPE can be only overriden via CMAKE_BUILD_TYPE eclass variable
352 # No -DCMAKE_BUILD_TYPE=xxx definitions will be in effect.
353 local cmakeargs=(
354 --no-warn-unused-cli
355 -C "${common_config}"
356 -DCMAKE_INSTALL_PREFIX="${EPREFIX}${PREFIX:-/usr}"
357 "${mycmakeargs_local[@]}"
358 -DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
359 -DCMAKE_INSTALL_DO_STRIP=OFF
360 -DCMAKE_USER_MAKE_RULES_OVERRIDE="${build_rules}"
361 "${MYCMAKEARGS}"
362 )
363
364 mkdir -p "${CMAKE_BUILD_DIR}"
365 pushd "${CMAKE_BUILD_DIR}" > /dev/null
366 debug-print "${LINENO} ${ECLASS} ${FUNCNAME}: mycmakeargs is ${mycmakeargs_local[*]}"
367 echo "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}"
368 "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}" || die "cmake failed"
369 popd > /dev/null
370 }
371
372 enable_cmake-utils_src_compile() {
373 debug-print-function ${FUNCNAME} "$@"
374
375 has src_configure ${CMAKE_EXPF} || cmake-utils_src_configure
376 cmake-utils_src_make "$@"
377 }
378
379 # @FUNCTION: cmake-utils_src_make
380 # @DESCRIPTION:
381 # Function for building the package. Automatically detects the build type.
382 # All arguments are passed to emake.
383 cmake-utils_src_make() {
384 debug-print-function ${FUNCNAME} "$@"
385
386 _check_build_dir
387 pushd "${CMAKE_BUILD_DIR}" > /dev/null
388 # first check if Makefile exist otherwise die
389 [[ -e Makefile ]] || die "Makefile not found. Error during configure stage."
390 if [[ -n ${CMAKE_VERBOSE} ]]; then
391 emake VERBOSE=1 "$@" || die "Make failed!"
392 else
393 emake "$@" || die "Make failed!"
394 fi
395 popd > /dev/null
396 }
397
398 enable_cmake-utils_src_install() {
399 debug-print-function ${FUNCNAME} "$@"
400
401 _check_build_dir
402 pushd "${CMAKE_BUILD_DIR}" > /dev/null
403 base_src_install "$@"
404 popd > /dev/null
405
406 # Backward compatibility, for non-array variables
407 if [[ -n "${DOCS}" ]] && [[ "$(declare -p DOCS 2>/dev/null 2>&1)" != "declare -a"* ]]; then
408 dodoc ${DOCS} || die "dodoc failed"
409 fi
410 if [[ -n "${HTML_DOCS}" ]] && [[ "$(declare -p HTML_DOCS 2>/dev/null 2>&1)" != "declare -a"* ]]; then
411 dohtml -r ${HTML_DOCS} || die "dohtml failed"
412 fi
413 }
414
415 enable_cmake-utils_src_test() {
416 debug-print-function ${FUNCNAME} "$@"
417 local ctestargs
418
419 _check_build_dir
420 pushd "${CMAKE_BUILD_DIR}" > /dev/null
421 [[ -e CTestTestfile.cmake ]] || { echo "No tests found. Skipping."; return 0 ; }
422
423 [[ -n ${TEST_VERBOSE} ]] && ctestargs="--extra-verbose --output-on-failure"
424 ctest ${ctestargs} "$@" || die "Tests failed."
425 popd > /dev/null
426 }
427
428 # @FUNCTION: cmake-utils_src_configure
429 # @DESCRIPTION:
430 # General function for configuring with cmake. Default behaviour is to start an
431 # out-of-source build.
432 cmake-utils_src_configure() {
433 _execute_optionaly "src_configure" "$@"
434 }
435
436 # @FUNCTION: cmake-utils_src_compile
437 # @DESCRIPTION:
438 # General function for compiling with cmake. Default behaviour is to check for
439 # EAPI and respectively to configure as well or just compile.
440 # Automatically detects the build type. All arguments are passed to emake.
441 cmake-utils_src_compile() {
442 _execute_optionaly "src_compile" "$@"
443 }
444
445 # @FUNCTION: cmake-utils_src_install
446 # @DESCRIPTION:
447 # Function for installing the package. Automatically detects the build type.
448 cmake-utils_src_install() {
449 _execute_optionaly "src_install" "$@"
450 }
451
452 # @FUNCTION: cmake-utils_src_test
453 # @DESCRIPTION:
454 # Function for testing the package. Automatically detects the build type.
455 cmake-utils_src_test() {
456 _execute_optionaly "src_test" "$@"
457 }
458
459 # Optionally executes phases based on WANT_CMAKE variable/USE flag.
460 _execute_optionaly() {
461 local phase="$1" ; shift
462 if [[ ${WANT_CMAKE} = always ]]; then
463 enable_cmake-utils_${phase} "$@"
464 else
465 use ${WANT_CMAKE} && enable_cmake-utils_${phase} "$@"
466 fi
467 }

  ViewVC Help
Powered by ViewVC 1.1.20