ENH: fix for Vista
[cmake.git] / Modules / CPackRPM.cmake
blobe441e69ea73abf281055b57ad9ab5128e49dbcbb
1 # CPack script for creating RPM package
2 # Author: Eric Noulard with the help of Alexander Neundorf.
3 # All variables used by CPackRPM begins with CPACK_RPM_ prefix
5 # Here comes the list of used variables:
8 IF(CMAKE_BINARY_DIR)
9   MESSAGE(FATAL_ERROR "CPackRPM.cmake may only be used by CPack internally.")
10 ENDIF(CMAKE_BINARY_DIR)
12 IF(NOT UNIX)
13   MESSAGE(FATAL_ERROR "CPackRPM.cmake may only be used under UNIX.")
14 ENDIF(NOT UNIX)
16 # rpmbuild is the basic command for building RPM package
17 # it may be a simple (symbolic) link to rpmb command.
18 FIND_PROGRAM(RPMBUILD_EXECUTABLE rpmbuild)
20 IF(NOT RPMBUILD_EXECUTABLE)
21   MESSAGE(FATAL_ERROR "RPM package requires rpmbuild executable")
22 ENDIF(NOT RPMBUILD_EXECUTABLE)
24 IF(CPACK_TOPLEVEL_DIRECTORY MATCHES ".* .*")
25   MESSAGE(FATAL_ERROR "${RPMBUILD_EXECUTABLE} can't handle paths with spaces, use a build directory without spaces for building RPMs.")
26 ENDIF(CPACK_TOPLEVEL_DIRECTORY MATCHES ".* .*")
28 # If rpmbuild is found 
29 # we try to discover alien since we may be on non RPM distro like Debian.
30 # In this case we may try to to use more advanced features
31 # like generating RPM directly from DEB using alien.
32 # FIXME feature not finished (yet)
33 FIND_PROGRAM(ALIEN_EXECUTABLE alien)
34 IF(ALIEN_EXECUTABLE)
35   MESSAGE(STATUS "alien found, we may be on a Debian based distro.")
36 ENDIF(ALIEN_EXECUTABLE)
38
39 # Use user-defined RPM specific variables value
40 # or generate reasonable default value from
41 # CPACK_xxx generic values.
42 # The variables comes from the needed (mandatory or not)
43 # values found in the RPM specification file aka ".spec" file.
44 # The variables which may/should be defined are:
47 # CPACK_RPM_PACKAGE_SUMMARY (mandatory)
48 IF(NOT CPACK_RPM_PACKAGE_SUMMARY)
49   # if neither var is defined lets use the name as summary
50   IF(NOT CPACK_PACKAGE_DESCRIPTION_SUMMARY)
51     STRING(TOLOWER "${CPACK_PACKAGE_NAME}" CPACK_RPM_PACKAGE_SUMMARY)
52   ELSE(NOT CPACK_PACKAGE_DESCRIPTION_SUMMARY)
53     SET(CPACK_RPM_PACKAGE_SUMMARY ${CPACK_PACKAGE_DESCRIPTION_SUMMARY})    
54   ENDIF(NOT CPACK_PACKAGE_DESCRIPTION_SUMMARY)
55 ENDIF(NOT CPACK_RPM_PACKAGE_SUMMARY)
57 # CPACK_RPM_PACKAGE_NAME (mandatory)
58 IF(NOT CPACK_RPM_PACKAGE_NAME)
59   STRING(TOLOWER "${CPACK_PACKAGE_NAME}" CPACK_RPM_PACKAGE_NAME)
60 ENDIF(NOT CPACK_RPM_PACKAGE_NAME)
62 # CPACK_RPM_PACKAGE_VERSION (mandatory)
63 IF(NOT CPACK_RPM_PACKAGE_VERSION)
64   IF(NOT CPACK_PACKAGE_VERSION)
65     MESSAGE(FATAL_ERROR "RPM package requires a package version")
66   ENDIF(NOT CPACK_PACKAGE_VERSION)
67   SET(CPACK_RPM_PACKAGE_VERSION ${CPACK_PACKAGE_VERSION})
68 ENDIF(NOT CPACK_RPM_PACKAGE_VERSION)
70 # CPACK_RPM_PACKAGE_ARCHITECTURE (optional)
71 IF(CPACK_RPM_PACKAGE_ARCHITECTURE)
72   SET(TMP_RPM_BUILDARCH "Buildarch: ${CPACK_RPM_PACKAGE_ARCHITECTURE}")
73   IF(CPACK_RPM_PACKAGE_DEBUG)
74     MESSAGE("CPackRPM:Debug: using user-specified build arch = ${CPACK_RPM_PACKAGE_ARCHITECTURE}")
75   ENDIF(CPACK_RPM_PACKAGE_DEBUG)
76 ELSE(CPACK_RPM_PACKAGE_ARCHITECTURE)
77   SET(TMP_RPM_BUILDARCH "")
78 ENDIF(CPACK_RPM_PACKAGE_ARCHITECTURE)
80 # CPACK_RPM_PACKAGE_RELEASE
81 # The RPM release is the numbering of the RPM package ITSELF
82 # this is the version of the PACKAGING and NOT the version
83 # of the CONTENT of the package.
84 # You may well need to generate a new RPM package release
85 # without changing the version of the packaged software.
86 # This is the case when the packaging is buggy (not) the software :=)
87 # If not set, 1 is a good candidate
88 IF(NOT CPACK_RPM_PACKAGE_RELEASE)
89   SET(CPACK_RPM_PACKAGE_RELEASE 1)
90 ENDIF(NOT CPACK_RPM_PACKAGE_RELEASE)
92 # CPACK_RPM_PACKAGE_LICENSE
93 IF(NOT CPACK_RPM_PACKAGE_LICENSE)
94   SET(CPACK_RPM_PACKAGE_LICENSE "unknown")
95 ENDIF(NOT CPACK_RPM_PACKAGE_LICENSE)
97 # CPACK_RPM_PACKAGE_GROUP
98 IF(NOT CPACK_RPM_PACKAGE_GROUP)
99   SET(CPACK_RPM_PACKAGE_GROUP "unknown")
100 ENDIF(NOT CPACK_RPM_PACKAGE_GROUP)
102 # CPACK_RPM_PACKAGE_VENDOR
103 IF(NOT CPACK_RPM_PACKAGE_VENDOR)
104   IF(CPACK_PACKAGE_VENDOR)
105     SET(CPACK_RPM_PACKAGE_VENDOR "${CPACK_PACKAGE_VENDOR}")
106   ELSE(CPACK_PACKAGE_VENDOR)
107     SET(CPACK_RPM_PACKAGE_VENDOR "unknown")
108   ENDIF(CPACK_PACKAGE_VENDOR)
109 ENDIF(NOT CPACK_RPM_PACKAGE_VENDOR)
111 # CPACK_RPM_PACKAGE_SOURCE
112 # The name of the source tarball in case we generate a source RPM
114 # CPACK_RPM_PACKAGE_DESCRIPTION
115 # The variable content may be either
116 #   - explicitely given by tthe user or
117 #   - filled with the content of CPACK_PACKAGE_DESCRIPTION_FILE
118 #     if it is defined
119 #   - set to a default value
121 IF (NOT CPACK_RPM_PACKAGE_DESCRIPTION)
122         IF (CPACK_PACKAGE_DESCRIPTION_FILE)
123                 FILE(READ ${CPACK_PACKAGE_DESCRIPTION_FILE} CPACK_RPM_PACKAGE_DESCRIPTION)
124         ELSE (CPACK_PACKAGE_DESCRIPTION_FILE)
125                 SET(CPACK_RPM_PACKAGE_DESCRIPTION "no package description available")
126         ENDIF (CPACK_PACKAGE_DESCRIPTION_FILE)
127 ENDIF (NOT CPACK_RPM_PACKAGE_DESCRIPTION)
129 # CPACK_RPM_SPEC_INSTALL_POST
130 # May be used to define a RPM post intallation script
131 # for example setting it to "/bin/true" may prevent
132 # rpmbuild from stripping binaries.
133 IF(CPACK_RPM_SPEC_INSTALL_POST)
134   SET(TMP_RPM_SPEC_INSTALL_POST "%define __spec_install_post ${CPACK_RPM_SPEC_INSTALL_POST}")
135 ENDIF(CPACK_RPM_SPEC_INSTALL_POST)
137 # CPACK_RPM_SPEC_MORE_DEFINE
138 # This is a generated spec rpm file spaceholder
139 IF(CPACK_RPM_SPEC_MORE_DEFINE)
140   IF(CPACK_RPM_PACKAGE_DEBUG)
141     MESSAGE("CPackRPM:Debug: User defined more define spec line specified:\n ${CPACK_RPM_SPEC_MORE_DEFINE}")
142   ENDIF(CPACK_RPM_PACKAGE_DEBUG)
143 ENDIF(CPACK_RPM_SPEC_MORE_DEFINE)
145 # CPACK_RPM_USER_BINARY_SPECFILE 
146 # FIXME when this is set then CPack should us the 
147 # user provided file.
149 # Now we may create the RPM build tree structure
150 SET(CPACK_RPM_ROOTDIR "${CPACK_TOPLEVEL_DIRECTORY}")
151 MESSAGE(STATUS "CPackRPM:Debug: Using CPACK_RPM_ROOTDIR=${CPACK_RPM_ROOTDIR}")
152 # Prepare RPM build tree
153 FILE(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR})
154 FILE(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR}/tmp)
155 FILE(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR}/BUILD)
156 FILE(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR}/RPMS)
157 FILE(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR}/SOURCES)
158 FILE(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR}/SPECS)
159 FILE(MAKE_DIRECTORY ${CPACK_RPM_ROOTDIR}/SRPMS)
161 #SET(CPACK_RPM_FILE_NAME "${CPACK_RPM_PACKAGE_NAME}-${CPACK_RPM_PACKAGE_VERSION}-${CPACK_RPM_PACKAGE_RELEASE}-${CPACK_RPM_PACKAGE_ARCHITECTURE}.rpm")
162 SET(CPACK_RPM_FILE_NAME "${CPACK_OUTPUT_FILE_NAME}")
163 # it seems rpmbuild can't handle spaces in the path
164 # neither escaping (as below) nor putting quotes around the path seem to help
165 #STRING(REGEX REPLACE " " "\\\\ " CPACK_RPM_DIRECTORY "${CPACK_TOPLEVEL_DIRECTORY}")
166 SET(CPACK_RPM_DIRECTORY "${CPACK_TOPLEVEL_DIRECTORY}")
167   
169 SET(CPACK_RPM_BINARY_SPECFILE "${CPACK_RPM_ROOTDIR}/SPECS/${CPACK_RPM_PACKAGE_NAME}.spec")
170 IF(CPACK_RPM_USER_BINARY_SPECFILE)
171   # User may have specified SPECFILE just use it
172   MESSAGE("CPackRPM: Will use user specified spec file: ${CPACK_RPM_USER_BINARY_SPECFILE}")
173   # Note that user provided file is processed for @var replacement
174   CONFIGURE_FILE(${CPACK_RPM_USER_BINARY_SPECFILE} ${CPACK_RPM_BINARY_SPECFILE} @ONLY)
175 ELSE(CPACK_RPM_USER_BINARY_SPECFILE)
176   # No User specified spec file generate a valid one using var values
177   IF(CPACK_RPM_PACKAGE_DEBUG)
178     MESSAGE("CPackRPM:Debug: CPACK_TOPLEVEL_DIRECTORY          = ${CPACK_TOPLEVEL_DIRECTORY}")
179     MESSAGE("CPackRPM:Debug: CPACK_TOPLEVEL_TAG                = ${CPACK_TOPLEVEL_TAG}")
180     MESSAGE("CPackRPM:Debug: CPACK_TEMPORARY_DIRECTORY         = ${PACK_TEMPORARY_DIRECTORY}")
181     MESSAGE("CPackRPM:Debug: CPACK_OUTPUT_FILE_NAME            = ${CPACK_OUTPUT_FILE_NAME}")
182     MESSAGE("CPackRPM:Debug: CPACK_OUTPUT_FILE_PATH            = ${CPACK_OUTPUT_FILE_PATH}")
183     MESSAGE("CPackRPM:Debug: CPACK_PACKAGE_FILE_NAME           = ${CPACK_PACKAGE_FILE_NAME}")
184     MESSAGE("CPackRPM:Debug: CPACK_RPM_BINARY_SPECFILE         = ${CPACK_RPM_BINARY_SPECFILE}")
185     MESSAGE("CPackRPM:Debug: CPACK_PACKAGE_INSTALL_DIRECTORY   = ${PACK_PACKAGE_INSTALL_DIRECTORY}")
186     MESSAGE("CPackRPM:Debug: CPACK_TEMPORARY_PACKAGE_FILE_NAME = ${CPACK_TEMPORARY_PACKAGE_FILE_NAME}")
187     MESSAGE("CPackRPM:Debug: CPACK_RPM_SPEC_INSTALL_POST       = ${CPACK_RPM_SPEC_INSTALL_POST}")
188   ENDIF(CPACK_RPM_PACKAGE_DEBUG)
189   FILE(WRITE ${CPACK_RPM_BINARY_SPECFILE}
190     "# -*- rpm-spec -*-
191 Buildroot:      ${CPACK_RPM_DIRECTORY}/${CPACK_PACKAGE_FILE_NAME}
192 Summary:        ${CPACK_RPM_PACKAGE_SUMMARY}
193 Name:           ${CPACK_RPM_PACKAGE_NAME}
194 Version:        ${CPACK_RPM_PACKAGE_VERSION}
195 Release:        ${CPACK_RPM_PACKAGE_RELEASE}
196 License:        ${CPACK_RPM_PACKAGE_LICENSE}
197 Group:          ${CPACK_RPM_PACKAGE_GROUP}
198 Vendor:         ${CPACK_RPM_PACKAGE_VENDOR}
199 ${TMP_RPM_BUILDARCH}
201 #%define prefix ${CMAKE_INSTALL_PREFIX}
202 %define _rpmdir ${CPACK_RPM_DIRECTORY}
203 %define _rpmfilename ${CPACK_RPM_FILE_NAME}
204 %define _unpackaged_files_terminate_build 0
205 %define _topdir ${CPACK_RPM_DIRECTORY}
206 ${TMP_RPM_SPEC_INSTALL_POST}
207 ${CPACK_RPM_SPEC_MORE_DEFINE}
209 %description
210 ${CPACK_RPM_PACKAGE_DESCRIPTION}
212 # This is a shortcutted spec file
213 # generated by CMake RPM generator
214 # we skip the _prepn _build and _install
215 # steps because CPack does that for us
216 #%prep
218 #%build
219   
220 #%install
222 %clean
224 %files
225 %defattr(-,root,root,-)
226 #%dir %{prefix}
227 #%{prefix}/*
230 %changelog
231 * Mon Aug 25 2008 Erk <eric.noulard@gmail.com>
232   Update generator to handle optional post-install
233 * Tue Aug 16 2007 Erk <eric.noulard@gmail.com>
234   Generated by CPack RPM Generator and associated macros
236 ENDIF(CPACK_RPM_USER_BINARY_SPECFILE)
239 IF(RPMBUILD_EXECUTABLE)
240   # Now call rpmbuild using the SPECFILE
241   EXECUTE_PROCESS(
242     COMMAND "${RPMBUILD_EXECUTABLE}" -bb "${CPACK_RPM_BINARY_SPECFILE}"
243     WORKING_DIRECTORY "${CPACK_TOPLEVEL_DIRECTORY}/${CPACK_PACKAGE_FILE_NAME}"
244     ERROR_FILE "${CPACK_TOPLEVEL_DIRECTORY}/rpmbuild.err"
245     OUTPUT_FILE "${CPACK_TOPLEVEL_DIRECTORY}/rpmbuild.out")
246   IF(CPACK_RPM_PACKAGE_DEBUG)
247     MESSAGE("CPackRPM:Debug: You may consult rpmbuild logs in: ")
248     MESSAGE("CPackRPM:Debug:    - ${CPACK_TOPLEVEL_DIRECTORY}/rpmbuild.err")
249     MESSAGE("CPackRPM:Debug:    - ${CPACK_TOPLEVEL_DIRECTORY}/rpmbuild.out")
250   ENDIF(CPACK_RPM_PACKAGE_DEBUG)
251 ELSE(RPMBUILD_EXECUTABLE)
252   IF(ALIEN_EXECUTABLE)
253     MESSAGE(FATAL_ERROR "RPM packaging through alien not done (yet)")
254   ENDIF(ALIEN_EXECUTABLE)
255 ENDIF(RPMBUILD_EXECUTABLE)