Removed curl dependency by using cmake internal curl
[platform/upstream/cmake.git] / Modules / CPackRPM.cmake
1 ##section Variables specific to CPack RPM generator
2 ##end
3 ##module
4 # - The builtin (binary) CPack RPM generator (Unix only)
5 # CPackRPM may be used to create RPM package using CPack.
6 # CPackRPM is a CPack generator thus it uses the CPACK_XXX variables
7 # used by CPack : http://www.cmake.org/Wiki/CMake:CPackConfiguration
8 #
9 # However CPackRPM has specific features which are controlled by
10 # the specifics CPACK_RPM_XXX variables. CPackRPM is a component aware
11 # generator so when CPACK_RPM_COMPONENT_INSTALL is ON some more
12 # CPACK_RPM_<ComponentName>_XXXX variables may be used in order
13 # to have component specific values. Note however that <componentName>
14 # refers to the **grouping name**. This may be either a component name
15 # or a component GROUP name.
16 # Usually those vars correspond to RPM spec file entities, one may find
17 # information about spec files here http://www.rpm.org/wiki/Docs.
18 # You'll find a detailed usage of CPackRPM on the wiki:
19 #  http://www.cmake.org/Wiki/CMake:CPackPackageGenerators#RPM_.28Unix_Only.29
20 # However as a handy reminder here comes the list of specific variables:
21 ##end
22 #
23 ##variable
24 #  CPACK_RPM_PACKAGE_SUMMARY - The RPM package summary.
25 #     Mandatory : YES
26 #     Default   : CPACK_PACKAGE_DESCRIPTION_SUMMARY
27 ##end
28 ##variable
29 #  CPACK_RPM_PACKAGE_NAME - The RPM package name.
30 #     Mandatory : YES
31 #     Default   : CPACK_PACKAGE_NAME
32 ##end
33 ##variable
34 #  CPACK_RPM_PACKAGE_VERSION - The RPM package version.
35 #     Mandatory : YES
36 #     Default   : CPACK_PACKAGE_VERSION
37 ##end
38 ##variable
39 #  CPACK_RPM_PACKAGE_ARCHITECTURE - The RPM package architecture.
40 #     Mandatory : NO
41 #     Default   : -
42 #     This may be set to "noarch" if you
43 #     know you are building a noarch package.
44 ##end
45 ##variable
46 #  CPACK_RPM_PACKAGE_RELEASE - The RPM package release.
47 #     Mandatory : YES
48 #     Default   : 1
49 #     This is the numbering of the RPM package
50 #     itself, i.e. the version of the packaging and not the version of the
51 #     content (see CPACK_RPM_PACKAGE_VERSION). One may change the default
52 #     value if the previous packaging was buggy and/or you want to put here
53 #     a fancy Linux distro specific numbering.
54 ##end
55 ##variable
56 #  CPACK_RPM_PACKAGE_LICENSE - The RPM package license policy.
57 #     Mandatory : YES
58 #     Default   : "unknown"
59 ##end
60 ##variable
61 #  CPACK_RPM_PACKAGE_GROUP - The RPM package group.
62 #     Mandatory : YES
63 #     Default   : "unknown"
64 ##end
65 ##variable
66 #  CPACK_RPM_PACKAGE_VENDOR - The RPM package vendor.
67 #     Mandatory : YES
68 #     Default   : CPACK_PACKAGE_VENDOR if set or "unknown"
69 ##end
70 ##variable
71 #  CPACK_RPM_PACKAGE_URL - The projects URL.
72 #     Mandatory : NO
73 #     Default   : -
74 ##end
75 ##variable
76 #  CPACK_RPM_PACKAGE_DESCRIPTION - RPM package description.
77 #     Mandatory : YES
78 #     Default   : CPACK_PACKAGE_DESCRIPTION_FILE if set or "no package description available"
79 ##end
80 ##variable
81 #  CPACK_RPM_COMPRESSION_TYPE - RPM compression type.
82 #     Mandatory : NO
83 #     Default   : -
84 #     May be used to override RPM compression type to be used
85 #     to build the RPM. For example some Linux distribution now default
86 #     to lzma or xz compression whereas older cannot use such RPM.
87 #     Using this one can enforce compression type to be used.
88 #     Possible value are: lzma, xz, bzip2 and gzip.
89 ##end
90 ##variable
91 #  CPACK_RPM_PACKAGE_REQUIRES - RPM spec requires field.
92 #     Mandatory : NO
93 #     Default   : -
94 #     May be used to set RPM dependencies (requires).
95 #     Note that you must enclose the complete requires string between quotes,
96 #     for example:
97 #     set(CPACK_RPM_PACKAGE_REQUIRES "python >= 2.5.0, cmake >= 2.8")
98 #     The required package list of an RPM file could be printed with
99 #     rpm -qp --requires file.rpm
100 ##end
101 ##variable
102 #  CPACK_RPM_PACKAGE_SUGGESTS - RPM spec suggest field.
103 #     Mandatory : NO
104 #     Default   : -
105 #     May be used to set weak RPM dependencies (suggests).
106 #     Note that you must enclose the complete requires string between quotes.
107 ##end
108 ##variable
109 #  CPACK_RPM_PACKAGE_PROVIDES - RPM spec provides field.
110 #     Mandatory : NO
111 #     Default   : -
112 #     May be used to set RPM dependencies (provides).
113 #     The provided package list of an RPM file could be printed with
114 #     rpm -qp --provides file.rpm
115 ##end
116 ##variable
117 #  CPACK_RPM_PACKAGE_OBSOLETES - RPM spec obsoletes field.
118 #     Mandatory : NO
119 #     Default   : -
120 #     May be used to set RPM packages that are obsoleted by this one.
121 ##end
122 ##variable
123 #  CPACK_RPM_PACKAGE_RELOCATABLE - build a relocatable RPM.
124 #     Mandatory : NO
125 #     Default   : CPACK_PACKAGE_RELOCATABLE
126 #     If this variable is set to TRUE or ON CPackRPM will try
127 #     to build a relocatable RPM package. A relocatable RPM may
128 #     be installed using rpm --prefix or --relocate in order to
129 #     install it at an alternate place see rpm(8).
130 #     Note that currently this may fail if CPACK_SET_DESTDIR is set to ON.
131 #     If CPACK_SET_DESTDIR is set then you will get a warning message
132 #     but if there is file installed with absolute path you'll get
133 #     unexpected behavior.
134 ##end
135 ##variable
136 #  CPACK_RPM_SPEC_INSTALL_POST - [deprecated].
137 #     Mandatory : NO
138 #     Default   : -
139 #     This way of specifying post-install script is deprecated use
140 #     CPACK_RPM_POST_INSTALL_SCRIPT_FILE
141 #     May be used to set an RPM post-install command inside the spec file.
142 #     For example setting it to "/bin/true" may be used to prevent
143 #     rpmbuild to strip binaries.
144 ##end
145 ##variable
146 #  CPACK_RPM_SPEC_MORE_DEFINE - RPM extended spec definitions lines.
147 #     Mandatory : NO
148 #     Default   : -
149 #     May be used to add any %define lines to the generated spec file.
150 ##end
151 ##variable
152 #  CPACK_RPM_PACKAGE_DEBUG - Toggle CPackRPM debug output.
153 #     Mandatory : NO
154 #     Default   : -
155 #     May be set when invoking cpack in order to trace debug information
156 #     during CPack RPM run. For example you may launch CPack like this
157 #     cpack -D CPACK_RPM_PACKAGE_DEBUG=1 -G RPM
158 ##end
159 ##variable
160 #  CPACK_RPM_USER_BINARY_SPECFILE - A user provided spec file.
161 #     Mandatory : NO
162 #     Default   : -
163 #     May be set by the user in order to specify a USER binary spec file
164 #     to be used by CPackRPM instead of generating the file.
165 #     The specified file will be processed by configure_file( @ONLY).
166 #     One can provide a component specific file by setting
167 #     CPACK_RPM_<componentName>_USER_BINARY_SPECFILE.
168 ##end
169 ##variable
170 #  CPACK_RPM_GENERATE_USER_BINARY_SPECFILE_TEMPLATE - Spec file template.
171 #     Mandatory : NO
172 #     Default   : -
173 #     If set CPack will generate a template for USER specified binary
174 #     spec file and stop with an error. For example launch CPack like this
175 #     cpack -D CPACK_RPM_GENERATE_USER_BINARY_SPECFILE_TEMPLATE=1 -G RPM
176 #     The user may then use this file in order to hand-craft is own
177 #     binary spec file which may be used with CPACK_RPM_USER_BINARY_SPECFILE.
178 ##end
179 ##variable
180 #  CPACK_RPM_PRE_INSTALL_SCRIPT_FILE
181 #  CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE
182 #     Mandatory : NO
183 #     Default   : -
184 #     May be used to embed a pre (un)installation script in the spec file.
185 #     The refered script file(s) will be read and directly
186 #     put after the %pre or %preun section
187 #     If CPACK_RPM_COMPONENT_INSTALL is set to ON the (un)install script for
188 #     each component can be overridden with
189 #     CPACK_RPM_<COMPONENT>_PRE_INSTALL_SCRIPT_FILE and
190 #     CPACK_RPM_<COMPONENT>_PRE_UNINSTALL_SCRIPT_FILE
191 #     One may verify which scriptlet has been included with
192 #      rpm -qp --scripts  package.rpm
193 ##end
194 ##variable
195 #  CPACK_RPM_POST_INSTALL_SCRIPT_FILE
196 #  CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE
197 #     Mandatory : NO
198 #     Default   : -
199 #     May be used to embed a post (un)installation script in the spec file.
200 #     The refered script file(s) will be read and directly
201 #     put after the %post or %postun section
202 #     If CPACK_RPM_COMPONENT_INSTALL is set to ON the (un)install script for
203 #     each component can be overridden with
204 #     CPACK_RPM_<COMPONENT>_POST_INSTALL_SCRIPT_FILE and
205 #     CPACK_RPM_<COMPONENT>_POST_UNINSTALL_SCRIPT_FILE
206 #     One may verify which scriptlet has been included with
207 #      rpm -qp --scripts  package.rpm
208 ##end
209 ##variable
210 #  CPACK_RPM_USER_FILELIST
211 #  CPACK_RPM_<COMPONENT>_USER_FILELIST
212 #     Mandatory : NO
213 #     Default   : -
214 #     May be used to explicitly specify %(<directive>) file line
215 #     in the spec file. Like %config(noreplace) or any other directive
216 #     that be found in the %files section. Since CPackRPM is generating
217 #     the list of files (and directories) the user specified files of
218 #     the CPACK_RPM_<COMPONENT>_USER_FILELIST list will be removed from the generated list.
219 ##end
220 ##variable
221 #  CPACK_RPM_CHANGELOG_FILE - RPM changelog file.
222 #     Mandatory : NO
223 #     Default   : -
224 #     May be used to embed a changelog in the spec file.
225 #     The refered file will be read and directly put after the %changelog
226 #     section.
227 ##end
228 ##variable
229 #  CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST - list of path to be excluded.
230 #     Mandatory : NO
231 #     Default   : /etc /etc/init.d /usr /usr/share /usr/share/doc /usr/bin /usr/lib /usr/lib64 /usr/include
232 #     May be used to exclude path (directories or files) from the auto-generated
233 #     list of paths discovered by CPack RPM. The defaut value contains a reasonable
234 #     set of values if the variable is not defined by the user. If the variable
235 #     is defined by the user then CPackRPM will NOT any of the default path.
236 #     If you want to add some path to the default list then you can use
237 #     CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION variable.
238 ##end
239 ##variable
240 #  CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION - additional list of path to be excluded.
241 #     Mandatory : NO
242 #     Default   : -
243 #     May be used to add more exclude path (directories or files) from the initial
244 #     default list of excluded paths. See CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST.
245 ##end
246
247 #=============================================================================
248 # Copyright 2007-2009 Kitware, Inc.
249 #
250 # Distributed under the OSI-approved BSD License (the "License");
251 # see accompanying file Copyright.txt for details.
252 #
253 # This software is distributed WITHOUT ANY WARRANTY; without even the
254 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
255 # See the License for more information.
256 #=============================================================================
257 # (To distribute this file outside of CMake, substitute the full
258 #  License text for the above reference.)
259
260 # Author: Eric Noulard with the help of Alexander Neundorf.
261
262 if(CMAKE_BINARY_DIR)
263   message(FATAL_ERROR "CPackRPM.cmake may only be used by CPack internally.")
264 endif()
265
266 if(NOT UNIX)
267   message(FATAL_ERROR "CPackRPM.cmake may only be used under UNIX.")
268 endif()
269
270 # rpmbuild is the basic command for building RPM package
271 # it may be a simple (symbolic) link to rpm command.
272 find_program(RPMBUILD_EXECUTABLE rpmbuild)
273
274 # Check version of the rpmbuild tool this would be easier to
275 # track bugs with users and CPackRPM debug mode.
276 # We may use RPM version in order to check for available version dependent features
277 if(RPMBUILD_EXECUTABLE)
278   execute_process(COMMAND ${RPMBUILD_EXECUTABLE} --version
279                   OUTPUT_VARIABLE _TMP_VERSION
280                   ERROR_QUIET
281                   OUTPUT_STRIP_TRAILING_WHITESPACE)
282   string(REGEX REPLACE "^.* " ""
283          RPMBUILD_EXECUTABLE_VERSION
284          ${_TMP_VERSION})
285   if(CPACK_RPM_PACKAGE_DEBUG)
286     message("CPackRPM:Debug: rpmbuild version is <${RPMBUILD_EXECUTABLE_VERSION}>")
287   endif()
288 endif()
289
290 if(NOT RPMBUILD_EXECUTABLE)
291   message(FATAL_ERROR "RPM package requires rpmbuild executable")
292 endif()
293
294 # Display lsb_release output if DEBUG mode enable
295 # This will help to diagnose problem with CPackRPM
296 # because we will know on which kind of Linux we are
297 if(CPACK_RPM_PACKAGE_DEBUG)
298   find_program(LSB_RELEASE_EXECUTABLE lsb_release)
299   if(LSB_RELEASE_EXECUTABLE)
300     execute_process(COMMAND ${LSB_RELEASE_EXECUTABLE} -a
301                     OUTPUT_VARIABLE _TMP_LSB_RELEASE_OUTPUT
302                     ERROR_QUIET
303                     OUTPUT_STRIP_TRAILING_WHITESPACE)
304     string(REGEX REPLACE "\n" ", "
305            LSB_RELEASE_OUTPUT
306            ${_TMP_LSB_RELEASE_OUTPUT})
307   else ()
308     set(LSB_RELEASE_OUTPUT "lsb_release not installed/found!")
309   endif()
310   message("CPackRPM:Debug: LSB_RELEASE  = ${LSB_RELEASE_OUTPUT}")
311 endif()
312
313 # We may use RPM version in the future in order
314 # to shut down warning about space in buildtree
315 # some recent RPM version should support space in different places.
316 # not checked [yet].
317 if(CPACK_TOPLEVEL_DIRECTORY MATCHES ".* .*")
318   message(FATAL_ERROR "${RPMBUILD_EXECUTABLE} can't handle paths with spaces, use a build directory without spaces for building RPMs.")
319 endif()
320
321 # If rpmbuild is found
322 # we try to discover alien since we may be on non RPM distro like Debian.
323 # In this case we may try to to use more advanced features
324 # like generating RPM directly from DEB using alien.
325 # FIXME feature not finished (yet)
326 find_program(ALIEN_EXECUTABLE alien)
327 if(ALIEN_EXECUTABLE)
328   message(STATUS "alien found, we may be on a Debian based distro.")
329 endif()
330
331 # Are we packaging components ?
332 if(CPACK_RPM_PACKAGE_COMPONENT)
333   set(CPACK_RPM_PACKAGE_COMPONENT_PART_NAME "-${CPACK_RPM_PACKAGE_COMPONENT}")
334 else()
335   set(CPACK_RPM_PACKAGE_COMPONENT_PART_NAME "")
336 endif()
337
338 set(WDIR "${CPACK_TOPLEVEL_DIRECTORY}/${CPACK_PACKAGE_FILE_NAME}${CPACK_RPM_PACKAGE_COMPONENT_PART_PATH}")
339
340 #
341 # Use user-defined RPM specific variables value
342 # or generate reasonable default value from
343 # CPACK_xxx generic values.
344 # The variables comes from the needed (mandatory or not)
345 # values found in the RPM specification file aka ".spec" file.
346 # The variables which may/should be defined are:
347 #
348
349 # CPACK_RPM_PACKAGE_SUMMARY (mandatory)
350 if(NOT CPACK_RPM_PACKAGE_SUMMARY)
351   # if neither var is defined lets use the name as summary
352   if(NOT CPACK_PACKAGE_DESCRIPTION_SUMMARY)
353     string(TOLOWER "${CPACK_PACKAGE_NAME}" CPACK_RPM_PACKAGE_SUMMARY)
354   else()
355     set(CPACK_RPM_PACKAGE_SUMMARY ${CPACK_PACKAGE_DESCRIPTION_SUMMARY})
356   endif()
357 endif()
358
359 # CPACK_RPM_PACKAGE_NAME (mandatory)
360 if(NOT CPACK_RPM_PACKAGE_NAME)
361   string(TOLOWER "${CPACK_PACKAGE_NAME}" CPACK_RPM_PACKAGE_NAME)
362 endif()
363
364 # CPACK_RPM_PACKAGE_VERSION (mandatory)
365 if(NOT CPACK_RPM_PACKAGE_VERSION)
366   if(NOT CPACK_PACKAGE_VERSION)
367     message(FATAL_ERROR "RPM package requires a package version")
368   endif()
369   set(CPACK_RPM_PACKAGE_VERSION ${CPACK_PACKAGE_VERSION})
370 endif()
371 # Replace '-' in version with '_'
372 # '-' character is  an Illegal RPM version character
373 # it is illegal because it is used to separate
374 # RPM "Version" from RPM "Release"
375 string(REPLACE "-" "_" CPACK_RPM_PACKAGE_VERSION ${CPACK_RPM_PACKAGE_VERSION})
376
377 # CPACK_RPM_PACKAGE_ARCHITECTURE (optional)
378 if(CPACK_RPM_PACKAGE_ARCHITECTURE)
379   set(TMP_RPM_BUILDARCH "Buildarch: ${CPACK_RPM_PACKAGE_ARCHITECTURE}")
380   if(CPACK_RPM_PACKAGE_DEBUG)
381     message("CPackRPM:Debug: using user-specified build arch = ${CPACK_RPM_PACKAGE_ARCHITECTURE}")
382   endif()
383 else()
384   set(TMP_RPM_BUILDARCH "")
385 endif()
386
387 # CPACK_RPM_PACKAGE_RELEASE
388 # The RPM release is the numbering of the RPM package ITSELF
389 # this is the version of the PACKAGING and NOT the version
390 # of the CONTENT of the package.
391 # You may well need to generate a new RPM package release
392 # without changing the version of the packaged software.
393 # This is the case when the packaging is buggy (not) the software :=)
394 # If not set, 1 is a good candidate
395 if(NOT CPACK_RPM_PACKAGE_RELEASE)
396   set(CPACK_RPM_PACKAGE_RELEASE 1)
397 endif()
398
399 # CPACK_RPM_PACKAGE_LICENSE
400 if(NOT CPACK_RPM_PACKAGE_LICENSE)
401   set(CPACK_RPM_PACKAGE_LICENSE "unknown")
402 endif()
403
404 # CPACK_RPM_PACKAGE_GROUP
405 if(NOT CPACK_RPM_PACKAGE_GROUP)
406   set(CPACK_RPM_PACKAGE_GROUP "unknown")
407 endif()
408
409 # CPACK_RPM_PACKAGE_VENDOR
410 if(NOT CPACK_RPM_PACKAGE_VENDOR)
411   if(CPACK_PACKAGE_VENDOR)
412     set(CPACK_RPM_PACKAGE_VENDOR "${CPACK_PACKAGE_VENDOR}")
413   else()
414     set(CPACK_RPM_PACKAGE_VENDOR "unknown")
415   endif()
416 endif()
417
418 # CPACK_RPM_PACKAGE_SOURCE
419 # The name of the source tarball in case we generate a source RPM
420
421 # CPACK_RPM_PACKAGE_DESCRIPTION
422 # The variable content may be either
423 #   - explicitly given by the user or
424 #   - filled with the content of CPACK_PACKAGE_DESCRIPTION_FILE
425 #     if it is defined
426 #   - set to a default value
427 #
428 if (NOT CPACK_RPM_PACKAGE_DESCRIPTION)
429         if (CPACK_PACKAGE_DESCRIPTION_FILE)
430                 file(READ ${CPACK_PACKAGE_DESCRIPTION_FILE} CPACK_RPM_PACKAGE_DESCRIPTION)
431         else ()
432                 set(CPACK_RPM_PACKAGE_DESCRIPTION "no package description available")
433         endif ()
434 endif ()
435
436 # CPACK_RPM_COMPRESSION_TYPE
437 #
438 if (CPACK_RPM_COMPRESSION_TYPE)
439    if(CPACK_RPM_PACKAGE_DEBUG)
440      message("CPackRPM:Debug: User Specified RPM compression type: ${CPACK_RPM_COMPRESSION_TYPE}")
441    endif()
442    if(CPACK_RPM_COMPRESSION_TYPE STREQUAL "lzma")
443      set(CPACK_RPM_COMPRESSION_TYPE_TMP "%define _binary_payload w9.lzdio")
444    endif()
445    if(CPACK_RPM_COMPRESSION_TYPE STREQUAL "xz")
446      set(CPACK_RPM_COMPRESSION_TYPE_TMP "%define _binary_payload w7.xzdio")
447    endif()
448    if(CPACK_RPM_COMPRESSION_TYPE STREQUAL "bzip2")
449      set(CPACK_RPM_COMPRESSION_TYPE_TMP "%define _binary_payload w9.bzdio")
450    endif()
451    if(CPACK_RPM_COMPRESSION_TYPE STREQUAL "gzip")
452      set(CPACK_RPM_COMPRESSION_TYPE_TMP "%define _binary_payload w9.gzdio")
453    endif()
454 else()
455    set(CPACK_RPM_COMPRESSION_TYPE_TMP "")
456 endif()
457
458 if(CPACK_PACKAGE_RELOCATABLE)
459   set(CPACK_RPM_PACKAGE_RELOCATABLE TRUE)
460 endif()
461 if(CPACK_RPM_PACKAGE_RELOCATABLE)
462   if(CPACK_RPM_PACKAGE_DEBUG)
463     message("CPackRPM:Debug: Trying to build a relocatable package")
464   endif()
465   if(CPACK_SET_DESTDIR AND (NOT CPACK_SET_DESTDIR STREQUAL "I_ON"))
466     message("CPackRPM:Warning: CPACK_SET_DESTDIR is set (=${CPACK_SET_DESTDIR}) while requesting a relocatable package (CPACK_RPM_PACKAGE_RELOCATABLE is set): this is not supported, the package won't be relocatable.")
467   else()
468     set(CPACK_RPM_PACKAGE_PREFIX ${CPACK_PACKAGING_INSTALL_PREFIX})
469   endif()
470 endif()
471
472 # Check if additional fields for RPM spec header are given
473 # There may be some COMPONENT specific variables as well
474 # If component specific var is not provided we use the global one
475 # for each component
476 foreach(_RPM_SPEC_HEADER URL REQUIRES SUGGESTS PROVIDES OBSOLETES PREFIX CONFLICTS AUTOPROV AUTOREQ AUTOREQPROV)
477     if(CPACK_RPM_PACKAGE_DEBUG)
478       message("CPackRPM:Debug: processing ${_RPM_SPEC_HEADER}")
479     endif()
480     if(CPACK_RPM_PACKAGE_COMPONENT)
481         if(DEFINED CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_PACKAGE_${_RPM_SPEC_HEADER})
482             if(CPACK_RPM_PACKAGE_DEBUG)
483               message("CPackRPM:Debug: using CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_PACKAGE_${_RPM_SPEC_HEADER}")
484             endif()
485             set(CPACK_RPM_PACKAGE_${_RPM_SPEC_HEADER}_TMP ${CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_PACKAGE_${_RPM_SPEC_HEADER}})
486         else()
487             if(DEFINED CPACK_RPM_PACKAGE_${_RPM_SPEC_HEADER})
488               if(CPACK_RPM_PACKAGE_DEBUG)
489                 message("CPackRPM:Debug: CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_PACKAGE_${_RPM_SPEC_HEADER} not defined")
490                 message("CPackRPM:Debug: using CPACK_RPM_PACKAGE_${_RPM_SPEC_HEADER}")
491               endif()
492               set(CPACK_RPM_PACKAGE_${_RPM_SPEC_HEADER}_TMP ${CPACK_RPM_PACKAGE_${_RPM_SPEC_HEADER}})
493             endif()
494         endif()
495     else()
496         if(DEFINED CPACK_RPM_PACKAGE_${_RPM_SPEC_HEADER})
497           if(CPACK_RPM_PACKAGE_DEBUG)
498             message("CPackRPM:Debug: using CPACK_RPM_PACKAGE_${_RPM_SPEC_HEADER}")
499           endif()
500           set(CPACK_RPM_PACKAGE_${_RPM_SPEC_HEADER}_TMP ${CPACK_RPM_PACKAGE_${_RPM_SPEC_HEADER}})
501         endif()
502     endif()
503
504   # Treat the RPM Spec keyword iff it has been properly defined
505   if(DEFINED CPACK_RPM_PACKAGE_${_RPM_SPEC_HEADER}_TMP)
506     # Transform NAME --> Name e.g. PROVIDES --> Provides
507     # The Upper-case first letter and lowercase tail is the
508     # appropriate value required in the final RPM spec file.
509     string(LENGTH ${_RPM_SPEC_HEADER} _PACKAGE_HEADER_STRLENGTH)
510     math(EXPR _PACKAGE_HEADER_STRLENGTH "${_PACKAGE_HEADER_STRLENGTH} - 1")
511     string(SUBSTRING ${_RPM_SPEC_HEADER} 1 ${_PACKAGE_HEADER_STRLENGTH} _PACKAGE_HEADER_TAIL)
512     string(TOLOWER "${_PACKAGE_HEADER_TAIL}" _PACKAGE_HEADER_TAIL)
513     string(SUBSTRING ${_RPM_SPEC_HEADER} 0 1 _PACKAGE_HEADER_NAME)
514     set(_PACKAGE_HEADER_NAME "${_PACKAGE_HEADER_NAME}${_PACKAGE_HEADER_TAIL}")
515     if(CPACK_RPM_PACKAGE_DEBUG)
516       message("CPackRPM:Debug: User defined ${_PACKAGE_HEADER_NAME}:\n ${CPACK_RPM_PACKAGE_${_RPM_SPEC_HEADER}_TMP}")
517     endif()
518     set(TMP_RPM_${_RPM_SPEC_HEADER} "${_PACKAGE_HEADER_NAME}: ${CPACK_RPM_PACKAGE_${_RPM_SPEC_HEADER}_TMP}")
519   else()
520     # Do not forget to unset previously set header (from previous component)
521     unset(TMP_RPM_${_RPM_SPEC_HEADER})
522   endif()
523 endforeach()
524
525 # CPACK_RPM_SPEC_INSTALL_POST
526 # May be used to define a RPM post intallation script
527 # for example setting it to "/bin/true" may prevent
528 # rpmbuild from stripping binaries.
529 if(CPACK_RPM_SPEC_INSTALL_POST)
530   if(CPACK_RPM_PACKAGE_DEBUG)
531     message("CPackRPM:Debug: User defined CPACK_RPM_SPEC_INSTALL_POST = ${CPACK_RPM_SPEC_INSTALL_POST}")
532   endif()
533   set(TMP_RPM_SPEC_INSTALL_POST "%define __spec_install_post ${CPACK_RPM_SPEC_INSTALL_POST}")
534 endif()
535
536 # CPACK_RPM_POST_INSTALL_SCRIPT_FILE (or CPACK_RPM_<COMPONENT>_POST_INSTALL_SCRIPT_FILE)
537 # CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE (or CPACK_RPM_<COMPONENT>_POST_UNINSTALL_SCRIPT_FILE)
538 # May be used to embed a post (un)installation script in the spec file.
539 # The refered script file(s) will be read and directly
540 # put after the %post or %postun section
541 if(CPACK_RPM_PACKAGE_COMPONENT)
542   if(CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_POST_INSTALL_SCRIPT_FILE)
543     set(CPACK_RPM_POST_INSTALL_READ_FILE ${CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_POST_INSTALL_SCRIPT_FILE})
544   else()
545     set(CPACK_RPM_POST_INSTALL_READ_FILE ${CPACK_RPM_POST_INSTALL_SCRIPT_FILE})
546   endif()
547   if(CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_POST_UNINSTALL_SCRIPT_FILE)
548     set(CPACK_RPM_POST_UNINSTALL_READ_FILE ${CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_POST_UNINSTALL_SCRIPT_FILE})
549   else()
550     set(CPACK_RPM_POST_UNINSTALL_READ_FILE ${CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE})
551   endif()
552 else()
553   set(CPACK_RPM_POST_INSTALL_READ_FILE ${CPACK_RPM_POST_INSTALL_SCRIPT_FILE})
554   set(CPACK_RPM_POST_UNINSTALL_READ_FILE ${CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE})
555 endif()
556
557 # Handle post-install file if it has been specified
558 if(CPACK_RPM_POST_INSTALL_READ_FILE)
559   if(EXISTS ${CPACK_RPM_POST_INSTALL_READ_FILE})
560     file(READ ${CPACK_RPM_POST_INSTALL_READ_FILE} CPACK_RPM_SPEC_POSTINSTALL)
561   else()
562     message("CPackRPM:Warning: CPACK_RPM_POST_INSTALL_SCRIPT_FILE <${CPACK_RPM_POST_INSTALL_READ_FILE}> does not exists - ignoring")
563   endif()
564 else()
565   # reset SPEC var value if no post install file has been specified
566   # (either globally or component-wise)
567   set(CPACK_RPM_SPEC_POSTINSTALL "")
568 endif()
569
570 # Handle post-uninstall file if it has been specified
571 if(CPACK_RPM_POST_UNINSTALL_READ_FILE)
572   if(EXISTS ${CPACK_RPM_POST_UNINSTALL_READ_FILE})
573     file(READ ${CPACK_RPM_POST_UNINSTALL_READ_FILE} CPACK_RPM_SPEC_POSTUNINSTALL)
574   else()
575     message("CPackRPM:Warning: CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE <${CPACK_RPM_POST_UNINSTALL_READ_FILE}> does not exists - ignoring")
576   endif()
577 else()
578   # reset SPEC var value if no post uninstall file has been specified
579   # (either globally or component-wise)
580   set(CPACK_RPM_SPEC_POSTUNINSTALL "")
581 endif()
582
583 # CPACK_RPM_PRE_INSTALL_SCRIPT_FILE (or CPACK_RPM_<COMPONENT>_PRE_INSTALL_SCRIPT_FILE)
584 # CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE (or CPACK_RPM_<COMPONENT>_PRE_UNINSTALL_SCRIPT_FILE)
585 # May be used to embed a pre (un)installation script in the spec file.
586 # The refered script file(s) will be read and directly
587 # put after the %pre or %preun section
588 if(CPACK_RPM_PACKAGE_COMPONENT)
589   if(CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_PRE_INSTALL_SCRIPT_FILE)
590     set(CPACK_RPM_PRE_INSTALL_READ_FILE ${CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_PRE_INSTALL_SCRIPT_FILE})
591   else()
592     set(CPACK_RPM_PRE_INSTALL_READ_FILE ${CPACK_RPM_PRE_INSTALL_SCRIPT_FILE})
593   endif()
594   if(CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_PRE_UNINSTALL_SCRIPT_FILE)
595     set(CPACK_RPM_PRE_UNINSTALL_READ_FILE ${CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_PRE_UNINSTALL_SCRIPT_FILE})
596   else()
597     set(CPACK_RPM_PRE_UNINSTALL_READ_FILE ${CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE})
598   endif()
599 else()
600   set(CPACK_RPM_PRE_INSTALL_READ_FILE ${CPACK_RPM_PRE_INSTALL_SCRIPT_FILE})
601   set(CPACK_RPM_PRE_UNINSTALL_READ_FILE ${CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE})
602 endif()
603
604 # Handle pre-install file if it has been specified
605 if(CPACK_RPM_PRE_INSTALL_READ_FILE)
606   if(EXISTS ${CPACK_RPM_PRE_INSTALL_READ_FILE})
607     file(READ ${CPACK_RPM_PRE_INSTALL_READ_FILE} CPACK_RPM_SPEC_PREINSTALL)
608   else()
609     message("CPackRPM:Warning: CPACK_RPM_PRE_INSTALL_SCRIPT_FILE <${CPACK_RPM_PRE_INSTALL_READ_FILE}> does not exists - ignoring")
610   endif()
611 else()
612   # reset SPEC var value if no pre-install file has been specified
613   # (either globally or component-wise)
614   set(CPACK_RPM_SPEC_PREINSTALL "")
615 endif()
616
617 # Handle pre-uninstall file if it has been specified
618 if(CPACK_RPM_PRE_UNINSTALL_READ_FILE)
619   if(EXISTS ${CPACK_RPM_PRE_UNINSTALL_READ_FILE})
620     file(READ ${CPACK_RPM_PRE_UNINSTALL_READ_FILE} CPACK_RPM_SPEC_PREUNINSTALL)
621   else()
622     message("CPackRPM:Warning: CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE <${CPACK_RPM_PRE_UNINSTALL_READ_FILE}> does not exists - ignoring")
623   endif()
624 else()
625   # reset SPEC var value if no pre-uninstall file has been specified
626   # (either globally or component-wise)
627   set(CPACK_RPM_SPEC_PREUNINSTALL "")
628 endif()
629
630 # CPACK_RPM_CHANGELOG_FILE
631 # May be used to embed a changelog in the spec file.
632 # The refered file will be read and directly put after the %changelog section
633 if(CPACK_RPM_CHANGELOG_FILE)
634   if(EXISTS ${CPACK_RPM_CHANGELOG_FILE})
635     file(READ ${CPACK_RPM_CHANGELOG_FILE} CPACK_RPM_SPEC_CHANGELOG)
636   else()
637     message(SEND_ERROR "CPackRPM:Warning: CPACK_RPM_CHANGELOG_FILE <${CPACK_RPM_CHANGELOG_FILE}> does not exists - ignoring")
638   endif()
639 else()
640   set(CPACK_RPM_SPEC_CHANGELOG "* Sun Jul 4 2010 Eric Noulard <eric.noulard@gmail.com> - ${CPACK_RPM_PACKAGE_VERSION}-${CPACK_RPM_PACKAGE_RELEASE}\n  Generated by CPack RPM (no Changelog file were provided)")
641 endif()
642
643 # CPACK_RPM_SPEC_MORE_DEFINE
644 # This is a generated spec rpm file spaceholder
645 if(CPACK_RPM_SPEC_MORE_DEFINE)
646   if(CPACK_RPM_PACKAGE_DEBUG)
647     message("CPackRPM:Debug: User defined more define spec line specified:\n ${CPACK_RPM_SPEC_MORE_DEFINE}")
648   endif()
649 endif()
650
651 # Now we may create the RPM build tree structure
652 set(CPACK_RPM_ROOTDIR "${CPACK_TOPLEVEL_DIRECTORY}")
653 message(STATUS "CPackRPM:Debug: Using CPACK_RPM_ROOTDIR=${CPACK_RPM_ROOTDIR}")
654 # Prepare RPM build tree
655 file(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR})
656 file(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR}/tmp)
657 file(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR}/BUILD)
658 file(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR}/RPMS)
659 file(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR}/SOURCES)
660 file(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR}/SPECS)
661 file(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR}/SRPMS)
662
663 #set(CPACK_RPM_FILE_NAME "${CPACK_RPM_PACKAGE_NAME}-${CPACK_RPM_PACKAGE_VERSION}-${CPACK_RPM_PACKAGE_RELEASE}-${CPACK_RPM_PACKAGE_ARCHITECTURE}.rpm")
664 set(CPACK_RPM_FILE_NAME "${CPACK_OUTPUT_FILE_NAME}")
665 # it seems rpmbuild can't handle spaces in the path
666 # neither escaping (as below) nor putting quotes around the path seem to help
667 #string(REGEX REPLACE " " "\\\\ " CPACK_RPM_DIRECTORY "${CPACK_TOPLEVEL_DIRECTORY}")
668 set(CPACK_RPM_DIRECTORY "${CPACK_TOPLEVEL_DIRECTORY}")
669
670 # if we are creating a relocatable package, omit parent directories of
671 # CPACK_RPM_PACKAGE_PREFIX. This is achieved by building a "filter list"
672 # which is passed to the find command that generates the content-list
673 if(CPACK_RPM_PACKAGE_RELOCATABLE)
674   # get a list of the elements in CPACK_RPM_PACKAGE_PREFIX and remove
675   # the final element (so the install-prefix dir itself is not omitted
676   # from the RPM's content-list)
677   string(REPLACE "/" ";" _CPACK_RPM_PACKAGE_PREFIX_ELEMS ".${CPACK_RPM_PACKAGE_PREFIX}")
678   list(REMOVE_AT _CPACK_RPM_PACKAGE_PREFIX_ELEMS -1)
679   # Now generate all of the parent dirs of CPACK_RPM_PACKAGE_PREFIX
680   foreach(_ELEM ${_CPACK_RPM_PACKAGE_PREFIX_ELEMS})
681     list(APPEND _TMP_LIST "${_ELEM}")
682     string(REPLACE ";" "/" _OMIT_DIR "${_TMP_LIST}")
683     set(_OMIT_DIR "-o -path ${_OMIT_DIR}")
684     separate_arguments(_OMIT_DIR)
685     list(APPEND _RPM_DIRS_TO_OMIT ${_OMIT_DIR})
686   endforeach()
687 endif()
688
689 if (CPACK_RPM_PACKAGE_DEBUG)
690    message("CPackRPM:Debug: Initial list of path to OMIT in RPM: ${_RPM_DIRS_TO_OMIT}")
691 endif()
692
693 if (NOT DEFINED CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST)
694   set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST /etc /etc/init.d /usr /usr/share /usr/share/doc /usr/bin /usr/lib /usr/lib64 /usr/include)
695   if (CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION)
696     message("CPackRPM:Debug: Adding ${CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION} to builtin omit list.")
697     list(APPEND CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST "${CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION}")
698   endif()
699 endif()
700
701 if(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST)
702   if (CPACK_RPM_PACKAGE_DEBUG)
703    message("CPackRPM:Debug: CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST= ${CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST}")
704  endif()
705   foreach(_DIR ${CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST})
706     list(APPEND _RPM_DIRS_TO_OMIT "-o;-path;.${_DIR}")
707   endforeach()
708 endif()
709 if (CPACK_RPM_PACKAGE_DEBUG)
710    message("CPackRPM:Debug: Final list of path to OMIT in RPM: ${_RPM_DIRS_TO_OMIT}")
711 endif()
712
713 # Use files tree to construct files command (spec file)
714 # We should not forget to include symlinks (thus -o -type l)
715 # We should include directory as well (thus -type d)
716 #   but not the main local dir "." (thus -a -not -name ".")
717 # We must remove the './' due to the local search and escape the
718 # file name by enclosing it between double quotes (thus the sed)
719 # Then we must authorize any man pages extension (adding * at the end)
720 # because rpmbuild may automatically compress those files
721 execute_process(COMMAND find . -type f -o -type l -o (-type d -a -not ( -name "." ${_RPM_DIRS_TO_OMIT} ) )
722                 COMMAND sed s:.*/man.*/.*:&*:
723                 COMMAND sed s/\\.\\\(.*\\\)/\"\\1\"/
724                 WORKING_DIRECTORY "${WDIR}"
725                 OUTPUT_VARIABLE CPACK_RPM_INSTALL_FILES)
726
727 # In component case, put CPACK_ABSOLUTE_DESTINATION_FILES_<COMPONENT>
728 #                   into CPACK_ABSOLUTE_DESTINATION_FILES_INTERNAL
729 #         otherwise, put CPACK_ABSOLUTE_DESTINATION_FILES
730 # This must be done BEFORE the CPACK_ABSOLUTE_DESTINATION_FILES_INTERNAL handling
731 if(CPACK_RPM_PACKAGE_COMPONENT)
732   if(CPACK_ABSOLUTE_DESTINATION_FILES)
733    set(COMPONENT_FILES_TAG "CPACK_ABSOLUTE_DESTINATION_FILES_${CPACK_RPM_PACKAGE_COMPONENT}")
734    set(CPACK_ABSOLUTE_DESTINATION_FILES_INTERNAL "${${COMPONENT_FILES_TAG}}")
735    if(CPACK_RPM_PACKAGE_DEBUG)
736      message("CPackRPM:Debug: Handling Absolute Destination Files: <${CPACK_ABSOLUTE_DESTINATION_FILES_INTERNAL}>")
737      message("CPackRPM:Debug: in component = ${CPACK_RPM_PACKAGE_COMPONENT}")
738    endif()
739   endif()
740 else()
741   if(CPACK_ABSOLUTE_DESTINATION_FILES)
742     set(CPACK_ABSOLUTE_DESTINATION_FILES_INTERNAL "${CPACK_ABSOLUTE_DESTINATION_FILES}")
743   endif()
744 endif()
745
746 # In component case, set CPACK_RPM_USER_FILELIST_INTERNAL with CPACK_RPM_<COMPONENT>_USER_FILELIST.
747 if(CPACK_RPM_PACKAGE_COMPONENT)
748   if(CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_USER_FILELIST)
749     set(CPACK_RPM_USER_FILELIST_INTERNAL ${CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_USER_FILELIST})
750     if(CPACK_RPM_PACKAGE_DEBUG)
751       message("CPackRPM:Debug: Handling User Filelist: <${CPACK_RPM_USER_FILELIST_INTERNAL}>")
752       message("CPackRPM:Debug: in component = ${CPACK_RPM_PACKAGE_COMPONENT}")
753     endif()
754   else()
755     set(CPACK_RPM_USER_FILELIST_INTERNAL "")
756   endif()
757 else()
758   if(CPACK_RPM_USER_FILELIST)
759     set(CPACK_RPM_USER_FILELIST_INTERNAL "${CPACK_RPM_USER_FILELIST}")
760   else()
761     set(CPACK_RPM_USER_FILELIST_INTERNAL "")
762   endif()
763 endif()
764
765 # Handle user specified file line list in CPACK_RPM_USER_FILELIST_INTERNAL
766 # Remove those files from CPACK_ABSOLUTE_DESTINATION_FILES_INTERNAL
767 #                      or CPACK_RPM_INSTALL_FILES,
768 # hence it must be done before these auto-generated lists are processed.
769 if(CPACK_RPM_USER_FILELIST_INTERNAL)
770   if(CPACK_RPM_PACKAGE_DEBUG)
771     message("CPackRPM:Debug: Handling User Filelist: <${CPACK_RPM_USER_FILELIST_INTERNAL}>")
772   endif()
773
774   # Create CMake list from CPACK_RPM_INSTALL_FILES
775   string(STRIP "${CPACK_RPM_INSTALL_FILES}" CPACK_RPM_INSTALL_FILES_LIST)
776   string(REPLACE "\n" ";" CPACK_RPM_INSTALL_FILES_LIST
777                           "${CPACK_RPM_INSTALL_FILES_LIST}")
778   string(REPLACE "\"" "" CPACK_RPM_INSTALL_FILES_LIST
779                           "${CPACK_RPM_INSTALL_FILES_LIST}")
780
781   set(CPACK_RPM_USER_INSTALL_FILES "")
782   foreach(F IN LISTS CPACK_RPM_USER_FILELIST_INTERNAL)
783     string(REGEX REPLACE "%[A-Za-z0-9\(\),-]* " "" F_PATH ${F})
784     string(REGEX MATCH "%[A-Za-z0-9\(\),-]*" F_PREFIX ${F})
785
786     if(CPACK_RPM_PACKAGE_DEBUG)
787       message("CPackRPM:Debug: F_PREFIX=<${F_PREFIX}>, F_PATH=<${F_PATH}>")
788     endif()
789     if(F_PREFIX)
790       set(F_PREFIX "${F_PREFIX} ")
791     endif()
792     # Rebuild the user list file
793     set(CPACK_RPM_USER_INSTALL_FILES "${CPACK_RPM_USER_INSTALL_FILES}${F_PREFIX}\"${F_PATH}\"\n")
794
795     # Remove from CPACK_RPM_INSTALL_FILES and CPACK_ABSOLUTE_DESTINATION_FILES_INTERNAL
796     list(REMOVE_ITEM CPACK_RPM_INSTALL_FILES_LIST ${F_PATH})
797     # ABSOLUTE destination files list may not exists at all
798     if (CPACK_ABSOLUTE_DESTINATION_FILES_INTERNAL)
799       list(REMOVE_ITEM CPACK_ABSOLUTE_DESTINATION_FILES_INTERNAL ${F_PATH})
800     endif()
801
802   endforeach()
803
804   # Rebuild CPACK_RPM_INSTALL_FILES
805   set(CPACK_RPM_INSTALL_FILES "")
806   foreach(F IN LISTS CPACK_RPM_INSTALL_FILES_LIST)
807     set(CPACK_RPM_INSTALL_FILES "${CPACK_RPM_INSTALL_FILES}\"${F}\"\n")
808   endforeach()
809 else()
810   set(CPACK_RPM_USER_INSTALL_FILES "")
811 endif()
812
813 if (CPACK_ABSOLUTE_DESTINATION_FILES_INTERNAL)
814   if(CPACK_RPM_PACKAGE_DEBUG)
815     message("CPackRPM:Debug: Handling Absolute Destination Files: ${CPACK_ABSOLUTE_DESTINATION_FILES_INTERNAL}")
816   endif()
817   # Remove trailing space
818   string(STRIP "${CPACK_RPM_INSTALL_FILES}" CPACK_RPM_INSTALL_FILES_LIST)
819   # Transform endline separated - string into CMake List
820   string(REPLACE "\n" ";" CPACK_RPM_INSTALL_FILES_LIST "${CPACK_RPM_INSTALL_FILES_LIST}")
821   # Remove unecessary quotes
822   string(REPLACE "\"" "" CPACK_RPM_INSTALL_FILES_LIST "${CPACK_RPM_INSTALL_FILES_LIST}")
823   # Remove ABSOLUTE install file from INSTALL FILE LIST
824   list(REMOVE_ITEM CPACK_RPM_INSTALL_FILES_LIST ${CPACK_ABSOLUTE_DESTINATION_FILES_INTERNAL})
825   # Rebuild INSTALL_FILES
826   set(CPACK_RPM_INSTALL_FILES "")
827   foreach(F IN LISTS CPACK_RPM_INSTALL_FILES_LIST)
828     set(CPACK_RPM_INSTALL_FILES "${CPACK_RPM_INSTALL_FILES}\"${F}\"\n")
829   endforeach()
830   # Build ABSOLUTE_INSTALL_FILES
831   set(CPACK_RPM_ABSOLUTE_INSTALL_FILES "")
832   foreach(F IN LISTS CPACK_ABSOLUTE_DESTINATION_FILES_INTERNAL)
833     set(CPACK_RPM_ABSOLUTE_INSTALL_FILES "${CPACK_RPM_ABSOLUTE_INSTALL_FILES}%config \"${F}\"\n")
834   endforeach()
835   if(CPACK_RPM_PACKAGE_DEBUG)
836     message("CPackRPM:Debug: CPACK_RPM_ABSOLUTE_INSTALL_FILES=${CPACK_RPM_ABSOLUTE_INSTALL_FILES}")
837     message("CPackRPM:Debug: CPACK_RPM_INSTALL_FILES=${CPACK_RPM_INSTALL_FILES}")
838   endif()
839 else()
840   # reset vars in order to avoid leakage of value(s) from one component to another
841   set(CPACK_RPM_ABSOLUTE_INSTALL_FILES "")
842 endif()
843
844 # Prepend directories in ${CPACK_RPM_INSTALL_FILES} with %dir
845 # This is necessary to avoid duplicate files since rpmbuild do
846 # recursion on its own when encountering a pathname which is a directory
847 # which is not flagged as %dir
848 string(STRIP "${CPACK_RPM_INSTALL_FILES}" CPACK_RPM_INSTALL_FILES_LIST)
849 string(REPLACE "\n" ";" CPACK_RPM_INSTALL_FILES_LIST
850                         "${CPACK_RPM_INSTALL_FILES_LIST}")
851 string(REPLACE "\"" "" CPACK_RPM_INSTALL_FILES_LIST
852                         "${CPACK_RPM_INSTALL_FILES_LIST}")
853 set(CPACK_RPM_INSTALL_FILES "")
854 foreach(F IN LISTS CPACK_RPM_INSTALL_FILES_LIST)
855   if(IS_DIRECTORY "${WDIR}/${F}")
856     set(CPACK_RPM_INSTALL_FILES "${CPACK_RPM_INSTALL_FILES}%dir \"${F}\"\n")
857   else()
858     set(CPACK_RPM_INSTALL_FILES "${CPACK_RPM_INSTALL_FILES}\"${F}\"\n")
859   endif()
860 endforeach()
861 set(CPACK_RPM_INSTALL_FILES_LIST "")
862
863 # The name of the final spec file to be used by rpmbuild
864 set(CPACK_RPM_BINARY_SPECFILE "${CPACK_RPM_ROOTDIR}/SPECS/${CPACK_RPM_PACKAGE_NAME}${CPACK_RPM_PACKAGE_COMPONENT_PART_NAME}.spec")
865
866 # Print out some debug information if we were asked for that
867 if(CPACK_RPM_PACKAGE_DEBUG)
868    message("CPackRPM:Debug: CPACK_TOPLEVEL_DIRECTORY          = ${CPACK_TOPLEVEL_DIRECTORY}")
869    message("CPackRPM:Debug: CPACK_TOPLEVEL_TAG                = ${CPACK_TOPLEVEL_TAG}")
870    message("CPackRPM:Debug: CPACK_TEMPORARY_DIRECTORY         = ${CPACK_TEMPORARY_DIRECTORY}")
871    message("CPackRPM:Debug: CPACK_OUTPUT_FILE_NAME            = ${CPACK_OUTPUT_FILE_NAME}")
872    message("CPackRPM:Debug: CPACK_OUTPUT_FILE_PATH            = ${CPACK_OUTPUT_FILE_PATH}")
873    message("CPackRPM:Debug: CPACK_PACKAGE_FILE_NAME           = ${CPACK_PACKAGE_FILE_NAME}")
874    message("CPackRPM:Debug: CPACK_RPM_BINARY_SPECFILE         = ${CPACK_RPM_BINARY_SPECFILE}")
875    message("CPackRPM:Debug: CPACK_PACKAGE_INSTALL_DIRECTORY   = ${CPACK_PACKAGE_INSTALL_DIRECTORY}")
876    message("CPackRPM:Debug: CPACK_TEMPORARY_PACKAGE_FILE_NAME = ${CPACK_TEMPORARY_PACKAGE_FILE_NAME}")
877 endif()
878
879 # protect @ in pathname in order to avoid their
880 # interpretation during the configure_file step
881 set(CPACK_RPM_INSTALL_FILES_LIST "${CPACK_RPM_INSTALL_FILES}")
882 set(PROTECTED_AT "@")
883 string(REPLACE "@" "\@PROTECTED_AT\@" CPACK_RPM_INSTALL_FILES "${CPACK_RPM_INSTALL_FILES_LIST}")
884 set(CPACK_RPM_INSTALL_FILES_LIST "")
885
886 #
887 # USER generated/provided spec file handling.
888 #
889
890 # We can have a component specific spec file.
891 if(CPACK_RPM_PACKAGE_COMPONENT AND CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_USER_BINARY_SPECFILE)
892   set(CPACK_RPM_USER_BINARY_SPECFILE ${CPACK_RPM_${CPACK_RPM_PACKAGE_COMPONENT}_USER_BINARY_SPECFILE})
893 endif()
894
895 # We should generate a USER spec file template:
896 #  - either because the user asked for it : CPACK_RPM_GENERATE_USER_BINARY_SPECFILE_TEMPLATE
897 #  - or the user did not provide one : NOT CPACK_RPM_USER_BINARY_SPECFILE
898 if(CPACK_RPM_GENERATE_USER_BINARY_SPECFILE_TEMPLATE OR NOT CPACK_RPM_USER_BINARY_SPECFILE)
899    file(WRITE ${CPACK_RPM_BINARY_SPECFILE}.in
900       "# -*- rpm-spec -*-
901 BuildRoot:      \@CPACK_RPM_DIRECTORY\@/\@CPACK_PACKAGE_FILE_NAME\@\@CPACK_RPM_PACKAGE_COMPONENT_PART_PATH\@
902 Summary:        \@CPACK_RPM_PACKAGE_SUMMARY\@
903 Name:           \@CPACK_RPM_PACKAGE_NAME\@\@CPACK_RPM_PACKAGE_COMPONENT_PART_NAME\@
904 Version:        \@CPACK_RPM_PACKAGE_VERSION\@
905 Release:        \@CPACK_RPM_PACKAGE_RELEASE\@
906 License:        \@CPACK_RPM_PACKAGE_LICENSE\@
907 Group:          \@CPACK_RPM_PACKAGE_GROUP\@
908 Vendor:         \@CPACK_RPM_PACKAGE_VENDOR\@
909 \@TMP_RPM_URL\@
910 \@TMP_RPM_REQUIRES\@
911 \@TMP_RPM_PROVIDES\@
912 \@TMP_RPM_OBSOLETES\@
913 \@TMP_RPM_CONFLICTS\@
914 \@TMP_RPM_AUTOPROV\@
915 \@TMP_RPM_AUTOREQ\@
916 \@TMP_RPM_AUTOREQPROV\@
917 \@TMP_RPM_BUILDARCH\@
918 \@TMP_RPM_PREFIX\@
919
920 %define _rpmdir \@CPACK_RPM_DIRECTORY\@
921 %define _rpmfilename \@CPACK_RPM_FILE_NAME\@
922 %define _unpackaged_files_terminate_build 0
923 %define _topdir \@CPACK_RPM_DIRECTORY\@
924 \@TMP_RPM_SPEC_INSTALL_POST\@
925 \@CPACK_RPM_SPEC_MORE_DEFINE\@
926 \@CPACK_RPM_COMPRESSION_TYPE_TMP\@
927
928 %description
929 \@CPACK_RPM_PACKAGE_DESCRIPTION\@
930
931 # This is a shortcutted spec file generated by CMake RPM generator
932 # we skip _install step because CPack does that for us.
933 # We do only save CPack installed tree in _prepr
934 # and then restore it in build.
935 %prep
936 mv $RPM_BUILD_ROOT \"\@CPACK_TOPLEVEL_DIRECTORY\@/tmpBBroot\"
937
938 #p build
939
940 %install
941 if [ -e $RPM_BUILD_ROOT ];
942 then
943   rm -rf $RPM_BUILD_ROOT
944 fi
945 mv \"\@CPACK_TOPLEVEL_DIRECTORY\@/tmpBBroot\" $RPM_BUILD_ROOT
946
947 %clean
948
949 %post
950 \@CPACK_RPM_SPEC_POSTINSTALL\@
951
952 %postun
953 \@CPACK_RPM_SPEC_POSTUNINSTALL\@
954
955 %pre
956 \@CPACK_RPM_SPEC_PREINSTALL\@
957
958 %preun
959 \@CPACK_RPM_SPEC_PREUNINSTALL\@
960
961 %files
962 %defattr(-,root,root,-)
963 \@CPACK_RPM_INSTALL_FILES\@
964 \@CPACK_RPM_ABSOLUTE_INSTALL_FILES\@
965 \@CPACK_RPM_USER_INSTALL_FILES\@
966
967 %changelog
968 \@CPACK_RPM_SPEC_CHANGELOG\@
969 ")
970   # Stop here if we were asked to only generate a template USER spec file
971   # The generated file may then be used as a template by user who wants
972   # to customize their own spec file.
973   if(CPACK_RPM_GENERATE_USER_BINARY_SPECFILE_TEMPLATE)
974      message(FATAL_ERROR "CPackRPM: STOP here Generated USER binary spec file templare is: ${CPACK_RPM_BINARY_SPECFILE}.in")
975   endif()
976 endif()
977
978 # After that we may either use a user provided spec file
979 # or generate one using appropriate variables value.
980 if(CPACK_RPM_USER_BINARY_SPECFILE)
981   # User may have specified SPECFILE just use it
982   message("CPackRPM: Will use USER specified spec file: ${CPACK_RPM_USER_BINARY_SPECFILE}")
983   # The user provided file is processed for @var replacement
984   configure_file(${CPACK_RPM_USER_BINARY_SPECFILE} ${CPACK_RPM_BINARY_SPECFILE} @ONLY)
985 else()
986   # No User specified spec file, will use the generated spec file
987   message("CPackRPM: Will use GENERATED spec file: ${CPACK_RPM_BINARY_SPECFILE}")
988   # Note the just created file is processed for @var replacement
989   configure_file(${CPACK_RPM_BINARY_SPECFILE}.in ${CPACK_RPM_BINARY_SPECFILE} @ONLY)
990 endif()
991
992 # remove AT protection
993 unset(PROTECTED_AT)
994
995 if(RPMBUILD_EXECUTABLE)
996   # Now call rpmbuild using the SPECFILE
997   execute_process(
998     COMMAND "${RPMBUILD_EXECUTABLE}" -bb
999             --define "_topdir ${CPACK_RPM_DIRECTORY}"
1000             --buildroot "${CPACK_RPM_DIRECTORY}/${CPACK_PACKAGE_FILE_NAME}${CPACK_RPM_PACKAGE_COMPONENT_PART_PATH}"
1001             "${CPACK_RPM_BINARY_SPECFILE}"
1002     WORKING_DIRECTORY "${CPACK_TOPLEVEL_DIRECTORY}/${CPACK_PACKAGE_FILE_NAME}${CPACK_RPM_PACKAGE_COMPONENT_PART_PATH}"
1003     RESULT_VARIABLE CPACK_RPMBUILD_EXEC_RESULT
1004     ERROR_FILE "${CPACK_TOPLEVEL_DIRECTORY}/rpmbuild${CPACK_RPM_PACKAGE_COMPONENT_PART_NAME}.err"
1005     OUTPUT_FILE "${CPACK_TOPLEVEL_DIRECTORY}/rpmbuild${CPACK_RPM_PACKAGE_COMPONENT_PART_NAME}.out")
1006   if(CPACK_RPM_PACKAGE_DEBUG OR CPACK_RPMBUILD_EXEC_RESULT)
1007     file(READ ${CPACK_TOPLEVEL_DIRECTORY}/rpmbuild${CPACK_RPM_PACKAGE_COMPONENT_PART_NAME}.err RPMBUILDERR)
1008     file(READ ${CPACK_TOPLEVEL_DIRECTORY}/rpmbuild${CPACK_RPM_PACKAGE_COMPONENT_PART_NAME}.out RPMBUILDOUT)
1009     message("CPackRPM:Debug: You may consult rpmbuild logs in: ")
1010     message("CPackRPM:Debug:    - ${CPACK_TOPLEVEL_DIRECTORY}/rpmbuild${CPACK_RPM_PACKAGE_COMPONENT_PART_NAME}.err")
1011     message("CPackRPM:Debug: *** ${RPMBUILDERR} ***")
1012     message("CPackRPM:Debug:    - ${CPACK_TOPLEVEL_DIRECTORY}/rpmbuild${CPACK_RPM_PACKAGE_COMPONENT_PART_NAME}.out")
1013     message("CPackRPM:Debug: *** ${RPMBUILDERR} ***")
1014   endif()
1015 else()
1016   if(ALIEN_EXECUTABLE)
1017     message(FATAL_ERROR "RPM packaging through alien not done (yet)")
1018   endif()
1019 endif()