aboutsummaryrefslogtreecommitdiffstats
path: root/cmake
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2010-02-16 05:36:49 -0800
committerChris Robinson <[email protected]>2010-02-16 05:36:49 -0800
commit2083884ae329072b316e725330af92f683786451 (patch)
tree9e4ababb91633c6ab5e5a6b17d634cfff79be073 /cmake
parent2f644c7ed5d72ec1a469be3e028923d475403f0b (diff)
Properly check for functions in Win32 shared libs
Diffstat (limited to 'cmake')
-rw-r--r--cmake/CheckSharedFunctionExists.c38
-rw-r--r--cmake/CheckSharedLibraryExists.cmake67
2 files changed, 105 insertions, 0 deletions
diff --git a/cmake/CheckSharedFunctionExists.c b/cmake/CheckSharedFunctionExists.c
new file mode 100644
index 00000000..31e3fa92
--- /dev/null
+++ b/cmake/CheckSharedFunctionExists.c
@@ -0,0 +1,38 @@
+#ifdef CHECK_SHARED_FUNCTION_EXISTS
+
+#include <stdlib.h>
+
+#ifndef CALLSTACK
+#define CALLSTACK
+#endif
+
+#ifdef _WIN32
+#ifdef ARGSTACK
+char __stdcall CHECK_SHARED_FUNCTION_EXISTS(ARGSTACK);
+#else
+char __stdcall CHECK_SHARED_FUNCTION_EXISTS(void);
+#endif
+#else
+char CHECK_SHARED_FUNCTION_EXISTS();
+#endif
+
+#ifdef __CLASSIC_C__
+int main(){
+ int ac;
+ char*av[];
+#else
+int main(int ac, char*av[]){
+#endif
+ CHECK_SHARED_FUNCTION_EXISTS(CALLSTACK);
+ if(ac > 1000)
+ {
+ return *av[0];
+ }
+ return 0;
+}
+
+#else /* CHECK_SHARED_FUNCTION_EXISTS */
+
+# error "CHECK_SHARED_FUNCTION_EXISTS has to specify the function"
+
+#endif /* CHECK_SHARED_FUNCTION_EXISTS */
diff --git a/cmake/CheckSharedLibraryExists.cmake b/cmake/CheckSharedLibraryExists.cmake
new file mode 100644
index 00000000..0dbb5b90
--- /dev/null
+++ b/cmake/CheckSharedLibraryExists.cmake
@@ -0,0 +1,67 @@
+# - 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
+# ARGCOUNT - number of arguments for stdcall functions
+# 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
+
+MACRO(CHECK_SHARED_LIBRARY_EXISTS LIBRARY FUNCTION ARGCOUNT LOCATION VARIABLE)
+ IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
+ SET(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
+ "-DCHECK_SHARED_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
+ IF(WIN32)
+ IF(${ARGCOUNT} GREATER 0)
+ SET(ARGSTACK "void*")
+ SET(CALLSTACK "NULL")
+ SET(CURARG 1)
+ WHILE(${ARGCOUNT} GREATER ${CURARG})
+ SET(ARGSTACK "${ARGSTACK},void*")
+ SET(CALLSTACK "${CALLSTACK},NULL")
+ MATH(EXPR CURARG "${CURARG} + 1")
+ ENDWHILE(${ARGCOUNT} GREATER ${CURARG})
+ ENDIF(${ARGCOUNT} GREATER 0)
+ SET(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION
+ "-D_WIN32 -DARGSTACK=\"${ARGSTACK}\" -DCALLSTACK=\"${CALLSTACK}\" ${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}")
+ ENDIF(WIN32)
+ 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(CMAKE_REQUIRED_LIBRARIES)
+ TRY_COMPILE(${VARIABLE}
+ ${CMAKE_BINARY_DIR}
+ ${CMAKE_SOURCE_DIR}/cmake/CheckSharedFunctionExists.c
+ COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
+ CMAKE_FLAGS
+ -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
+ -DLINK_DIRECTORIES:STRING=${LOCATION}
+ "-DLINK_LIBRARIES:STRING=${CHECK_LIBRARY_EXISTS_LIBRARIES}"
+ 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(${VARIABLE})
+ 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(${VARIABLE})
+ ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
+ENDMACRO(CHECK_SHARED_LIBRARY_EXISTS)