5c79b4477afc3222a54eee9ae80ac9f0a99a568b
[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 # This CMake definition supports several -D command line options:
21 #
22 #       -DDEV_PKG=1                     Development package configuration
23 #       -DBUILD_DOC=1           Man pages generated
24 #       -DPRESERVE_PTYPE=1      Do not change the processor type when naming deb packages
25 #       -DPACK_EXTERNALS=1      Include external libraries used to build in the run-time package
26 #                                               (This makes some stand-alone unit testing of bindings possible, it
27 #                                               is not meant to be used for production package generation.)
28 #       -DSKIP_EXTERNALS=1      Do not use Nano/NNG submodules when building; uee installed packages
29 #       -DMAN_PREFIX=<path>     Supply a path where man pages are installed (default: /usr/share/man)
30
31 project( rmr LANGUAGES C )
32 cmake_minimum_required( VERSION 3.5 )
33
34 set( major_version "1" )                # should be automatically populated from git tag later, but until CI process sets a tag we use this
35 set( minor_version "0" )
36 set( patch_level "42" )
37
38 set( install_root "${CMAKE_INSTALL_PREFIX}" )
39 set( install_lib "lib" )
40 set( install_inc "include/rmr" )
41 if( MAN_PREFIX )
42         set( install_man ${MAN_PREFIX} )                        # is there a cmake var for this -- can't find one
43 else()
44         set( install_man "/usr/share/man" )                     # this needs to be fixed so it's not hard coded
45 endif()
46
47 # Must use GNUInstallDirs to install libraries into correct
48 # locations on all platforms.
49 include( GNUInstallDirs )
50
51 # nano/nng install using LIBDIR as established by the gnu include; it varies from system
52 # to system, and we don't trust that it is always set, so we default to lib.
53 #
54 if( NOT CMAKE_INSTALL_LIBDIR )
55         set( CMAKE_INSTALL_LIBDIR "lib" )
56 endif()
57
58 # ---------------- extract some things from git ------------------------------
59
60 # commit id for the version string
61 execute_process(
62         COMMAND bash -c "git rev-parse --short HEAD|awk '{printf\"%s\", $0}'"
63         OUTPUT_VARIABLE git_id
64 )
65
66 # version information for library names and version string
67 execute_process(
68         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' "
69         OUTPUT_VARIABLE mmp_version_str
70         ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE
71 )
72
73 # extra indicator to show that the build was based on modified file(s) and not the true commit
74 # (no hope of reproducing the exact library for debugging). Used only for the internal version
75 # string.
76 execute_process(
77         COMMAND bash -c "git diff --shortstat|awk -v fmt=%s -v r=-rotten '{ s=r } END { printf( fmt, s ) }'"
78         OUTPUT_VARIABLE spoiled_str
79 )
80
81 # uncomment these lines once CI starts adding a tag on merge
82 #set( mmp_version ${mmp_version_str} )
83 #list( GET mmp_version 0 major_version )
84 #list( GET mmp_version 1 minor_version )
85 #list( GET mmp_version 2 patch_level )
86
87
88 # define constants used in the version string
89 add_definitions(
90         -DGIT_ID=${git_id}
91         -DMAJOR_VER=${major_version}
92         -DMINOR_VER=${minor_version}
93         -DPATCH_VER=${patch_level}
94 )
95
96 # ---------------- suss out pkg gen tools so we don't fail generating packages that the system cannot support --------------
97
98 # deb packages use underbars, and package manager(s) seem to flip the *_64 processor type
99 # to the old (non-standard) amd64 string, so we do it here for consistency. Set -DPRESERVE_PTYPE=1 
100 # to prevent the flip. RPM packages will always be given the system generated processor type string.
101 #
102 if( ${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64" )
103         if( NOT PRESERVE_PTYPE )
104                 set( deb_sys_name "amd64" )
105         else()
106                 set( deb_sys_name ${CMAKE_SYSTEM_PROCESSOR} )
107         endif()
108 else()
109         set( deb_sys_name ${CMAKE_SYSTEM_PROCESSOR} )
110 endif()
111 unset( PRESERVE_PTYPE CACHE )                                   # we don't want this to persist
112
113 set( rpm_sys_name ${CMAKE_SYSTEM_PROCESSOR} )
114
115 if( DEV_PKG )
116         set( deb_pkg_name "rmr-dev" )
117         set( rpm_pkg_name "rmr-devel" )
118 else()
119         set( deb_pkg_name "rmr" )
120         set( rpm_pkg_name "rmr" )
121 endif()
122
123 set( pkg_label "rmr${spoiled_str}-${major_version}.${minor_version}.${patch_level}-${sys_name}" )
124 set( rpm_pkg_label "${rpm_pkg_name}${spoiled_str}-${major_version}.${minor_version}.${patch_level}-${rpm_sys_name}" )
125 set( deb_pkg_label "${deb_pkg_name}${spoiled_str}_${major_version}.${minor_version}.${patch_level}_${deb_sys_name}" )
126 message( "+++ pkg name: ${deb_pkg_label}.deb" )
127 message( "+++ pkg name: ${rpm_pkg_label}.rpm" )
128
129 set( out_yml /tmp/build_output.yml )                                    # we will record package names (we record only untainted names)
130 find_program( rpm NAMES rpmbuild )                                              # rpm package gen requires this to be installed
131
132 set( gen_rpm 0 )
133 if( "${rpm}" MATCHES "rpm-NOTFOUND" )                          # cannot build rpm
134         set( pkg_list "DEB" )
135         message( "### make package will generate only deb package; cannot find support to generate rpm packages" )
136 else()
137         set( pkg_list "DEB;RPM" )
138         set( gen_rpm 1 )
139         message( "+++ make package will generate both deb and rpm packages" )
140 endif()
141
142 execute_process(
143         COMMAND bash -c "printf '# RMr build generated list of package paths\n---\n' >${out_yml}"
144 )
145
146 execute_process(
147         COMMAND bash -c " echo deb: ${CMAKE_CURRENT_BINARY_DIR}/${deb_pkg_label}.deb  >>${out_yml}"
148 )
149
150 if( gen_rpm )
151         execute_process(
152                 COMMAND bash -c " echo rpm: ${CMAKE_CURRENT_BINARY_DIR}/${rpm_pkg_label}.rpm  >>${out_yml}"
153         )
154 endif()
155
156 execute_process(
157         COMMAND bash -c "printf '...\n' >>${out_yml}"
158 )
159
160 # ---------------- setup nano/nng things ---------------------------------------
161 if( NOT SKIP_EXTERNALS )
162         set( need_ext 1 )                               # we force dependences on these for right build order
163     execute_process( COMMAND  git submodule update --init -- ext/nng
164             WORKING_DIRECTORY  ${CMAKE_CURRENT_SOURCE_DIR}
165     )
166
167     execute_process( COMMAND  git submodule update --init -- ext/nanomsg
168             WORKING_DIRECTORY  ${CMAKE_CURRENT_SOURCE_DIR}
169     )
170     if( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/ext/nng/CMakeLists.txt )
171         message( FATAL_ERROR "cannot find nng in our git source as a submodule: Giving up" )    # this will abort which seems wrong, but tdam.
172     endif()
173
174         include( ExternalProject )
175         ExternalProject_Add(
176                 ext_nng
177                 SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext/nng"
178                 CMAKE_ARGS "-DBUILD_SHARED_LIBS=1"
179                 CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}"
180                 BUILD_COMMAND "make"
181                 UPDATE_COMMAND ""
182                 TEST_COMMAND ""
183                 STEP_TARGETS build
184         )
185         ExternalProject_Add(
186                 nanomsg
187                 SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext/nanomsg"
188                 BUILD_COMMAND "make"
189                 UPDATE_COMMAND ""
190                 CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}"
191                 TEST_COMMAND ""
192                 STEP_TARGETS build
193         )
194
195         # it seems impossible to install everything that lands in {bin}/lib, so we need to
196         # hard code (shudder) some things. Even worse, we have to make exceptions for
197         # builds on apple (osx) since their naming convention wandered off the path.
198         set( nng_major 1 )
199         set( nng_minor 1.0 )
200         set( nano_major 5 )
201         set( nano_minor 1.0 )
202         set( so ${CMAKE_SHARED_LIBRARY_SUFFIX} )        # cmake variables are impossibly long :(
203         if( NOT APPLE )                                 # probably breaks in windows, but idc
204                 set( nng_so_suffix ${so} )
205                 set( nng_so_suffix_m ${so}.${nng_major} )
206                 set( nng_so_suffix_mm ${so}.${nng_major}.${nng_minor} )
207
208                 set( nano_so_suffix ${so} )
209                 set( nano_so_suffix_m ${so}.${nano_major} )
210                 set( nano_so_suffix_mm ${so}.${nano_major}.${nano_minor} )
211         else()
212                 # of course apple puts versions before the suffix :(
213                 set( nng_so_suffix ${so} )                                                                      # so has a lead dot, so NOT needed
214                 set( nng_so_suffix_m ".${nng_major}${so}" )                                     # these need leading dots
215                 set( nng_so_suffix_mm ".${nng_major}.${nng_minor}${so}" )
216
217                 set( nano_so_suffix ${so} )
218                 set( nano_so_suffix_m ".${nano_major}${so}" )
219                 set( nano_so_suffix_mm ".${nano_major}.${nano_minor}${so}" )
220         endif()
221
222         message( "+++ installing nano/nng with: ${nano_major}.${nano_minor} | ${nng_major}.${nng_minor}" )
223 else()
224         if( PACK_EXTERNALS )
225                 # This makes some stand-alone unit testing possible for bindings and transport layer testing;
226                 # it is not meant for production packages.
227                 #
228                 unset( SKIP_EXTERNALS  CACHE )  # must remove so as not to trap user into a never ending failure
229                 unset( PACK_EXTERNALS  CACHE )
230                 message( FATAL_ERROR "ERROR: PACK_EXTERNALS can be set only if SKIP_EXTERNALS is unset (=0, or not supplied on command line)" )
231         endif()
232         set( need_ext 0 )
233 endif()
234
235
236
237 # this gets us round a chicken/egg problem. include files don't exist until make is run
238 # but Cmake insists on having these exist when we add them to include directories to
239 # enable rmr code to find them after we build them.
240 #
241 execute_process( COMMAND "mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/include/nng" )
242 include_directories( "${CMAKE_CURRENT_BINARY_DIR}/include" )
243
244
245 # Compiler flags
246 #
247 set( CMAKE_POSITION_INDEPENDENT_CODE ON )
248 set( CMAKE_CXX_FLAGS "-g -Wall " )
249
250 # Include modules
251 add_subdirectory( src/rmr/common )
252 add_subdirectory( src/rmr/nanomsg )
253 add_subdirectory( src/rmr/nng )
254 add_subdirectory( doc )                         # this will auto skip if {X}fm is not available
255
256
257 # shared and static libraries are built from the same object files.
258 # Nanomsg based library (librmr ) while nng based is librmr_nng.
259 # library is built by pulling object files from either the nano or nng  and common subdirs
260 #
261 add_library( rmr_shared SHARED "$<TARGET_OBJECTS:nano_objects>;$<TARGET_OBJECTS:common_objects>" )
262
263 set_target_properties( rmr_shared
264         PROPERTIES
265                 OUTPUT_NAME "rmr"
266                 SOVERSION ${major_version}
267                 VERSION ${major_version}.${minor_version}.${patch_level} )
268
269 add_library( rmr_nng_shared SHARED "$<TARGET_OBJECTS:nng_objects>;$<TARGET_OBJECTS:common_objects>" )
270 set_target_properties( rmr_nng_shared
271         PROPERTIES
272         OUTPUT_NAME "rmr_nng"
273         SOVERSION ${major_version}
274         VERSION ${major_version}.${minor_version}.${patch_level} )
275
276 # we only build/export the static archive (.a) if generating a dev package
277 if( DEV_PKG )
278         add_library( rmr_nng_static STATIC "$<TARGET_OBJECTS:nng_objects>;$<TARGET_OBJECTS:common_objects>" )
279         set_target_properties( rmr_nng_static
280                 PROPERTIES
281                 OUTPUT_NAME "rmr_nng"
282                 SOVERSION ${major_version}
283                 VERSION ${major_version}.${minor_version}.${patch_level} )
284
285         add_library( rmr_static STATIC "$<TARGET_OBJECTS:nano_objects>;$<TARGET_OBJECTS:common_objects>" )
286
287         set_target_properties( rmr_static
288                 PROPERTIES
289                 OUTPUT_NAME "rmr"
290                 SOVERSION ${major_version}
291                 VERSION ${major_version}.${minor_version}.${patch_level} )
292 endif()
293
294 # if externals need to be built, then we must force them to be built first by depending on them
295 if( need_ext )
296         if( DEV_PKG )
297                 add_dependencies( rmr_static;rmr_shared nanomsg )
298                 add_dependencies( rmr_nng_shared;rmr_nng_static ext_nng )
299         else()
300                 add_dependencies( rmr_shared nanomsg )
301                 add_dependencies( rmr_nng_shared ext_nng )
302         endif()
303 endif()
304
305 #
306 if( APPLE  )
307         message( "### apple hack: forcing hard coded library paths for nng/nano dynamic libraries" )
308         target_link_libraries( rmr_shared ${CMAKE_CURRENT_BINARY_DIR}/lib/libnanomsg${nano_so_suffix} )
309         target_link_libraries( rmr_nng_shared ${CMAKE_CURRENT_BINARY_DIR}/lib/libnng${nng_so_suffix} )
310 endif()
311
312 # Define what should be installed, and where they should go. For dev package we install
313 # only the RMr headers, man pages and archive (.a) files.  The run-time package gets just
314 # the library (.so) files and nothing more.
315 #
316 if( DEV_PKG )
317         set( target_list "rmr_nng_static;rmr_static" )
318 else()
319         set( target_list "rmr_nng_shared;rmr_shared" )
320 endif()
321
322 install( TARGETS ${target_list} EXPORT LibraryConfig
323         LIBRARY  DESTINATION ${install_lib}
324         ARCHIVE  DESTINATION ${install_lib}
325         PUBLIC_HEADER DESTINATION ${install_inc}
326 )
327
328
329 unset( DEV_PKG  CACHE )                 # prevent from being a hiddent setting if user redoes things
330
331 # install any nano/nng libraries in to the deb as well, but ONLY if asked for on the 'cmake ..' command
332 # (sure would be nice if FILEs allowed for globbing; sadlyy it does not.)
333 #
334 if( PACK_EXTERNALS )
335         message( "+++ including nano and nng libraries in the deb" )
336         install( FILES
337                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnanomsg${nano_so_suffix}
338                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnanomsg${nano_so_suffix_m}
339                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnanomsg${nano_so_suffix_mm}
340                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnng${nng_so_suffix}
341                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnng${nng_so_suffix_m}
342                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnng${nng_so_suffix_mm}
343
344                 DESTINATION ${install_lib}
345         )
346 endif()
347
348 unset( SKIP_EXTERNALS  CACHE )  # prevent these from being applied next build unless specifically set on comd line
349 unset( PACK_EXTERNALS  CACHE )
350
351 IF( EXISTS "${CMAKE_ROOT}/Modules/CPack.cmake" )
352         include( InstallRequiredSystemLibraries )
353
354         set( CPACK_DEBIAN_PACKAGE_NAME ${deb_pkg_name} )
355         set( CPACK_RPM_PACKAGE_NAME ${rpm_pkg_name} )
356
357         set( CPACK_set_DESTDIR "on" )
358         set( CPACK_PACKAGING_INSTALL_PREFIX "${install_root}" )
359         set( CPACK_GENERATOR "${pkg_list}" )
360
361         set( CPACK_PACKAGE_DESCRIPTION "Thin library for RIC xAPP messaging routed based on message type." )
362         set( CPACK_PACKAGE_DESCRIPTION_SUMMARY "RIC message routing library" )
363         set( CPACK_PACKAGE_VENDOR "None" )
364         set( CPACK_PACKAGE_CONTACT "None" )
365         set( CPACK_PACKAGE_VERSION_MAJOR "${major_version}" )
366         set( CPACK_PACKAGE_VERSION_MINOR "${minor_version}" )
367         set( CPACK_PACKAGE_VERSION_PATCH "${patch_level}" )
368         set( CPACK_PACKAGE "${pkg_label}" )                                             # generic name for old versions of cpack
369         set( CPACK_DEBIAN_FILE_NAME "${deb_pkg_label}.deb" )
370         set( CPACK_RPM_FILE_NAME "${rpm_pkg_label}.rpm" )
371
372         # we build and ship the libraries, so there is NO dependency
373
374         set( CPACK_DEBIAN_PACKAGE_PRIORITY "optional" )
375         set( CPACK_DEBIAN_PACKAGE_SECTION "ric" )
376         set( CPACK_DEBIAN_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR} )
377         set( CPACK_RPM_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR} )
378
379         # this seems ingnored if included
380         #set( CPACK_COMPONENTS_ALL Libraries ApplicationData )
381
382         INCLUDE( CPack )
383 ENDIF()