CMakeLists.txt 8.03 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 19 20 21
############################################################################
# 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
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
############################################################################
22

23
cmake_minimum_required(VERSION 2.8.12)
24

25

26 27 28 29 30 31 32 33 34 35 36
project(bctoolbox)

set(BCTOOLBOX_VERSION_MAJOR 0)
set(BCTOOLBOX_VERSION_MINOR 0)
set(BCTOOLBOX_VERSION_PATCH 2)
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}")

37 38
set(BCTOOLBOX_SO_VERSION 0)
set(BCTOOLBOXTESTER_SO_VERSION 0)
jehan's avatar
jehan committed
39

40 41
option(ENABLE_SHARED "Build shared library." YES)
option(ENABLE_STATIC "Build static library." YES)
jehan's avatar
jehan committed
42
option(ENABLE_POLARSSL "Enable polarssl support" ON)
jehan's avatar
jehan committed
43
option(ENABLE_MBEDTLS "Enable mabedtls support" ON)
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
44
option(ENABLE_TESTS "Enable compilation of tests helper library" ON)
jehan's avatar
jehan committed
45 46 47 48 49

include(CheckLibraryExists)
include(CheckSymbolExists)
include(CheckCSourceCompiles)
include(CMakePushCheckState)
50
include(GNUInstallDirs)
jehan's avatar
jehan committed
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
add_subdirectory(build)

# Note for Gnu/Unix systems:
# The library install director should ideally be set, according to GNUInstallDirs,
# to ${CMAKE_INSTALL_LIBDIR} for libraries, which are OS specific.
# HOWEVER autotools does not care about these specificities and ALWAYS sets them
# to $(exec_prefix)/lib (cf https://www.gnu.org/prep/standards/html_node/Directory-Variables.html)
# When using a custom prefix, we should follow the autotools guidelines because any project using pkg-config to find our library
# will expect it to be in $(exec_prefix)/lib, and NOT in $(exec_prefix)/${CMAKE_INSTALL_LIBDIR} since
# pkg-config does not let us introduce an -rpath settings to tell it where our library is located.
# Conversely, when NOT using a custom prefix, we can properly install our library in ${CMAKE_INSTALL_LIBDIR}
# because systems add extra locations for pkg-config to search in.
if("${CPACK_GENERATOR}" STREQUAL "RPM" OR "${CPACK_GENERATOR}" STREQUAL "DEB")
	# do nothing, we are using the "officials" install prefix
else()
	# force them to the default autotools values
	message("Setting CMAKE_INSTALL_LIBDIR to ${CMAKE_INSTALL_PREFIX}/lib for non-package build")
	set(CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/lib)
endif()

jehan's avatar
jehan committed
72 73
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

Ghislain MARY's avatar
Ghislain MARY committed
74
if(NOT CMAKE_INSTALL_RPATH AND CMAKE_INSTALL_PREFIX)
75
	set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_LIBDIR})
Ghislain MARY's avatar
Ghislain MARY committed
76
	message(STATUS "Setting install rpath to ${CMAKE_INSTALL_RPATH}")
77
endif()
78
set(CMAKE_MACOSX_RPATH ON)
79

80
if(ENABLE_MBEDTLS)
81
	find_package(MbedTLS)
82
	if(MBEDTLS_FOUND)
Ghislain MARY's avatar
Ghislain MARY committed
83
		message(STATUS "Using mbedTLS")
84
	endif()
85 86 87 88
endif()
if(ENABLE_POLARSSL AND NOT MBEDTLS_FOUND)
	find_package(PolarSSL REQUIRED)
	if(POLARSSL_FOUND)
Ghislain MARY's avatar
Ghislain MARY committed
89
		message(STATUS "Using polarSSL")
90 91 92
		if(CTR_DRBG_FREE)
			set(HAVE_CTR_DRGB_FREE 1)
		endif()
93 94
	endif()
endif()
jehan's avatar
jehan committed
95

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

103
if(MBEDTLS_FOUND)
johan's avatar
johan committed
104 105
	get_filename_component(mbedtls_library_path "${MBEDTLS_LIBRARIES}" PATH)
	set(LIBS_PRIVATE "${LIBS_PRIVATE} -L${mbedlts_library_path}")
106 107
endif()
if(POLARSSL_FOUND)
johan's avatar
johan committed
108 109 110 111
	get_filename_component(polarssl_library_path "${POLARSSL_LIBRARIES}" PATH)
	set(LIBS_PRIVATE "${LIBS_PRIVATE} -L${polarssl_library_path} -lpolarssl")
endif()

112
if(ENABLE_TESTS)
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
113 114 115 116 117
	find_package(CUnit)
	if(CUNIT_FOUND)
		cmake_push_check_state(RESET)
		list(APPEND CMAKE_REQUIRED_INCLUDES ${CUNIT_INCLUDE_DIRS})
		list(APPEND CMAKE_REQUIRED_LIBRARIES ${CUNIT_LIBRARIES})
118 119
		check_symbol_exists("CU_get_suite" "CUnit/CUnit.h" HAVE_CU_GET_SUITE)
		check_symbol_exists("CU_curses_run_tests" "CUnit/CUnit.h" HAVE_CU_CURSES)
120
		check_symbol_exists("CU_set_trace_handler" "CUnit/Util.h" HAVE_CU_SET_TRACE_HANDLER)
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
121
		cmake_pop_check_state()
122

123
		set(TESTER_REQUIRES_PRIVATE "cunit")
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
124 125 126
	else()
		message(WARNING "Could not find the cunit library!")
	endif()
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
127 128
endif()

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

132
if(ENABLE_TESTS AND CUNIT_FOUND)
133 134 135
	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
136

jehan's avatar
jehan committed
137 138
find_package(Threads REQUIRED)

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

145
if(MBEDTLS_FOUND)
johan's avatar
johan committed
146
	include_directories(${MBEDTLS_INCLUDE_DIRS})
147 148
endif()
if(POLARSSL_FOUND)
johan's avatar
johan committed
149 150 151
	include_directories(${POLARSSL_INCLUDE_DIRS})
endif()

johan's avatar
johan committed
152 153 154 155
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
156 157 158
set(STRICT_OPTIONS_CPP )
set(STRICT_OPTIONS_C )
if(NOT MSVC)
159

jehan's avatar
jehan committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	list(APPEND STRICT_OPTIONS_CPP "-Wall" "-Wuninitialized")
	if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
		list(APPEND STRICT_OPTIONS_CPP "-Wno-error=unknown-warning-option" "-Qunused-arguments" "-Wno-tautological-compare" "-Wno-builtin-requires-header" "-Wno-unused-function" "-Wno-gnu-designator" "-Wno-array-bounds")
	elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
		list(APPEND STRICT_OPTIONS_CPP "-Wno-error=pragmas")
	endif()
	if(APPLE)
		list(APPEND STRICT_OPTIONS_CPP "-Wno-error=unknown-warning-option" "-Qunused-arguments" "-Wno-tautological-compare" "-Wno-unused-function" "-Wno-array-bounds")
	endif()
	if(ENABLE_STRICT)
		list(APPEND STRICT_OPTIONS_CPP "-Werror" "-Wno-error=unknown-pragmas" "-Wuninitialized" "-fno-strict-aliasing")
		list(APPEND STRICT_OPTIONS_C "-Wdeclaration-after-statement" "-Wstrict-prototypes" "-Wno-error=strict-prototypes")
	endif()
endif()
if(STRICT_OPTIONS_CPP)
	list(REMOVE_DUPLICATES STRICT_OPTIONS_CPP)
endif()
if(STRICT_OPTIONS_C)
	list(REMOVE_DUPLICATES STRICT_OPTIONS_C)
endif()
set(LINK_FLAGS )
if(APPLE)
	list(APPEND LINK_FLAGS "-framework Foundation")
	if(IOS)
		list(APPEND LINK_FLAGS "-framework CoreFoundation" "-framework CFNetwork" "-framework UIKit")
	endif()
endif()
string(REPLACE ";" " " LINK_FLAGS_STR "${LINK_FLAGS}")


if(LINPHONE_BUILDER_GROUP_EXTERNAL_SOURCE_PATH_BUILDERS)
	set(EXPORT_TARGETS_NAME "LinphoneBuilder")
else()
	set(EXPORT_TARGETS_NAME "BcToolbox")
endif()

add_compile_options(${STRICT_OPTIONS_CPP} ${STRICT_OPTIONS_C})
jehan's avatar
jehan committed
197
check_library_exists("rt" "clock_gettime" "" HAVE_LIBRT)
jehan's avatar
jehan committed
198 199 200

add_subdirectory(include)
add_subdirectory(src)
201 202 203
if(ENABLE_TESTS AND CUNIT_FOUND)
	add_subdirectory(tester)
endif()
jehan's avatar
jehan committed
204 205 206 207

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
	"${CMAKE_CURRENT_BINARY_DIR}/BcToolboxConfigVersion.cmake"
208
	VERSION ${PROJECT_VERSION}
jehan's avatar
jehan committed
209 210 211 212 213 214 215
	COMPATIBILITY AnyNewerVersion
)
configure_file(cmake/BcToolboxConfig.cmake.in
	"${CMAKE_CURRENT_BINARY_DIR}/BcToolboxConfig.cmake"
	@ONLY
)

Ghislain MARY's avatar
Ghislain MARY committed
216
set(CONFIG_PACKAGE_LOCATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/cmake")
jehan's avatar
jehan committed
217 218
install(EXPORT ${EXPORT_TARGETS_NAME}Targets
	FILE BcToolboxTargets.cmake
Ghislain MARY's avatar
Ghislain MARY committed
219
	DESTINATION ${CONFIG_PACKAGE_LOCATION}
jehan's avatar
jehan committed
220 221 222 223
)
install(FILES
	"${CMAKE_CURRENT_BINARY_DIR}/BcToolboxConfig.cmake"
	"${CMAKE_CURRENT_BINARY_DIR}/BcToolboxConfigVersion.cmake"
Ghislain MARY's avatar
Ghislain MARY committed
224
	DESTINATION ${CONFIG_PACKAGE_LOCATION}
jehan's avatar
jehan committed
225
)