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