mosquitto/libcommon/CMakeLists.txt
Yann E. MORIN a487cd4f8f libcommon: check for getrandom()
Not all C libraries are glibc, or impersonating it; for example, musl
does not pretend to be any version of glibc. Thus, building on musl
fails when openssl is disabled, because no random-providing function is
detected, although musl does provide getrandom().

uClibc-ng on the other hand, can impersonate glibc, but availability of
getrandom() is not guatranteed there: getrandom() can be compiled out of
uClinbc-ng, or uClibc-ng can be too old to have it.

Add a configure-time check that getrandom() is available, as a fallback
when TLS is not enabled (and thus openssl is not used), and when not on
Win32 (where getting random numbers is always possible, at least from a
build perspective).

However, building with the plain Makefiles should keep working, so
slightly rework the defines checks in the code to account for the fact
that HAVE_GETRANDOM may already be defined at configure time.

Signed-off-by: Yann E. MORIN <yann.morin@orange.com>
2026-02-25 08:27:42 +00:00

94 lines
2.4 KiB
CMake

set(C_SRC
base64_common.c
cjson_common.c
file_common.c
memory_common.c
mqtt_common.c
password_common.c
property_common.c
random_common.c
strings_common.c
time_common.c
topic_common.c
utf8_common.c
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_base64.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_cjson.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_file.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_memory.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_properties.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_random.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_string.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_time.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_topic.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_utf8.h"
)
if(WIN32)
add_library(libmosquitto_common SHARED
${C_SRC}
)
else()
add_library(libmosquitto_common OBJECT
${C_SRC}
)
endif()
target_include_directories(libmosquitto_common
PUBLIC
"${CJSON_INCLUDE_DIRS}"
)
target_link_libraries(libmosquitto_common
PUBLIC
common-options
cJSON
)
if(ARGON2_FOUND)
target_link_libraries(libmosquitto_common PRIVATE argon2)
endif()
if (WITH_TLS)
target_link_libraries(libmosquitto_common
PUBLIC
OpenSSL::SSL
)
elseif(NOT WIN32)
include(CheckSymbolExists)
check_symbol_exists(getrandom "sys/random.h" GETRANDOM_FOUND)
if(GETRANDOM_FOUND)
add_definitions("-DHAVE_GETRANDOM")
else()
message(FATAL_ERROR "C library does not provide getrandom(); enable WITH_TLS instead")
endif()
endif()
if(INC_MEMTRACK)
target_compile_definitions(libmosquitto_common PUBLIC "WITH_MEMORY_TRACKING")
endif()
option(ALLOC_MISMATCH_INVALID_READ "Memory function mismatch detection." OFF)
if(ALLOC_MISMATCH_INVALID_READ)
target_compile_definitions(libmosquitto_common PRIVATE "ALLOC_MISMATCH_INVALID_READ")
endif()
option(ALLOC_MISMATCH_ABORT "Memory function mismatch abort." OFF)
if(ALLOC_MISMATCH_ABORT)
target_compile_definitions(libmosquitto_common PRIVATE "ALLOC_MISMATCH_ABORT")
endif()
set_target_properties(libmosquitto_common PROPERTIES
OUTPUT_NAME mosquitto_common
VERSION ${VERSION}
SOVERSION 1
POSITION_INDEPENDENT_CODE 1
)
install(TARGETS libmosquitto_common
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)