CMakeLists.txt 8.27 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
project(bctoolbox)

set(BCTOOLBOX_VERSION_MAJOR 0)
set(BCTOOLBOX_VERSION_MINOR 0)
30 31 32 33
set(BCTOOLBOX_VERSION_PATCH 3)
set(BCTOOLBOX_SO_VERSION 0)
set(BCTOOLBOXTESTER_SO_VERSION 0)

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
option(ENABLE_SHARED "Build shared library." ON)
option(ENABLE_STATIC "Build static library." ON)
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)
44
option(ENABLE_STRICT "Pass strict flags to the compiler" ON)
45 46
option(ENABLE_TESTS_COMPONENT "Enable compilation of tests helper library" ON)
option(ENABLE_TESTS "Enable compilation of tests" ON)
jehan's avatar
jehan committed
47 48 49 50 51

include(CheckLibraryExists)
include(CheckSymbolExists)
include(CheckCSourceCompiles)
include(CMakePushCheckState)
52
include(GNUInstallDirs)
jehan's avatar
jehan committed
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
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
74 75
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

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

82
if(ENABLE_MBEDTLS)
83
	find_package(MbedTLS)
84
	if(MBEDTLS_FOUND)
Ghislain MARY's avatar
Ghislain MARY committed
85
		message(STATUS "Using mbedTLS")
86
	endif()
87 88 89 90
endif()
if(ENABLE_POLARSSL AND NOT MBEDTLS_FOUND)
	find_package(PolarSSL REQUIRED)
	if(POLARSSL_FOUND)
Ghislain MARY's avatar
Ghislain MARY committed
91
		message(STATUS "Using polarSSL")
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 107
	get_filename_component(mbedtls_library_path "${MBEDTLS_LIBRARIES}" PATH)
	set(LIBS_PRIVATE "${LIBS_PRIVATE} -L${mbedlts_library_path}")
108 109
endif()
if(POLARSSL_FOUND)
johan's avatar
johan committed
110 111 112 113
	get_filename_component(polarssl_library_path "${POLARSSL_LIBRARIES}" PATH)
	set(LIBS_PRIVATE "${LIBS_PRIVATE} -L${polarssl_library_path} -lpolarssl")
endif()

114
if(ENABLE_TESTS_COMPONENT)
115 116 117 118 119
	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})
120 121
		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)
122
		check_symbol_exists("CU_set_trace_handler" "CUnit/Util.h" HAVE_CU_SET_TRACE_HANDLER)
123
		cmake_pop_check_state()
124

125
		set(TESTER_REQUIRES_PRIVATE "cunit")
126 127 128
	else()
		message(WARNING "Could not find the cunit library!")
	endif()
129 130
endif()

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

134
if(ENABLE_TESTS_COMPONENT AND CUNIT_FOUND)
135 136 137
	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
138

139 140
find_package(Threads REQUIRED)

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

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

154 155 156 157
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
158 159 160
set(STRICT_OPTIONS_CPP )
set(STRICT_OPTIONS_C )
if(NOT MSVC)
161

jehan's avatar
jehan committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	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)
178
	string(REPLACE ";" " " STRICT_OPTIONS_CPP "${STRICT_OPTIONS_CPP}")
jehan's avatar
jehan committed
179 180 181
endif()
if(STRICT_OPTIONS_C)
	list(REMOVE_DUPLICATES STRICT_OPTIONS_C)
182
	string(REPLACE ";" " " STRICT_OPTIONS_C "${STRICT_OPTIONS_C}")
jehan's avatar
jehan committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
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()

jehan's avatar
jehan committed
200
check_library_exists("rt" "clock_gettime" "" HAVE_LIBRT)
jehan's avatar
jehan committed
201 202 203

add_subdirectory(include)
add_subdirectory(src)
204
if(ENABLE_TESTS AND ENABLE_TESTS_COMPONENT AND CUNIT_FOUND)
205 206
	add_subdirectory(tester)
endif()
jehan's avatar
jehan committed
207 208 209 210

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

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