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

set(BCTOOLBOX_VERSION_MAJOR 0)
set(BCTOOLBOX_VERSION_MINOR 0)
31 32 33 34
set(BCTOOLBOX_VERSION_PATCH 3)
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}")

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_STRICT "Pass strict flags to the compiler" ON)
46 47
option(ENABLE_TESTS_COMPONENT "Enable compilation of tests helper library" ON)
option(ENABLE_TESTS "Enable compilation of tests" ON)
jehan's avatar
jehan committed
48 49 50 51 52

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

55 56
add_subdirectory(build)

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

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

64
if(ENABLE_MBEDTLS)
65
	find_package(MbedTLS)
66
	if(MBEDTLS_FOUND)
Ghislain MARY's avatar
Ghislain MARY committed
67
		message(STATUS "Using mbedTLS")
68
	endif()
69 70 71 72
endif()
if(ENABLE_POLARSSL AND NOT MBEDTLS_FOUND)
	find_package(PolarSSL REQUIRED)
	if(POLARSSL_FOUND)
Ghislain MARY's avatar
Ghislain MARY committed
73
		message(STATUS "Using polarSSL")
74 75 76
		if(CTR_DRBG_FREE)
			set(HAVE_CTR_DRGB_FREE 1)
		endif()
77 78
	endif()
endif()
jehan's avatar
jehan committed
79

80
if(HAVE_SSL_GET_DTLS_SRTP_PROTECTION_PROFILE)
Ghislain MARY's avatar
Ghislain MARY committed
81
	message(STATUS "DTLS SRTP available")
johan's avatar
johan committed
82 83
	set(HAVE_DTLS_SRTP 1)
else()
Ghislain MARY's avatar
Ghislain MARY committed
84
	message(STATUS "DTLS SRTP not available")
johan's avatar
johan committed
85 86
endif()

87
if(MBEDTLS_FOUND)
johan's avatar
johan committed
88 89
	get_filename_component(mbedtls_library_path "${MBEDTLS_LIBRARIES}" PATH)
	set(LIBS_PRIVATE "${LIBS_PRIVATE} -L${mbedlts_library_path}")
90 91
endif()
if(POLARSSL_FOUND)
johan's avatar
johan committed
92 93 94 95
	get_filename_component(polarssl_library_path "${POLARSSL_LIBRARIES}" PATH)
	set(LIBS_PRIVATE "${LIBS_PRIVATE} -L${polarssl_library_path} -lpolarssl")
endif()

96
if(ENABLE_TESTS_COMPONENT)
97
	find_package(CUnit 3.0.1) # we need cunit 3.0.1 for automated junit output
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
98
	if(CUNIT_FOUND)
99
		set(HAVE_CU_FIXED_JUNIT 1)
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
100 101 102
		cmake_push_check_state(RESET)
		list(APPEND CMAKE_REQUIRED_INCLUDES ${CUNIT_INCLUDE_DIRS})
		list(APPEND CMAKE_REQUIRED_LIBRARIES ${CUNIT_LIBRARIES})
103 104
		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)
105
		check_symbol_exists("CU_set_trace_handler" "CUnit/Util.h" HAVE_CU_SET_TRACE_HANDLER)
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
106
		cmake_pop_check_state()
107

108
		set(TESTER_REQUIRES_PRIVATE "cunit")
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
109
	else()
110 111 112 113 114 115 116 117 118
		# if we found Cunit but with improper version, do not enable HAVE_CU_FIXED_JUNIT and search again for CUnit
		if (CUNIT_LIBRARIES)
			find_package(CUnit)
		endif()

		if (NOT CUNIT_FOUND)
			message(WARNING "Could not find the Cunit library, disable tests.")
			set(ENABLE_TESTS_COMPONENT OFF CACHE BOOL "Enable compilation of tests helper library" FORCE)
		endif()
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
119
	endif()
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
120 121
endif()

jehan's avatar
jehan committed
122
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/bctoolbox.pc.in ${CMAKE_CURRENT_BINARY_DIR}/bctoolbox.pc)
123
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/bctoolbox.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
jehan's avatar
jehan committed
124

125
if(ENABLE_TESTS_COMPONENT AND CUNIT_FOUND)
126 127 128
	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
129

jehan's avatar
jehan committed
130 131
find_package(Threads REQUIRED)

jehan's avatar
jehan committed
132 133 134
include_directories(
	include
	src
johan's avatar
johan committed
135
	${CMAKE_CURRENT_BINARY_DIR}
jehan's avatar
jehan committed
136
)
johan's avatar
johan committed
137

138
if(MBEDTLS_FOUND)
johan's avatar
johan committed
139
	include_directories(${MBEDTLS_INCLUDE_DIRS})
140 141
endif()
if(POLARSSL_FOUND)
johan's avatar
johan committed
142 143 144
	include_directories(${POLARSSL_INCLUDE_DIRS})
endif()

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

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

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

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