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