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