| 1 |
# Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors. |
| 2 |
# SPDX-License-Identifier: mit |
| 3 |
|
| 4 |
cmake_minimum_required(VERSION 3.22...3.26) |
| 5 |
|
| 6 |
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake) |
| 7 |
|
| 8 |
# Follow all steps below in order to calculate new ABI version when updating the library |
| 9 |
# NOTE: THIS IS UNRELATED to the actual project version |
| 10 |
# |
| 11 |
# 1. If the library source code has changed at all since the last update, then increment revision |
| 12 |
# 2. If any interfaces have been added, removed, or changed since the last update, increment current and set revision to 0. |
| 13 |
# 3. If any interfaces have been added since the last public release, then increment age. |
| 14 |
# 4. If any interfaces have been removed since the last public release, then set age to 0. |
| 15 |
|
| 16 |
set(RMQ_SOVERSION_CURRENT 11) |
| 17 |
set(RMQ_SOVERSION_REVISION 2) |
| 18 |
set(RMQ_SOVERSION_AGE 7) |
| 19 |
|
| 20 |
include(VersionFunctions) |
| 21 |
get_library_version(RMQ_VERSION) |
| 22 |
compute_soversion(${RMQ_SOVERSION_CURRENT} ${RMQ_SOVERSION_REVISION} ${RMQ_SOVERSION_AGE} RMQ_SOVERSION) |
| 23 |
|
| 24 |
project(rabbitmq-c |
| 25 |
VERSION ${RMQ_VERSION} |
| 26 |
DESCRIPTION "C RabbitMQ AMQP client library" |
| 27 |
LANGUAGES C) |
| 28 |
|
| 29 |
set(CMAKE_C_STANDARD 99) |
| 30 |
set(CMAKE_C_STANDARD_REQUIRED ON) |
| 31 |
set(CMAKE_C_EXTENSIONS ON) |
| 32 |
|
| 33 |
set(CMAKE_C_VISIBILITY_PRESET hidden) |
| 34 |
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON) |
| 35 |
|
| 36 |
set(default_build_type "Release") |
| 37 |
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) |
| 38 |
message(STATUS "Setting build type to '${default_build_type}' as none was specified.") |
| 39 |
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE STRING "Choose the type of build." FORCE) |
| 40 |
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo") |
| 41 |
endif() |
| 42 |
|
| 43 |
include(CheckSymbolExists) |
| 44 |
include(CheckLibraryExists) |
| 45 |
include(CMakeDependentOption) |
| 46 |
include(CMakePushCheckState) |
| 47 |
include(GNUInstallDirs) |
| 48 |
|
| 49 |
# Detect if we need to link against a socket library: |
| 50 |
cmake_push_check_state() |
| 51 |
if (WIN32) |
| 52 |
# Always use WinSock2 on Windows |
| 53 |
set(SOCKET_LIBRARIES ws2_32) |
| 54 |
else () |
| 55 |
# Is it in the default link? |
| 56 |
check_symbol_exists(getaddrinfo "sys/types.h;sys/socket.h;netdb.h" HAVE_GETADDRINFO) |
| 57 |
if (NOT (HAVE_GETADDRINFO EQUAL 1)) |
| 58 |
SET(CMAKE_REQUIRED_LIBRARIES "socket") |
| 59 |
check_symbol_exists(getaddrinfo "sys/types.h;sys/socket.h;netdb.h" HAVE_GETADDRINFO2) |
| 60 |
if (HAVE_GETADDRINFO2 EQUAL 1) |
| 61 |
set(SOCKET_LIBRARIES socket) |
| 62 |
else () |
| 63 |
SET(CMAKE_REQUIRED_LIBRARIES "socket;nsl") |
| 64 |
check_symbol_exists(getaddrinfo "sys/types.h;sys/socket.h;netdb.h" HAVE_GETADDRINFO3) |
| 65 |
if (HAVE_GETADDRINFO3 EQUAL 1) |
| 66 |
set(SOCKET_LIBRARIES socket nsl) |
| 67 |
else () |
| 68 |
message(FATAL_ERROR "Cannot find name resolution library (containing symbol getaddrinfo)") |
| 69 |
endif () |
| 70 |
endif () |
| 71 |
endif () |
| 72 |
|
| 73 |
set(CMAKE_REQUIRED_LIBRARIES ${SOCKET_LIBRARIES}) |
| 74 |
check_symbol_exists(socket "sys/types.h;sys/socket.h" HAVE_SOCKET) |
| 75 |
if (NOT HAVE_SOCKET EQUAL 1) |
| 76 |
set(CMAKE_REQUIRED_LIBRARIES socket ${SOCKET_LIBRARIES}) |
| 77 |
check_symbol_exists(socket "sys/types.h;sys/socket.h" HAVE_SOCKET2) |
| 78 |
if (HAVE_SOCKET2 EQUAL 1) |
| 79 |
set(SOCKET_LIBRARIES socket ${SOCKET_LIBRARIES}) |
| 80 |
else () |
| 81 |
set(CMAKE_REQUIRED_LIBRARIES socket nsl ${SOCKET_LIBRARIES}) |
| 82 |
check_symbol_exists(socket "sys/types.h;sys/socket.h" HAVE_SOCKET3) |
| 83 |
if (HAVE_SOCKET3 EQUAL 1) |
| 84 |
set(SOCKET_LIBRARIES socket nsl ${SOCKET_LIBRARIES}) |
| 85 |
else () |
| 86 |
message(FATAL_ERROR "Cannot find socket library (containing symbol socket)") |
| 87 |
endif () |
| 88 |
endif () |
| 89 |
endif () |
| 90 |
endif () |
| 91 |
cmake_pop_check_state() |
| 92 |
|
| 93 |
cmake_push_check_state() |
| 94 |
set(CMAKE_REQUIRED_LIBRARIES ${SOCKET_LIBRARIES}) |
| 95 |
check_symbol_exists(poll poll.h HAVE_POLL) |
| 96 |
if (NOT HAVE_POLL) |
| 97 |
if (WIN32) |
| 98 |
set(HAVE_SELECT 1) |
| 99 |
else() |
| 100 |
check_symbol_exists(select sys/select.h HAVE_SELECT) |
| 101 |
endif() |
| 102 |
if (NOT HAVE_SELECT) |
| 103 |
message(FATAL_ERROR "rabbitmq-c requires poll() or select() to be available") |
| 104 |
endif() |
| 105 |
endif() |
| 106 |
cmake_pop_check_state() |
| 107 |
|
| 108 |
check_library_exists(rt clock_gettime "time.h" CLOCK_GETTIME_NEEDS_LIBRT) |
| 109 |
check_library_exists(rt posix_spawnp "spawn.h" POSIX_SPAWNP_NEEDS_LIBRT) |
| 110 |
if (CLOCK_GETTIME_NEEDS_LIBRT OR POSIX_SPAWNP_NEEDS_LIBRT) |
| 111 |
set(LIBRT rt) |
| 112 |
endif() |
| 113 |
|
| 114 |
option(ENABLE_SSL_SUPPORT "Enable SSL support" ON) |
| 115 |
|
| 116 |
if (ENABLE_SSL_SUPPORT) |
| 117 |
# Manually check OpenSSL version because BoringSSL doesn't support version checking via find_package |
| 118 |
set(RMQ_OPENSSL_MIN_VERSION 1.1.1) |
| 119 |
find_package(OpenSSL REQUIRED) |
| 120 |
if(OPENSSL_VERSION) # Will be empty for BoringSSL |
| 121 |
if(OPENSSL_VERSION VERSION_LESS RMQ_OPENSSL_MIN_VERSION) |
| 122 |
MESSAGE(FATAL_ERROR "Found OpenSSL version ${OPENSSL_VERSION} but ${RMQ_OPENSSL_MIN_VERSION} or later is required") |
| 123 |
endif() |
| 124 |
endif() |
| 125 |
|
| 126 |
cmake_push_check_state() |
| 127 |
set(THREADS_PREFER_PTHREAD_FLAG ON) |
| 128 |
find_package(Threads REQUIRED) |
| 129 |
cmake_pop_check_state() |
| 130 |
|
| 131 |
cmake_push_check_state() |
| 132 |
set(CMAKE_REQUIRED_LIBRARIES OpenSSL::SSL) |
| 133 |
check_symbol_exists(ENGINE_new openssl/engine.h HAS_OPENSSL_ENGINE) |
| 134 |
cmake_pop_check_state() |
| 135 |
|
| 136 |
cmake_dependent_option(ENABLE_SSL_ENGINE_API "Enable support for deprecated OpenSSL ENGINE feature" ON "HAS_OPENSSL_ENGINE" OFF) |
| 137 |
endif() |
| 138 |
|
| 139 |
if(PROJECT_IS_TOP_LEVEL) |
| 140 |
include(CTest) |
| 141 |
endif() |
| 142 |
|
| 143 |
option(BUILD_SHARED_LIBS "Build rabbitmq-c as a shared library" ON) |
| 144 |
option(BUILD_STATIC_LIBS "Build rabbitmq-c as a static library" ON) |
| 145 |
option(INSTALL_STATIC_LIBS "Install rabbitmq-c static library" ON) |
| 146 |
|
| 147 |
option(BUILD_EXAMPLES "Build Examples" OFF) |
| 148 |
option(BUILD_TOOLS "Build Tools (requires POPT Library)" OFF) |
| 149 |
cmake_dependent_option(BUILD_TOOLS_DOCS "Build man pages for tools (requires xmlto)" OFF "BUILD_TOOLS" OFF) |
| 150 |
option(BUILD_API_DOCS "Build Doxygen API docs" OFF) |
| 151 |
option(RUN_SYSTEM_TESTS "Run system tests (i.e. tests requiring an accessible RabbitMQ server instance on localhost)" OFF) |
| 152 |
option(BUILD_OSSFUZZ "Build OSSFUZZ" OFF) |
| 153 |
|
| 154 |
if (NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS) |
| 155 |
message(FATAL_ERROR "One or both of BUILD_SHARED_LIBS or BUILD_STATIC_LIBS must be set to ON to build") |
| 156 |
endif() |
| 157 |
|
| 158 |
set(targets_export_name rabbitmq-targets) |
| 159 |
|
| 160 |
add_subdirectory(librabbitmq) |
| 161 |
|
| 162 |
if(BUILD_EXAMPLES) |
| 163 |
if(NOT BUILD_SHARED_LIBS) |
| 164 |
message(FATAL_ERROR "Examples require -DBUILD_SHARED_LIBS=ON") |
| 165 |
endif() |
| 166 |
add_subdirectory(examples) |
| 167 |
endif() |
| 168 |
|
| 169 |
if(BUILD_TOOLS) |
| 170 |
find_package(POPT 1.14 REQUIRED) |
| 171 |
if(BUILD_TOOLS_DOCS) |
| 172 |
find_package(XMLTO REQUIRED) |
| 173 |
endif() |
| 174 |
add_subdirectory(tools) |
| 175 |
endif() |
| 176 |
|
| 177 |
if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING) |
| 178 |
if (NOT BUILD_STATIC_LIBS) |
| 179 |
message(FATAL_ERROR |
| 180 |
"Tests can only be built against static libraries " |
| 181 |
"(set BUILD_STATIC_LIBS=ON)") |
| 182 |
endif () |
| 183 |
add_subdirectory(tests) |
| 184 |
endif () |
| 185 |
|
| 186 |
if(BUILD_OSSFUZZ) |
| 187 |
if (NOT BUILD_STATIC_LIBS) |
| 188 |
message(FATAL_ERROR "OSS-FUZZ can only be built against static libraries " "(set BUILD_STATIC_LIBS=ON)") |
| 189 |
endif () |
| 190 |
add_subdirectory(fuzz) |
| 191 |
endif () |
| 192 |
|
| 193 |
if (BUILD_API_DOCS) |
| 194 |
find_package(Doxygen REQUIRED) |
| 195 |
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/docs/Doxyfile @ONLY) |
| 196 |
|
| 197 |
add_custom_target(docs |
| 198 |
COMMAND ${DOXYGEN_EXECUTABLE} |
| 199 |
VERBATIM |
| 200 |
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/docs |
| 201 |
DEPENDS rabbitmq |
| 202 |
COMMENT "Generating API documentation" |
| 203 |
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in |
| 204 |
) |
| 205 |
endif () |
| 206 |
|
| 207 |
foreach (lib ${SOCKET_LIBRARIES}) |
| 208 |
set(libs_private "${libs_private} -l${lib}") |
| 209 |
endforeach(lib) |
| 210 |
set(libs_private "${libs_private} -l${LIBRT}") |
| 211 |
if (ENABLE_SSL_SUPPORT) |
| 212 |
set(libs_private "${libs_private} -lssl -lcrypto ${CMAKE_THREAD_LIBS_INIT}") |
| 213 |
endif() |
| 214 |
|
| 215 |
set(prefix ${CMAKE_INSTALL_PREFIX}) |
| 216 |
set(exec_prefix "\${prefix}") |
| 217 |
cmake_path(APPEND libdir "\${exec_prefix}" "${CMAKE_INSTALL_LIBDIR}") |
| 218 |
cmake_path(APPEND includedir "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}") |
| 219 |
|
| 220 |
configure_file(cmake/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/librabbitmq/config.h) |
| 221 |
configure_file(librabbitmq.pc.in ${CMAKE_CURRENT_BINARY_DIR}/librabbitmq.pc @ONLY) |
| 222 |
|
| 223 |
include(CMakePackageConfigHelpers) |
| 224 |
set(RMQ_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/rabbitmq-c) |
| 225 |
set(project_config "${CMAKE_CURRENT_BINARY_DIR}/rabbitmq-c-config.cmake") |
| 226 |
set(version_config "${CMAKE_CURRENT_BINARY_DIR}/rabbitmq-c-config-version.cmake") |
| 227 |
|
| 228 |
write_basic_package_version_file( |
| 229 |
"${version_config}" |
| 230 |
VERSION ${RMQ_VERSION} |
| 231 |
COMPATIBILITY SameMajorVersion) |
| 232 |
|
| 233 |
configure_package_config_file( |
| 234 |
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/rabbitmq-c-config.cmake.in" |
| 235 |
"${project_config}" |
| 236 |
INSTALL_DESTINATION "${RMQ_CMAKE_DIR}") |
| 237 |
|
| 238 |
|
| 239 |
if(BUILD_SHARED_LIBS) |
| 240 |
list(APPEND INSTALL_TARGETS rabbitmq) |
| 241 |
endif() |
| 242 |
if(BUILD_STATIC_LIBS AND INSTALL_STATIC_LIBS) |
| 243 |
list(APPEND INSTALL_TARGETS rabbitmq-static) |
| 244 |
endif() |
| 245 |
|
| 246 |
export(TARGETS ${INSTALL_TARGETS} |
| 247 |
NAMESPACE rabbitmq:: |
| 248 |
FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake) |
| 249 |
|
| 250 |
install(FILES ${project_config} ${version_config} |
| 251 |
DESTINATION ${RMQ_CMAKE_DIR} |
| 252 |
COMPONENT rabbitmq-c-development |
| 253 |
) |
| 254 |
|
| 255 |
install(EXPORT ${targets_export_name} |
| 256 |
DESTINATION ${RMQ_CMAKE_DIR} |
| 257 |
NAMESPACE rabbitmq:: |
| 258 |
COMPONENT rabbitmq-c-development |
| 259 |
) |
| 260 |
|
| 261 |
install(FILES |
| 262 |
${CMAKE_CURRENT_BINARY_DIR}/librabbitmq.pc |
| 263 |
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig |
| 264 |
) |
| 265 |
|
| 266 |
if (BUILD_SHARED_LIBS) |
| 267 |
message(STATUS "Building rabbitmq as a shared library - yes") |
| 268 |
else () |
| 269 |
message(STATUS "Building rabbitmq as a shared library - no") |
| 270 |
endif () |
| 271 |
|
| 272 |
if (BUILD_STATIC_LIBS) |
| 273 |
message(STATUS "Building rabbitmq as a static library - yes") |
| 274 |
else () |
| 275 |
message(STATUS "Building rabbitmq as a static library - no") |
| 276 |
endif () |