aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/cmake/Modules/Packager.cmake
blob: 019f18c4ea2d0090fe9bec94f22dbdb1a7c013b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Copyright (c) 2017-2019 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Generate DEB / RPM packages

# Default variables

if (NOT DEFINED ENV{VENDOR})
  set(VENDOR "Cisco Systems" CACHE STRING "Vendor")
else ()
  set(VENDOR ENV{VENDOR} CACHE STRING "Vendor")
endif ()

if (NOT DEFINED ENV{CONTACT})
  set(CONTACT "msardara@cisco.com" CACHE STRING "Contact")
else ()
  set(CONTACT ENV{CONTACT} CACHE STRING "Contact")
endif()

if (NOT DEFINED ENV{PACKAGE_MAINTAINER})
  set(PACKAGE_MAINTAINER "Mauro Sardara (msardara@cisco.com)" CACHE STRING "Maintainer")
else ()
  set(PACKAGE_MAINTAINER ENV{PACKAGE_MAINTAINER} CACHE STRING "Maintainer")
endif()

if (NOT DEFINED ENV{CPACK_PACKAGING_INSTALL_PREFIX})
  set(CPACK_PACKAGING_INSTALL_PREFIX "/usr")
else ()
  set(CPACK_PACKAGING_INSTALL_PREFIX ENV{CPACK_PACKAGING_INSTALL_PREFIX})
endif()

set(CPACK_COMPONENTS_ALL library headers utils documentation)

function (make_package_internal PACKAGE_NAME PACKAGE_VERSION ARCHITECTURE GENERATOR TYPE DESCRIPTION HOMEPAGE)
  set(CPACK_GENERATOR ${GENERATOR})
  set(CPACK_PACKAGE_VENDOR ${VENDOR})
  set(CPACK_PACKAGE_CONTACT ${CONTACT})

  set(CPACK_${GENERATOR}_COMPONENT_INSTALL ON)
  set(CPACK_${TYPE}_PACKAGE_MAINTAINER ${PACKAGE_MAINTAINER})
  set(CPACK_${TYPE}_PACKAGE_NAME ${PACKAGE_NAME})
  set(CPACK_${TYPE}_PACKAGE_VERSION ${PACKAGE_VERSION})
  set(CPACK_${TYPE}_PACKAGE_ARCHITECTURE ${ARCHITECTURE})
  set(CPACK_${TYPE}_PACKAGE_RELEASE 1)
  set(CPACK_${TYPE}_PACKAGE_VENDOR ${VENDOR})
  set(CPACK_${TYPE}_PACKAGE_DESCRIPTION ${DESCRIPTION})
  set(CPACK_${TYPE}_PACKAGE_HOMEPAGE ${HOMEPAGE})

  include(CPack)
endfunction()

function(make_deb_package PACKAGE_NAME PACKAGE_VERSION ARCHITECTURE DEPS BUILD_DEPS DESCRIPTION HOMEPAGE)

  set(TYPE "DEBIAN")
  set(GENERATOR "DEB")

  set(CPACK_${TYPE}_LIBRARY_PACKAGE_NAME "${PACKAGE_NAME}")
  set(CPACK_${TYPE}_UTILS_PACKAGE_NAME "${PACKAGE_NAME}-utils")
  set(CPACK_${TYPE}_HEADERS_PACKAGE_NAME "${PACKAGE_NAME}-dev")
  set(CPACK_${TYPE}_DOCUMENTATION_PACKAGE_NAME "${PACKAGE_NAME}-doc")

  set(CPACK_${TYPE}_LIBRARY_FILE_NAME "${CPACK_${TYPE}_LIBRARY_PACKAGE_NAME}_${PACKAGE_VERSION}_${ARCHITECTURE}.deb")
  set(CPACK_${TYPE}_UTILS_FILE_NAME "${CPACK_${TYPE}_UTILS_PACKAGE_NAME}_${PACKAGE_VERSION}_${ARCHITECTURE}.deb")
  set(CPACK_${TYPE}_HEADERS_FILE_NAME "${CPACK_${TYPE}_HEADERS_PACKAGE_NAME}_${PACKAGE_VERSION}_${ARCHITECTURE}.deb")
  set(CPACK_${TYPE}_DOCUMENTATION_FILE_NAME "${CPACK_${TYPE}_DOCUMENTATION_PACKAGE_NAME}_${PACKAGE_VERSION}_${ARCHITECTURE}.deb")

  set(CPACK_DEBIAN_LIBRARY_PACKAGE_SHLIBDEPS OFF)

  set(CPACK_${TYPE}_LIBRARY_PACKAGE_DEPENDS ${DEPS})
  set(CPACK_${TYPE}_UTILS_PACKAGE_DEPENDS ${CPACK_${TYPE}_LIBRARY_PACKAGE_NAME})
  set(CPACK_${TYPE}_HEADERS_PACKAGE_DEPENDS ${BUILD_DEPS})
  set(CPACK_${TYPE}_DOCUMENTATION_PACKAGE_DEPENDS "")

  make_package_internal(${PACKAGE_NAME} ${PACKAGE_VERSION} ${ARCHITECTURE} ${GENERATOR} ${TYPE} ${DESCRIPTION} ${HOMEPAGE})
endfunction()

function(make_rpm_package PACKAGE_NAME PACKAGE_VERSION ARCHITECTURE DEPS BUILD_DEPS DESCRIPTION HOMEPAGE)
  set(TYPE "RPM")
  set(GENERATOR "RPM")

  set(CPACK_${TYPE}_LIBRARY_PACKAGE_NAME "${PACKAGE_NAME}")
  set(CPACK_${TYPE}_UTILS_PACKAGE_NAME "${PACKAGE_NAME}-utils")
  set(CPACK_${TYPE}_HEADERS_PACKAGE_NAME "${PACKAGE_NAME}-devel")
  set(CPACK_${TYPE}_DOCUMENTATION_PACKAGE_NAME "${PACKAGE_NAME}-doc")

  set(CPACK_${TYPE}_LIBRARY_FILE_NAME "${CPACK_${TYPE}_LIBRARY_PACKAGE_NAME}-${PACKAGE_VERSION}.${ARCHITECTURE}.rpm")
  set(CPACK_${TYPE}_LIBRARY_FILE_NAME "${CPACK_${TYPE}_UTILS_PACKAGE_NAME}-${PACKAGE_VERSION}.${ARCHITECTURE}.rpm")
  set(CPACK_${TYPE}_HEADERS_FILE_NAME "${CPACK_${TYPE}_HEADERS_PACKAGE_NAME}-${PACKAGE_VERSION}.${ARCHITECTURE}.rpm")
  set(CPACK_${TYPE}_DOCUMENTATION_FILE_NAME "${CPACK_${TYPE}_DOCUMENTATION_PACKAGE_NAME}-${PACKAGE_VERSION}.${ARCHITECTURE}.rpm")

  set(CPACK_${TYPE}_LIBRARY_PACKAGE_AUTOREQ OFF)

  set(CPACK_${TYPE}_LIBRARY_PACKAGE_REQUIRES ${DEPS})
  set(CPACK_${TYPE}_UTILS_PACKAGE_DEPENDS ${CPACK_${TYPE}_LIBRARY_PACKAGE_NAME})
  set(CPACK_${TYPE}_HEADERS_PACKAGE_REQUIRES ${BUILD_DEPS})
  set(CPACK_${TYPE}_DOCUMENTATION_PACKAGE_REQUIRES "")

  set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "/usr/etc" "/usr/lib/python2.7" "/usr/lib/python2.7/site-packages")

  make_package_internal(${PACKAGE_NAME} ${PACKAGE_VERSION} ${ARCHITECTURE} ${GENERATOR} ${TYPE} ${DESCRIPTION} ${HOMEPAGE})
endfunction()

function(make_tgz_package PACKAGE_NAME PACKAGE_VERSION ARCHITECTURE)

  set(TYPE "ARCHIVE")
  set(GENERATOR "TGZ")

  set(CPACK_${TYPE}_COMPONENT_INSTALL ON)
  set(CPACK_${TYPE}_LIBRARY_FILE_NAME "${PACKAGE_NAME}_${PACKAGE_VERSION}_${ARCHITECTURE}")
  set(CPACK_${TYPE}_UTILS_FILE_NAME "${PACKAGE_NAME}-utils_${PACKAGE_VERSION}_${ARCHITECTURE}")
  set(CPACK_${TYPE}_HEADERS_FILE_NAME "${PACKAGE_NAME}-dev_${PACKAGE_VERSION}_${ARCHITECTURE}")
  set(CPACK_${TYPE}_DOCUMENTATION_FILE_NAME "${PACKAGE_NAME}-doc_${PACKAGE_VERSION}_${ARCHITECTURE}")

  set(CPACK_GENERATOR ${GENERATOR})
  set(CPACK_PACKAGE_VENDOR ${VENDOR})
  set(CPACK_PACKAGE_CONTACT ${CONTACT})

  include(CPack)

endfunction()

function (make_package DEPS_DEB DEPS_RPM BUILD_DEPS_DEB BUILD_DEPS_RPM DESCRIPTION HOMEPAGE)

  if (NOT DEFINED ENV{PACKAGE_NAME})
    string(TOLOWER ${CMAKE_PROJECT_NAME} PACKAGE_NAME)
  else ()
    string(TOLOWER $ENV{PACKAGE_NAME} PACKAGE_NAME)
  endif ()

  # Get the version
  execute_process(COMMAND bash ${CMAKE_SOURCE_DIR}/scripts/version
    OUTPUT_VARIABLE PACKAGE_VERSION)

  if (PACKAGE_VERSION)
    string(STRIP ${PACKAGE_VERSION} PACKAGE_VERSION)
  else ()
    set(PACKAGE_VERSION 1.0)
  endif ()

  if (EXISTS "/etc/lsb-release")
    execute_process(COMMAND grep -oP "(?<=DISTRIB_ID=).*" /etc/lsb-release OUTPUT_VARIABLE DISTRIB_ID)
    execute_process(COMMAND grep -oP "(?<=DISTRIB_RELEASE=).*" /etc/lsb-release OUTPUT_VARIABLE DISTRIB_RELEASE)
    execute_process(COMMAND grep -oP "(?<=DISTRIB_CODENAME=).*" /etc/lsb-release OUTPUT_VARIABLE DISTRIB_CODENAME)
    execute_process(COMMAND grep -oP "(?<=DISTRIB_DESCRIPTION=).*" /etc/lsb-release OUTPUT_VARIABLE DISTRIB_DESCRIPTION)
    execute_process(COMMAND uname -m COMMAND tr -d '\n' OUTPUT_VARIABLE ARCHITECTURE)

    if (${ARCHITECTURE} STREQUAL "x86_64")
      set(ARCHITECTURE "amd64")
    endif()

    make_deb_package(${PACKAGE_NAME}
                     ${PACKAGE_VERSION}
                     ${ARCHITECTURE}
                     ${DEPS_DEB}
                     ${BUILD_DEPS_DEB}
                     ${DESCRIPTION}
                     ${HOMEPAGE})

  elseif(EXISTS "/etc/redhat-release")
    execute_process(COMMAND sudo yum install -y redhat-lsb)
    execute_process(COMMAND lsb_release -si OUTPUT_VARIABLE DISTRIB_ID)
    execute_process(COMMAND lsb_release -sr OUTPUT_VARIABLE DISTRIB_RELEASE)
    execute_process(COMMAND lsb_release -sc OUTPUT_VARIABLE DISTRIB_CODENAME)
    execute_process(COMMAND lsb_release -sd OUTPUT_VARIABLE DISTRIB_DESCRIPTION)
    execute_process(COMMAND uname -m -m COMMAND tr -d '\n' OUTPUT_VARIABLE ARCHITECTURE)

    make_rpm_package(${PACKAGE_NAME}
                     ${PACKAGE_VERSION}
                     ${ARCHITECTURE}
                     ${DEPS_RPM}
                     ${BUILD_DEPS_RPM}
                     ${DESCRIPTION}
                     ${HOMEPAGE})
  else()
    execute_process(COMMAND uname -m COMMAND tr -d '\n' OUTPUT_VARIABLE ARCHITECTURE)

    if (${ARCHITECTURE} STREQUAL "x86_64")
      set(ARCHITECTURE "amd64")
    endif()

    # Other linux system. Create a tar.gz package
    make_tgz_package(${PACKAGE_NAME}
                     ${PACKAGE_VERSION}
                     ${ARCHITECTURE})

  endif()
endfunction()