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