Add CheckCXXLibraryExists.cmake module and use it in FindTreadsCXX.cmake.
authorAndreas Schuh <andreas.schuh.84@gmail.com>
Mon, 17 Mar 2014 13:17:44 +0000 (13:17 +0000)
committerAndreas Schuh <andreas.schuh.84@gmail.com>
Mon, 17 Mar 2014 13:17:44 +0000 (13:17 +0000)
CMakeLists.txt
cmake/CheckCXXLibraryExists.cmake [new file with mode: 0644]
cmake/FindThreadsCxx.cmake

index 91046a8..3556afa 100644 (file)
@@ -103,7 +103,7 @@ foreach (fname IN ITEMS strtoll strtoq)
 endforeach ()
 
 set (CMAKE_THREAD_PREFER_PTHREAD TRUE)
-find_package (ThreadsCxx)
+find_package (ThreadsCXX)
 if (Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
   set (GFLAGS_HAVE_PTHREAD TRUE)
   check_type_size (pthread_rwlock_t GFLAGS_HAVE_RWLOCK LANGUAGE CXX)
diff --git a/cmake/CheckCXXLibraryExists.cmake b/cmake/CheckCXXLibraryExists.cmake
new file mode 100644 (file)
index 0000000..eb0885f
--- /dev/null
@@ -0,0 +1,80 @@
+#.rst:
+# CheckLibraryExists
+# ------------------
+#
+# Check if the function exists.
+#
+# CHECK_LIBRARY_EXISTS (LIBRARY FUNCTION LOCATION VARIABLE)
+#
+# ::
+#
+#   LIBRARY  - the name of the library you are looking for
+#   FUNCTION - the name of the function
+#   LOCATION - location where the library should be found
+#   VARIABLE - variable to store the result
+#
+#
+#
+# The following variables may be set before calling this macro to modify
+# the way the check is run:
+#
+# ::
+#
+#   CMAKE_REQUIRED_FLAGS = string of compile command line flags
+#   CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
+#   CMAKE_REQUIRED_LIBRARIES = list of libraries to link
+
+#=============================================================================
+# Copyright 2002-2009 Kitware, Inc.
+#
+# Distributed under the OSI-approved BSD License (the "License");
+# see accompanying file Copyright.txt for details.
+#
+# This software is distributed WITHOUT ANY WARRANTY; without even the
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the License for more information.
+#=============================================================================
+# (To distribute this file outside of CMake, substitute the full
+#  License text for the above reference.)
+
+
+
+macro(CHECK_CXX_LIBRARY_EXISTS LIBRARY FUNCTION LOCATION VARIABLE)
+  if("${VARIABLE}" MATCHES "^${VARIABLE}$")
+    set(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
+      "-DCHECK_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
+    message(STATUS "Looking for ${FUNCTION} in ${LIBRARY}")
+    set(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
+    if(CMAKE_REQUIRED_LIBRARIES)
+      set(CHECK_LIBRARY_EXISTS_LIBRARIES
+        ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${CMAKE_REQUIRED_LIBRARIES})
+    endif()
+    configure_file(${CMAKE_ROOT}/Modules/CheckFunctionExists.c
+                   ${CMAKE_BINARY_DIR}/CheckFunctionExists.cxx COPYONLY)
+    try_compile(${VARIABLE}
+      ${CMAKE_BINARY_DIR}
+      ${CMAKE_BINARY_DIR}/CheckFunctionExists.cxx
+      COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
+      LINK_LIBRARIES ${CHECK_LIBRARY_EXISTS_LIBRARIES}
+      CMAKE_FLAGS
+      -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
+      -DLINK_DIRECTORIES:STRING=${LOCATION}
+      OUTPUT_VARIABLE OUTPUT)
+
+    if(${VARIABLE})
+      message(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - found")
+      set(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
+      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
+        "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
+        "passed with the following output:\n"
+        "${OUTPUT}\n\n")
+    else()
+      message(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - not found")
+      set(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
+      file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
+        "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
+        "failed with the following output:\n"
+        "${OUTPUT}\n\n")
+    endif()
+  endif()
+endmacro()
index 625b826..cfeb578 100644 (file)
@@ -34,7 +34,7 @@
 #  License text for the above reference.)
 
 include (CheckIncludeFileCXX)
-include (CheckLibraryExists)
+include (CheckCXXLibraryExists)
 include (CheckCXXSymbolExists)
 set(Threads_FOUND FALSE)
 
@@ -67,7 +67,7 @@ else()
 
       if(NOT CMAKE_HAVE_THREADS_LIBRARY)
         # Do we have -lpthreads
-        CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE)
+        CHECK_CXX_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE)
         if(CMAKE_HAVE_PTHREADS_CREATE)
           set(CMAKE_THREAD_LIBS_INIT "-lpthreads")
           set(CMAKE_HAVE_THREADS_LIBRARY 1)
@@ -75,7 +75,7 @@ else()
         endif()
 
         # Ok, how about -lpthread
-        CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE)
+        CHECK_CXX_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE)
         if(CMAKE_HAVE_PTHREAD_CREATE)
           set(CMAKE_THREAD_LIBS_INIT "-lpthread")
           set(CMAKE_HAVE_THREADS_LIBRARY 1)
@@ -84,7 +84,7 @@ else()
 
         if(CMAKE_SYSTEM MATCHES "SunOS.*")
           # On sun also check for -lthread
-          CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE)
+          CHECK_CXX_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE)
           if(CMAKE_HAVE_THR_CREATE)
             set(CMAKE_THREAD_LIBS_INIT "-lthread")
             set(CMAKE_HAVE_THREADS_LIBRARY 1)
@@ -153,7 +153,7 @@ if(CMAKE_USE_PTHREADS_INIT)
     # but we need to maintain compatibility here.
     # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads
     # are available.
-    CHECK_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
+    CHECK_CXX_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA)
     if(CMAKE_HAVE_HP_CMA)
       set(CMAKE_THREAD_LIBS_INIT "-lcma")
       set(CMAKE_HP_PTHREADS_INIT 1)