blob: c9ca0ed7a184288df80a17ad3dd24a133a7a2912 [file] [log] [blame]
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -04001#
2#==================================================================================
E. Scott Daniels29d87162019-06-12 13:48:25 -04003# Copyright (c) 2019 Nokia
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -04004# 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
20project( rmr LANGUAGES C )
21cmake_minimum_required( VERSION 3.5 )
22
E. Scott Danielsd7109572019-04-18 14:01:16 +000023set( major_version "1" ) # should be automatically populated from git tag later, but until CI process sets a tag we use this
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040024set( minor_version "0" )
E. Scott Daniels29d87162019-06-12 13:48:25 -040025set( patch_level "34" )
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040026
27set( install_root "${CMAKE_INSTALL_PREFIX}" )
28set( install_lib "lib" )
29set( install_inc "include/rmr" )
30if( MAN_PREFIX )
31 set( install_man ${MAN_PREFIX} ) # is there a cmake var for this -- can't find one
32else()
33 set( install_man "/usr/share/man" ) # this needs to be fixed so it's not hard coded
34endif()
35
36# Must use GNUInstallDirs to install libraries into correct
37# locations on all platforms.
38include( GNUInstallDirs )
39
E. Scott Danielsa9435332019-05-09 14:39:19 +000040# 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#
43if( NOT CMAKE_INSTALL_LIBDIR )
44 set( CMAKE_INSTALL_LIBDIR "lib" )
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040045endif()
46
E. Scott Daniels68c5cf12019-04-12 15:14:40 +000047# ---------------- extract some things from git ------------------------------
48
49# commit id for the version string
E. Scott Daniels29d87162019-06-12 13:48:25 -040050execute_process(
51 COMMAND bash -c "git rev-parse --short HEAD|awk '{printf\"%s\", $0}'"
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040052 OUTPUT_VARIABLE git_id
53)
54
E. Scott Daniels68c5cf12019-04-12 15:14:40 +000055# version information for library names and version string
E. Scott Daniels29d87162019-06-12 13:48:25 -040056execute_process(
E. Scott Daniels68c5cf12019-04-12 15:14:40 +000057 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
E. Scott Daniels29d87162019-06-12 13:48:25 -040063# (no hope of reproducing the exact library for debugging). Used only for the internal version
E. Scott Daniels68c5cf12019-04-12 15:14:40 +000064# string.
E. Scott Daniels29d87162019-06-12 13:48:25 -040065execute_process(
66 COMMAND bash -c "git diff --shortstat|awk -v fmt=%s -v r=-rotten '{ s=r } END { printf( fmt, s ) }'"
E. Scott Daniels68c5cf12019-04-12 15:14:40 +000067 OUTPUT_VARIABLE spoiled_str
68)
69
E. Scott Danielsd7109572019-04-18 14:01:16 +000070# 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 )
E. Scott Daniels68c5cf12019-04-12 15:14:40 +000075
E. Scott Daniels68c5cf12019-04-12 15:14:40 +000076
77# define constants used in the version string
E. Scott Daniels29d87162019-06-12 13:48:25 -040078add_definitions(
79 -DGIT_ID=${git_id}
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040080 -DMAJOR_VER=${major_version}
81 -DMINOR_VER=${minor_version}
E. Scott Daniels68c5cf12019-04-12 15:14:40 +000082 -DPATCH_VER=${patch_level}${spoiled_str}
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040083)
84
E. Scott Daniels29d87162019-06-12 13:48:25 -040085# ---------------- suss out pkg gen tools so we don't fail generating packages that the system cannot support --------------
86
87
88if( DEV_PKG )
89 set( dev_tag "-dev" )
90endif()
91set( pkg_label "${CMAKE_PROJECT_NAME}-${major_version}.${minor_version}.${patch_level}.${CMAKE_SYSTEM_PROCESSOR}${dev_tag}" )
92message( "+++ building ${pkg_label}${spoiled_str}" )
93set( out_yml /tmp/build_output.yml ) # we will record package names (we record only untainted names)
94find_program( rpm NAMES rpmbuild ) # rpm package gen requires this to be installed
95
96set( gen_rpm 0 )
97if( "${rpm}" MATCHES "rpm-NOTFOUND" ) # cannot build rpm
98 set( pkg_list "DEB" )
99 message( "### make package will generate only deb package; cannot find support to generate rpm packages" )
100else()
101 set( pkg_list "DEB;RPM" )
102 set( gen_rpm 1 )
103 message( "+++ make package will generate both deb and rpm packages" )
104endif()
105
106execute_process(
107 COMMAND bash -c "printf '# RMr build generated list of package paths\n---\n' >${out_yml}"
E. Scott Daniels8a03ad02019-06-11 15:57:02 -0400108)
109
E. Scott Daniels29d87162019-06-12 13:48:25 -0400110execute_process(
111 COMMAND bash -c " echo deb: ${CMAKE_CURRENT_BINARY_DIR}/${pkg_label}.deb >>${out_yml}"
112)
113
114if( gen_rpm )
115 execute_process(
116 COMMAND bash -c " echo rpm: ${CMAKE_CURRENT_BINARY_DIR}/${pkg_label}.rpm >>${out_yml}"
117 )
118endif()
119
120execute_process(
121 COMMAND bash -c "printf '...\n' >>${out_yml}"
122)
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400123
124# ---------------- setup nano/nng things ---------------------------------------
125if( NOT SKIP_EXTERNALS )
126 set( need_ext 1 ) # we force dependences on these for right build order
127 execute_process( COMMAND git submodule update --init -- ext/nng
128 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
129 )
130
131 execute_process( COMMAND git submodule update --init -- ext/nanomsg
132 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
133 )
134 if( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/ext/nng/CMakeLists.txt )
135 message( FATAL_ERROR "cannot find nng in our git source as a submodule: Giving up" ) # this will abort which seems wrong, but tdam.
136 endif()
137
138 include( ExternalProject )
139 ExternalProject_Add(
140 ext_nng
141 SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext/nng"
142 CMAKE_ARGS "-DBUILD_SHARED_LIBS=1"
143 CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}"
144 BUILD_COMMAND "make"
145 UPDATE_COMMAND ""
146 TEST_COMMAND ""
147 STEP_TARGETS build
148 )
149 ExternalProject_Add(
150 nanomsg
151 SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext/nanomsg"
152 BUILD_COMMAND "make"
153 UPDATE_COMMAND ""
154 CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}"
155 TEST_COMMAND ""
156 STEP_TARGETS build
157 )
158
159 # it seems impossible to install everything that lands in {bin}/lib, so we need to
160 # hard code (shudder) some things. Even worse, we have to make exceptions for
161 # builds on apple (osx) since their naming convention wandered off the path.
162 set( nng_major 1 )
163 set( nng_minor 1.0 )
164 set( nano_major 5 )
165 set( nano_minor 1.0 )
166 set( so ${CMAKE_SHARED_LIBRARY_SUFFIX} ) # cmake variables are impossibly long :(
167 if( NOT APPLE ) # probably breaks in windows, but idc
168 set( nng_so_suffix ${so} )
169 set( nng_so_suffix_m ${so}.${nng_major} )
170 set( nng_so_suffix_mm ${so}.${nng_major}.${nng_minor} )
171
172 set( nano_so_suffix ${so} )
173 set( nano_so_suffix_m ${so}.${nano_major} )
174 set( nano_so_suffix_mm ${so}.${nano_major}.${nano_minor} )
175 else()
176 # of course apple puts versions before the suffix :(
177 set( nng_so_suffix ${so} ) # so has a lead dot, so NOT needed
178 set( nng_so_suffix_m ".${nng_major}${so}" ) # these need leading dots
179 set( nng_so_suffix_mm ".${nng_major}.${nng_minor}${so}" )
180
181 set( nano_so_suffix ${so} )
182 set( nano_so_suffix_m ".${nano_major}${so}" )
183 set( nano_so_suffix_mm ".${nano_major}.${nano_minor}${so}" )
184 endif()
185
186 message( "+++ installing nano/nng with: ${nano_major}.${nano_minor} | ${nng_major}.${nng_minor}" )
187else()
E. Scott Daniels29d87162019-06-12 13:48:25 -0400188 if( PACK_EXTERNALS )
189 unset( SKIP_EXTERNALS CACHE ) # must remove so as not to trap user into a never ending failure
190 unset( PACK_EXTERNALS CACHE )
191 message( FATAL_ERROR "ERROR: PACK_EXTERNALS can be set only if SKIP_EXTERNALS is unset (=0, or not supplied on command line)" )
192 endif()
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400193 set( need_ext 0 )
194endif()
E. Scott Daniels29d87162019-06-12 13:48:25 -0400195
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400196
197
198# this gets us round a chicken/egg problem. include files don't exist until make is run
199# but Cmake insists on having these exist when we add them to include directories to
200# enable rmr code to find them after we build them.
201#
202execute_process( COMMAND "mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/include/nng" )
203include_directories( "${CMAKE_CURRENT_BINARY_DIR}/include" )
204
205
206# Compiler flags
207#
208set( CMAKE_POSITION_INDEPENDENT_CODE ON )
209set( CMAKE_CXX_FLAGS "-g -Wall " )
210
211# Include modules
E. Scott Daniels68c1ab22019-05-17 17:50:59 +0000212add_subdirectory( src/rmr/common )
213add_subdirectory( src/rmr/nanomsg )
214add_subdirectory( src/rmr/nng )
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400215add_subdirectory( doc ) # this will auto skip if {X}fm is not available
216
217
E. Scott Daniels29d87162019-06-12 13:48:25 -0400218# shared and static libraries are built from the same object files.
219# Nanomsg based library (librmr ) while nng based is librmr_nng.
220# library is built by pulling object files from either the nano or nng and common subdirs
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400221#
222add_library( rmr_shared SHARED "$<TARGET_OBJECTS:nano_objects>;$<TARGET_OBJECTS:common_objects>" )
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400223
E. Scott Daniels29d87162019-06-12 13:48:25 -0400224set_target_properties( rmr_shared
225 PROPERTIES
226 OUTPUT_NAME "rmr"
227 SOVERSION ${major_version}
E. Scott Daniels68c5cf12019-04-12 15:14:40 +0000228 VERSION ${major_version}.${minor_version}.${patch_level} )
229
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400230add_library( rmr_nng_shared SHARED "$<TARGET_OBJECTS:nng_objects>;$<TARGET_OBJECTS:common_objects>" )
E. Scott Daniels29d87162019-06-12 13:48:25 -0400231set_target_properties( rmr_nng_shared
232 PROPERTIES
233 OUTPUT_NAME "rmr_nng"
234 SOVERSION ${major_version}
235 VERSION ${major_version}.${minor_version}.${patch_level} )
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400236
E. Scott Daniels29d87162019-06-12 13:48:25 -0400237# we only build/export the static archive (.a) if generating a dev package
238if( DEV_PKG )
239 add_library( rmr_nng_static STATIC "$<TARGET_OBJECTS:nng_objects>;$<TARGET_OBJECTS:common_objects>" )
240 set_target_properties( rmr_nng_static
241 PROPERTIES
242 OUTPUT_NAME "rmr_nng"
243 SOVERSION ${major_version}
244 VERSION ${major_version}.${minor_version}.${patch_level} )
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400245
E. Scott Daniels29d87162019-06-12 13:48:25 -0400246 add_library( rmr_static STATIC "$<TARGET_OBJECTS:nano_objects>;$<TARGET_OBJECTS:common_objects>" )
247
248 set_target_properties( rmr_static
249 PROPERTIES
250 OUTPUT_NAME "rmr"
251 SOVERSION ${major_version}
252 VERSION ${major_version}.${minor_version}.${patch_level} )
253endif()
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400254
255# if externals need to be built, then we must force them to be built first by depending on them
256if( need_ext )
E. Scott Daniels29d87162019-06-12 13:48:25 -0400257 if( DEV_PKG )
258 add_dependencies( rmr_static;rmr_shared nanomsg )
259 add_dependencies( rmr_nng_shared;rmr_nng_static ext_nng )
260 else()
261 add_dependencies( rmr_shared nanomsg )
262 add_dependencies( rmr_nng_shared ext_nng )
263 endif()
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400264endif()
265
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400266#
267if( APPLE )
268 message( "### apple hack: forcing hard coded library paths for nng/nano dynamic libraries" )
269 target_link_libraries( rmr_shared ${CMAKE_CURRENT_BINARY_DIR}/lib/libnanomsg${nano_so_suffix} )
270 target_link_libraries( rmr_nng_shared ${CMAKE_CURRENT_BINARY_DIR}/lib/libnng${nng_so_suffix} )
271endif()
272
273# Define directories where package should drop things when installed
274# In CMake speak archive == *.a library == *.so, so both are needed
275# Headers from the common directory are forced to install by the local CM file in common. At
276# the moment, there are no header files specific to either nano or nng, so to the public
277# header directive is moot, but needed if some day there is one.
278#
E. Scott Daniels29d87162019-06-12 13:48:25 -0400279if( DEV_PKG )
280 set( target_list "rmr_nng_shared;rmr_nng_static;rmr_shared;rmr_static" )
281else()
282 set( target_list "rmr_nng_shared;rmr_shared" )
283endif()
284
285install( TARGETS ${target_list} EXPORT LibraryConfig
286 LIBRARY DESTINATION ${install_lib}
287 ARCHIVE DESTINATION ${install_lib}
288 PUBLIC_HEADER DESTINATION ${install_inc}
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400289)
290
291
E. Scott Daniels29d87162019-06-12 13:48:25 -0400292unset( DEV_PKG CACHE ) # prevent from being a hiddent setting if user redoes things
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400293
E. Scott Daniels29d87162019-06-12 13:48:25 -0400294# install any nano/nng libraries in to the deb as well, but ONLY if asked for on the 'cmake ..' command
295# (sure would be nice if FILEs allowed for globbing; sadlyy it does not.)
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400296#
E. Scott Daniels29d87162019-06-12 13:48:25 -0400297if( PACK_EXTERNALS )
E. Scott Daniels257323c2019-06-14 12:30:21 -0400298 message( "+++ including nano and nng libraries in the deb" )
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400299 install( FILES
E. Scott Danielsa9435332019-05-09 14:39:19 +0000300 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnanomsg${nano_so_suffix}
301 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnanomsg${nano_so_suffix_m}
302 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnanomsg${nano_so_suffix_mm}
303 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnng${nng_so_suffix}
304 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnng${nng_so_suffix_m}
305 ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}/libnng${nng_so_suffix_mm}
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400306
307 DESTINATION ${install_lib}
308 )
309endif()
310
E. Scott Daniels257323c2019-06-14 12:30:21 -0400311unset( SKIP_EXTERNALS CACHE ) # prevent these from being applied next build unless specifically set on comd line
312unset( PACK_EXTERNALS CACHE )
313
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400314IF( EXISTS "${CMAKE_ROOT}/Modules/CPack.cmake" )
315 include( InstallRequiredSystemLibraries )
316
317 set( CPACK_set_DESTDIR "on" )
318 set( CPACK_PACKAGING_INSTALL_PREFIX "${install_root}" )
E. Scott Daniels29d87162019-06-12 13:48:25 -0400319 set( CPACK_GENERATOR "${pkg_list}" )
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400320
321 set( CPACK_PACKAGE_DESCRIPTION "Thin library for RIC xAPP messaging routed based on message type." )
322 set( CPACK_PACKAGE_DESCRIPTION_SUMMARY "RIC message routing library" )
323 set( CPACK_PACKAGE_VENDOR "None" )
324 set( CPACK_PACKAGE_CONTACT "None" )
325 set( CPACK_PACKAGE_VERSION_MAJOR "${major_version}" )
326 set( CPACK_PACKAGE_VERSION_MINOR "${minor_version}" )
327 set( CPACK_PACKAGE_VERSION_PATCH "${patch_level}" )
E. Scott Daniels29d87162019-06-12 13:48:25 -0400328 set( CPACK_PACKAGE_FILE_NAME "${pkg_label}${spoiled_str}" )
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400329
330 # we build and ship the libraries, so there is NO dependency
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400331
332 set( CPACK_DEBIAN_PACKAGE_PRIORITY "optional" )
333 set( CPACK_DEBIAN_PACKAGE_SECTION "ric" )
334 set( CPACK_DEBIAN_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR} )
E. Scott Daniels29d87162019-06-12 13:48:25 -0400335 set( CPACK_RPM_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR} )
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400336
337 # this seems ingnored if included
338 #set( CPACK_COMPONENTS_ALL Libraries ApplicationData )
339
340 INCLUDE( CPack )
341ENDIF()