Macros.cmake (6315B)
1 #*************************************************************************** 2 # _ _ ____ _ 3 # Project ___| | | | _ \| | 4 # / __| | | | |_) | | 5 # | (__| |_| | _ <| |___ 6 # \___|\___/|_| \_\_____| 7 # 8 # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 9 # 10 # This software is licensed as described in the file COPYING, which 11 # you should have received as part of this distribution. The terms 12 # are also available at https://curl.se/docs/copyright.html. 13 # 14 # You may opt to use, copy, modify, merge, publish, distribute and/or sell 15 # copies of the Software, and permit persons to whom the Software is 16 # furnished to do so, under the terms of the COPYING file. 17 # 18 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19 # KIND, either express or implied. 20 # 21 # SPDX-License-Identifier: curl 22 # 23 ########################################################################### 24 # File defines convenience macros for available feature testing 25 26 # Check if header file exists and add it to the list. 27 # This macro is intended to be called multiple times with a sequence of 28 # possibly dependent header files. Some headers depend on others to be 29 # compiled correctly. 30 macro(check_include_file_concat_curl _file _variable) 31 check_include_files("${CURL_INCLUDES};${_file}" ${_variable}) 32 if(${_variable}) 33 list(APPEND CURL_INCLUDES ${_file}) 34 endif() 35 endmacro() 36 37 set(CURL_TEST_DEFINES "") # Initialize global variable 38 39 # For other curl specific tests, use this macro. 40 # Return result in variable: CURL_TEST_OUTPUT 41 macro(curl_internal_test _curl_test) 42 if(NOT DEFINED "${_curl_test}") 43 string(REPLACE ";" " " _cmake_required_definitions "${CMAKE_REQUIRED_DEFINITIONS}") 44 set(_curl_test_add_libraries "") 45 if(CMAKE_REQUIRED_LIBRARIES) 46 set(_curl_test_add_libraries 47 "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}") 48 endif() 49 50 message(STATUS "Performing Test ${_curl_test}") 51 try_compile(${_curl_test} 52 ${PROJECT_BINARY_DIR} 53 "${CMAKE_CURRENT_SOURCE_DIR}/CMake/CurlTests.c" 54 CMAKE_FLAGS 55 "-DCOMPILE_DEFINITIONS:STRING=-D${_curl_test} ${CURL_TEST_DEFINES} ${CMAKE_REQUIRED_FLAGS} ${_cmake_required_definitions}" 56 "${_curl_test_add_libraries}" 57 OUTPUT_VARIABLE CURL_TEST_OUTPUT) 58 if(${_curl_test}) 59 set(${_curl_test} 1 CACHE INTERNAL "Curl test") 60 message(STATUS "Performing Test ${_curl_test} - Success") 61 else() 62 set(${_curl_test} "" CACHE INTERNAL "Curl test") 63 message(STATUS "Performing Test ${_curl_test} - Failed") 64 endif() 65 endif() 66 endmacro() 67 68 # Option for dependencies that accepts an 'AUTO' value, which enables the dependency if detected. 69 macro(curl_dependency_option _option_name _find_name _desc_name) 70 set(${_option_name} "AUTO" CACHE STRING "Build curl with ${_desc_name} support (AUTO, ON or OFF)") 71 set_property(CACHE ${_option_name} PROPERTY STRINGS "AUTO" "ON" "OFF") 72 73 if(${_option_name} STREQUAL "AUTO") 74 find_package(${_find_name}) 75 elseif(${_option_name}) 76 find_package(${_find_name} REQUIRED) 77 endif() 78 endmacro() 79 80 # Convert the passed paths to libpath linker options and add them to CMAKE_REQUIRED_*. 81 macro(curl_required_libpaths _libpaths_arg) 82 if(CMAKE_VERSION VERSION_LESS 3.31) 83 set(_libpaths "${_libpaths_arg}") 84 foreach(_libpath IN LISTS _libpaths) 85 list(APPEND CMAKE_REQUIRED_LINK_OPTIONS "${CMAKE_LIBRARY_PATH_FLAG}${_libpath}") 86 endforeach() 87 else() 88 list(APPEND CMAKE_REQUIRED_LINK_DIRECTORIES "${_libpaths_arg}") 89 endif() 90 endmacro() 91 92 # Pre-fill variables set by a check_type_size() call. 93 macro(curl_prefill_type_size _type _size) 94 set(HAVE_SIZEOF_${_type} TRUE) 95 set(SIZEOF_${_type} ${_size}) 96 set(SIZEOF_${_type}_CODE "#define SIZEOF_${_type} ${_size}") 97 endmacro() 98 99 # Internal: Recurse into target libraries and collect their include directories 100 # and macro definitions. 101 macro(curl_collect_target_options _target) 102 get_target_property(_val ${_target} INTERFACE_INCLUDE_DIRECTORIES) 103 if(_val) 104 list(APPEND _includes ${_val}) 105 endif() 106 get_target_property(_val ${_target} INCLUDE_DIRECTORIES) 107 if(_val) 108 list(APPEND _includes ${_val}) 109 endif() 110 get_target_property(_val ${_target} COMPILE_DEFINITIONS) 111 if(_val) 112 list(APPEND _definitions ${_val}) 113 endif() 114 get_target_property(_val ${_target} LINK_LIBRARIES) 115 if(_val) 116 foreach(_lib IN LISTS _val) 117 if(TARGET "${_lib}") 118 curl_collect_target_options(${_lib}) 119 endif() 120 endforeach() 121 endif() 122 unset(_val) 123 endmacro() 124 125 # Create a clang-tidy target for test targets 126 macro(curl_add_clang_tidy_test_target _target_clang_tidy _target) 127 if(CURL_CLANG_TIDY) 128 129 set(_includes "") 130 set(_definitions "") 131 132 # Collect header directories and macro definitions applying to the directory 133 get_directory_property(_val INCLUDE_DIRECTORIES) 134 if(_val) 135 list(APPEND _includes ${_val}) 136 endif() 137 get_directory_property(_val COMPILE_DEFINITIONS) 138 if(_val) 139 list(APPEND _definitions ${_val}) 140 endif() 141 unset(_val) 142 143 # Collect header directories and macro definitions from lib dependencies 144 curl_collect_target_options(${_target}) 145 146 list(REMOVE_ITEM _includes "") 147 string(REPLACE ";" ";-I" _includes ";${_includes}") 148 list(REMOVE_DUPLICATES _includes) 149 150 list(REMOVE_ITEM _definitions "") 151 string(REPLACE ";" ";-D" _definitions ";${_definitions}") 152 list(REMOVE_DUPLICATES _definitions) 153 list(SORT _definitions) # Sort like CMake does 154 155 # Assemble source list 156 set(_sources "") 157 foreach(_source IN ITEMS ${ARGN}) 158 if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_source}") # if not in source tree 159 set(_source "${CMAKE_CURRENT_BINARY_DIR}/${_source}") # look in the build tree, for generated files, e.g. lib1521.c 160 endif() 161 list(APPEND _sources "${_source}") 162 endforeach() 163 164 add_custom_target(${_target_clang_tidy} USES_TERMINAL 165 WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" 166 COMMAND ${CMAKE_C_CLANG_TIDY} ${_sources} -- ${_includes} ${_definitions} 167 DEPENDS ${_sources}) 168 add_dependencies(tests-clang-tidy ${_target_clang_tidy}) 169 170 unset(_includes) 171 unset(_definitions) 172 unset(_sources) 173 endif() 174 endmacro()