From 40e3df54a92500264f48359811bb5bb641bd01c4 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Mon, 10 Oct 2022 02:17:57 +0000 Subject: [PATCH] Fix test_strerror_r not works --- .gitignore | 1 + CMakeLists.txt | 8 +++++--- TestStrerrorR.cmake | 13 ++++++++++--- test_strerror_r.c | 2 ++ 4 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 44f2f18..0b7ecba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,18 +47,20 @@ endif() check_symbol_exists(strcasecmp "string.h" HAVE_STRCASECMP) check_symbol_exists(strncasecmp "string.h" HAVE_STRNCASECMP) check_symbol_exists(strerror_r "string.h" HAVE_STRERROR_R) -if (HAVE_STRERROR_R) - test_strerror_r(HAVE_GNU_STRERROR_R) -endif() +set(HAVE_GNU_SOURCE OFF) if (NOT WIN32) set(TMP "${CMAKE_REQUIRED_DEFINITIONS}") set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) check_symbol_exists(fcloseall "stdio.h" HAVE_FCLOSEALL) if (HAVE_FCLOSEALL) add_compile_definitions(_GNU_SOURCE) + set(HAVE_GNU_SOURCE ON) endif() set(CMAKE_REQUIRED_DEFINITIONS "${TMP}") endif() +if (HAVE_STRERROR_R) + test_strerror_r(HAVE_GNU_STRERROR_R ${HAVE_GNU_SOURCE}) +endif() configure_file("${CMAKE_CURRENT_SOURCE_DIR}/utils_config.h.in" "${CMAKE_CURRENT_BINARY_DIR}/utils_config.h") if (${CMAKE_C_COMPILER_ID} STREQUAL GNU) diff --git a/TestStrerrorR.cmake b/TestStrerrorR.cmake index 801f2c6..c07847a 100644 --- a/TestStrerrorR.cmake +++ b/TestStrerrorR.cmake @@ -1,12 +1,19 @@ -function(test_strerror_r OUTPUT_VAR) +function(test_strerror_r OUTPUT_VAR HAVE_GNU_SOURCE) if (DEFINED STRERROR_R_TEST_RESULT_CACHE) set(${OUTPUT_VAR} ${STRERROR_R_TEST_RESULT_CACHE} PARENT_SCOPE) message(STATUS "Check strerror_r is GNU complatable - ${STRERROR_R_TEST_RESULT_CACHE} (Cached)") else() message(STATUS "Check strerror_r is GNU complatable") - try_compile(STRERROR_R_TEST_RESULT "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/test_strerror_r.c") + if (HAVE_GNU_SOURCE) + set(TMP_DEF -D_GNU_SOURCE) + endif() + try_compile(STRERROR_R_TEST_RESULT "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/test_strerror_r.c" COMPILE_DEFINITIONS -Werror=int-conversion ${TMP_DEF}) set(STRERROR_R_TEST_RESULT_CACHE ${STRERROR_R_TEST_RESULT} CACHE BOOL "Is strerror_r is GNU complatable.") - set(${OUTPUT_VAR} ${STRERROR_R_TEST_RESULT} PARENT_SCOPE) + if (STRERROR_R_TEST_RESULT) + set(${OUTPUT_VAR} 1 PARENT_SCOPE) + else() + unset(${OUTPUT_VAR} PARENT_SCOPE) + endif() message(STATUS "Check strerror_r is GNU complatable - ${STRERROR_R_TEST_RESULT}") endif() endfunction() diff --git a/test_strerror_r.c b/test_strerror_r.c index 3473e4d..8ac8796 100644 --- a/test_strerror_r.c +++ b/test_strerror_r.c @@ -1,4 +1,6 @@ +#include #include +#include int main() { char buf[280];