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.
8macro(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})
14endmacro(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.
20macro(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})
26endmacro(CHECK_INCLUDE_FILE_CONCAT)
27
28# For other curl specific tests, use this macro.
29macro(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()
59endmacro(CURL_INTERNAL_TEST)
60
61macro(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()
95endmacro(CURL_INTERNAL_TEST_RUN)
96
97macro(CURL_NROFF_CHECK)
98  find_program(NROFF NAMES gnroff nroff)
99  if(NROFF)
100    # Need a way to write to stdin, this will do
101    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/nroff-input.txt" "test")
102    # Tests for a valid nroff option to generate a manpage
103    foreach(_MANOPT "-man" "-mandoc")
104      execute_process(COMMAND "${NROFF}" ${_MANOPT}
105        OUTPUT_VARIABLE NROFF_MANOPT_OUTPUT
106        INPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/nroff-input.txt"
107        ERROR_QUIET)
108      # Save the option if it was valid
109      if(NROFF_MANOPT_OUTPUT)
110        message("Found *nroff option: -- ${_MANOPT}")
111        set(NROFF_MANOPT ${_MANOPT})
112        set(NROFF_USEFUL ON)
113        break()
114      endif()
115    endforeach()
116    # No need for the temporary file
117    file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/nroff-input.txt")
118    if(NOT NROFF_USEFUL)
119      message(WARNING "Found no *nroff option to get plaintext from man pages")
120    endif()
121  else()
122    message(WARNING "Found no *nroff program")
123  endif()
124endmacro(CURL_NROFF_CHECK)
125