Permalink
Browse files

cmake: provide SafeCheckSymbolExists

Provide a safe way to handle check_symbol_exists calls.
Works around problems with -Werror and -Wpedantic as well as issues
where required definitions where not set before checks.
  • Loading branch information...
ingwinlu committed Sep 9, 2018
1 parent cdb98b2 commit d4f4fb6770577c6d4ec93b6edd57954be5e3c98e
@@ -25,7 +25,7 @@
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ~~~
include (CheckFunctionExists)
include (SafeCheckSymbolExists)
if (ICONV_INCLUDE_DIR)
set (ICONV_FIND_QUIETLY TRUE)
@@ -77,7 +77,7 @@ if (WIN32)
set (ICONV_FOUND TRUE)
endif ()
else ()
check_function_exists (iconv HAVE_ICONV_IN_LIBC)
safe_check_symbol_exists (iconv "iconv.h" HAVE_ICONV_IN_LIBC)
if (ICONV_INCLUDE_DIR AND HAVE_ICONV_IN_LIBC)
set (ICONV_FOUND TRUE)
set (ICONV_LIBRARY
@@ -8,10 +8,10 @@
# PLUGINPROCESS_NOTFOUND_INFO - A string describing which pluginprocess dependency is missing
#
# ~~~
include (CheckFunctionExists)
include (SafeCheckSymbolExists)
check_function_exists (mkfifo HAVE_MKFIFO)
check_function_exists (fork HAVE_FORK)
safe_check_symbol_exists (mkfifo "sys/types.h;sys/stat.h" HAVE_MKFIFO)
safe_check_symbol_exists (fork "sys/types.h;unistd.h" HAVE_FORK)
if (HAVE_MKFIFO)
if (HAVE_FORK)
@@ -0,0 +1,44 @@
include (CheckSymbolExists)
include (CMakePushCheckState)
# ~~~
# Check if a symbol is exported
#
# Same as check_symbol_exists but works around the problem of not detecting
# symbols when -Wpedantic is on.
#
# Also automatically adds the definitions of the current directory to
# CMAKE_REQUIRED_DEFINITIONS.
#
# https://issues.libelektra.org/2218
# ~~~
macro (safe_check_symbol_exists SYMBOL FILES VARIABLE)
set (CMAKE_C_FLAGS_OLD ${CMAKE_C_FLAGS})
string (REPLACE "-Wpedantic"
""
CMAKE_C_FLAGS
${CMAKE_C_FLAGS})
cmake_push_check_state (RESET)
get_directory_property (DEFS COMPILE_DEFINITIONS)
prepend (DEFS "-D" ${DEFS})
list (APPEND CMAKE_REQUIRED_DEFINITIONS
${DEFS})
check_symbol_exists ("${SYMBOL}" "${FILES}" "${VARIABLE}")
cmake_pop_check_state ()
set (CMAKE_C_FLAGS ${CMAKE_C_FLAGS_OLD})
endmacro ()
# Prepends prefix onto all argn list members and saves the result to var Can be replaced by list(TRANSFORM .. PREPEND ..) once we use CMake
# 3.12
function (prepend var prefix)
set (temp "")
foreach (f ${ARGN})
list (APPEND temp
"${prefix}${f}")
endforeach (f)
set (${var} "${temp}" PARENT_SCOPE)
endfunction (prepend)
@@ -190,6 +190,8 @@ you up to date with the multi-language support provided by Elektra.
tools . *(René Schwaiger)*
- The CMake configuration step now displays less debug messages about found libraries. *(René Schwaiger)*
- Building the Haskell binding should now work again. *(René Schwaiger)*
- Provide a wrapper around `check_symbol_exists` that handles issues with
`-Werror -Wpedantic`. *(Lukas Winkler)*
### Docker
View
@@ -1,34 +1,19 @@
include (CheckIncludeFile)
include (CheckSymbolExists)
include (SafeCheckSymbolExists)
include (CheckTypeSize)
include (TestBigEndian)
include (LibAddBinding)
check_symbol_exists (clearenv "stdlib.h" HAVE_CLEARENV)
check_symbol_exists (setenv "stdlib.h" HAVE_SETENV)
check_symbol_exists (futimens "sys/stat.h" HAVE_FUTIMENS)
list (APPEND CMAKE_REQUIRED_DEFINITIONS
-D_GNU_SOURCE) # look for hsearch_r function family
check_symbol_exists (hsearch_r "search.h" HAVE_HSEARCHR)
list (REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS
-D_GNU_SOURCE)
# Work around a bug on macOS where `futimes` can not be located if we specify the compiler switch `-Werror`
if (APPLE)
set (CMAKE_C_FLAGS_OLD ${CMAKE_C_FLAGS})
string (REPLACE "-Werror"
""
CMAKE_C_FLAGS
${CMAKE_C_FLAGS})
check_symbol_exists (futimes "sys/time.h" HAVE_FUTIMES)
set (CMAKE_C_FLAGS ${CMAKE_C_FLAGS_OLD})
else (APPLE)
check_symbol_exists (futimes "sys/time.h" HAVE_FUTIMES)
endif (APPLE)
check_symbol_exists (glob "glob.h" HAVE_GLOB)
check_symbol_exists (clock_gettime "time.h" HAVE_CLOCK_GETTIME)
safe_check_symbol_exists (clearenv "stdlib.h" HAVE_CLEARENV)
safe_check_symbol_exists (setenv "stdlib.h" HAVE_SETENV)
safe_check_symbol_exists (futimens "sys/stat.h" HAVE_FUTIMENS)
add_definitions (-D_GNU_SOURCE -D_DARWIN_C_SOURCE)
safe_check_symbol_exists (hsearch_r "search.h" HAVE_HSEARCHR)
safe_check_symbol_exists (futimes "sys/time.h" HAVE_FUTIMES)
safe_check_symbol_exists (glob "glob.h" HAVE_GLOB)
safe_check_symbol_exists (clock_gettime "time.h" HAVE_CLOCK_GETTIME)
check_include_file (ctype.h HAVE_CTYPE_H)
check_include_file (errno.h HAVE_ERRNO_H)
@@ -1,5 +1,6 @@
include (LibAddMacros)
include (LibAddBinding)
include (SafeCheckSymbolExists)
if (DEPENDENCY_PHASE)
if (APPLE)
@@ -11,11 +12,10 @@ if (DEPENDENCY_PHASE)
find_package (JNI QUIET)
if (JNI_FOUND)
include (CheckSymbolExists)
set (DIRS ${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2} ${JAVA_AWT_INCLUDE_PATH})
set (LIBS ${JAVA_MAWT_LIBRARY} ${JNI_LIBRARIES} ${JAVA_AWT_LIBRARY} ${JAVA_JVM_LIBRARY}) # for check_symbol_exists
set (CMAKE_REQUIRED_INCLUDES ${DIRS})
check_symbol_exists (JNI_VERSION_1_8 jni.h JNI_CORRECT_VERSION)
safe_check_symbol_exists (JNI_VERSION_1_8 jni.h JNI_CORRECT_VERSION)
unset (CMAKE_REQUIRED_INCLUDES)
if (NOT JNI_CORRECT_VERSION)
@@ -1,26 +1,15 @@
include (CheckSymbolExists)
include (SafeCheckSymbolExists)
include (LibAddMacros)
include (CMakePushCheckState)
# We need GNU_SOURCE and XOPEN_SOURCE for full functionality
set (REQUIRED_FEATURE_MACROS -D_GNU_SOURCE -D_XOPEN_SOURCE=500 -D_DARWIN_C_SOURCE)
add_definitions (${REQUIRED_FEATURE_MACROS})
add_definitions (-D_GNU_SOURCE -D_DARWIN_C_SOURCE)
safe_check_symbol_exists (nftw "ftw.h" HAVE_NFTW)
safe_check_symbol_exists (mkdtemp "stdlib.h;unistd.h" HAVE_MKDTEMP)
safe_check_symbol_exists (setenv "stdlib.h" HAVE_SETENV)
cmake_push_check_state (RESET)
set (CMAKE_REQUIRED_DEFINITIONS ${REQUIRED_FEATURE_MACROS})
set (CMAKE_C_FLAGS_OLD ${CMAKE_C_FLAGS})
# On some system the check for `ftw.h` will produce warnings. In that case the detection of `ftw.h` will fail, if we use the flag `-Werror`.
# We make sure that this is not the case by temporarily disabling `-Werror`.
string (REPLACE "-Werror"
""
CMAKE_C_FLAGS
${CMAKE_C_FLAGS})
check_symbol_exists (nftw "ftw.h" HAVE_NFTW)
set (CMAKE_C_FLAGS ${CMAKE_C_FLAGS_OLD})
cmake_pop_check_state ()
if (NOT (HAVE_MKDTEMP AND HAVE_SETENV))
message (FATAL_ERROR "cframework requires mkdtemp && setenv, disable ENABLE_TESTING to build without testing")
endif (NOT (HAVE_MKDTEMP AND HAVE_SETENV))
if (HAVE_NFTW)
add_definitions (-DUSE_NFTW)

0 comments on commit d4f4fb6

Please sign in to comment.