From bcc1a6ecf1eadcf69dd9efa02fee69ee63949ca0 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Wed, 2 Feb 2022 09:52:15 +0100 Subject: [PATCH] cmake: ensure we globally match on _GNU_SOURCE --- CMake/_Include.cmake | 16 ++++++++++++++++ CMakeLists.txt | 2 ++ contrib/bin/memaslap/CMakeLists.txt | 1 - test/CMakeLists.txt | 2 -- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CMake/_Include.cmake b/CMake/_Include.cmake index 22f99a8c..901375d7 100644 --- a/CMake/_Include.cmake +++ b/CMake/_Include.cmake @@ -1,6 +1,11 @@ include(_Configure) configure_init(${CMAKE_BINARY_DIR}/mem_config.h) +add_compile_definitions(${GLOBAL_DEFINITIONS}) +# list(TRANSFORM) requires >=3.12 +string(REPLACE ";" " -D" GLOBAL_DEFINITION_FLAGS "${GLOBAL_DEFINITIONS}") +set(GLOBAL_DEFINITION_FLAGS -D${GLOBAL_DEFINITION_FLAGS}) + if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) include(CTest) endif() @@ -32,6 +37,7 @@ macro(check_symbol NAME HEADER) if(${ARGC} GREATER 1) string(APPEND CMAKE_REQUIRED_FLAGS " ${ARGV2}") endif() + list(APPEND CMAKE_REQUIRED_DEFINITIONS ${GLOBAL_DEFINITION_FLAGS}) check_symbol_exists(${NAME} ${HEADER} ${HAVE}) cmake_pop_check_state() endmacro() @@ -43,6 +49,7 @@ macro(check_cxx_symbol NAME HEADER) if(${ARGC} GREATER 1) string(APPEND CMAKE_REQUIRED_FLAGS " ${ARGN}") endif() + list(APPEND CMAKE_REQUIRED_DEFINITIONS ${GLOBAL_DEFINITION_FLAGS}) check_cxx_symbol_exists(${NAME} ${HEADER} ${HAVE}) cmake_pop_check_state() endmacro() @@ -54,6 +61,7 @@ macro(check_include HEADER) if(${ARGC} GREATER 1) string(APPEND CMAKE_REQUIRED_FLAGS " ${ARGN}") endif() + list(APPEND CMAKE_REQUIRED_DEFINITIONS ${GLOBAL_DEFINITION_FLAGS}) check_include_file(${HEADER} ${HAVE}) cmake_pop_check_state() endmacro() @@ -65,6 +73,7 @@ macro(check_cxx_include HEADER) if(${ARGC} GREATER 1) string(APPEND CMAKE_REQUIRED_FLAGS " ${ARGN}") endif() + list(APPEND CMAKE_REQUIRED_DEFINITIONS ${GLOBAL_DEFINITION_FLAGS}) check_include_file_cxx(${HEADER} ${HAVE}) cmake_pop_check_state() endmacro() @@ -76,18 +85,25 @@ macro(check_type TYPE) if(${ARGC} GREATER 1) list(APPEND CMAKE_EXTRA_INCLUDE_FILES ${ARGN}) endif() + list(APPEND CMAKE_REQUIRED_DEFINITIONS ${GLOBAL_DEFINITION_FLAGS}) check_type_size(${TYPE} ${HAVE}) cmake_pop_check_state() endmacro() include(CheckCSourceCompiles) macro(check_c_source SOURCE HAVE) configure_define(${HAVE}) + cmake_push_check_state() + list(APPEND CMAKE_REQUIRED_DEFINITIONS ${GLOBAL_DEFINITION_FLAGS}) check_c_source_compiles("${SOURCE}" ${HAVE}) + cmake_pop_check_state() endmacro() include(CheckCXXSourceCompiles) macro(check_cxx_source SOURCE HAVE) configure_define(${HAVE}) + cmake_push_check_state() + list(APPEND CMAKE_REQUIRED_DEFINITIONS ${GLOBAL_DEFINITION_FLAGS}) check_cxx_source_compiles("${SOURCE}" ${HAVE}) + cmake_pop_check_state() endmacro() include(CheckBacktrace) diff --git a/CMakeLists.txt b/CMakeLists.txt index 601f3802..1603c2c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,8 @@ set(PROJECT_CONTACT "Michael Wallner ") set(CXX_STANDARD 11) set(CMAKE_POSITION_INDEPENDENT_CODE ON) +set(GLOBAL_DEFINITIONS _GNU_SOURCE) + include(CMakeConfig.txt) include(GNUInstallDirs) list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake") diff --git a/contrib/bin/memaslap/CMakeLists.txt b/contrib/bin/memaslap/CMakeLists.txt index 3f7f927d..581c01bd 100644 --- a/contrib/bin/memaslap/CMakeLists.txt +++ b/contrib/bin/memaslap/CMakeLists.txt @@ -1,5 +1,4 @@ if(ENABLE_MEMASLAP AND CMAKE_USE_PTHREADS_INIT) - add_definitions(-D_GNU_SOURCE) include(CheckAtomics) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b9ce01ae..efc3e708 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,8 +12,6 @@ configure_define_string(LIBMEMCACHED_WITH_SASL_CONF) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake") include(Catch) -add_definitions(-D_GNU_SOURCE) -set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) check_symbol(pipe2 unistd.h) check_symbol(SOCK_NONBLOCK sys/socket.h) check_symbol(SOCK_CLOEXEC sys/socket.h) -- 2.30.2