2 #==================================================================================
3 # Copyright (c) 2019-2021 Nokia
4 # Copyright (c) 2018-2021 AT&T Intellectual Property.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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 #==================================================================================
20 # This CMake definition supports several -D command line options:
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.
38 # See ci/build_all for an example of how to build and test
40 project( rmr LANGUAGES C )
41 cmake_minimum_required( VERSION 3.5 )
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 "8" )
45 set( patch_level "3" )
47 set( install_root "${CMAKE_INSTALL_PREFIX}" )
48 set( install_inc "include/rmr" )
50 set( install_man ${MAN_PREFIX} ) # is there a cmake var for this -- can't find one
52 set( install_man "/usr/share/man" ) # this needs to be fixed so it's not hard coded
55 # Must use GNUInstallDirs to install libraries into correct locations on all platforms.
56 include( GNUInstallDirs )
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.
61 if( NOT CMAKE_INSTALL_LIBDIR )
62 set( CMAKE_INSTALL_LIBDIR "lib" )
65 if( IGNORE_LIBDIR ) # if set, then force to lib otherwise use "system preference"
66 set( install_lib "lib" )
68 set( install_lib "${CMAKE_INSTALL_LIBDIR}" )
70 unset(IGNORE_LIBDIR CACHE ) # we don't want this to persist
71 message( "+++ RMR library install target directory: ${install_lib}" )
73 # ---------------- extract some things from git ------------------------------
75 # commit id for the version string
77 COMMAND bash -c "git rev-parse --short HEAD|awk '{printf\"%s\", $0}'"
78 OUTPUT_VARIABLE git_id
81 # version information for library names and version string
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
87 message( "+++ mmp version from tag: '${mmp_version_str}'" )
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
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
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 )
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}" )
108 message( "+++ debugging is set to off (use -DDEBUG=1 to enable)" )
110 unset( DEBUG CACHE ) # we don't want this to persist
113 # define constants used in the version string, debugging, etc.
116 -DMAJOR_VER=${major_version}
117 -DMINOR_VER=${minor_version}
118 -DPATCH_VER=${patch_level}
122 # ---------------- suss out pkg gen tools so we don't fail generating packages that the system cannot support --------------
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.
128 if( ${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64" )
129 if( NOT PRESERVE_PTYPE )
130 set( deb_sys_name "amd64" )
132 set( deb_sys_name ${CMAKE_SYSTEM_PROCESSOR} )
135 set( deb_sys_name ${CMAKE_SYSTEM_PROCESSOR} )
137 unset( PRESERVE_PTYPE CACHE ) # we don't want this to persist
139 set( rpm_sys_name ${CMAKE_SYSTEM_PROCESSOR} )
142 set( deb_pkg_name "rmr-dev" )
143 set( rpm_pkg_name "rmr-devel" )
145 set( deb_pkg_name "rmr" )
146 set( rpm_pkg_name "rmr" )
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" )
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
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" )
162 message( "+++ pkg name: ${rpm_pkg_label}.rpm" ) # debugging if we think we can gen rpm too
163 set( pkg_list "DEB;RPM" )
165 message( "+++ make package will generate both deb and rpm packages" )
168 # ---------------- setup nano/nng things ---------------------------------------
170 set( PACK_EXTERNALS 0 )
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}
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.
182 include( ExternalProject )
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}"
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.
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} )
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}" )
211 message( "+++ building with nng: ${nng_major}.${nng_minor}" )
214 # This makes some stand-alone unit testing possible for bindings and transport layer testing;
215 # it is not meant for production packages.
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)" )
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.
231 execute_process( COMMAND "mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/include/nng" )
232 include_directories( "${CMAKE_CURRENT_BINARY_DIR}/include" )
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 " )
244 message( "+++ profiling is off (use -DGPROF=1 to enable" )
245 set( CMAKE_C_FLAGS "-g " )
247 unset( GPROF CACHE ) # we don't want this to persist
249 if( DEFINED OPT_LEVEL )
250 message( "+++ optimisation is forced to ${OPT_LEVEL}" )
251 set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O${OPT_LEVEL} " )
253 message( "+++ optimisation will default (use -DOPT_LEVEL=n to force specific level, or disable)" )
255 unset( OPT_LEVEL CACHE ) # no optimisation flage does NOT percist
257 message( "+++ compiler flags: ${CMAKE_C_FLAGS}" )
261 add_subdirectory( src/rmr/common )
263 add_subdirectory( src/rmr/nng )
265 add_subdirectory( src/rmr/si )
266 add_subdirectory( doc ) # this will auto skip if {X}fm is not available
269 # shared and static libraries are built from the same object files.
270 # librmr_* is a combination of common and * specific rmr functions.
274 add_library( rmr_nng_shared SHARED "$<TARGET_OBJECTS:nng_objects>;$<TARGET_OBJECTS:common_objects>" )
275 set_target_properties( rmr_nng_shared
277 OUTPUT_NAME "rmr_nng"
278 SOVERSION ${major_version}
279 VERSION ${major_version}.${minor_version}.${patch_level} )
281 # we only build/export the static archive (.a) if generating a dev package
283 add_library( rmr_nng_static STATIC "$<TARGET_OBJECTS:nng_objects>;$<TARGET_OBJECTS:common_objects>" )
284 set_target_properties( rmr_nng_static
286 OUTPUT_NAME "rmr_nng"
287 SOVERSION ${major_version}
288 VERSION ${major_version}.${minor_version}.${patch_level} )
292 add_library( rmr_si_shared SHARED "$<TARGET_OBJECTS:rmr_si_objects>;$<TARGET_OBJECTS:common_objects>" )
293 set_target_properties( rmr_si_shared
296 SOVERSION ${major_version}
297 VERSION ${major_version}.${minor_version}.${patch_level} )
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.
302 add_library( rmr_si_static STATIC "$<TARGET_OBJECTS:rmr_si_objects>;$<TARGET_OBJECTS:common_objects>" )
303 set_target_properties( rmr_si_static
306 SOVERSION ${major_version}
307 VERSION ${major_version}.${minor_version}.${patch_level} )
310 # if externals need to be built, then we must force them to be built first by depending on them
313 add_dependencies( rmr_nng_shared;rmr_nng_static ext_nng )
315 add_dependencies( rmr_nng_shared ext_nng )
320 # ------------- testing -------------------------------------------------------
322 # cmake cannot set env vars, so we have to passed desired vars on the wrapper command
324 NAME drive_unit_tests
325 COMMAND bash ../test/run_unit_tests.sh CMBUILD=${CMAKE_CURRENT_BINARY_DIR}
326 WORKING_DIRECTORY ../test
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.
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
338 # --- support binaries that depend on the libs identified above ---------------
339 add_subdirectory( src/support )
341 # ------------- packaging -----------------------------------------------------
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} )
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.
357 set( target_list "rmr_nng_static;rmr_si_static" )
359 set( target_list "rmr_nng_shared;rmr_si_shared" )
363 set( target_list "rmr_si_static" )
365 set( target_list "rmr_si_shared" )
369 install( TARGETS ${target_list} EXPORT LibraryConfig
370 LIBRARY DESTINATION ${install_lib}
371 ARCHIVE DESTINATION ${install_lib}
372 PUBLIC_HEADER DESTINATION ${install_inc}
376 unset( DEV_PKG CACHE ) # prevent from being a hidden setting if user redoes things
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
383 message( "+++ including nano and nng libraries in the deb" )
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}
389 DESTINATION ${install_lib}
393 unset( SKIP_EXTERNALS CACHE ) # prevent these from being applied next build unless specifically set on comd line
394 unset( PACK_EXTERNALS CACHE )
396 IF( EXISTS "${CMAKE_ROOT}/Modules/CPack.cmake" )
397 include( InstallRequiredSystemLibraries )
399 set( CPACK_DEBIAN_PACKAGE_NAME ${deb_pkg_name} )
400 set( CPACK_RPM_PACKAGE_NAME ${rpm_pkg_name} )
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" )
405 set( CPACK_set_DESTDIR "on" )
406 set( CPACK_PACKAGING_INSTALL_PREFIX "${install_root}" )
407 set( CPACK_GENERATOR "${pkg_list}" )
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" )
420 # there is not an NNG package, so we cannot define a dependency
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} )
427 # this seems ingnored if included
428 #set( CPACK_COMPONENTS_ALL Libraries ApplicationData )