Skip to content

Commit 5080293

Browse files
committed
cmake/LinuxPackaging: add rules to bundle deb/rpm
Include files to generate debs and rpms. Based on libiio's LinuxPackaging script, sharing the same options: ENABLE_PACKAGING enables deb/rpm generation and DEB_DETECT_DEPENDENCIES auto sets the required version as the installed libraries version. Signed-off-by: Jorge Marques <[email protected]>
1 parent 9db3469 commit 5080293

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
8080
# Fix linking on 10.14+ with Homebrew. See https://stackoverflow.com/questions/54068035
8181
LINK_DIRECTORIES(/usr/local/lib)
8282
endif()
83+
if(ENABLE_PACKAGING)
84+
# /usr/local/lib might not be in LD_LIBRAY_PATH
85+
set(CMAKE_INSTALL_RPATH "/usr/local/lib")
86+
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
87+
endif()
8388

8489
add_definitions(-D_GNU_SOURCE
8590
-DOSC_VERSION="${OSC_VERSION}"
@@ -337,3 +342,10 @@ if(NOT EXISTS "${TARGET_PATH}")
337342
endif()
338343

339344
add_subdirectory(plugins)
345+
option(ENABLE_PACKAGING "Create .deb/.rpm or .tar.gz packages via 'make package'" OFF)
346+
347+
if(ENABLE_PACKAGING)
348+
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
349+
include(cmake/LinuxPackaging.cmake)
350+
endif()
351+
endif()

cmake/LinuxPackaging.cmake

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# support creating some basic binpkgs via `make package`
2+
set(CPACK_SET_DESTDIR ON)
3+
set(CPACK_GENERATOR TGZ;DEB)
4+
5+
FIND_PROGRAM(RPMBUILD_CMD rpmbuild)
6+
if (RPMBUILD_CMD)
7+
set(CPACK_PACKAGE_RELOCATABLE OFF)
8+
set(CPACK_GENERATOR ${CPACK_GENERATOR};RPM)
9+
set(CPACK_RPM_PACKAGE_REQUIRES "libiio >= 0.19, gtk3 >= 3.24, libgtkdatabox >= 1.0.0, libjansson > 2.12, libmatio >= 1.5.17")
10+
endif()
11+
12+
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY 0)
13+
set(CPACK_PACKAGE_VERSION_MAJOR ${OSC_VERSION_MAJOR})
14+
set(CPACK_PACKAGE_VERSION_MINOR ${OSC_VERSION_MINOR})
15+
set(CPACK_PACKAGE_VERSION_PATCH ${OSC_VERSION_GIT})
16+
set(CPACK_BUNDLE_NAME osc)
17+
set(CPACK_PACKAGE_VERSION ${OSCIO_VERSION})
18+
# debian specific package settings
19+
set(CPACK_PACKAGE_CONTACT "Engineerzone <https://wiki.analog.com/resources/tools-software/linux-software/iio_oscilloscope>")
20+
21+
option(DEB_DETECT_DEPENDENCIES "Detect dependencies for .deb packages" OFF)
22+
23+
# if we are going to be looking for things, make sure we have the utilities
24+
if(DEB_DETECT_DEPENDENCIES)
25+
FIND_PROGRAM(DPKG_CMD dpkg)
26+
FIND_PROGRAM(DPKGQ_CMD dpkg-query)
27+
endif()
28+
29+
# if we want to, and have the capabilities find what is needed,
30+
# based on what backends are turned on and what libraries are installed
31+
if(DEB_DETECT_DEPENDENCIES AND DPKG_CMD AND DPKGQ_CMD)
32+
message(STATUS "querying installed packages on system for dependancies")
33+
execute_process(COMMAND "${DPKG_CMD}" --print-architecture
34+
OUTPUT_VARIABLE CPACK_DEBIAN_PACKAGE_ARCHITECTURE
35+
OUTPUT_STRIP_TRAILING_WHITESPACE)
36+
set(PACKAGES "libiio libgtk-3 libgtkdatabox libjansson libmatio")
37+
# find the version of the installed package, which is hard to do in
38+
# cmake first, turn the list into an list (seperated by semicolons)
39+
string(REGEX REPLACE " " ";" PACKAGES ${PACKAGES})
40+
# then iterate over each
41+
foreach(package ${PACKAGES})
42+
# List packages matching given pattern ${package},
43+
# key is the glob (*) at the end of the ${package} name,
44+
# so we don't need to be so specific
45+
execute_process(COMMAND "${DPKG_CMD}" -l ${package}*
46+
OUTPUT_VARIABLE DPKG_PACKAGES)
47+
# returns a string, in a format:
48+
# ii libxml2:amd64 2.9.4+dfsg1- amd64 GNOME XML library
49+
# 'ii' means installed - which is what we are looking for
50+
STRING(REGEX MATCHALL "ii ${package}[a-z0-9A-Z.-]*"
51+
DPKG_INSTALLED_PACKAGES ${DPKG_PACKAGES})
52+
# get rid of the 'ii', and split things up, so we can look
53+
# at the name
54+
STRING(REGEX REPLACE "ii " ";" NAME_INSTALLED_PACKAGES
55+
${DPKG_INSTALLED_PACKAGES})
56+
foreach(match ${NAME_INSTALLED_PACKAGES})
57+
# ignore packages marked as dev, debug,
58+
# documentations, or utils
59+
STRING(REGEX MATCHALL "dev|dbg|doc|utils" TEMP_TEST
60+
${match})
61+
if("${TEMP_TEST}" STREQUAL "")
62+
# find the actual version, executes:
63+
# dpkg-query --showformat='\${Version}'
64+
# --show libusb-1.0-0
65+
execute_process(COMMAND "${DPKGQ_CMD}"
66+
--showformat='\${Version}'
67+
--show "${match}"
68+
OUTPUT_VARIABLE DPKGQ_VER)
69+
# debian standard is package_ver-debian_ver,
70+
# "'2.9.4+dfsg1-2.1'"
71+
# ignore patches and debian suffix version, and
72+
# remove single quote
73+
string(REGEX REPLACE "[+-][a-z0-9A-Z.]*" ""
74+
DPKGQ_VER ${DPKGQ_VER})
75+
string(REGEX REPLACE "'" "" DPKGQ_VER
76+
${DPKGQ_VER})
77+
# build the string for the Debian dependancy
78+
set(CPACK_DEBIAN_PACKAGE_DEPENDS
79+
"${CPACK_DEBIAN_PACKAGE_DEPENDS}"
80+
"${match} (>= ${DPKGQ_VER}), ")
81+
endif()
82+
endforeach(match)
83+
endforeach(package)
84+
# remove the dangling end comma
85+
string(REGEX REPLACE ", $" "" CPACK_DEBIAN_PACKAGE_DEPENDS
86+
${CPACK_DEBIAN_PACKAGE_DEPENDS})
87+
else()
88+
# assume everything is turned on, and running on a modern OS
89+
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libiio0 (>= 0.19), libgtk-3-0 (>= 3.24.18), libgtkdatabox (>= 1.0.0), libjansson4 (> 2.12), libmatio11 (>= 1.5.17)")
90+
message(STATUS "Using default dependencies for packaging")
91+
endif()
92+
93+
message(STATUS "Package dependencies (.deb): " ${CPACK_DEBIAN_PACKAGE_DEPENDS})
94+
if (CPACK_RPM_PACKAGE_REQUIRES)
95+
message(STATUS "Package dependencies (.rpm): " ${CPACK_RPM_PACKAGE_REQUIRES})
96+
endif()
97+
98+
if(${CMAKE_MAJOR_VERSION} LESS 3)
99+
# old versions of cmake dont include this, but the same vintage of dpkg requires it
100+
IF(NOT CPACK_DEBIAN_PACKAGE_ARCHITECTURE)
101+
FIND_PROGRAM(DPKG_CMD dpkg)
102+
IF(NOT DPKG_CMD)
103+
MESSAGE(STATUS "Can not find dpkg in your path, default to i386.")
104+
SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE i386)
105+
ENDIF(NOT DPKG_CMD)
106+
EXECUTE_PROCESS(COMMAND "${DPKG_CMD}" --print-architecture
107+
OUTPUT_VARIABLE CPACK_DEBIAN_PACKAGE_ARCHITECTURE
108+
OUTPUT_STRIP_TRAILING_WHITESPACE)
109+
ENDIF(NOT CPACK_DEBIAN_PACKAGE_ARCHITECTURE)
110+
endif()
111+
include(CPack)

0 commit comments

Comments
 (0)