• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1## -*- mode:cmake; coding:utf-8; -*-
2# Copyright (c) 2010 Daniel Pfeifer <daniel@pfeifer-mail.de>
3# Changes Copyright (c) 2011 2012 Rüdiger Sonderfeld <ruediger@c-plusplus.de>
4#
5# UploadPPA.cmake is free software. It comes without any warranty,
6# to the extent permitted by applicable law. You can redistribute it
7# and/or modify it under the terms of the Do What The Fuck You Want
8# To Public License, Version 2, as published by Sam Hocevar. See
9# http://sam.zoy.org/wtfpl/COPYING for more details.
10#
11##
12# Documentation
13#
14# This CMake module uploads a project to a PPA.  It creates all the files
15# necessary (similar to CPack) and uses debuild(1) and dput(1) to create the
16# package and upload it to a PPA.  A PPA is a Personal Package Archive and can
17# be used by Debian/Ubuntu or other apt/deb based distributions to install and
18# update packages from a remote repository.
19# Canonicals Launchpad (http://launchpad.net) is usually used to host PPAs.
20# See https://help.launchpad.net/Packaging/PPA for further information
21# about PPAs.
22#
23# UploadPPA.cmake uses similar settings to CPack and the CPack DEB Generator.
24# Additionally the following variables are used
25#
26# CPACK_DEBIAN_PACKAGE_BUILD_DEPENDS to specify build dependencies
27# (cmake is added as default)
28# CPACK_DEBIAN_RESOURCE_FILE_CHANGELOG should point to a file containing the
29# changelog in debian format.  If not set it checks whether a file
30# debian/changelog exists in the source directory or creates a simply changelog
31# file.
32# CPACK_DEBIAN_UPDATE_CHANGELOG if set to True then UploadPPA.cmake adds a new
33# entry to the changelog with the current version number and distribution name
34# (lsb_release -c is used).  This can be useful because debuild uses the latest
35# version number from the changelog and the version number set in
36# CPACK_PACKAGE_VERSION.  If they mismatch the creation of the package fails.
37#
38## A.Hoarau : CHANGELOG_MESSAGE can be used to pass a custom changelog message
39# Check packages
40#
41# ./configure -DENABLE_PPA=On
42# make dput
43# cd build/Debian/${DISTRI}
44# dpkg-source -x vobsub2srt_1.0pre4-ppa1.dsc
45# cd vobsub2srt-1.0pre4/
46# debuild -i -us -uc -sa -b
47#
48# Check the lintian warnings!
49#
50##
51# TODO
52# I plan to add support for git dch (from git-buildpackage) to auto generate
53# the changelog.
54##
55
56find_program(DEBUILD_EXECUTABLE debuild)
57find_program(DPUT_EXECUTABLE dput)
58
59if(NOT DEBUILD_EXECUTABLE OR NOT DPUT_EXECUTABLE)
60  message(WARNING "Debuild or dput not installed, please run sudo apt-get install devscripts")
61  return()
62endif(NOT DEBUILD_EXECUTABLE OR NOT DPUT_EXECUTABLE)
63
64
65if(NOT PROJECT_PPA_DISTRIB_TARGET)
66execute_process(
67    COMMAND lsb_release -cs
68    OUTPUT_VARIABLE DISTRI
69    OUTPUT_STRIP_TRAILING_WHITESPACE)
70    set(PROJECT_PPA_DISTRIB_TARGET ${DISTRI})
71    message(STATUS "PROJECT_PPA_DISTRIB_TARGET NOT provided, so using system settings : ${DISTRI}")
72endif()
73
74foreach(DISTRI ${PROJECT_PPA_DISTRIB_TARGET})
75message(STATUS "Building for ${DISTRI}")
76
77# Strip "-dirty" flag from package version.
78# It can be added by, e.g., git describe but it causes trouble with debuild etc.
79string(REPLACE "-dirty" "" CPACK_PACKAGE_VERSION ${CPACK_PACKAGE_VERSION})
80message(STATUS "version: ${CPACK_PACKAGE_VERSION}")
81
82# DEBIAN/control
83# debian policy enforce lower case for package name
84# Package: (mandatory)
85IF(NOT CPACK_DEBIAN_PACKAGE_NAME)
86  STRING(TOLOWER "${CPACK_PACKAGE_NAME}" CPACK_DEBIAN_PACKAGE_NAME)
87ENDIF(NOT CPACK_DEBIAN_PACKAGE_NAME)
88
89# Section: (recommended)
90IF(NOT CPACK_DEBIAN_PACKAGE_SECTION)
91  SET(CPACK_DEBIAN_PACKAGE_SECTION "devel")
92ENDIF(NOT CPACK_DEBIAN_PACKAGE_SECTION)
93
94# Priority: (recommended)
95IF(NOT CPACK_DEBIAN_PACKAGE_PRIORITY)
96  SET(CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
97ENDIF(NOT CPACK_DEBIAN_PACKAGE_PRIORITY)
98
99if(NOT CPACK_DEBIAN_PACKAGE_MAINTAINER)
100  set(CPACK_DEBIAN_PACKAGE_MAINTAINER ${CPACK_PACKAGE_CONTACT})
101endif()
102
103if(NOT CPACK_PACKAGE_DESCRIPTION AND EXISTS ${CPACK_PACKAGE_DESCRIPTION_FILE})
104  file(STRINGS ${CPACK_PACKAGE_DESCRIPTION_FILE} DESC_LINES)
105  foreach(LINE ${DESC_LINES})
106    set(deb_long_description "${deb_long_description} ${LINE}\n")
107  endforeach(LINE ${DESC_LINES})
108else()
109  # add space before each line
110  string(REPLACE "\n" "\n " deb_long_description " ${CPACK_PACKAGE_DESCRIPTION}")
111endif()
112
113if(PPA_DEBIAN_VERSION)
114  set(DEBIAN_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}-${PPA_DEBIAN_VERSION}~${DISTRI}1")
115elseif(NOT PPA_DEBIAN_VERSION AND NOT PROJECT_PPA_DISTRIB_TARGET)
116  message(WARNING "Variable PPA_DEBIAN_VERSION not set! Building 'native' package!")
117  set(DEBIAN_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}")
118else()
119  set(DEBIAN_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}~${DISTRI}1")
120endif()
121
122message(STATUS "Debian version: ${DEBIAN_PACKAGE_VERSION}")
123
124set(DEBIAN_SOURCE_DIR ${CMAKE_BINARY_DIR}/Debian/${DISTRI}/${CPACK_DEBIAN_PACKAGE_NAME}_${DEBIAN_PACKAGE_VERSION})
125
126##############################################################################
127# debian/control
128
129set(debian_control ${DEBIAN_SOURCE_DIR}/debian/control)
130list(APPEND CPACK_DEBIAN_PACKAGE_BUILD_DEPENDS "cmake" "debhelper (>= 7.0.50)")
131list(REMOVE_DUPLICATES CPACK_DEBIAN_PACKAGE_BUILD_DEPENDS)
132list(SORT CPACK_DEBIAN_PACKAGE_BUILD_DEPENDS)
133string(REPLACE ";" ", " build_depends "${CPACK_DEBIAN_PACKAGE_BUILD_DEPENDS}")
134string(REPLACE ";" ", " bin_depends "${CPACK_DEBIAN_PACKAGE_DEPENDS}")
135file(WRITE ${debian_control}
136  "Source: ${CPACK_DEBIAN_PACKAGE_NAME}\n"
137  "Section: ${CPACK_DEBIAN_PACKAGE_SECTION}\n"
138  "Priority: ${CPACK_DEBIAN_PACKAGE_PRIORITY}\n"
139  "Maintainer: ${CPACK_DEBIAN_PACKAGE_MAINTAINER}\n"
140  "Build-Depends: ${build_depends}\n"
141  "Standards-Version: 3.9.7\n"
142  "Homepage: ${CPACK_DEBIAN_PACKAGE_HOMEPAGE}\n"
143  "\n"
144  "Package: ${CPACK_DEBIAN_PACKAGE_NAME}\n"
145  "Architecture: ${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}\n"
146  "Depends: ${bin_depends}, \${shlibs:Depends}, \${misc:Depends}\n"
147  "Description: ${CPACK_PACKAGE_DESCRIPTION_SUMMARY}\n"
148  "${deb_long_description}"
149  )
150
151file(APPEND ${debian_control}
152  "\n\n"
153  "Package: ${CPACK_DEBIAN_PACKAGE_NAME}-dbg\n"
154  "Priority: extra\n"
155  "Section: debug\n"
156  "Architecture: any\n"
157  "Depends: ${CPACK_DEBIAN_PACKAGE_NAME} (= \${binary:Version}), \${shlibs:Depends}, \${misc:Depends}\n"
158  "Description: ${CPACK_PACKAGE_DESCRIPTION_SUMMARY}\n"
159  "${deb_long_description}"
160  "\n .\n"
161  " This is the debugging symbols for the ${CPACK_DEBIAN_PACKAGE_NAME} library"
162 )
163
164foreach(COMPONENT ${CPACK_COMPONENTS_ALL})
165  string(TOUPPER ${COMPONENT} UPPER_COMPONENT)
166  set(DEPENDS "${CPACK_DEBIAN_PACKAGE_NAME}")
167  foreach(DEP ${CPACK_COMPONENT_${UPPER_COMPONENT}_DEPENDS})
168    set(DEPENDS "${DEPENDS}, ${CPACK_DEBIAN_PACKAGE_NAME}-${DEP}")
169  endforeach(DEP ${CPACK_COMPONENT_${UPPER_COMPONENT}_DEPENDS})
170  file(APPEND ${debian_control} "\n"
171    "Package: ${CPACK_DEBIAN_PACKAGE_NAME}-${COMPONENT}\n"
172    "Architecture: ${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}\n"
173    "Depends: ${DEPENDS}\n"
174    "Description: ${CPACK_PACKAGE_DESCRIPTION_SUMMARY}"
175    ": ${CPACK_COMPONENT_${UPPER_COMPONENT}_DISPLAY_NAME}\n"
176    "${deb_long_description}"
177    "\n .\n"
178    " ${CPACK_COMPONENT_${UPPER_COMPONENT}_DESCRIPTION}\n"
179	)
180endforeach(COMPONENT ${CPACK_COMPONENTS_ALL})
181
182
183
184##############################################################################
185# debian/copyright
186set(debian_copyright ${DEBIAN_SOURCE_DIR}/debian/copyright)
187configure_file(${CPACK_RESOURCE_FILE_LICENSE} ${debian_copyright} COPYONLY)
188
189##############################################################################
190# debian/rules
191set(debian_rules ${DEBIAN_SOURCE_DIR}/debian/rules)
192
193file(WRITE ${debian_rules}
194	"#!/usr/bin/make -f\n"
195	"\nexport DH_VERBOSE=1"
196	"\n\n%:\n"
197	"\tdh  $@ --buildsystem=cmake\n"
198	"\noverride_dh_auto_configure:\n"
199	"\tDESTDIR=\"$(CURDIR)/debian/${CPACK_DEBIAN_PACKAGE_NAME}\" dh_auto_configure -- -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_SHARED_LIBS=ON -DPACKAGE_TGZ=OFF"
200	"\n\noverride_dh_auto_install:\n"
201	"\tdh_auto_install --destdir=\"$(CURDIR)/debian/${CPACK_DEBIAN_PACKAGE_NAME}\" --buildsystem=cmake"
202	"\n\noverride_dh_strip:\n"
203	"\tdh_strip --dbg-package=${CPACK_DEBIAN_PACKAGE_NAME}-dbg"
204)
205
206execute_process(COMMAND chmod +x ${debian_rules})
207
208##############################################################################
209# debian/compat
210file(WRITE ${DEBIAN_SOURCE_DIR}/debian/compat "7")
211
212##############################################################################
213# debian/source/format
214file(WRITE ${DEBIAN_SOURCE_DIR}/debian/source/format "3.0 (native)")
215
216##############################################################################
217
218# debian/changelog
219set(debian_changelog ${DEBIAN_SOURCE_DIR}/debian/changelog)
220if(NOT CPACK_DEBIAN_RESOURCE_FILE_CHANGELOG)
221  set(CPACK_DEBIAN_RESOURCE_FILE_CHANGELOG ${CMAKE_SOURCE_DIR}/debian/changelog)
222endif()
223
224# TODO add support for git dch (git-buildpackage)
225if(CHANGELOG_MESSAGE)
226	set(output_changelog_msg ${CHANGELOG_MESSAGE})
227else()
228	set(output_changelog_msg "* Package created with CMake")
229endif(CHANGELOG_MESSAGE)
230message(STATUS "Changelog message : \"${output_changelog_msg}\"")
231if(EXISTS ${CPACK_DEBIAN_RESOURCE_FILE_CHANGELOG})
232  configure_file(${CPACK_DEBIAN_RESOURCE_FILE_CHANGELOG} ${debian_changelog} COPYONLY)
233
234  if(CPACK_DEBIAN_UPDATE_CHANGELOG)
235    file(READ ${debian_changelog} debian_changelog_content)
236    execute_process(
237      COMMAND date -R
238      OUTPUT_VARIABLE DATE_TIME
239      OUTPUT_STRIP_TRAILING_WHITESPACE)
240    file(WRITE ${debian_changelog}
241      "${CPACK_DEBIAN_PACKAGE_NAME} (${DEBIAN_PACKAGE_VERSION}) ${DISTRI}; urgency=low\n\n"
242      "  ${output_changelog_msg}\n\n"
243      " -- ${CPACK_DEBIAN_PACKAGE_MAINTAINER}  ${DATE_TIME}\n\n"
244      )
245    file(APPEND ${debian_changelog} ${debian_changelog_content})
246  endif()
247
248else()
249  execute_process(
250    COMMAND date -R
251    OUTPUT_VARIABLE DATE_TIME
252    OUTPUT_STRIP_TRAILING_WHITESPACE)
253  file(WRITE ${debian_changelog}
254    "${CPACK_DEBIAN_PACKAGE_NAME} (${DEBIAN_PACKAGE_VERSION}) ${DISTRI}; urgency=low\n\n"
255    "  ${output_changelog_msg}\n\n"
256    " -- ${CPACK_DEBIAN_PACKAGE_MAINTAINER}  ${DATE_TIME}\n"
257    )
258   #configure_file(${debian_changelog} ${CPACK_DEBIAN_RESOURCE_FILE_CHANGELOG}  COPYONLY)
259endif()
260
261
262##########################################################################
263# Templates
264
265if (CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA)
266	foreach(CF ${CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA})
267	get_filename_component(CF_NAME ${CF} NAME)
268	message("Writing debian/${CF_NAME}")
269	configure_file(${CF} ${DEBIAN_SOURCE_DIR}/debian/${CF_NAME} @ONLY)
270	endforeach()
271endif(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA)
272
273
274##########################################################################
275# .orig.tar.gz
276#execute_process(COMMAND date +%y%m%d
277#  OUTPUT_VARIABLE day_suffix
278#  OUTPUT_STRIP_TRAILING_WHITESPACE
279#  )
280
281set(CPACK_SOURCE_IGNORE_FILES
282   ${CPACK_SOURCE_IGNORE_FILES}
283  "/build.*/"
284  "/Testing/"
285  "/test/"
286  "/tmp/"
287  "/packaging/"
288  "/debian/"
289  "/\\\\.git.*"
290  "/\\\\.idea/"
291  "/\\\\.codelite/"
292  "*~$")
293
294#set(package_file_name "${CPACK_DEBIAN_PACKAGE_NAME}_${DEBIAN_PACKAGE_VERSION}")
295set(package_file_name "${CPACK_DEBIAN_PACKAGE_NAME}_${CPACK_PACKAGE_VERSION}")
296
297file(WRITE "${CMAKE_BINARY_DIR}/Debian/${DISTRI}/cpack.cmake"
298  "set(CPACK_GENERATOR TGZ)\n"
299  "set(CPACK_PACKAGE_NAME \"${CPACK_DEBIAN_PACKAGE_NAME}\")\n"
300  "set(CPACK_PACKAGE_VERSION \"${CPACK_PACKAGE_VERSION}\")\n"
301  "set(CPACK_PACKAGE_FILE_NAME \"${package_file_name}.orig\")\n"
302  "set(CPACK_PACKAGE_DESCRIPTION \"${CPACK_PACKAGE_NAME} Source\")\n"
303  "set(CPACK_IGNORE_FILES \"${CPACK_SOURCE_IGNORE_FILES}\")\n"
304  "set(CPACK_INSTALLED_DIRECTORIES \"${CPACK_SOURCE_INSTALLED_DIRECTORIES}\")\n"
305  "set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF)\n"
306  )
307
308set(orig_file "${CMAKE_BINARY_DIR}/Debian/${DISTRI}/${package_file_name}.orig.tar.gz")
309
310add_custom_command(OUTPUT ${orig_file}
311  COMMAND cpack --config ${CMAKE_BINARY_DIR}/Debian/${DISTRI}/cpack.cmake
312  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/Debian/${DISTRI}
313  )
314
315add_custom_command(OUTPUT ${DEBIAN_SOURCE_DIR}/CMakeLists.txt
316	COMMAND tar zxf ${orig_file}
317	WORKING_DIRECTORY ${DEBIAN_SOURCE_DIR}
318	DEPENDS ${orig_file}
319	)
320
321##############################################################################
322# debuild -S
323set(DEB_SOURCE_CHANGES
324  ${CPACK_DEBIAN_PACKAGE_NAME}_${DEBIAN_PACKAGE_VERSION}_source.changes
325  )
326
327add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/Debian/${DISTRI}/${DEB_SOURCE_CHANGES}
328  COMMAND ${DEBUILD_EXECUTABLE} --no-tgz-check -S
329  WORKING_DIRECTORY ${DEBIAN_SOURCE_DIR}
330  )
331add_custom_target(debuild_${DISTRI} ALL
332				DEPENDS ${DEBIAN_SOURCE_DIR}/CMakeLists.txt
333						${CMAKE_BINARY_DIR}/Debian/${DISTRI}/${DEB_SOURCE_CHANGES}
334		)
335##############################################################################
336# dput ppa:your-lp-id/ppa <source.changes>
337message(STATUS "Upload PPA is ${UPLOAD_PPA}")
338if(UPLOAD_PPA)
339    if (EXISTS ${DPUT_CONFIG_IN})
340        set(DPUT_DIST ${DISTRI})
341        configure_file(
342            ${DPUT_CONFIG_IN}
343            ${CMAKE_BINARY_DIR}/Debian/${DISTRI}/dput.cf
344            @ONLY
345        )
346        add_custom_target(dput_${DISTRI} ALL
347            COMMAND ${DPUT_EXECUTABLE} -c ${CMAKE_BINARY_DIR}/Debian/${DISTRI}/dput.cf ${DPUT_HOST} ${DEB_SOURCE_CHANGES}
348            DEPENDS debuild_${DISTRI}
349            WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/Debian/${DISTRI}
350        )
351    else()
352        add_custom_target(dput_${DISTRI} ALL
353            COMMAND ${DPUT_EXECUTABLE} ${DPUT_HOST} ${DEB_SOURCE_CHANGES}
354            DEPENDS debuild_${DISTRI}
355            WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/Debian/${DISTRI}
356        )
357    endif()
358endif()
359endforeach(DISTRI)
360