CMakeLists.txt 7.66 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 2.8.12)
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 29
project(bctoolbox)

set(BCTOOLBOX_VERSION_MAJOR 0)
30
set(BCTOOLBOX_VERSION_MINOR 4)
Sandrine Avakian's avatar
Sandrine Avakian committed
31
set(BCTOOLBOX_VERSION_PATCH 0)
32 33 34
set(BCTOOLBOX_SO_VERSION 0)
set(BCTOOLBOXTESTER_SO_VERSION 0)

35 36 37 38 39 40
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}")

Ghislain MARY's avatar
Ghislain MARY committed
41

42 43
option(ENABLE_SHARED "Build shared library." ON)
option(ENABLE_STATIC "Build static library." ON)
jehan's avatar
jehan committed
44
option(ENABLE_POLARSSL "Enable polarssl support" ON)
jehan's avatar
jehan committed
45
option(ENABLE_MBEDTLS "Enable mabedtls 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

Ghislain MARY's avatar
Ghislain MARY committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

macro(apply_compile_flags SOURCE_FILES)
	if(${SOURCE_FILES})
		set(options "")
		foreach(a ${ARGV})
			if(STRICT_OPTIONS_${a})
				string(REPLACE ";" " " options_${a} "${STRICT_OPTIONS_${a}}")
				set(options "${options} ${options_${a}}")
			endif()
		endforeach()
		if(options)
			set_source_files_properties(${${SOURCE_FILES}} PROPERTIES COMPILE_FLAGS "${options}")
		endif()
	endif()
endmacro()


jehan's avatar
jehan committed
67 68 69 70
include(CheckLibraryExists)
include(CheckSymbolExists)
include(CheckCSourceCompiles)
include(CMakePushCheckState)
71
include(GNUInstallDirs)
jehan's avatar
jehan committed
72

73 74
add_subdirectory(build)

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

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

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
	find_package(BCUnit 3.0 REQUIRED)
116
	cmake_push_check_state(RESET)
117 118 119 120 121
	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)
122
	cmake_pop_check_state()
123
	set(TESTER_REQUIRES_PRIVATE "bcunit")
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
124 125
endif()

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

129
if(ENABLE_TESTS_COMPONENT AND BCUNIT_FOUND)
130 131 132
	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
133

jehan's avatar
jehan committed
134 135
find_package(Threads REQUIRED)

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

142
if(MBEDTLS_FOUND)
johan's avatar
johan committed
143
	include_directories(${MBEDTLS_INCLUDE_DIRS})
144 145
endif()
if(POLARSSL_FOUND)
johan's avatar
johan committed
146 147 148
	include_directories(${POLARSSL_INCLUDE_DIRS})
endif()

johan's avatar
johan committed
149 150 151 152
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
153 154
set(STRICT_OPTIONS_CPP )
set(STRICT_OPTIONS_C )
155
set(STRICT_OPTIONS_CXX )
156 157 158 159 160
if(MSVC)
	if(ENABLE_STRICT)
		list(APPEND STRICT_OPTIONS_CPP "/WX")
	endif()
else()
jehan's avatar
jehan committed
161 162 163 164 165 166 167 168 169 170 171
	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")
172
		list(APPEND STRICT_OPTIONS_C "-Werror -Wdeclaration-after-statement" "-Wstrict-prototypes" "-Wno-error=strict-prototypes")
jehan's avatar
jehan committed
173 174 175 176
	endif()
endif()
if(STRICT_OPTIONS_CPP)
	list(REMOVE_DUPLICATES STRICT_OPTIONS_CPP)
177
	string(REPLACE ";" " " STRICT_OPTIONS_CPP "${STRICT_OPTIONS_CPP}")
jehan's avatar
jehan committed
178 179 180
endif()
if(STRICT_OPTIONS_C)
	list(REMOVE_DUPLICATES STRICT_OPTIONS_C)
181
	string(REPLACE ";" " " STRICT_OPTIONS_C "${STRICT_OPTIONS_C}")
jehan's avatar
jehan committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
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
199
check_library_exists("rt" "clock_gettime" "" HAVE_LIBRT)
jehan's avatar
jehan committed
200 201 202

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

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

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