enhance(API): Add multi-threaded call
[ric-plt/lib/rmr.git] / CMakeLists.txt
1
2 #
3 #==================================================================================
4 #       Copyright (c) 2019 Nokia 
5 #       Copyright (c) 2018-2019 AT&T Intellectual Property.
6 #
7 #   Licensed under the Apache License, Version 2.0 (the "License");
8 #   you may not use this file except in compliance with the License.
9 #   You may obtain a copy of the License at
10 #
11 #       http://www.apache.org/licenses/LICENSE-2.0
12 #
13 #   Unless required by applicable law or agreed to in writing, software
14 #   distributed under the License is distributed on an "AS IS" BASIS,
15 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 #   See the License for the specific language governing permissions and
17 #   limitations under the License.
18 #==================================================================================
19 #
20
21 project( rmr LANGUAGES C )
22 cmake_minimum_required( VERSION 3.5 )
23
24 set( major_version "1" )                # should be automatically populated from git tag later, but until CI process sets a tag we use this
25 set( minor_version "0" )
26 set( patch_level "26" )
27
28 set( install_root "${CMAKE_INSTALL_PREFIX}" )
29 set( install_lib "lib" )
30 set( install_inc "include/rmr" )
31 if( MAN_PREFIX )
32         set( install_man ${MAN_PREFIX} )                        # is there a cmake var for this -- can't find one
33 else()
34         set( install_man "/usr/share/man" )                     # this needs to be fixed so it's not hard coded
35 endif()
36
37 # Must use GNUInstallDirs to install libraries into correct
38 # locations on all platforms.
39 include( GNUInstallDirs )
40
41 # nano/nng install using LIBDIR as established by the gnu include; it varies from system
42 # to system, and we don't trust that it is always set, so we default to lib.
43 #
44 if( NOT CMAKE_INSTALL_LIBDIR )
45         set( CMAKE_INSTALL_LIBDIR "lib" )
46 endif()
47
48 # ---------------- extract some things from git ------------------------------
49
50 # commit id for the version string
51 execute_process( 
52         COMMAND bash -c "git rev-parse --short HEAD|awk '{printf\"%s\", $0}'" 
53         OUTPUT_VARIABLE git_id
54 )
55
56 # version information for library names and version string
57 execute_process( 
58         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' "
59         OUTPUT_VARIABLE mmp_version_str
60         ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE
61 )
62
63 # extra indicator to show that the build was based on modified file(s) and not the true commit
64 # (no hope of reproducing the exact library for debugging). Used only for the internal version 
65 # string.
66 execute_process( 
67         COMMAND bash -c "git diff --shortstat|awk -v fmt=%s -v r=-rotten '{ s=r } END { printf( fmt, s ) }'" 
68         OUTPUT_VARIABLE spoiled_str
69 )
70
71 # uncomment these lines once CI starts adding a tag on merge
72 #set( mmp_version ${mmp_version_str} )
73 #list( GET mmp_version 0 major_version )
74 #list( GET mmp_version 1 minor_version )
75 #list( GET mmp_version 2 patch_level )
76
77 message( "+++ building ${major_version}.${minor_version}.${patch_level}${spoiled_str}" )
78
79 # define constants used in the version string
80 add_definitions( 
81         -DGIT_ID=${git_id} 
82         -DMAJOR_VER=${major_version}
83         -DMINOR_VER=${minor_version}
84         -DPATCH_VER=${patch_level}${spoiled_str}
85 )
86
87
88 # ---------------- setup nano/nng things ---------------------------------------
89 if( NOT SKIP_EXTERNALS )
90         set( need_ext 1 )                               # we force dependences on these for right build order
91     execute_process( COMMAND  git submodule update --init -- ext/nng
92             WORKING_DIRECTORY  ${CMAKE_CURRENT_SOURCE_DIR}
93     )
94
95     execute_process( COMMAND  git submodule update --init -- ext/nanomsg
96             WORKING_DIRECTORY  ${CMAKE_CURRENT_SOURCE_DIR}
97     )
98     if( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/ext/nng/CMakeLists.txt )
99         message( FATAL_ERROR "cannot find nng in our git source as a submodule: Giving up" )    # this will abort which seems wrong, but tdam.
100     endif()
101
102         include( ExternalProject )
103         ExternalProject_Add(
104                 ext_nng
105                 SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext/nng"
106                 CMAKE_ARGS "-DBUILD_SHARED_LIBS=1"
107                 CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}"
108                 BUILD_COMMAND "make"
109                 UPDATE_COMMAND ""
110                 TEST_COMMAND ""
111                 STEP_TARGETS build
112         )
113         ExternalProject_Add(
114                 nanomsg
115                 SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext/nanomsg"
116                 BUILD_COMMAND "make"
117                 UPDATE_COMMAND ""
118                 CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}"
119                 TEST_COMMAND ""
120                 STEP_TARGETS build
121         )
122
123         # it seems impossible to install everything that lands in {bin}/lib, so we need to
124         # hard code (shudder) some things. Even worse, we have to make exceptions for
125         # builds on apple (osx) since their naming convention wandered off the path.
126         set( nng_major 1 )
127         set( nng_minor 1.0 )
128         set( nano_major 5 )
129         set( nano_minor 1.0 )
130         set( so ${CMAKE_SHARED_LIBRARY_SUFFIX} )        # cmake variables are impossibly long :(
131         if( NOT APPLE )                                 # probably breaks in windows, but idc
132                 set( nng_so_suffix ${so} )
133                 set( nng_so_suffix_m ${so}.${nng_major} )
134                 set( nng_so_suffix_mm ${so}.${nng_major}.${nng_minor} )
135
136                 set( nano_so_suffix ${so} )
137                 set( nano_so_suffix_m ${so}.${nano_major} )
138                 set( nano_so_suffix_mm ${so}.${nano_major}.${nano_minor} )
139         else()
140                 # of course apple puts versions before the suffix :(
141                 set( nng_so_suffix ${so} )                                                                      # so has a lead dot, so NOT needed
142                 set( nng_so_suffix_m ".${nng_major}${so}" )                                     # these need leading dots
143                 set( nng_so_suffix_mm ".${nng_major}.${nng_minor}${so}" )
144
145                 set( nano_so_suffix ${so} )
146                 set( nano_so_suffix_m ".${nano_major}${so}" )
147                 set( nano_so_suffix_mm ".${nano_major}.${nano_minor}${so}" )
148         endif()
149
150         message( "+++ installing nano/nng with: ${nano_major}.${nano_minor} | ${nng_major}.${nng_minor}" )
151 else()
152         set( need_ext 0 )
153 endif()
154 unset( SKIP_EXTERNALS  CACHE )  # prevent it from being applied next build unless specifically set on comd line
155
156
157 # this gets us round a chicken/egg problem. include files don't exist until make is run
158 # but Cmake insists on having these exist when we add them to include directories to
159 # enable rmr code to find them after we build them.
160 #
161 execute_process( COMMAND "mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/include/nng" )
162 include_directories( "${CMAKE_CURRENT_BINARY_DIR}/include" )
163
164
165 # Compiler flags
166 #
167 set( CMAKE_POSITION_INDEPENDENT_CODE ON )
168 set( CMAKE_CXX_FLAGS "-g -Wall " )
169
170 # Include modules
171 add_subdirectory( src/rmr/common )
172 add_subdirectory( src/rmr/nanomsg )
173 add_subdirectory( src/rmr/nng )
174 add_subdirectory( doc )                         # this will auto skip if {X}fm is not available
175
176
177 # shared and static libraries built from the same object files
178 # Nanomsg based library (librmr )
179 # library is built by pulling object files from nano and common subdirs
180 #
181 add_library( rmr_shared SHARED "$<TARGET_OBJECTS:nano_objects>;$<TARGET_OBJECTS:common_objects>" )
182 add_library( rmr_static STATIC "$<TARGET_OBJECTS:nano_objects>;$<TARGET_OBJECTS:common_objects>" )
183
184 # both libraries to be named with librmr prefix and given version numbers with sym links
185 set_target_properties( rmr_shared 
186         PROPERTIES 
187                 OUTPUT_NAME "rmr" 
188                 SOVERSION ${major_version} 
189                 VERSION ${major_version}.${minor_version}.${patch_level} )
190
191 set_target_properties( rmr_static 
192         PROPERTIES 
193                 OUTPUT_NAME "rmr" 
194                 SOVERSION ${major_version} 
195                 VERSION ${major_version}.${minor_version}.${patch_level} )
196
197
198 # NNG based library (librmr_nng )
199 # library is built by pulling objects from nng and common subdirs.
200 #
201 add_library( rmr_nng_shared SHARED "$<TARGET_OBJECTS:nng_objects>;$<TARGET_OBJECTS:common_objects>" )
202 add_library( rmr_nng_static STATIC "$<TARGET_OBJECTS:nng_objects>;$<TARGET_OBJECTS:common_objects>" )
203
204
205
206 # if externals need to be built, then we must force them to be built first by depending on them
207 if( need_ext )
208         add_dependencies( rmr_static;rmr_shared nanomsg )
209         add_dependencies( rmr_nng_shared;rmr_nng_static ext_nng )
210 endif()
211
212
213 # both libraries to be named with librmr_nng prefix and given version numbers with sym links
214 set_target_properties( rmr_nng_shared 
215         PROPERTIES 
216                 OUTPUT_NAME "rmr_nng" 
217                 SOVERSION ${major_version} 
218                 VERSION ${major_version}.${minor_version}.${patch_level} )
219
220 set_target_properties( rmr_nng_static 
221         PROPERTIES 
222                 OUTPUT_NAME "rmr_nng" 
223                 SOVERSION ${major_version} 
224                 VERSION ${major_version}.${minor_version}.${patch_level} )
225
226 #
227 if( APPLE  )
228         message( "### apple hack: forcing hard coded library paths for nng/nano dynamic libraries" )
229         target_link_libraries( rmr_shared ${CMAKE_CURRENT_BINARY_DIR}/lib/libnanomsg${nano_so_suffix} )
230         target_link_libraries( rmr_nng_shared ${CMAKE_CURRENT_BINARY_DIR}/lib/libnng${nng_so_suffix} )
231 endif()
232
233 # Define directories where package should drop things when installed
234 # In CMake speak archive == *.a  library == *.so, so both are needed
235 # Headers from the common directory are forced to install by the local CM file in common. At
236 # the moment, there are no header files specific to either nano or nng, so to the public
237 # header directive is moot, but needed if some day there is one.
238 #
239 #install( TARGETS rmr_nng_shared;rmr_nng_static;rmr_shared;rmr_static;rmr_nng_shared_mm EXPORT LibraryConfig
240 install( TARGETS rmr_nng_shared;rmr_nng_static;rmr_shared;rmr_static EXPORT LibraryConfig
241     ARCHIVE  DESTINATION ${install_lib}
242     LIBRARY  DESTINATION ${install_lib}
243     PUBLIC_HEADER DESTINATION ${install_inc}
244 )
245
246
247
248 # install any nano/nng libraries in to the deb as well, but ONLY if we created them.
249 # (sure would be nice if FILEs allowed for globbing; sadlyy it does not.
250 #
251 if( need_ext )
252         message( "including nano and nng libraries in the deb" )
253         install( FILES
254                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnanomsg${nano_so_suffix}
255                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnanomsg${nano_so_suffix_m}
256                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnanomsg${nano_so_suffix_mm}
257                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnng${nng_so_suffix}
258                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnng${nng_so_suffix_m}
259                 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnng${nng_so_suffix_mm}
260
261                 DESTINATION ${install_lib}
262         )
263 endif()
264
265
266 IF( EXISTS "${CMAKE_ROOT}/Modules/CPack.cmake" )
267         include( InstallRequiredSystemLibraries )
268
269         set( CPACK_set_DESTDIR "on" )
270         set( CPACK_PACKAGING_INSTALL_PREFIX "${install_root}" )
271         set( CPACK_GENERATOR "DEB" )
272
273         set( CPACK_PACKAGE_DESCRIPTION "Thin library for RIC xAPP messaging routed based on message type." )
274         set( CPACK_PACKAGE_DESCRIPTION_SUMMARY "RIC message routing library" )
275         set( CPACK_PACKAGE_VENDOR "None" )
276         set( CPACK_PACKAGE_CONTACT "None" )
277         set( CPACK_PACKAGE_VERSION_MAJOR "${major_version}" )
278         set( CPACK_PACKAGE_VERSION_MINOR "${minor_version}" )
279         set( CPACK_PACKAGE_VERSION_PATCH "${patch_level}" )
280         set( CPACK_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}_${major_version}.${minor_version}.${CPACK_PACKAGE_VERSION_PATCH}${spoiled_str}" )
281         set( CPACK_SOURCE_PACKAGE_FILE_NAME "vric${CMAKE_PROJECT_NAME}_${major_version}.${minor_version}.${CPACK_PACKAGE_VERSION_PATCH}" )
282
283         # we build and ship the libraries, so there is NO dependency
284         #set( CPACK_DEBIAN_PACKAGE_DEPENDS "nanomsg ( >= 1.1.4 ), nng ( >= 1.1.1 )" )
285
286         set( CPACK_DEBIAN_PACKAGE_PRIORITY "optional" )
287         set( CPACK_DEBIAN_PACKAGE_SECTION "ric" )
288         set( CPACK_DEBIAN_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR} )
289
290         # this seems ingnored if included
291         #set( CPACK_COMPONENTS_ALL Libraries ApplicationData )
292
293         INCLUDE( CPack )
294 ENDIF()