Debian Codegraph @

matio-1.5.30/cmake/conan_provider.cmake

raw
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2024 JFrog
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in all
13 # copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22
23 set(CONAN_MINIMUM_VERSION 2.0.5)
24
25 # Create a new policy scope and set the minimum required cmake version so the
26 # features behind a policy setting like if(... IN_LIST ...) behaves as expected
27 # even if the parent project does not specify a minimum cmake version or a minimum
28 # version less than this module requires (e.g. 3.0) before the first project() call.
29 # (see: https://cmake.org/cmake/help/latest/variable/CMAKE_PROJECT_TOP_LEVEL_INCLUDES.html)
30 #
31 # The policy-affecting calls like cmake_policy(SET...) or `cmake_minimum_required` only
32 # affects the current policy scope, i.e. between the PUSH and POP in this case.
33 #
34 # https://cmake.org/cmake/help/book/mastering-cmake/chapter/Policies.html#the-policy-stack
35 cmake_policy(PUSH)
36 cmake_minimum_required(VERSION 3.24)
37
38
39 function(detect_os os os_api_level os_sdk os_subsystem os_version)
40 # it could be cross compilation
41 message(STATUS "CMake-Conan: cmake_system_name=${CMAKE_SYSTEM_NAME}")
42 if(CMAKE_SYSTEM_NAME AND NOT CMAKE_SYSTEM_NAME STREQUAL "Generic")
43 if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
44 set(${os} Macos PARENT_SCOPE)
45 elseif(CMAKE_SYSTEM_NAME STREQUAL "QNX")
46 set(${os} Neutrino PARENT_SCOPE)
47 elseif(CMAKE_SYSTEM_NAME STREQUAL "CYGWIN")
48 set(${os} Windows PARENT_SCOPE)
49 set(${os_subsystem} cygwin PARENT_SCOPE)
50 elseif(CMAKE_SYSTEM_NAME MATCHES "^MSYS")
51 set(${os} Windows PARENT_SCOPE)
52 set(${os_subsystem} msys2 PARENT_SCOPE)
53 elseif(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
54 # https://github.com/emscripten-core/emscripten/blob/4.0.6/cmake/Modules/Platform/Emscripten.cmake#L17C1-L17C34
55 set(${os} Emscripten PARENT_SCOPE)
56 else()
57 set(${os} ${CMAKE_SYSTEM_NAME} PARENT_SCOPE)
58 endif()
59 if(CMAKE_SYSTEM_NAME STREQUAL "Android")
60 if(DEFINED ANDROID_PLATFORM)
61 string(REGEX MATCH "[0-9]+" _os_api_level ${ANDROID_PLATFORM})
62 elseif(DEFINED CMAKE_SYSTEM_VERSION)
63 set(_os_api_level ${CMAKE_SYSTEM_VERSION})
64 endif()
65 message(STATUS "CMake-Conan: android api level=${_os_api_level}")
66 set(${os_api_level} ${_os_api_level} PARENT_SCOPE)
67 endif()
68 if(CMAKE_SYSTEM_NAME MATCHES "Darwin|iOS|tvOS|watchOS")
69 # CMAKE_OSX_SYSROOT contains the full path to the SDK for MakeFile/Ninja
70 # generators, but just has the original input string for Xcode.
71 if(NOT IS_DIRECTORY ${CMAKE_OSX_SYSROOT})
72 set(_os_sdk ${CMAKE_OSX_SYSROOT})
73 else()
74 if(CMAKE_OSX_SYSROOT MATCHES Simulator)
75 set(apple_platform_suffix simulator)
76 else()
77 set(apple_platform_suffix os)
78 endif()
79 if(CMAKE_OSX_SYSROOT MATCHES AppleTV)
80 set(_os_sdk "appletv${apple_platform_suffix}")
81 elseif(CMAKE_OSX_SYSROOT MATCHES iPhone)
82 set(_os_sdk "iphone${apple_platform_suffix}")
83 elseif(CMAKE_OSX_SYSROOT MATCHES Watch)
84 set(_os_sdk "watch${apple_platform_suffix}")
85 endif()
86 endif()
87 if(DEFINED os_sdk)
88 message(STATUS "CMake-Conan: cmake_osx_sysroot=${CMAKE_OSX_SYSROOT}")
89 set(${os_sdk} ${_os_sdk} PARENT_SCOPE)
90 endif()
91 if(DEFINED CMAKE_OSX_DEPLOYMENT_TARGET)
92 message(STATUS "CMake-Conan: cmake_osx_deployment_target=${CMAKE_OSX_DEPLOYMENT_TARGET}")
93 set(${os_version} ${CMAKE_OSX_DEPLOYMENT_TARGET} PARENT_SCOPE)
94 endif()
95 endif()
96 endif()
97 endfunction()
98
99
100 function(detect_arch arch)
101 # CMAKE_OSX_ARCHITECTURES can contain multiple architectures, but Conan only supports one.
102 # Therefore this code only finds one. If the recipes support multiple architectures, the
103 # build will work. Otherwise, there will be a linker error for the missing architecture(s).
104 if(DEFINED CMAKE_OSX_ARCHITECTURES)
105 string(REPLACE " " ";" apple_arch_list "${CMAKE_OSX_ARCHITECTURES}")
106 list(LENGTH apple_arch_list apple_arch_count)
107 if(apple_arch_count GREATER 1)
108 message(WARNING "CMake-Conan: Multiple architectures detected, this will only work if Conan recipe(s) produce fat binaries.")
109 endif()
110 endif()
111 if(CMAKE_SYSTEM_NAME MATCHES "Darwin|iOS|tvOS|watchOS" AND NOT CMAKE_OSX_ARCHITECTURES STREQUAL "")
112 set(host_arch ${CMAKE_OSX_ARCHITECTURES})
113 elseif(MSVC)
114 set(host_arch ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID})
115 else()
116 set(host_arch ${CMAKE_SYSTEM_PROCESSOR})
117 endif()
118 if(host_arch MATCHES "aarch64|arm64|ARM64")
119 set(_arch armv8)
120 elseif(host_arch MATCHES "armv7|armv7-a|armv7l|ARMV7")
121 set(_arch armv7)
122 elseif(host_arch MATCHES armv7s)
123 set(_arch armv7s)
124 elseif(host_arch MATCHES "i686|i386|X86")
125 set(_arch x86)
126 elseif(host_arch MATCHES "AMD64|amd64|x86_64|x64")
127 set(_arch x86_64)
128 endif()
129 if(EMSCRIPTEN)
130 # https://github.com/emscripten-core/emscripten/blob/4.0.6/cmake/Modules/Platform/Emscripten.cmake#L294C1-L294C80
131 set(_arch wasm)
132 endif()
133 message(STATUS "CMake-Conan: cmake_system_processor=${_arch}")
134 set(${arch} ${_arch} PARENT_SCOPE)
135 endfunction()
136
137
138 function(detect_cxx_standard cxx_standard)
139 set(${cxx_standard} ${CMAKE_CXX_STANDARD} PARENT_SCOPE)
140 if(CMAKE_CXX_EXTENSIONS)
141 set(${cxx_standard} "gnu${CMAKE_CXX_STANDARD}" PARENT_SCOPE)
142 endif()
143 endfunction()
144
145
146 macro(detect_gnu_libstdcxx)
147 # _conan_is_gnu_libstdcxx true if GNU libstdc++
148 check_cxx_source_compiles("
149 #include <cstddef>
150 #if !defined(__GLIBCXX__) && !defined(__GLIBCPP__)
151 static_assert(false);
152 #endif
153 int main(){}" _conan_is_gnu_libstdcxx)
154
155 # _conan_gnu_libstdcxx_is_cxx11_abi true if C++11 ABI
156 check_cxx_source_compiles("
157 #include <string>
158 static_assert(sizeof(std::string) != sizeof(void*), \"using libstdc++\");
159 int main () {}" _conan_gnu_libstdcxx_is_cxx11_abi)
160
161 set(_conan_gnu_libstdcxx_suffix "")
162 if(_conan_gnu_libstdcxx_is_cxx11_abi)
163 set(_conan_gnu_libstdcxx_suffix "11")
164 endif()
165 unset (_conan_gnu_libstdcxx_is_cxx11_abi)
166 endmacro()
167
168
169 macro(detect_libcxx)
170 # _conan_is_libcxx true if LLVM libc++
171 check_cxx_source_compiles("
172 #include <cstddef>
173 #if !defined(_LIBCPP_VERSION)
174 static_assert(false);
175 #endif
176 int main(){}" _conan_is_libcxx)
177 endmacro()
178
179
180 function(detect_lib_cxx lib_cxx)
181 if(CMAKE_SYSTEM_NAME STREQUAL "Android")
182 message(STATUS "CMake-Conan: android_stl=${CMAKE_ANDROID_STL_TYPE}")
183 set(${lib_cxx} ${CMAKE_ANDROID_STL_TYPE} PARENT_SCOPE)
184 return()
185 endif()
186
187 include(CheckCXXSourceCompiles)
188
189 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
190 detect_gnu_libstdcxx()
191 set(${lib_cxx} "libstdc++${_conan_gnu_libstdcxx_suffix}" PARENT_SCOPE)
192 elseif(CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
193 set(${lib_cxx} "libc++" PARENT_SCOPE)
194 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
195 # Check for libc++
196 detect_libcxx()
197 if(_conan_is_libcxx)
198 set(${lib_cxx} "libc++" PARENT_SCOPE)
199 return()
200 endif()
201
202 # Check for libstdc++
203 detect_gnu_libstdcxx()
204 if(_conan_is_gnu_libstdcxx)
205 set(${lib_cxx} "libstdc++${_conan_gnu_libstdcxx_suffix}" PARENT_SCOPE)
206 return()
207 endif()
208
209 # TODO: it would be an error if we reach this point
210 elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
211 # Do nothing - compiler.runtime and compiler.runtime_type
212 # should be handled separately: https://github.com/conan-io/cmake-conan/pull/516
213 return()
214 else()
215 # TODO: unable to determine, ask user to provide a full profile file instead
216 endif()
217 endfunction()
218
219
220 function(detect_compiler compiler compiler_version compiler_runtime compiler_runtime_type)
221 if(DEFINED CMAKE_CXX_COMPILER_ID)
222 set(_compiler ${CMAKE_CXX_COMPILER_ID})
223 set(_compiler_version ${CMAKE_CXX_COMPILER_VERSION})
224 else()
225 if(NOT DEFINED CMAKE_C_COMPILER_ID)
226 message(FATAL_ERROR "C or C++ compiler not defined")
227 endif()
228 set(_compiler ${CMAKE_C_COMPILER_ID})
229 set(_compiler_version ${CMAKE_C_COMPILER_VERSION})
230 endif()
231
232 message(STATUS "CMake-Conan: CMake compiler=${_compiler}")
233 message(STATUS "CMake-Conan: CMake compiler version=${_compiler_version}")
234
235 if(_compiler MATCHES MSVC)
236 set(_compiler "msvc")
237 string(SUBSTRING ${MSVC_VERSION} 0 3 _compiler_version)
238 # Configure compiler.runtime and compiler.runtime_type settings for MSVC
239 if(CMAKE_MSVC_RUNTIME_LIBRARY)
240 set(_msvc_runtime_library ${CMAKE_MSVC_RUNTIME_LIBRARY})
241 else()
242 set(_msvc_runtime_library MultiThreaded$<$<CONFIG:Debug>:Debug>DLL) # default value documented by CMake
243 endif()
244
245 set(_KNOWN_MSVC_RUNTIME_VALUES "")
246 list(APPEND _KNOWN_MSVC_RUNTIME_VALUES MultiThreaded MultiThreadedDLL)
247 list(APPEND _KNOWN_MSVC_RUNTIME_VALUES MultiThreadedDebug MultiThreadedDebugDLL)
248 list(APPEND _KNOWN_MSVC_RUNTIME_VALUES MultiThreaded$<$<CONFIG:Debug>:Debug> MultiThreaded$<$<CONFIG:Debug>:Debug>DLL)
249
250 # only accept the 6 possible values, otherwise we don't don't know to map this
251 if(NOT _msvc_runtime_library IN_LIST _KNOWN_MSVC_RUNTIME_VALUES)
252 message(FATAL_ERROR "CMake-Conan: unable to map MSVC runtime: ${_msvc_runtime_library} to Conan settings")
253 endif()
254
255 # Runtime is "dynamic" in all cases if it ends in DLL
256 if(_msvc_runtime_library MATCHES ".*DLL$")
257 set(_compiler_runtime "dynamic")
258 else()
259 set(_compiler_runtime "static")
260 endif()
261 message(STATUS "CMake-Conan: CMake compiler.runtime=${_compiler_runtime}")
262
263 # Only define compiler.runtime_type when explicitly requested
264 # If a generator expression is used, let Conan handle it conditional on build_type
265 if(NOT _msvc_runtime_library MATCHES "<CONFIG:Debug>:Debug>")
266 if(_msvc_runtime_library MATCHES "Debug")
267 set(_compiler_runtime_type "Debug")
268 else()
269 set(_compiler_runtime_type "Release")
270 endif()
271 message(STATUS "CMake-Conan: CMake compiler.runtime_type=${_compiler_runtime_type}")
272 endif()
273
274 unset(_KNOWN_MSVC_RUNTIME_VALUES)
275
276 elseif(_compiler MATCHES AppleClang)
277 set(_compiler "apple-clang")
278 string(REPLACE "." ";" VERSION_LIST ${_compiler_version})
279 list(GET VERSION_LIST 0 _compiler_version)
280 elseif(_compiler MATCHES Clang)
281 set(_compiler "clang")
282 string(REPLACE "." ";" VERSION_LIST ${_compiler_version})
283 list(GET VERSION_LIST 0 _compiler_version)
284 elseif(_compiler MATCHES GNU)
285 set(_compiler "gcc")
286 string(REPLACE "." ";" VERSION_LIST ${_compiler_version})
287 list(GET VERSION_LIST 0 _compiler_version)
288 endif()
289
290 message(STATUS "CMake-Conan: [settings] compiler=${_compiler}")
291 message(STATUS "CMake-Conan: [settings] compiler.version=${_compiler_version}")
292 if (_compiler_runtime)
293 message(STATUS "CMake-Conan: [settings] compiler.runtime=${_compiler_runtime}")
294 endif()
295 if (_compiler_runtime_type)
296 message(STATUS "CMake-Conan: [settings] compiler.runtime_type=${_compiler_runtime_type}")
297 endif()
298
299 set(${compiler} ${_compiler} PARENT_SCOPE)
300 set(${compiler_version} ${_compiler_version} PARENT_SCOPE)
301 set(${compiler_runtime} ${_compiler_runtime} PARENT_SCOPE)
302 set(${compiler_runtime_type} ${_compiler_runtime_type} PARENT_SCOPE)
303 endfunction()
304
305
306 function(detect_build_type build_type)
307 get_property(multiconfig_generator GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
308 if(NOT multiconfig_generator)
309 # Only set when we know we are in a single-configuration generator
310 # Note: we may want to fail early if `CMAKE_BUILD_TYPE` is not defined
311 set(${build_type} ${CMAKE_BUILD_TYPE} PARENT_SCOPE)
312 endif()
313 endfunction()
314
315
316 macro(set_conan_compiler_if_appleclang lang command output_variable)
317 if(CMAKE_${lang}_COMPILER_ID STREQUAL "AppleClang")
318 execute_process(COMMAND xcrun --find ${command}
319 OUTPUT_VARIABLE _xcrun_out OUTPUT_STRIP_TRAILING_WHITESPACE)
320 cmake_path(GET _xcrun_out PARENT_PATH _xcrun_toolchain_path)
321 cmake_path(GET CMAKE_${lang}_COMPILER PARENT_PATH _compiler_parent_path)
322 if ("${_xcrun_toolchain_path}" STREQUAL "${_compiler_parent_path}")
323 set(${output_variable} "")
324 endif()
325 unset(_xcrun_out)
326 unset(_xcrun_toolchain_path)
327 unset(_compiler_parent_path)
328 endif()
329 endmacro()
330
331
332 macro(append_compiler_executables_configuration)
333 set(_conan_c_compiler "")
334 set(_conan_cpp_compiler "")
335 set(_conan_rc_compiler "")
336 set(_conan_compilers_list "")
337 if(CMAKE_C_COMPILER)
338 set(_conan_c_compiler "\"c\":\"${CMAKE_C_COMPILER}\"")
339 set_conan_compiler_if_appleclang(C cc _conan_c_compiler)
340 list(APPEND _conan_compilers_list ${_conan_c_compiler})
341 else()
342 message(WARNING "CMake-Conan: The C compiler is not defined. "
343 "Please define CMAKE_C_COMPILER or enable the C language.")
344 endif()
345 if(CMAKE_CXX_COMPILER)
346 set(_conan_cpp_compiler "\"cpp\":\"${CMAKE_CXX_COMPILER}\"")
347 set_conan_compiler_if_appleclang(CXX c++ _conan_cpp_compiler)
348 list(APPEND _conan_compilers_list ${_conan_cpp_compiler})
349 else()
350 message(WARNING "CMake-Conan: The C++ compiler is not defined. "
351 "Please define CMAKE_CXX_COMPILER or enable the C++ language.")
352 endif()
353 if(CMAKE_RC_COMPILER)
354 set(_conan_rc_compiler "\"rc\":\"${CMAKE_RC_COMPILER}\"")
355 list(APPEND _conan_compilers_list ${_conan_rc_compiler})
356 # Not necessary to warn if RC not defined
357 endif()
358 if(NOT "x${_conan_compilers_list}" STREQUAL "x")
359 string(REPLACE ";" "," _conan_compilers_list "${_conan_compilers_list}")
360 string(APPEND profile "tools.build:compiler_executables={${_conan_compilers_list}}\n")
361 endif()
362 unset(_conan_c_compiler)
363 unset(_conan_cpp_compiler)
364 unset(_conan_rc_compiler)
365 unset(_conan_compilers_list)
366 endmacro()
367
368
369 function(detect_host_profile output_file)
370 detect_os(os os_api_level os_sdk os_subsystem os_version)
371 detect_arch(arch)
372 detect_compiler(compiler compiler_version compiler_runtime compiler_runtime_type)
373 detect_cxx_standard(compiler_cppstd)
374 detect_lib_cxx(compiler_libcxx)
375 detect_build_type(build_type)
376
377 set(profile "")
378 string(APPEND profile "[settings]\n")
379 if(arch)
380 string(APPEND profile arch=${arch} "\n")
381 endif()
382 if(os)
383 string(APPEND profile os=${os} "\n")
384 endif()
385 if(os_api_level)
386 string(APPEND profile os.api_level=${os_api_level} "\n")
387 endif()
388 if(os_version)
389 string(APPEND profile os.version=${os_version} "\n")
390 endif()
391 if(os_sdk)
392 string(APPEND profile os.sdk=${os_sdk} "\n")
393 endif()
394 if(os_subsystem)
395 string(APPEND profile os.subsystem=${os_subsystem} "\n")
396 endif()
397 if(compiler)
398 string(APPEND profile compiler=${compiler} "\n")
399 endif()
400 if(compiler_version)
401 string(APPEND profile compiler.version=${compiler_version} "\n")
402 endif()
403 if(compiler_runtime)
404 string(APPEND profile compiler.runtime=${compiler_runtime} "\n")
405 endif()
406 if(compiler_runtime_type)
407 string(APPEND profile compiler.runtime_type=${compiler_runtime_type} "\n")
408 endif()
409 if(compiler_cppstd)
410 string(APPEND profile compiler.cppstd=${compiler_cppstd} "\n")
411 endif()
412 if(compiler_libcxx)
413 string(APPEND profile compiler.libcxx=${compiler_libcxx} "\n")
414 endif()
415 if(build_type)
416 string(APPEND profile "build_type=${build_type}\n")
417 endif()
418
419 if(NOT DEFINED output_file)
420 set(file_name "${CMAKE_BINARY_DIR}/profile")
421 else()
422 set(file_name ${output_file})
423 endif()
424
425 string(APPEND profile "[conf]\n")
426 string(APPEND profile "tools.cmake.cmaketoolchain:generator=${CMAKE_GENERATOR}\n")
427
428 # propagate compilers via profile
429 append_compiler_executables_configuration()
430
431 if(os STREQUAL "Android")
432 string(APPEND profile "tools.android:ndk_path=${CMAKE_ANDROID_NDK}\n")
433 endif()
434
435 message(STATUS "CMake-Conan: Creating profile ${file_name}")
436 file(WRITE ${file_name} ${profile})
437 message(STATUS "CMake-Conan: Profile: \n${profile}")
438 endfunction()
439
440
441 function(conan_profile_detect_default)
442 message(STATUS "CMake-Conan: Checking if a default profile exists")
443 execute_process(COMMAND ${CONAN_COMMAND} profile path default
444 RESULT_VARIABLE return_code
445 OUTPUT_VARIABLE conan_stdout
446 ERROR_VARIABLE conan_stderr
447 ECHO_ERROR_VARIABLE # show the text output regardless
448 ECHO_OUTPUT_VARIABLE
449 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
450 if(NOT ${return_code} EQUAL "0")
451 message(STATUS "CMake-Conan: The default profile doesn't exist, detecting it.")
452 execute_process(COMMAND ${CONAN_COMMAND} profile detect
453 RESULT_VARIABLE return_code
454 OUTPUT_VARIABLE conan_stdout
455 ERROR_VARIABLE conan_stderr
456 ECHO_ERROR_VARIABLE # show the text output regardless
457 ECHO_OUTPUT_VARIABLE
458 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
459 endif()
460 endfunction()
461
462
463 function(conan_install)
464 set(conan_output_folder ${CMAKE_BINARY_DIR}/conan)
465 # Invoke "conan install" with the provided arguments
466 set(conan_args ${conan_args} -of=${conan_output_folder})
467 message(STATUS "CMake-Conan: conan install ${CMAKE_SOURCE_DIR} ${conan_args} ${ARGN}")
468
469
470 # In case there was not a valid cmake executable in the PATH, we inject the
471 # same we used to invoke the provider to the PATH
472 if(DEFINED PATH_TO_CMAKE_BIN)
473 set(old_path $ENV{PATH})
474 set(ENV{PATH} "$ENV{PATH}:${PATH_TO_CMAKE_BIN}")
475 endif()
476
477 execute_process(COMMAND ${CONAN_COMMAND} install ${CMAKE_SOURCE_DIR} ${conan_args} ${ARGN} --format=json
478 RESULT_VARIABLE return_code
479 OUTPUT_VARIABLE conan_stdout
480 ERROR_VARIABLE conan_stderr
481 ECHO_ERROR_VARIABLE # show the text output regardless
482 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
483
484 if(DEFINED PATH_TO_CMAKE_BIN)
485 set(ENV{PATH} "${old_path}")
486 endif()
487
488 if(NOT "${return_code}" STREQUAL "0")
489 message(FATAL_ERROR "Conan install failed='${return_code}'")
490 endif()
491
492 # the files are generated in a folder that depends on the layout used, if
493 # one is specified, but we don't know a priori where this is.
494 # TODO: this can be made more robust if Conan can provide this in the json output
495 string(JSON conan_generators_folder GET "${conan_stdout}" graph nodes 0 generators_folder)
496 cmake_path(CONVERT ${conan_generators_folder} TO_CMAKE_PATH_LIST conan_generators_folder)
497
498 message(STATUS "CMake-Conan: CONAN_GENERATORS_FOLDER=${conan_generators_folder}")
499 set_property(GLOBAL PROPERTY CONAN_GENERATORS_FOLDER "${conan_generators_folder}")
500 # reconfigure on conanfile changes
501 string(JSON conanfile GET "${conan_stdout}" graph nodes 0 label)
502 message(STATUS "CMake-Conan: CONANFILE=${CMAKE_SOURCE_DIR}/${conanfile}")
503 set_property(DIRECTORY ${CMAKE_SOURCE_DIR} APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/${conanfile}")
504 # success
505 set_property(GLOBAL PROPERTY CONAN_INSTALL_SUCCESS TRUE)
506
507 endfunction()
508
509
510 function(conan_get_version conan_command conan_current_version)
511 execute_process(
512 COMMAND ${conan_command} --version
513 OUTPUT_VARIABLE conan_output
514 RESULT_VARIABLE conan_result
515 OUTPUT_STRIP_TRAILING_WHITESPACE
516 )
517 if(conan_result)
518 message(FATAL_ERROR "CMake-Conan: Error when trying to run Conan")
519 endif()
520
521 string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" conan_version ${conan_output})
522 set(${conan_current_version} ${conan_version} PARENT_SCOPE)
523 endfunction()
524
525
526 function(conan_version_check)
527 set(options )
528 set(one_value_args MINIMUM CURRENT)
529 set(multi_value_args )
530 cmake_parse_arguments(conan_version_check
531 "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN})
532
533 if(NOT conan_version_check_MINIMUM)
534 message(FATAL_ERROR "CMake-Conan: Required parameter MINIMUM not set!")
535 endif()
536 if(NOT conan_version_check_CURRENT)
537 message(FATAL_ERROR "CMake-Conan: Required parameter CURRENT not set!")
538 endif()
539
540 if(conan_version_check_CURRENT VERSION_LESS conan_version_check_MINIMUM)
541 message(FATAL_ERROR "CMake-Conan: Conan version must be ${conan_version_check_MINIMUM} or later")
542 endif()
543 endfunction()
544
545
546 macro(construct_profile_argument argument_variable profile_list)
547 set(${argument_variable} "")
548 if("${profile_list}" STREQUAL "CONAN_HOST_PROFILE")
549 set(_arg_flag "--profile:host=")
550 elseif("${profile_list}" STREQUAL "CONAN_BUILD_PROFILE")
551 set(_arg_flag "--profile:build=")
552 endif()
553
554 set(_profile_list "${${profile_list}}")
555 list(TRANSFORM _profile_list REPLACE "auto-cmake" "${CMAKE_BINARY_DIR}/conan_host_profile")
556 list(TRANSFORM _profile_list PREPEND ${_arg_flag})
557 set(${argument_variable} ${_profile_list})
558
559 unset(_arg_flag)
560 unset(_profile_list)
561 endmacro()
562
563
564 macro(conan_provide_dependency method package_name)
565 set_property(GLOBAL PROPERTY CONAN_PROVIDE_DEPENDENCY_INVOKED TRUE)
566 get_property(_conan_install_success GLOBAL PROPERTY CONAN_INSTALL_SUCCESS)
567 if(NOT _conan_install_success)
568 find_program(CONAN_COMMAND "conan" REQUIRED)
569 conan_get_version(${CONAN_COMMAND} CONAN_CURRENT_VERSION)
570 conan_version_check(MINIMUM ${CONAN_MINIMUM_VERSION} CURRENT ${CONAN_CURRENT_VERSION})
571 message(STATUS "CMake-Conan: first find_package() found. Installing dependencies with Conan")
572 if("default" IN_LIST CONAN_HOST_PROFILE OR "default" IN_LIST CONAN_BUILD_PROFILE)
573 conan_profile_detect_default()
574 endif()
575 if("auto-cmake" IN_LIST CONAN_HOST_PROFILE)
576 detect_host_profile(${CMAKE_BINARY_DIR}/conan_host_profile)
577 endif()
578 construct_profile_argument(_host_profile_flags CONAN_HOST_PROFILE)
579 construct_profile_argument(_build_profile_flags CONAN_BUILD_PROFILE)
580 if(EXISTS "${CMAKE_SOURCE_DIR}/conanfile.py")
581 file(READ "${CMAKE_SOURCE_DIR}/conanfile.py" outfile)
582 if(NOT "${outfile}" MATCHES ".*CMakeDeps.*")
583 message(WARNING "Cmake-conan: CMakeDeps generator was not defined in the conanfile")
584 endif()
585 set(generator "")
586 elseif (EXISTS "${CMAKE_SOURCE_DIR}/conanfile.txt")
587 file(READ "${CMAKE_SOURCE_DIR}/conanfile.txt" outfile)
588 if(NOT "${outfile}" MATCHES ".*CMakeDeps.*")
589 message(WARNING "Cmake-conan: CMakeDeps generator was not defined in the conanfile. "
590 "Please define the generator as it will be mandatory in the future")
591 endif()
592 set(generator "-g;CMakeDeps")
593 endif()
594 get_property(_multiconfig_generator GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
595 if(NOT _multiconfig_generator)
596 message(STATUS "CMake-Conan: Installing single configuration ${CMAKE_BUILD_TYPE}")
597 conan_install(${_host_profile_flags} ${_build_profile_flags} ${CONAN_INSTALL_ARGS} ${generator})
598 else()
599 message(STATUS "CMake-Conan: Installing both Debug and Release")
600 conan_install(${_host_profile_flags} ${_build_profile_flags} -s build_type=Release ${CONAN_INSTALL_ARGS} ${generator})
601 conan_install(${_host_profile_flags} ${_build_profile_flags} -s build_type=Debug ${CONAN_INSTALL_ARGS} ${generator})
602 endif()
603 unset(_host_profile_flags)
604 unset(_build_profile_flags)
605 unset(_multiconfig_generator)
606 unset(_conan_install_success)
607 else()
608 message(STATUS "CMake-Conan: find_package(${ARGV1}) found, 'conan install' already ran")
609 unset(_conan_install_success)
610 endif()
611
612 get_property(_conan_generators_folder GLOBAL PROPERTY CONAN_GENERATORS_FOLDER)
613
614 # Ensure that we consider Conan-provided packages ahead of any other,
615 # irrespective of other settings that modify the search order or search paths
616 # This follows the guidelines from the find_package documentation
617 # (https://cmake.org/cmake/help/latest/command/find_package.html):
618 # find_package (<PackageName> PATHS paths... NO_DEFAULT_PATH)
619 # find_package (<PackageName>)
620
621 # Filter out `REQUIRED` from the argument list, as the first call may fail
622 set(_find_args_${package_name} "${ARGN}")
623 list(REMOVE_ITEM _find_args_${package_name} "REQUIRED")
624 if(NOT "MODULE" IN_LIST _find_args_${package_name})
625 find_package(${package_name} ${_find_args_${package_name}} BYPASS_PROVIDER PATHS "${_conan_generators_folder}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
626 unset(_find_args_${package_name})
627 endif()
628
629 # Invoke find_package a second time - if the first call succeeded,
630 # this will simply reuse the result. If not, fall back to CMake default search
631 # behaviour, also allowing modules to be searched.
632 if(NOT ${package_name}_FOUND)
633 list(FIND CMAKE_MODULE_PATH "${_conan_generators_folder}" _index)
634 if(_index EQUAL -1)
635 list(PREPEND CMAKE_MODULE_PATH "${_conan_generators_folder}")
636 endif()
637 unset(_index)
638 find_package(${package_name} ${ARGN} BYPASS_PROVIDER)
639 list(REMOVE_ITEM CMAKE_MODULE_PATH "${_conan_generators_folder}")
640 endif()
641 endmacro()
642
643
644 cmake_language(
645 SET_DEPENDENCY_PROVIDER conan_provide_dependency
646 SUPPORTED_METHODS FIND_PACKAGE
647 )
648
649
650 macro(conan_provide_dependency_check)
651 set(_conan_provide_dependency_invoked FALSE)
652 get_property(_conan_provide_dependency_invoked GLOBAL PROPERTY CONAN_PROVIDE_DEPENDENCY_INVOKED)
653 if(NOT _conan_provide_dependency_invoked)
654 message(WARNING "Conan is correctly configured as dependency provider, "
655 "but Conan has not been invoked. Please add at least one "
656 "call to `find_package()`.")
657 if(DEFINED CONAN_COMMAND)
658 # supress warning in case `CONAN_COMMAND` was specified but unused.
659 set(_conan_command ${CONAN_COMMAND})
660 unset(_conan_command)
661 endif()
662 endif()
663 unset(_conan_provide_dependency_invoked)
664 endmacro()
665
666
667 # Add a deferred call at the end of processing the top-level directory
668 # to check if the dependency provider was invoked at all.
669 cmake_language(DEFER DIRECTORY "${CMAKE_SOURCE_DIR}" CALL conan_provide_dependency_check)
670
671 # Configurable variables for Conan profiles
672 set(CONAN_HOST_PROFILE "default;auto-cmake" CACHE STRING "Conan host profile")
673 set(CONAN_BUILD_PROFILE "default" CACHE STRING "Conan build profile")
674 set(CONAN_INSTALL_ARGS "--build=missing" CACHE STRING "Command line arguments for conan install")
675
676 find_program(_cmake_program NAMES cmake NO_PACKAGE_ROOT_PATH NO_CMAKE_PATH NO_CMAKE_ENVIRONMENT_PATH NO_CMAKE_SYSTEM_PATH NO_CMAKE_FIND_ROOT_PATH)
677 if(NOT _cmake_program)
678 get_filename_component(PATH_TO_CMAKE_BIN "${CMAKE_COMMAND}" DIRECTORY)
679 set(PATH_TO_CMAKE_BIN "${PATH_TO_CMAKE_BIN}" CACHE INTERNAL "Path where the CMake executable is")
680 endif()
681
682 cmake_policy(POP)