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