cmake: ensure we globally match on _GNU_SOURCE
authorMichael Wallner <mike@php.net>
Wed, 2 Feb 2022 08:52:15 +0000 (09:52 +0100)
committerMichael Wallner <mike@php.net>
Wed, 2 Feb 2022 09:14:46 +0000 (10:14 +0100)
CMake/_Include.cmake
CMakeLists.txt
contrib/bin/memaslap/CMakeLists.txt
test/CMakeLists.txt

index 22f99a8c193e1d73d7aeb6872de1f30b3555c607..901375d70e7fddd431aa0ccadec5d82c1405bfa6 100644 (file)
@@ -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)
index 601f3802ea4d400b4a3e3a196cad039da00cc633..1603c2c40793342462a5183219d9af5bff306320 100644 (file)
@@ -16,6 +16,8 @@ set(PROJECT_CONTACT "Michael Wallner <mike@php.net>")
 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")
index 3f7f927d50b7ed68fd5e01ac7161093118095523..581c01bd154132f57b10b20e0b4fb513053ead3f 100644 (file)
@@ -1,5 +1,4 @@
 if(ENABLE_MEMASLAP AND CMAKE_USE_PTHREADS_INIT)
-    add_definitions(-D_GNU_SOURCE)
 
     include(CheckAtomics)
 
index b9ce01aef72ca84db4eaba55ea3ec066e4b30c7e..efc3e70874ac759eb73ba2228e46a8841afc27b0 100644 (file)
@@ -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)