Use proper types for some variables
[openal-soft.git] / cmake / CheckSharedLibraryExists.cmake
blob0dbb5b9008ff9948d6a98ac65919fcce110ca8f8
1 # - Check if the function exists.
2 # CHECK_LIBRARY_EXISTS (LIBRARY FUNCTION LOCATION VARIABLE)
4 #  LIBRARY  - the name of the library you are looking for
5 #  FUNCTION - the name of the function
6 #  ARGCOUNT - number of arguments for stdcall functions
7 #  LOCATION - location where the library should be found
8 #  VARIABLE - variable to store the result
10 # The following variables may be set before calling this macro to
11 # modify the way the check is run:
13 #  CMAKE_REQUIRED_FLAGS = string of compile command line flags
14 #  CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
15 #  CMAKE_REQUIRED_LIBRARIES = list of libraries to link
17 MACRO(CHECK_SHARED_LIBRARY_EXISTS LIBRARY FUNCTION ARGCOUNT LOCATION VARIABLE)
18   IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
19     SET(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION 
20       "-DCHECK_SHARED_FUNCTION_EXISTS=${FUNCTION} ${CMAKE_REQUIRED_FLAGS}")
21     IF(WIN32)
22         IF(${ARGCOUNT} GREATER 0)
23             SET(ARGSTACK "void*")
24             SET(CALLSTACK "NULL")
25             SET(CURARG 1)
26             WHILE(${ARGCOUNT} GREATER ${CURARG})
27                 SET(ARGSTACK "${ARGSTACK},void*")
28                 SET(CALLSTACK "${CALLSTACK},NULL")
29                 MATH(EXPR CURARG "${CURARG} + 1")
30             ENDWHILE(${ARGCOUNT} GREATER ${CURARG})
31         ENDIF(${ARGCOUNT} GREATER 0)
32         SET(MACRO_CHECK_LIBRARY_EXISTS_DEFINITION 
33             "-D_WIN32 -DARGSTACK=\"${ARGSTACK}\" -DCALLSTACK=\"${CALLSTACK}\" ${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}")
34     ENDIF(WIN32)
35     MESSAGE(STATUS "Looking for ${FUNCTION} in ${LIBRARY}")
36     SET(CHECK_LIBRARY_EXISTS_LIBRARIES ${LIBRARY})
37     IF(CMAKE_REQUIRED_LIBRARIES)
38       SET(CHECK_LIBRARY_EXISTS_LIBRARIES 
39         ${CHECK_LIBRARY_EXISTS_LIBRARIES} ${CMAKE_REQUIRED_LIBRARIES})
40     ENDIF(CMAKE_REQUIRED_LIBRARIES)
41     TRY_COMPILE(${VARIABLE}
42       ${CMAKE_BINARY_DIR}
43       ${CMAKE_SOURCE_DIR}/cmake/CheckSharedFunctionExists.c
44       COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
45       CMAKE_FLAGS 
46       -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_LIBRARY_EXISTS_DEFINITION}
47       -DLINK_DIRECTORIES:STRING=${LOCATION}
48       "-DLINK_LIBRARIES:STRING=${CHECK_LIBRARY_EXISTS_LIBRARIES}"
49       OUTPUT_VARIABLE OUTPUT)
51     IF(${VARIABLE})
52       MESSAGE(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - found")
53       SET(${VARIABLE} 1 CACHE INTERNAL "Have library ${LIBRARY}")
54       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log 
55         "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
56         "passed with the following output:\n"
57         "${OUTPUT}\n\n")
58     ELSE(${VARIABLE})
59       MESSAGE(STATUS "Looking for ${FUNCTION} in ${LIBRARY} - not found")
60       SET(${VARIABLE} "" CACHE INTERNAL "Have library ${LIBRARY}")
61       FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log 
62         "Determining if the function ${FUNCTION} exists in the ${LIBRARY} "
63         "failed with the following output:\n"
64         "${OUTPUT}\n\n")
65     ENDIF(${VARIABLE})
66   ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
67 ENDMACRO(CHECK_SHARED_LIBRARY_EXISTS)