Imported Upstream version 7.40.0
[platform/upstream/curl.git] / CMake / Macros.cmake
1 #File defines convenience macros for available feature testing
2
3 # This macro checks if the symbol exists in the library and if it
4 # does, it prepends library to the list.  It is intended to be called
5 # multiple times with a sequence of possibly dependent libraries in
6 # order of least-to-most-dependent.  Some libraries depend on others
7 # to link correctly.
8 macro(CHECK_LIBRARY_EXISTS_CONCAT LIBRARY SYMBOL VARIABLE)
9   check_library_exists("${LIBRARY};${CURL_LIBS}" ${SYMBOL} "${CMAKE_LIBRARY_PATH}"
10     ${VARIABLE})
11   if(${VARIABLE})
12     set(CURL_LIBS ${LIBRARY} ${CURL_LIBS})
13   endif(${VARIABLE})
14 endmacro(CHECK_LIBRARY_EXISTS_CONCAT)
15
16 # Check if header file exists and add it to the list.
17 # This macro is intended to be called multiple times with a sequence of
18 # possibly dependent header files.  Some headers depend on others to be
19 # compiled correctly.
20 macro(CHECK_INCLUDE_FILE_CONCAT FILE VARIABLE)
21   check_include_files("${CURL_INCLUDES};${FILE}" ${VARIABLE})
22   if(${VARIABLE})
23     set(CURL_INCLUDES ${CURL_INCLUDES} ${FILE})
24     set(CURL_TEST_DEFINES "${CURL_TEST_DEFINES} -D${VARIABLE}")
25   endif(${VARIABLE})
26 endmacro(CHECK_INCLUDE_FILE_CONCAT)
27
28 # For other curl specific tests, use this macro.
29 macro(CURL_INTERNAL_TEST CURL_TEST)
30   if(NOT DEFINED "${CURL_TEST}")
31     set(MACRO_CHECK_FUNCTION_DEFINITIONS
32       "-D${CURL_TEST} ${CURL_TEST_DEFINES} ${CMAKE_REQUIRED_FLAGS}")
33     if(CMAKE_REQUIRED_LIBRARIES)
34       set(CURL_TEST_ADD_LIBRARIES
35         "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
36     endif(CMAKE_REQUIRED_LIBRARIES)
37
38     message(STATUS "Performing Curl Test ${CURL_TEST}")
39     try_compile(${CURL_TEST}
40       ${CMAKE_BINARY_DIR}
41       ${CMAKE_CURRENT_SOURCE_DIR}/CMake/CurlTests.c
42       CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
43       "${CURL_TEST_ADD_LIBRARIES}"
44       OUTPUT_VARIABLE OUTPUT)
45     if(${CURL_TEST})
46       set(${CURL_TEST} 1 CACHE INTERNAL "Curl test ${FUNCTION}")
47       message(STATUS "Performing Curl Test ${CURL_TEST} - Success")
48       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
49         "Performing Curl Test ${CURL_TEST} passed with the following output:\n"
50         "${OUTPUT}\n")
51     else(${CURL_TEST})
52       message(STATUS "Performing Curl Test ${CURL_TEST} - Failed")
53       set(${CURL_TEST} "" CACHE INTERNAL "Curl test ${FUNCTION}")
54       file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
55         "Performing Curl Test ${CURL_TEST} failed with the following output:\n"
56         "${OUTPUT}\n")
57     endif(${CURL_TEST})
58   endif()
59 endmacro(CURL_INTERNAL_TEST)
60
61 macro(CURL_INTERNAL_TEST_RUN CURL_TEST)
62   if(NOT DEFINED "${CURL_TEST}_COMPILE")
63     set(MACRO_CHECK_FUNCTION_DEFINITIONS
64       "-D${CURL_TEST} ${CMAKE_REQUIRED_FLAGS}")
65     if(CMAKE_REQUIRED_LIBRARIES)
66       set(CURL_TEST_ADD_LIBRARIES
67         "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
68     endif(CMAKE_REQUIRED_LIBRARIES)
69
70     message(STATUS "Performing Curl Test ${CURL_TEST}")
71     try_run(${CURL_TEST} ${CURL_TEST}_COMPILE
72       ${CMAKE_BINARY_DIR}
73       ${CMAKE_CURRENT_SOURCE_DIR}/CMake/CurlTests.c
74       CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
75       "${CURL_TEST_ADD_LIBRARIES}"
76       OUTPUT_VARIABLE OUTPUT)
77     if(${CURL_TEST}_COMPILE AND NOT ${CURL_TEST})
78       set(${CURL_TEST} 1 CACHE INTERNAL "Curl test ${FUNCTION}")
79       message(STATUS "Performing Curl Test ${CURL_TEST} - Success")
80     else(${CURL_TEST}_COMPILE AND NOT ${CURL_TEST})
81       message(STATUS "Performing Curl Test ${CURL_TEST} - Failed")
82       set(${CURL_TEST} "" CACHE INTERNAL "Curl test ${FUNCTION}")
83       file(APPEND "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log"
84         "Performing Curl Test ${CURL_TEST} failed with the following output:\n"
85         "${OUTPUT}")
86       if(${CURL_TEST}_COMPILE)
87         file(APPEND
88           "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log"
89           "There was a problem running this test\n")
90       endif(${CURL_TEST}_COMPILE)
91       file(APPEND "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log"
92         "\n\n")
93     endif(${CURL_TEST}_COMPILE AND NOT ${CURL_TEST})
94   endif()
95 endmacro(CURL_INTERNAL_TEST_RUN)