CMakeLists.txt 6.99 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
############################################################################
# CMakeLists.txt
# Copyright (C) 2016  Belledonne Communications, Grenoble France
#
############################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
jehan's avatar
jehan committed
20 21
#
############################################################################
22

23
cmake_minimum_required(VERSION 3.2) #since cmake 3.2 is available as a patch for yocto Fido
24
set(CMAKE_MACOSX_RPATH ON) # Before cmake 3.0.0, MACOSX_RPATH was not set to ON by default - however this is no good reason to not enable it by default
25

26 27 28
project(bctoolbox)

set(BCTOOLBOX_VERSION_MAJOR 0)
Ronan's avatar
Ronan committed
29 30
set(BCTOOLBOX_VERSION_MINOR 6)
set(BCTOOLBOX_VERSION_PATCH 0)
31
set(BCTOOLBOX_SO_VERSION 1)
32
set(BCTOOLBOXTESTER_SO_VERSION 1)
33

34 35 36 37 38 39
set(BCTOOLBOX_VERSION "${BCTOOLBOX_VERSION_MAJOR}.${BCTOOLBOX_VERSION_MINOR}.${BCTOOLBOX_VERSION_PATCH}")
set(PROJECT_VERSION_MAJOR ${BCTOOLBOX_VERSION_MAJOR})
set(PROJECT_VERSION_MINOR ${BCTOOLBOX_VERSION_MINOR})
set(PROJECT_VERSION_PATCH ${BCTOOLBOX_VERSION_PATCH})
set(PROJECT_VERSION "${BCTOOLBOX_VERSION}")

40

41 42
option(ENABLE_SHARED "Build shared library." ON)
option(ENABLE_STATIC "Build static library." ON)
jehan's avatar
jehan committed
43
option(ENABLE_POLARSSL "Enable polarssl support" ON)
jehan's avatar
jehan committed
44
option(ENABLE_MBEDTLS "Enable mabedtls support" ON)
45
option(ENABLE_DECAF "Enable Elliptic Curve Cryptography support" ON)
46
option(ENABLE_STRICT "Pass strict flags to the compiler" ON)
47 48
option(ENABLE_TESTS_COMPONENT "Enable compilation of tests helper library" ON)
option(ENABLE_TESTS "Enable compilation of tests" ON)
jehan's avatar
jehan committed
49

50 51
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS NO)
52

jehan's avatar
jehan committed
53 54 55 56
include(CheckLibraryExists)
include(CheckSymbolExists)
include(CheckCSourceCompiles)
include(CMakePushCheckState)
57
include(GNUInstallDirs)
58
include(cmake/BcToolboxCMakeUtils.cmake)
jehan's avatar
jehan committed
59 60 61

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

62
if(NOT CPACK_GENERATOR AND NOT CMAKE_INSTALL_RPATH AND CMAKE_INSTALL_PREFIX)
Ghislain MARY's avatar
Ghislain MARY committed
63
	set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_FULL_LIBDIR})
Ghislain MARY's avatar
Ghislain MARY committed
64
	message(STATUS "Setting install rpath to ${CMAKE_INSTALL_RPATH}")
65 66
endif()

67 68 69 70
if(APPLE)
	find_package(Iconv REQUIRED)
endif()

71
if(ENABLE_MBEDTLS)
72
	find_package(MbedTLS)
73
	if(MBEDTLS_FOUND)
Ghislain MARY's avatar
Ghislain MARY committed
74
		message(STATUS "Using mbedTLS")
johan's avatar
johan committed
75
		set(HAVE_MBEDTLS 1)
76
	endif()
77
endif()
78

79
if(ENABLE_DECAF)
80
	find_package(Decaf CONFIG)
81 82 83 84 85 86
	if(DECAF_FOUND)
		message(STATUS "Using Decaf")
		set(HAVE_DECAF 1)
	endif()
endif()

87 88 89
if(ENABLE_POLARSSL AND NOT MBEDTLS_FOUND)
	find_package(PolarSSL REQUIRED)
	if(POLARSSL_FOUND)
Ghislain MARY's avatar
Ghislain MARY committed
90
		message(STATUS "Using polarSSL")
jehan's avatar
jehan committed
91
		set(HAVE_POLARSSL 1)
92 93 94
		if(CTR_DRBG_FREE)
			set(HAVE_CTR_DRGB_FREE 1)
		endif()
95 96
	endif()
endif()
jehan's avatar
jehan committed
97

98
if(HAVE_SSL_GET_DTLS_SRTP_PROTECTION_PROFILE)
Ghislain MARY's avatar
Ghislain MARY committed
99
	message(STATUS "DTLS SRTP available")
johan's avatar
johan committed
100 101
	set(HAVE_DTLS_SRTP 1)
else()
Ghislain MARY's avatar
Ghislain MARY committed
102
	message(STATUS "DTLS SRTP not available")
johan's avatar
johan committed
103 104
endif()

105
if(MBEDTLS_FOUND)
johan's avatar
johan committed
106
	get_filename_component(mbedtls_library_path "${MBEDTLS_LIBRARIES}" PATH)
jehan's avatar
jehan committed
107 108
	string(REPLACE ";" " " MBEDTLS_LIBRARIES_STR "${MBEDTLS_LIBRARIES}")
	set(LIBS_PRIVATE "${LIBS_PRIVATE} ${MBEDTLS_LIBRARIES_STR}")
109 110
endif()
if(POLARSSL_FOUND)
johan's avatar
johan committed
111 112 113 114
	get_filename_component(polarssl_library_path "${POLARSSL_LIBRARIES}" PATH)
	set(LIBS_PRIVATE "${LIBS_PRIVATE} -L${polarssl_library_path} -lpolarssl")
endif()

115
if(ENABLE_TESTS_COMPONENT)
116 117
  find_package(BcUnit CONFIG REQUIRED)
  cmake_push_check_state(RESET)
118 119 120 121 122
	list(APPEND CMAKE_REQUIRED_INCLUDES ${BCUNIT_INCLUDE_DIRS})
	list(APPEND CMAKE_REQUIRED_LIBRARIES ${BCUNIT_LIBRARIES})
	check_symbol_exists("CU_get_suite" "BCUnit/BCUnit.h" HAVE_CU_GET_SUITE)
	check_symbol_exists("CU_curses_run_tests" "BCUnit/BCUnit.h" HAVE_CU_CURSES)
	check_symbol_exists("CU_set_trace_handler" "BCUnit/Util.h" HAVE_CU_SET_TRACE_HANDLER)
123
	cmake_pop_check_state()
124
	set(TESTER_REQUIRES_PRIVATE "bcunit")
125 126
endif()

jehan's avatar
jehan committed
127
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/bctoolbox.pc.in ${CMAKE_CURRENT_BINARY_DIR}/bctoolbox.pc)
128
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/bctoolbox.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
jehan's avatar
jehan committed
129

130
if(ENABLE_TESTS_COMPONENT)
131 132 133
	configure_file(${CMAKE_CURRENT_SOURCE_DIR}/bctoolbox-tester.pc.in ${CMAKE_CURRENT_BINARY_DIR}/bctoolbox-tester.pc)
	install(FILES ${CMAKE_CURRENT_BINARY_DIR}/bctoolbox-tester.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
endif()
jehan's avatar
jehan committed
134

135 136
find_package(Threads REQUIRED)

jehan's avatar
jehan committed
137 138 139
include_directories(
	include
	src
140
	${CMAKE_CURRENT_BINARY_DIR}
jehan's avatar
jehan committed
141
)
johan's avatar
johan committed
142

143 144 145 146 147 148
set(BCTOOLBOX_CPPFLAGS )
if(ENABLE_STATIC)
	set(BCTBX_STATIC 1)
	list(APPEND BCTOOLBOX_CPPFLAGS "-DBCTBX_STATIC")
endif()

149
bc_init_compilation_flags(STRICT_OPTIONS_CPP STRICT_OPTIONS_C STRICT_OPTIONS_CXX ENABLE_STRICT)
150
if(CMAKE_VERSION VERSION_LESS 3.1 AND NOT APPLE AND NOT MSVC)
151
	list(APPEND STRICT_OPTIONS_CXX "-std=c++11")
jehan's avatar
jehan committed
152
endif()
153

154
set(EXPORT_TARGETS_NAME "bctoolbox")
jehan's avatar
jehan committed
155

jehan's avatar
jehan committed
156
check_library_exists("rt" "clock_gettime" "" HAVE_LIBRT)
jehan's avatar
jehan committed
157
check_library_exists("dl" "dladdr" "" HAVE_LIBDL)
158
check_include_file("execinfo.h" HAVE_EXECINFO)
jehan's avatar
jehan committed
159

160 161 162 163
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/config.h PROPERTIES GENERATED ON)
add_definitions("-DHAVE_CONFIG_H")

jehan's avatar
jehan committed
164 165
add_subdirectory(include)
add_subdirectory(src)
166
if(ENABLE_TESTS AND ENABLE_TESTS_COMPONENT)
167 168
	add_subdirectory(tester)
endif()
jehan's avatar
jehan committed
169

170 171
add_subdirectory(build)

jehan's avatar
jehan committed
172
include(CMakePackageConfigHelpers)
173 174
set(CONFIG_PACKAGE_LOCATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake")

jehan's avatar
jehan committed
175
write_basic_package_version_file(
176
	"${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_TARGETS_NAME}ConfigVersion.cmake"
177
	VERSION ${PROJECT_VERSION}
jehan's avatar
jehan committed
178 179
	COMPATIBILITY AnyNewerVersion
)
180 181 182 183
configure_package_config_file(cmake/BcToolboxConfig.cmake.in
	"${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_TARGETS_NAME}Config.cmake"
	INSTALL_DESTINATION ${CONFIG_PACKAGE_LOCATION}
	NO_SET_AND_CHECK_MACRO
jehan's avatar
jehan committed
184
)
185 186

configure_file(cmake/BcToolboxCMakeUtils.cmake "${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_TARGETS_NAME}CMakeUtils.cmake" COPYONLY)
jehan's avatar
jehan committed
187 188
configure_file(cmake/BcGitVersion.cmake "${CMAKE_CURRENT_BINARY_DIR}/BcGitVersion.cmake" COPYONLY)
configure_file(cmake/gitversion.h.in "${CMAKE_CURRENT_BINARY_DIR}/gitversion.h.in" COPYONLY)
jehan's avatar
jehan committed
189 190

install(EXPORT ${EXPORT_TARGETS_NAME}Targets
191
	FILE "${EXPORT_TARGETS_NAME}Targets.cmake"
Ghislain MARY's avatar
Ghislain MARY committed
192
	DESTINATION ${CONFIG_PACKAGE_LOCATION}
jehan's avatar
jehan committed
193
)
194

jehan's avatar
jehan committed
195
install(FILES
196 197 198
	"${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_TARGETS_NAME}Config.cmake"
	"${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_TARGETS_NAME}ConfigVersion.cmake"
	"${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_TARGETS_NAME}CMakeUtils.cmake"
jehan's avatar
jehan committed
199 200
	"${CMAKE_CURRENT_BINARY_DIR}/BcGitVersion.cmake"
	"${CMAKE_CURRENT_BINARY_DIR}/gitversion.h.in"
Ghislain MARY's avatar
Ghislain MARY committed
201
	DESTINATION ${CONFIG_PACKAGE_LOCATION}
jehan's avatar
jehan committed
202
)