Commit 3c94017d7be3a8adb6fd9225ac5af19fb59f4f04

Adrian Antonana 2019-12-09T16:12:38

cmake: add cpack for generating packages

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5a0d849..de68266 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -95,3 +95,60 @@ install(EXPORT ${TARGETS_EXPORT_NAME}
 
 # add to CMake registry
 export(PACKAGE ${PROJECT_NAME})
+
+#---------------------------------------------------------------------------------------
+# Create release packages
+#---------------------------------------------------------------------------------------
+# package release version
+set(PACKAGE_RELEASE_VERSION 1)
+
+# determine distribution and architecture
+find_program(LSB_RELEASE lsb_release)
+
+execute_process(COMMAND uname -m OUTPUT_VARIABLE MACHINE_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+if(LSB_RELEASE)
+    execute_process(COMMAND lsb_release -sr OUTPUT_VARIABLE LINUX_DISTRO_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
+    execute_process(COMMAND lsb_release -si OUTPUT_VARIABLE LINUX_DISTRO OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+    string(TOLOWER ${LINUX_DISTRO} LINUX_DISTRO)
+endif()
+
+# default CPack generators
+set(CPACK_GENERATOR TGZ STGZ)
+
+# extra CPack generators
+if(LINUX_DISTRO STREQUAL "debian" OR LINUX_DISTRO STREQUAL "ubuntu" OR LINUX_DISTRO STREQUAL "linuxmint")
+    list(APPEND CPACK_GENERATOR DEB)
+elseif(LINUX_DISTRO STREQUAL "fedora" OR LINUX_DISTRO STREQUAL "opensuse" OR LINUX_DISTRO STREQUAL "centos")
+    list(APPEND CPACK_GENERATOR RPM)
+endif()
+
+# general CPack config
+set(CPACK_PACKAGE_DIRECTORY ${CMAKE_BINARY_DIR}/packages)
+message(STATUS "CPack: packages will be generated under ${CPACK_PACKAGE_DIRECTORY}")
+set(CPACK_PACKAGE_NAME "lib${PROJECT_NAME}")
+set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
+set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "LibTomMath")
+set(CPACK_PACKAGE_VENDOR "LibTomMath")
+set(CPACK_PACKAGE_CONTACT "libtom@googlegroups.com")
+set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
+set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${PACKAGE_RELEASE_VERSION}_${LINUX_DISTRO}-${LINUX_DISTRO_VERSION}_${MACHINE_ARCH})
+set(CPACK_STRIP_FILES ON)
+
+# deb specific CPack config
+set(CPACK_DEBIAN_PACKAGE_RELEASE ${PACKAGE_RELEASE_VERSION})
+set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
+set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
+set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://www.libtom.net/LibTomMath")
+set(CPACK_DEBIAN_PACKAGE_SECTION "devel")
+
+# rpm specific CPack config
+set(CPACK_RPM_PACKAGE_URL "https://www.libtom.net/LibTomMath")
+set(CPACK_RPM_PACKAGE_RELEASE ${PACKAGE_RELEASE_VERSION})
+set(CPACK_RPM_PACKAGE_ARCHITECTURE ${MACHINE_ARCH})
+set(CPACK_RPM_PACKAGE_NAME "lib${PROJECT_NAME}-${PROJECT_VERSION}")
+set(CPACK_RPM_FILE_NAME "lib${PROJECT_NAME}_${PROJECT_VERSION}-${CPACK_RPM_PACKAGE_RELEASE}_${LINUX_DISTRO}-${LINUX_DISTRO_VERSION}_${CPACK_RPM_PACKAGE_ARCHITECTURE}.rpm")
+set(CPACK_RPM_PACKAGE_LICENSE "WTFPL")
+
+include(CPack)