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