enhance(ci): Add multiple package build
[ric-plt/lib/rmr.git] / CMakeLists.txt
1 #
2 #==================================================================================
3 #       Copyright (c) 2019 Nokia
4 #       Copyright (c) 2018-2019 AT&T Intellectual Property.
5 #
6 #   Licensed under the Apache License, Version 2.0 (the "License");
7 #   you may not use this file except in compliance with the License.
8 #   You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #   Unless required by applicable law or agreed to in writing, software
13 #   distributed under the License is distributed on an "AS IS" BASIS,
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #   See the License for the specific language governing permissions and
16 #   limitations under the License.
17 #==================================================================================
18 #
19
20 project( rmr LANGUAGES C )
21 cmake_minimum_required( VERSION 3.5 )
22
23 set( major_version "1" )                # should be automatically populated from git tag later, but until CI process sets a tag we use this
24 set( minor_version "0" )
25 set( patch_level "34" )
26
27 set( install_root "${CMAKE_INSTALL_PREFIX}" )
28 set( install_lib "lib" )
29 set( install_inc "include/rmr" )
30 if( MAN_PREFIX )
31         set( install_man ${MAN_PREFIX} )                        # is there a cmake var for this -- can't find one
32 else()
33         set( install_man "/usr/share/man" )                     # this needs to be fixed so it's not hard coded
34 endif()
35
36 # Must use GNUInstallDirs to install libraries into correct
37 # locations on all platforms.
38 include( GNUInstallDirs )
39
40 # nano/nng install using LIBDIR as established by the gnu include; it varies from system
41 # to system, and we don't trust that it is always set, so we default to lib.
42 #
43 if( NOT CMAKE_INSTALL_LIBDIR )
44         set( CMAKE_INSTALL_LIBDIR "lib" )
45 endif()
46
47 # ---------------- extract some things from git ------------------------------
48
49 # commit id for the version string
50 execute_process(
51         COMMAND bash -c "git rev-parse --short HEAD|awk '{printf\"%s\", $0}'"
52         OUTPUT_VARIABLE git_id
53 )
54
55 # version information for library names and version string
56 execute_process(
57         COMMAND bash -c "git describe --tags --abbrev=0 HEAD 2>/dev/null | awk -v tag=0.0.4095 ' { tag=$1 } END{ print  tag suffix }'|sed 's/\\./;/g' "
58         OUTPUT_VARIABLE mmp_version_str
59         ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE
60 )
61
62 # extra indicator to show that the build was based on modified file(s) and not the true commit
63 # (no hope of reproducing the exact library for debugging). Used only for the internal version
64 # string.
65 execute_process(
66         COMMAND bash -c "git diff --shortstat|awk -v fmt=%s -v r=-rotten '{ s=r } END { printf( fmt, s ) }'"
67         OUTPUT_VARIABLE spoiled_str
68 )
69
70 # uncomment these lines once CI starts adding a tag on merge
71 #set( mmp_version ${mmp_version_str} )
72 #list( GET mmp_version 0 major_version )
73 #list( GET mmp_version 1 minor_version )
74 #list( GET mmp_version 2 patch_level )
75
76
77 # define constants used in the version string
78 add_definitions(
79         -DGIT_ID=${git_id}
80         -DMAJOR_VER=${major_version}
81         -DMINOR_VER=${minor_version}
82         -DPATCH_VER=${patch_level}
83 )
84
85 # ---------------- suss out pkg gen tools so we don't fail generating packages that the system cannot support --------------
86
87
88 if( DEV_PKG )
89         set( dev_tag "-dev" )
90         set( pkg_name "rmr-dev" )
91 else()
92         set( pkg_name "rmr" )
93 endif()
94 set( rpm_pkg_label "${CMAKE_PROJECT_NAME}${dev_tag}${spoiled_str}-${major_version}.${minor_version}.${patch_level}-${CMAKE_SYSTEM_PROCESSOR}" )
95 set( deb_pkg_label "${CMAKE_PROJECT_NAME}${dev_tag}${spoiled_str}_${major_version}.${minor_version}.${patch_level}_${CMAKE_SYSTEM_PROCESSOR}" )
96 message( "+++ building ${deb_pkg_label}" )
97 message( "+++ building ${rpm_pkg_label}" )
98 set( out_yml /tmp/build_output.yml )                                    # we will record package names (we record only untainted names)
99 find_program( rpm NAMES rpmbuild )                                              # rpm package gen requires this to be installed
100
101 set( gen_rpm 0 )
102 if( "${rpm}" MATCHES "rpm-NOTFOUND" )                          # cannot build rpm
103         set( pkg_list "DEB" )
104         message( "### make package will generate only deb package; cannot find support to generate rpm packages" )
105 else()
106         set( pkg_list "DEB;RPM" )
107         set( gen_rpm 1 )
108         message( "+++ make package will generate both deb and rpm packages" )
109 endif()
110
111 execute_process(
112         COMMAND bash -c "printf '# RMr build generated list of package paths\n---\n' >${out_yml}"
113 )
114
115 execute_process(
116         COMMAND bash -c " echo deb: ${CMAKE_CURRENT_BINARY_DIR}/${deb_pkg_label}.deb  >>${out_yml}"
117 )
118
119 if( gen_rpm )
120         execute_process(
121                 COMMAND bash -c " echo rpm: ${CMAKE_CURRENT_BINARY_DIR}/${rpm_pkg_label}.rpm  >>${out_yml}"
122         )
123 endif()
124
125 execute_process(
126         COMMAND bash -c "printf '...\n' >>${out_yml}"
127 )
128
129 # ---------------- setup nano/nng things ---------------------------------------
130 if( NOT SKIP_EXTERNALS )
131         set( need_ext 1 )                               # we force dependences on these for right build order
132     execute_process( COMMAND  git submodule update --init -- ext/nng
133             WORKING_DIRECTORY  ${CMAKE_CURRENT_SOURCE_DIR}
134     )
135
136     execute_process( COMMAND  git submodule update --init -- ext/nanomsg
137             WORKING_DIRECTORY  ${CMAKE_CURRENT_SOURCE_DIR}
138     )
139     if( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/ext/nng/CMakeLists.txt )
140         message( FATAL_ERROR "cannot find nng in our git source as a submodule: Giving up" )    # this will abort which seems wrong, but tdam.
141     endif()
142
143         include( ExternalProject )
144         ExternalProject_Add(
145                 ext_nng
146                 SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext/nng"
147                 CMAKE_ARGS "-DBUILD_SHARED_LIBS=1"
148                 CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}"
149                 BUILD_COMMAND "make"
150                 UPDATE_COMMAND ""
151                 TEST_COMMAND ""
152                 STEP_TARGETS build
153         )
154         ExternalProject_Add(
155                 nanomsg
156                 SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext/nanomsg"
157                 BUILD_COMMAND "make"
158                 UPDATE_COMMAND ""
159                 CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}"
160                 TEST_COMMAND ""
161                 STEP_TARGETS build
162         )
163
164         # it seems impossible to install everything that lands in {bin}/lib, so we need to
165         # hard code (shudder) some things. Even worse, we have to make exceptions for
166         # builds on apple (osx) since their naming convention wandered off the path.
167         set( nng_major 1 )
168         set( nng_minor 1.0 )
169         set( nano_major 5 )
170         set( nano_minor 1.0 )
171         set( so ${CMAKE_SHARED_LIBRARY_SUFFIX} )        # cmake variables are impossibly long :(
172         if( NOT APPLE )                                 # probably breaks in windows, but idc
173                 set( nng_so_suffix ${so} )
174                 set( nng_so_suffix_m ${so}.${nng_major} )
175                 set( nng_so_suffix_mm ${so}.${nng_major}.${nng_minor} )
176
177                 set( nano_so_suffix ${so} )
178                 set( nano_so_suffix_m ${so}.${nano_major} )
179                 set( nano_so_suffix_mm ${so}.${nano_major}.${nano_minor} )
180         else()
181                 # of course apple puts versions before the suffix :(
182                 set( nng_so_suffix ${so} )                                                                      # so has a lead dot, so NOT needed
183                 set( nng_so_suffix_m ".${nng_major}${so}" )                                     # these need leading dots
184                 set( nng_so_suffix_mm ".${nng_major}.${nng_minor}${so}" )
185
186                 set( nano_so_suffix ${so} )
187                 set( nano_so_suffix_m ".${nano_major}${so}" )
188                 set( nano_so_suffix_mm ".${nano_major}.${nano_minor}${so}" )
189         endif()
190
191         message( "+++ installing nano/nng with: ${nano_major}.${nano_minor} | ${nng_major}.${nng_minor}" )
192 else()
193         if( PACK_EXTERNALS )
194                 unset( SKIP_EXTERNALS  CACHE )  # must remove so as not to trap user into a never ending failure
195                 unset( PACK_EXTERNALS  CACHE )
196                 message( FATAL_ERROR "ERROR: PACK_EXTERNALS can be set only if SKIP_EXTERNALS is unset (=0, or not supplied on command line)" )
197         endif()
198         set( need_ext 0 )
199 endif()
200
201
202
203 # this gets us round a chicken/egg problem. include files don't exist until make is run
204 # but Cmake insists on having these exist when we add them to include directories to
205 # enable rmr code to find them after we build them.
206 #
207 execute_process( COMMAND "mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/include/nng" )
208 include_directories( "${CMAKE_CURRENT_BINARY_DIR}/include" )
209
210
211 # Compiler flags
212 #
213 set( CMAKE_POSITION_INDEPENDENT_CODE ON )
214 set( CMAKE_CXX_FLAGS "-g -Wall " )
215
216 # Include modules
217 add_subdirectory( src/rmr/common )
218 add_subdirectory( src/rmr/nanomsg )
219 add_subdirectory( src/rmr/nng )
220 add_subdirectory( doc )                         # this will auto skip if {X}fm is not available
221
222
223 # shared and static libraries are built from the same object files.
224 # Nanomsg based library (librmr ) while nng based is librmr_nng.
225 # library is built by pulling object files from either the nano or nng  and common subdirs
226 #
227 add_library( rmr_shared SHARED "$<TARGET_OBJECTS:nano_objects>;$<TARGET_OBJECTS:common_objects>" )
228
229 set_target_properties( rmr_shared
230         PROPERTIES
231                 OUTPUT_NAME "rmr"
232                 SOVERSION ${major_version}
233                 VERSION ${major_version}.${minor_version}.${patch_level} )
234
235 add_library( rmr_nng_shared SHARED "$<TARGET_OBJECTS:nng_objects>;$<TARGET_OBJECTS:common_objects>" )
236 set_target_properties( rmr_nng_shared
237         PROPERTIES
238         OUTPUT_NAME "rmr_nng"
239         SOVERSION ${major_version}
240         VERSION ${major_version}.${minor_version}.${patch_level} )
241
242 # we only build/export the static archive (.a) if generating a dev package
243 if( DEV_PKG )
244         add_library( rmr_nng_static STATIC "$<TARGET_OBJECTS:nng_objects>;$<TARGET_OBJECTS:common_objects>" )
245         set_target_properties( rmr_nng_static
246                 PROPERTIES
247                 OUTPUT_NAME "rmr_nng"
248                 SOVERSION ${major_version}
249                 VERSION ${major_version}.${minor_version}.${patch_level} )
250
251         add_library( rmr_static STATIC "$<TARGET_OBJECTS:nano_objects>;$<TARGET_OBJECTS:common_objects>" )
252
253         set_target_properties( rmr_static
254                 PROPERTIES
255                 OUTPUT_NAME "rmr"
256                 SOVERSION ${major_version}
257                 VERSION ${major_version}.${minor_version}.${patch_level} )
258 endif()
259
260 # if externals need to be built, then we must force them to be built first by depending on them
261 if( need_ext )
262         if( DEV_PKG )
263                 add_dependencies( rmr_static;rmr_shared nanomsg )
264                 add_dependencies( rmr_nng_shared;rmr_nng_static ext_nng )
265         else()
266                 add_dependencies( rmr_shared nanomsg )
267                 add_dependencies( rmr_nng_shared ext_nng )
268         endif()
269 endif()
270
271 #
272 if( APPLE  )
273         message( "### apple hack: forcing hard coded library paths for nng/nano dynamic libraries" )
274         target_link_libraries( rmr_shared ${CMAKE_CURRENT_BINARY_DIR}/lib/libnanomsg${nano_so_suffix} )
275         target_link_libraries( rmr_nng_shared ${CMAKE_CURRENT_BINARY_DIR}/lib/libnng${nng_so_suffix} )
276 endif()
277
278 # Define directories where package should drop things when installed
279 # In CMake speak archive == *.a  library == *.so, so both are needed
280 # Headers from the common directory are forced to install by the local CM file in common. At
281 # the moment, there are no header files specific to either nano or nng, so to the public
282 # header directive is moot, but needed if some day there is one.
283 #
284 if( DEV_PKG )
285         set( target_list "rmr_nng_shared;rmr_nng_static;rmr_shared;rmr_static" )
286 else()
287         set( target_list "rmr_nng_shared;rmr_shared" )
288 endif()
289
290 install( TARGETS ${target_list} EXPORT LibraryConfig
291         LIBRARY  DESTINATION ${install_lib}
292         ARCHIVE  DESTINATION ${install_lib}
293         PUBLIC_HEADER DESTINATION ${install_inc}
294 )
295
296
297 unset( DEV_PKG  CACHE )                 # prevent from being a hiddent setting if user redoes things
298
299 # install any nano/nng libraries in to the deb as well, but ONLY if asked for on the 'cmake ..' command
300 # (sure would be nice if FILEs allowed for globbing; sadlyy it does not.)
301 #
302 if( PACK_EXTERNALS )
303         message( "+++ including nano and nng libraries in the deb" )
304         install( FILES
305                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnanomsg${nano_so_suffix}
306                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnanomsg${nano_so_suffix_m}
307                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnanomsg${nano_so_suffix_mm}
308                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnng${nng_so_suffix}
309                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnng${nng_so_suffix_m}
310                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnng${nng_so_suffix_mm}
311
312                 DESTINATION ${install_lib}
313         )
314 endif()
315
316 unset( SKIP_EXTERNALS  CACHE )  # prevent these from being applied next build unless specifically set on comd line
317 unset( PACK_EXTERNALS  CACHE )
318
319 IF( EXISTS "${CMAKE_ROOT}/Modules/CPack.cmake" )
320         include( InstallRequiredSystemLibraries )
321
322         set( CPACK_PACKAGE_NAME ${pkg_name} )
323         set( CPACK_set_DESTDIR "on" )
324         set( CPACK_PACKAGING_INSTALL_PREFIX "${install_root}" )
325         set( CPACK_GENERATOR "${pkg_list}" )
326
327         set( CPACK_PACKAGE_DESCRIPTION "Thin library for RIC xAPP messaging routed based on message type." )
328         set( CPACK_PACKAGE_DESCRIPTION_SUMMARY "RIC message routing library" )
329         set( CPACK_PACKAGE_VENDOR "None" )
330         set( CPACK_PACKAGE_CONTACT "None" )
331         set( CPACK_PACKAGE_VERSION_MAJOR "${major_version}" )
332         set( CPACK_PACKAGE_VERSION_MINOR "${minor_version}" )
333         set( CPACK_PACKAGE_VERSION_PATCH "${patch_level}" )
334         set( CPACK_DEBIAN_FILE_NAME "${deb_pkg_label}.deb" )
335         set( CPACK_RPM_FILE_NAME "${rpm_pkg_label}.rpm" )
336
337         # we build and ship the libraries, so there is NO dependency
338
339         set( CPACK_DEBIAN_PACKAGE_PRIORITY "optional" )
340         set( CPACK_DEBIAN_PACKAGE_SECTION "ric" )
341         set( CPACK_DEBIAN_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR} )
342         set( CPACK_RPM_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR} )
343
344         # this seems ingnored if included
345         #set( CPACK_COMPONENTS_ALL Libraries ApplicationData )
346
347         INCLUDE( CPack )
348 ENDIF()