conan_provider.cmake 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. set(CONAN_MINIMUM_VERSION 2.0.5)
  23. # Create a new policy scope and set the minimum required cmake version so the
  24. # features behind a policy setting like if(... IN_LIST ...) behaves as expected
  25. # even if the parent project does not specify a minimum cmake version or a minimum
  26. # version less than this module requires (e.g. 3.0) before the first project() call.
  27. # (see: https://cmake.org/cmake/help/latest/variable/CMAKE_PROJECT_TOP_LEVEL_INCLUDES.html)
  28. #
  29. # The policy-affecting calls like cmake_policy(SET...) or `cmake_minimum_required` only
  30. # affects the current policy scope, i.e. between the PUSH and POP in this case.
  31. #
  32. # https://cmake.org/cmake/help/book/mastering-cmake/chapter/Policies.html#the-policy-stack
  33. cmake_policy(PUSH)
  34. cmake_minimum_required(VERSION 3.24)
  35. function(detect_os OS OS_API_LEVEL OS_SDK OS_SUBSYSTEM OS_VERSION)
  36. # it could be cross compilation
  37. message(STATUS "CMake-Conan: cmake_system_name=${CMAKE_SYSTEM_NAME}")
  38. if(CMAKE_SYSTEM_NAME AND NOT CMAKE_SYSTEM_NAME STREQUAL "Generic")
  39. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  40. set(${OS} Macos PARENT_SCOPE)
  41. elseif(CMAKE_SYSTEM_NAME STREQUAL "QNX")
  42. set(${OS} Neutrino PARENT_SCOPE)
  43. elseif(CMAKE_SYSTEM_NAME STREQUAL "CYGWIN")
  44. set(${OS} Windows PARENT_SCOPE)
  45. set(${OS_SUBSYSTEM} cygwin PARENT_SCOPE)
  46. elseif(CMAKE_SYSTEM_NAME MATCHES "^MSYS")
  47. set(${OS} Windows PARENT_SCOPE)
  48. set(${OS_SUBSYSTEM} msys2 PARENT_SCOPE)
  49. else()
  50. set(${OS} ${CMAKE_SYSTEM_NAME} PARENT_SCOPE)
  51. endif()
  52. if(CMAKE_SYSTEM_NAME STREQUAL "Android")
  53. if(DEFINED ANDROID_PLATFORM)
  54. string(REGEX MATCH "[0-9]+" _OS_API_LEVEL ${ANDROID_PLATFORM})
  55. elseif(DEFINED CMAKE_SYSTEM_VERSION)
  56. set(_OS_API_LEVEL ${CMAKE_SYSTEM_VERSION})
  57. endif()
  58. message(STATUS "CMake-Conan: android api level=${_OS_API_LEVEL}")
  59. set(${OS_API_LEVEL} ${_OS_API_LEVEL} PARENT_SCOPE)
  60. endif()
  61. if(CMAKE_SYSTEM_NAME MATCHES "Darwin|iOS|tvOS|watchOS")
  62. # CMAKE_OSX_SYSROOT contains the full path to the SDK for MakeFile/Ninja
  63. # generators, but just has the original input string for Xcode.
  64. if(NOT IS_DIRECTORY ${CMAKE_OSX_SYSROOT})
  65. set(_OS_SDK ${CMAKE_OSX_SYSROOT})
  66. else()
  67. if(CMAKE_OSX_SYSROOT MATCHES Simulator)
  68. set(apple_platform_suffix simulator)
  69. else()
  70. set(apple_platform_suffix os)
  71. endif()
  72. if(CMAKE_OSX_SYSROOT MATCHES AppleTV)
  73. set(_OS_SDK "appletv${apple_platform_suffix}")
  74. elseif(CMAKE_OSX_SYSROOT MATCHES iPhone)
  75. set(_OS_SDK "iphone${apple_platform_suffix}")
  76. elseif(CMAKE_OSX_SYSROOT MATCHES Watch)
  77. set(_OS_SDK "watch${apple_platform_suffix}")
  78. endif()
  79. endif()
  80. if(DEFINED _OS_SDK)
  81. message(STATUS "CMake-Conan: cmake_osx_sysroot=${CMAKE_OSX_SYSROOT}")
  82. set(${OS_SDK} ${_OS_SDK} PARENT_SCOPE)
  83. endif()
  84. if(DEFINED CMAKE_OSX_DEPLOYMENT_TARGET)
  85. message(STATUS "CMake-Conan: cmake_osx_deployment_target=${CMAKE_OSX_DEPLOYMENT_TARGET}")
  86. set(${OS_VERSION} ${CMAKE_OSX_DEPLOYMENT_TARGET} PARENT_SCOPE)
  87. endif()
  88. endif()
  89. endif()
  90. endfunction()
  91. function(detect_arch ARCH)
  92. # CMAKE_OSX_ARCHITECTURES can contain multiple architectures, but Conan only supports one.
  93. # Therefore this code only finds one. If the recipes support multiple architectures, the
  94. # build will work. Otherwise, there will be a linker error for the missing architecture(s).
  95. if(DEFINED CMAKE_OSX_ARCHITECTURES)
  96. string(REPLACE " " ";" apple_arch_list "${CMAKE_OSX_ARCHITECTURES}")
  97. list(LENGTH apple_arch_list apple_arch_count)
  98. if(apple_arch_count GREATER 1)
  99. message(WARNING "CMake-Conan: Multiple architectures detected, this will only work if Conan recipe(s) produce fat binaries.")
  100. endif()
  101. endif()
  102. if(CMAKE_SYSTEM_NAME MATCHES "Darwin|iOS|tvOS|watchOS" AND NOT CMAKE_OSX_ARCHITECTURES STREQUAL "")
  103. set(host_arch ${CMAKE_OSX_ARCHITECTURES})
  104. elseif(MSVC)
  105. set(host_arch ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID})
  106. else()
  107. set(host_arch ${CMAKE_SYSTEM_PROCESSOR})
  108. endif()
  109. if(host_arch MATCHES "aarch64|arm64|ARM64")
  110. set(_ARCH armv8)
  111. elseif(host_arch MATCHES "armv7|armv7-a|armv7l|ARMV7")
  112. set(_ARCH armv7)
  113. elseif(host_arch MATCHES armv7s)
  114. set(_ARCH armv7s)
  115. elseif(host_arch MATCHES "i686|i386|X86")
  116. set(_ARCH x86)
  117. elseif(host_arch MATCHES "AMD64|amd64|x86_64|x64")
  118. set(_ARCH x86_64)
  119. endif()
  120. message(STATUS "CMake-Conan: cmake_system_processor=${_ARCH}")
  121. set(${ARCH} ${_ARCH} PARENT_SCOPE)
  122. endfunction()
  123. function(detect_cxx_standard CXX_STANDARD)
  124. set(${CXX_STANDARD} ${CMAKE_CXX_STANDARD} PARENT_SCOPE)
  125. if(CMAKE_CXX_EXTENSIONS)
  126. set(${CXX_STANDARD} "gnu${CMAKE_CXX_STANDARD}" PARENT_SCOPE)
  127. endif()
  128. endfunction()
  129. macro(detect_gnu_libstdcxx)
  130. # _CONAN_IS_GNU_LIBSTDCXX true if GNU libstdc++
  131. check_cxx_source_compiles("
  132. #include <cstddef>
  133. #if !defined(__GLIBCXX__) && !defined(__GLIBCPP__)
  134. static_assert(false);
  135. #endif
  136. int main(){}" _CONAN_IS_GNU_LIBSTDCXX)
  137. # _CONAN_GNU_LIBSTDCXX_IS_CXX11_ABI true if C++11 ABI
  138. check_cxx_source_compiles("
  139. #include <string>
  140. static_assert(sizeof(std::string) != sizeof(void*), \"using libstdc++\");
  141. int main () {}" _CONAN_GNU_LIBSTDCXX_IS_CXX11_ABI)
  142. set(_CONAN_GNU_LIBSTDCXX_SUFFIX "")
  143. if(_CONAN_GNU_LIBSTDCXX_IS_CXX11_ABI)
  144. set(_CONAN_GNU_LIBSTDCXX_SUFFIX "11")
  145. endif()
  146. unset (_CONAN_GNU_LIBSTDCXX_IS_CXX11_ABI)
  147. endmacro()
  148. macro(detect_libcxx)
  149. # _CONAN_IS_LIBCXX true if LLVM libc++
  150. check_cxx_source_compiles("
  151. #include <cstddef>
  152. #if !defined(_LIBCPP_VERSION)
  153. static_assert(false);
  154. #endif
  155. int main(){}" _CONAN_IS_LIBCXX)
  156. endmacro()
  157. function(detect_lib_cxx LIB_CXX)
  158. if(CMAKE_SYSTEM_NAME STREQUAL "Android")
  159. message(STATUS "CMake-Conan: android_stl=${CMAKE_ANDROID_STL_TYPE}")
  160. set(${LIB_CXX} ${CMAKE_ANDROID_STL_TYPE} PARENT_SCOPE)
  161. return()
  162. endif()
  163. include(CheckCXXSourceCompiles)
  164. if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  165. detect_gnu_libstdcxx()
  166. set(${LIB_CXX} "libstdc++${_CONAN_GNU_LIBSTDCXX_SUFFIX}" PARENT_SCOPE)
  167. elseif(CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
  168. set(${LIB_CXX} "libc++" PARENT_SCOPE)
  169. elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
  170. # Check for libc++
  171. detect_libcxx()
  172. if(_CONAN_IS_LIBCXX)
  173. set(${LIB_CXX} "libc++" PARENT_SCOPE)
  174. return()
  175. endif()
  176. # Check for libstdc++
  177. detect_gnu_libstdcxx()
  178. if(_CONAN_IS_GNU_LIBSTDCXX)
  179. set(${LIB_CXX} "libstdc++${_CONAN_GNU_LIBSTDCXX_SUFFIX}" PARENT_SCOPE)
  180. return()
  181. endif()
  182. # TODO: it would be an error if we reach this point
  183. elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  184. # Do nothing - compiler.runtime and compiler.runtime_type
  185. # should be handled separately: https://github.com/conan-io/cmake-conan/pull/516
  186. return()
  187. else()
  188. # TODO: unable to determine, ask user to provide a full profile file instead
  189. endif()
  190. endfunction()
  191. function(detect_compiler COMPILER COMPILER_VERSION COMPILER_RUNTIME COMPILER_RUNTIME_TYPE)
  192. if(DEFINED CMAKE_CXX_COMPILER_ID)
  193. set(_COMPILER ${CMAKE_CXX_COMPILER_ID})
  194. set(_COMPILER_VERSION ${CMAKE_CXX_COMPILER_VERSION})
  195. else()
  196. if(NOT DEFINED CMAKE_C_COMPILER_ID)
  197. message(FATAL_ERROR "C or C++ compiler not defined")
  198. endif()
  199. set(_COMPILER ${CMAKE_C_COMPILER_ID})
  200. set(_COMPILER_VERSION ${CMAKE_C_COMPILER_VERSION})
  201. endif()
  202. message(STATUS "CMake-Conan: CMake compiler=${_COMPILER}")
  203. message(STATUS "CMake-Conan: CMake compiler version=${_COMPILER_VERSION}")
  204. if(_COMPILER MATCHES MSVC)
  205. set(_COMPILER "msvc")
  206. string(SUBSTRING ${MSVC_VERSION} 0 3 _COMPILER_VERSION)
  207. # Configure compiler.runtime and compiler.runtime_type settings for MSVC
  208. if(CMAKE_MSVC_RUNTIME_LIBRARY)
  209. set(_msvc_runtime_library ${CMAKE_MSVC_RUNTIME_LIBRARY})
  210. else()
  211. set(_msvc_runtime_library MultiThreaded$<$<CONFIG:Debug>:Debug>DLL) # default value documented by CMake
  212. endif()
  213. set(_KNOWN_MSVC_RUNTIME_VALUES "")
  214. list(APPEND _KNOWN_MSVC_RUNTIME_VALUES MultiThreaded MultiThreadedDLL)
  215. list(APPEND _KNOWN_MSVC_RUNTIME_VALUES MultiThreadedDebug MultiThreadedDebugDLL)
  216. list(APPEND _KNOWN_MSVC_RUNTIME_VALUES MultiThreaded$<$<CONFIG:Debug>:Debug> MultiThreaded$<$<CONFIG:Debug>:Debug>DLL)
  217. # only accept the 6 possible values, otherwise we don't don't know to map this
  218. if(NOT _msvc_runtime_library IN_LIST _KNOWN_MSVC_RUNTIME_VALUES)
  219. message(FATAL_ERROR "CMake-Conan: unable to map MSVC runtime: ${_msvc_runtime_library} to Conan settings")
  220. endif()
  221. # Runtime is "dynamic" in all cases if it ends in DLL
  222. if(_msvc_runtime_library MATCHES ".*DLL$")
  223. set(_COMPILER_RUNTIME "dynamic")
  224. else()
  225. set(_COMPILER_RUNTIME "static")
  226. endif()
  227. message(STATUS "CMake-Conan: CMake compiler.runtime=${_COMPILER_RUNTIME}")
  228. # Only define compiler.runtime_type when explicitly requested
  229. # If a generator expression is used, let Conan handle it conditional on build_type
  230. if(NOT _msvc_runtime_library MATCHES "<CONFIG:Debug>:Debug>")
  231. if(_msvc_runtime_library MATCHES "Debug")
  232. set(_COMPILER_RUNTIME_TYPE "Debug")
  233. else()
  234. set(_COMPILER_RUNTIME_TYPE "Release")
  235. endif()
  236. message(STATUS "CMake-Conan: CMake compiler.runtime_type=${_COMPILER_RUNTIME_TYPE}")
  237. endif()
  238. unset(_KNOWN_MSVC_RUNTIME_VALUES)
  239. elseif(_COMPILER MATCHES AppleClang)
  240. set(_COMPILER "apple-clang")
  241. string(REPLACE "." ";" VERSION_LIST ${CMAKE_CXX_COMPILER_VERSION})
  242. list(GET VERSION_LIST 0 _COMPILER_VERSION)
  243. elseif(_COMPILER MATCHES Clang)
  244. set(_COMPILER "clang")
  245. string(REPLACE "." ";" VERSION_LIST ${CMAKE_CXX_COMPILER_VERSION})
  246. list(GET VERSION_LIST 0 _COMPILER_VERSION)
  247. elseif(_COMPILER MATCHES GNU)
  248. set(_COMPILER "gcc")
  249. string(REPLACE "." ";" VERSION_LIST ${CMAKE_CXX_COMPILER_VERSION})
  250. list(GET VERSION_LIST 0 _COMPILER_VERSION)
  251. endif()
  252. message(STATUS "CMake-Conan: [settings] compiler=${_COMPILER}")
  253. message(STATUS "CMake-Conan: [settings] compiler.version=${_COMPILER_VERSION}")
  254. if (_COMPILER_RUNTIME)
  255. message(STATUS "CMake-Conan: [settings] compiler.runtime=${_COMPILER_RUNTIME}")
  256. endif()
  257. if (_COMPILER_RUNTIME_TYPE)
  258. message(STATUS "CMake-Conan: [settings] compiler.runtime_type=${_COMPILER_RUNTIME_TYPE}")
  259. endif()
  260. set(${COMPILER} ${_COMPILER} PARENT_SCOPE)
  261. set(${COMPILER_VERSION} ${_COMPILER_VERSION} PARENT_SCOPE)
  262. set(${COMPILER_RUNTIME} ${_COMPILER_RUNTIME} PARENT_SCOPE)
  263. set(${COMPILER_RUNTIME_TYPE} ${_COMPILER_RUNTIME_TYPE} PARENT_SCOPE)
  264. endfunction()
  265. function(detect_build_type BUILD_TYPE)
  266. get_property(_MULTICONFIG_GENERATOR GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  267. if(NOT _MULTICONFIG_GENERATOR)
  268. # Only set when we know we are in a single-configuration generator
  269. # Note: we may want to fail early if `CMAKE_BUILD_TYPE` is not defined
  270. set(${BUILD_TYPE} ${CMAKE_BUILD_TYPE} PARENT_SCOPE)
  271. endif()
  272. endfunction()
  273. macro(set_conan_compiler_if_appleclang lang command output_variable)
  274. if(CMAKE_${lang}_COMPILER_ID STREQUAL "AppleClang")
  275. execute_process(COMMAND xcrun --find ${command}
  276. OUTPUT_VARIABLE _xcrun_out OUTPUT_STRIP_TRAILING_WHITESPACE)
  277. cmake_path(GET _xcrun_out PARENT_PATH _xcrun_toolchain_path)
  278. cmake_path(GET CMAKE_${lang}_COMPILER PARENT_PATH _compiler_parent_path)
  279. if ("${_xcrun_toolchain_path}" STREQUAL "${_compiler_parent_path}")
  280. set(${output_variable} "")
  281. endif()
  282. unset(_xcrun_out)
  283. unset(_xcrun_toolchain_path)
  284. unset(_compiler_parent_path)
  285. endif()
  286. endmacro()
  287. macro(append_compiler_executables_configuration)
  288. set(_conan_c_compiler "")
  289. set(_conan_cpp_compiler "")
  290. if(CMAKE_C_COMPILER)
  291. set(_conan_c_compiler "\"c\":\"${CMAKE_C_COMPILER}\",")
  292. set_conan_compiler_if_appleclang(C cc _conan_c_compiler)
  293. else()
  294. message(WARNING "CMake-Conan: The C compiler is not defined. "
  295. "Please define CMAKE_C_COMPILER or enable the C language.")
  296. endif()
  297. if(CMAKE_CXX_COMPILER)
  298. set(_conan_cpp_compiler "\"cpp\":\"${CMAKE_CXX_COMPILER}\"")
  299. set_conan_compiler_if_appleclang(CXX c++ _conan_cpp_compiler)
  300. else()
  301. message(WARNING "CMake-Conan: The C++ compiler is not defined. "
  302. "Please define CMAKE_CXX_COMPILER or enable the C++ language.")
  303. endif()
  304. if(NOT "x${_conan_c_compiler}${_conan_cpp_compiler}" STREQUAL "x")
  305. string(APPEND PROFILE "tools.build:compiler_executables={${_conan_c_compiler}${_conan_cpp_compiler}}\n")
  306. endif()
  307. unset(_conan_c_compiler)
  308. unset(_conan_cpp_compiler)
  309. endmacro()
  310. function(detect_host_profile output_file)
  311. detect_os(MYOS MYOS_API_LEVEL MYOS_SDK MYOS_SUBSYSTEM MYOS_VERSION)
  312. detect_arch(MYARCH)
  313. detect_compiler(MYCOMPILER MYCOMPILER_VERSION MYCOMPILER_RUNTIME MYCOMPILER_RUNTIME_TYPE)
  314. detect_cxx_standard(MYCXX_STANDARD)
  315. detect_lib_cxx(MYLIB_CXX)
  316. detect_build_type(MYBUILD_TYPE)
  317. set(PROFILE "")
  318. string(APPEND PROFILE "[settings]\n")
  319. if(MYARCH)
  320. string(APPEND PROFILE arch=${MYARCH} "\n")
  321. endif()
  322. if(MYOS)
  323. string(APPEND PROFILE os=${MYOS} "\n")
  324. endif()
  325. if(MYOS_API_LEVEL)
  326. string(APPEND PROFILE os.api_level=${MYOS_API_LEVEL} "\n")
  327. endif()
  328. if(MYOS_VERSION)
  329. string(APPEND PROFILE os.version=${MYOS_VERSION} "\n")
  330. endif()
  331. if(MYOS_SDK)
  332. string(APPEND PROFILE os.sdk=${MYOS_SDK} "\n")
  333. endif()
  334. if(MYOS_SUBSYSTEM)
  335. string(APPEND PROFILE os.subsystem=${MYOS_SUBSYSTEM} "\n")
  336. endif()
  337. if(MYCOMPILER)
  338. string(APPEND PROFILE compiler=${MYCOMPILER} "\n")
  339. endif()
  340. if(MYCOMPILER_VERSION)
  341. string(APPEND PROFILE compiler.version=${MYCOMPILER_VERSION} "\n")
  342. endif()
  343. if(MYCOMPILER_RUNTIME)
  344. string(APPEND PROFILE compiler.runtime=${MYCOMPILER_RUNTIME} "\n")
  345. endif()
  346. if(MYCOMPILER_RUNTIME_TYPE)
  347. string(APPEND PROFILE compiler.runtime_type=${MYCOMPILER_RUNTIME_TYPE} "\n")
  348. endif()
  349. if(MYCXX_STANDARD)
  350. string(APPEND PROFILE compiler.cppstd=${MYCXX_STANDARD} "\n")
  351. endif()
  352. if(MYLIB_CXX)
  353. string(APPEND PROFILE compiler.libcxx=${MYLIB_CXX} "\n")
  354. endif()
  355. if(MYBUILD_TYPE)
  356. string(APPEND PROFILE "build_type=${MYBUILD_TYPE}\n")
  357. endif()
  358. if(NOT DEFINED output_file)
  359. set(_FN "${CMAKE_BINARY_DIR}/profile")
  360. else()
  361. set(_FN ${output_file})
  362. endif()
  363. string(APPEND PROFILE "[conf]\n")
  364. string(APPEND PROFILE "tools.cmake.cmaketoolchain:generator=${CMAKE_GENERATOR}\n")
  365. # propagate compilers via profile
  366. append_compiler_executables_configuration()
  367. if(MYOS STREQUAL "Android")
  368. string(APPEND PROFILE "tools.android:ndk_path=${CMAKE_ANDROID_NDK}\n")
  369. endif()
  370. message(STATUS "CMake-Conan: Creating profile ${_FN}")
  371. file(WRITE ${_FN} ${PROFILE})
  372. message(STATUS "CMake-Conan: Profile: \n${PROFILE}")
  373. endfunction()
  374. function(conan_profile_detect_default)
  375. message(STATUS "CMake-Conan: Checking if a default profile exists")
  376. execute_process(COMMAND ${CONAN_COMMAND} profile path default
  377. RESULT_VARIABLE return_code
  378. OUTPUT_VARIABLE conan_stdout
  379. ERROR_VARIABLE conan_stderr
  380. ECHO_ERROR_VARIABLE # show the text output regardless
  381. ECHO_OUTPUT_VARIABLE
  382. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  383. if(NOT ${return_code} EQUAL "0")
  384. message(STATUS "CMake-Conan: The default profile doesn't exist, detecting it.")
  385. execute_process(COMMAND ${CONAN_COMMAND} profile detect
  386. RESULT_VARIABLE return_code
  387. OUTPUT_VARIABLE conan_stdout
  388. ERROR_VARIABLE conan_stderr
  389. ECHO_ERROR_VARIABLE # show the text output regardless
  390. ECHO_OUTPUT_VARIABLE
  391. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  392. endif()
  393. endfunction()
  394. function(conan_install)
  395. cmake_parse_arguments(ARGS CONAN_ARGS ${ARGN})
  396. set(CONAN_OUTPUT_FOLDER ${CMAKE_BINARY_DIR}/conan)
  397. # Invoke "conan install" with the provided arguments
  398. set(CONAN_ARGS ${CONAN_ARGS} -of=${CONAN_OUTPUT_FOLDER})
  399. message(STATUS "CMake-Conan: conan install ${CMAKE_SOURCE_DIR} ${CONAN_ARGS} ${ARGN}")
  400. # In case there was not a valid cmake executable in the PATH, we inject the
  401. # same we used to invoke the provider to the PATH
  402. if(DEFINED PATH_TO_CMAKE_BIN)
  403. set(_OLD_PATH $ENV{PATH})
  404. set(ENV{PATH} "$ENV{PATH}:${PATH_TO_CMAKE_BIN}")
  405. endif()
  406. execute_process(COMMAND ${CONAN_COMMAND} install ${CMAKE_SOURCE_DIR} ${CONAN_ARGS} ${ARGN} --format=json
  407. RESULT_VARIABLE return_code
  408. OUTPUT_VARIABLE conan_stdout
  409. ERROR_VARIABLE conan_stderr
  410. ECHO_ERROR_VARIABLE # show the text output regardless
  411. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  412. if(DEFINED PATH_TO_CMAKE_BIN)
  413. set(ENV{PATH} "${_OLD_PATH}")
  414. endif()
  415. if(NOT "${return_code}" STREQUAL "0")
  416. message(FATAL_ERROR "Conan install failed='${return_code}'")
  417. endif()
  418. # the files are generated in a folder that depends on the layout used, if
  419. # one is specified, but we don't know a priori where this is.
  420. # TODO: this can be made more robust if Conan can provide this in the json output
  421. string(JSON CONAN_GENERATORS_FOLDER GET ${conan_stdout} graph nodes 0 generators_folder)
  422. cmake_path(CONVERT ${CONAN_GENERATORS_FOLDER} TO_CMAKE_PATH_LIST CONAN_GENERATORS_FOLDER)
  423. # message("conan stdout: ${conan_stdout}")
  424. message(STATUS "CMake-Conan: CONAN_GENERATORS_FOLDER=${CONAN_GENERATORS_FOLDER}")
  425. set_property(GLOBAL PROPERTY CONAN_GENERATORS_FOLDER "${CONAN_GENERATORS_FOLDER}")
  426. # reconfigure on conanfile changes
  427. string(JSON CONANFILE GET ${conan_stdout} graph nodes 0 label)
  428. message(STATUS "CMake-Conan: CONANFILE=${CMAKE_SOURCE_DIR}/${CONANFILE}")
  429. set_property(DIRECTORY ${CMAKE_SOURCE_DIR} APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/${CONANFILE}")
  430. # success
  431. set_property(GLOBAL PROPERTY CONAN_INSTALL_SUCCESS TRUE)
  432. endfunction()
  433. function(conan_get_version conan_command conan_current_version)
  434. execute_process(
  435. COMMAND ${conan_command} --version
  436. OUTPUT_VARIABLE conan_output
  437. RESULT_VARIABLE conan_result
  438. OUTPUT_STRIP_TRAILING_WHITESPACE
  439. )
  440. if(conan_result)
  441. message(FATAL_ERROR "CMake-Conan: Error when trying to run Conan")
  442. endif()
  443. string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" conan_version ${conan_output})
  444. set(${conan_current_version} ${conan_version} PARENT_SCOPE)
  445. endfunction()
  446. function(conan_version_check)
  447. set(options )
  448. set(oneValueArgs MINIMUM CURRENT)
  449. set(multiValueArgs )
  450. cmake_parse_arguments(CONAN_VERSION_CHECK
  451. "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  452. if(NOT CONAN_VERSION_CHECK_MINIMUM)
  453. message(FATAL_ERROR "CMake-Conan: Required parameter MINIMUM not set!")
  454. endif()
  455. if(NOT CONAN_VERSION_CHECK_CURRENT)
  456. message(FATAL_ERROR "CMake-Conan: Required parameter CURRENT not set!")
  457. endif()
  458. if(CONAN_VERSION_CHECK_CURRENT VERSION_LESS CONAN_VERSION_CHECK_MINIMUM)
  459. message(FATAL_ERROR "CMake-Conan: Conan version must be ${CONAN_VERSION_CHECK_MINIMUM} or later")
  460. endif()
  461. endfunction()
  462. macro(construct_profile_argument argument_variable profile_list)
  463. set(${argument_variable} "")
  464. if("${profile_list}" STREQUAL "CONAN_HOST_PROFILE")
  465. set(_arg_flag "--profile:host=")
  466. elseif("${profile_list}" STREQUAL "CONAN_BUILD_PROFILE")
  467. set(_arg_flag "--profile:build=")
  468. endif()
  469. set(_profile_list "${${profile_list}}")
  470. list(TRANSFORM _profile_list REPLACE "auto-cmake" "${CMAKE_BINARY_DIR}/conan_host_profile")
  471. list(TRANSFORM _profile_list PREPEND ${_arg_flag})
  472. set(${argument_variable} ${_profile_list})
  473. unset(_arg_flag)
  474. unset(_profile_list)
  475. endmacro()
  476. macro(conan_provide_dependency method package_name)
  477. set_property(GLOBAL PROPERTY CONAN_PROVIDE_DEPENDENCY_INVOKED TRUE)
  478. get_property(_conan_install_success GLOBAL PROPERTY CONAN_INSTALL_SUCCESS)
  479. if(NOT _conan_install_success)
  480. find_program(CONAN_COMMAND "conan" REQUIRED)
  481. conan_get_version(${CONAN_COMMAND} CONAN_CURRENT_VERSION)
  482. conan_version_check(MINIMUM ${CONAN_MINIMUM_VERSION} CURRENT ${CONAN_CURRENT_VERSION})
  483. message(STATUS "CMake-Conan: first find_package() found. Installing dependencies with Conan")
  484. if("default" IN_LIST CONAN_HOST_PROFILE OR "default" IN_LIST CONAN_BUILD_PROFILE)
  485. conan_profile_detect_default()
  486. endif()
  487. if("auto-cmake" IN_LIST CONAN_HOST_PROFILE)
  488. detect_host_profile(${CMAKE_BINARY_DIR}/conan_host_profile)
  489. endif()
  490. construct_profile_argument(_host_profile_flags CONAN_HOST_PROFILE)
  491. construct_profile_argument(_build_profile_flags CONAN_BUILD_PROFILE)
  492. if(EXISTS "${CMAKE_SOURCE_DIR}/conanfile.py")
  493. file(READ "${CMAKE_SOURCE_DIR}/conanfile.py" outfile)
  494. if(NOT "${outfile}" MATCHES ".*CMakeDeps.*")
  495. message(WARNING "Cmake-conan: CMakeDeps generator was not defined in the conanfile")
  496. endif()
  497. set(generator "")
  498. elseif (EXISTS "${CMAKE_SOURCE_DIR}/conanfile.txt")
  499. file(READ "${CMAKE_SOURCE_DIR}/conanfile.txt" outfile)
  500. if(NOT "${outfile}" MATCHES ".*CMakeDeps.*")
  501. message(WARNING "Cmake-conan: CMakeDeps generator was not defined in the conanfile. "
  502. "Please define the generator as it will be mandatory in the future")
  503. endif()
  504. set(generator "-g;CMakeDeps")
  505. endif()
  506. get_property(_multiconfig_generator GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  507. if(NOT _multiconfig_generator)
  508. message(STATUS "CMake-Conan: Installing single configuration ${CMAKE_BUILD_TYPE}")
  509. conan_install(${_host_profile_flags} ${_build_profile_flags} ${CONAN_INSTALL_ARGS} ${generator})
  510. else()
  511. message(STATUS "CMake-Conan: Installing both Debug and Release")
  512. conan_install(${_host_profile_flags} ${_build_profile_flags} -s build_type=Release ${CONAN_INSTALL_ARGS} ${generator})
  513. conan_install(${_host_profile_flags} ${_build_profile_flags} -s build_type=Debug ${CONAN_INSTALL_ARGS} ${generator})
  514. endif()
  515. unset(_host_profile_flags)
  516. unset(_build_profile_flags)
  517. unset(_multiconfig_generator)
  518. unset(_conan_install_success)
  519. else()
  520. message(STATUS "CMake-Conan: find_package(${ARGV1}) found, 'conan install' already ran")
  521. unset(_conan_install_success)
  522. endif()
  523. get_property(_conan_generators_folder GLOBAL PROPERTY CONAN_GENERATORS_FOLDER)
  524. # Ensure that we consider Conan-provided packages ahead of any other,
  525. # irrespective of other settings that modify the search order or search paths
  526. # This follows the guidelines from the find_package documentation
  527. # (https://cmake.org/cmake/help/latest/command/find_package.html):
  528. # find_package (<PackageName> PATHS paths... NO_DEFAULT_PATH)
  529. # find_package (<PackageName>)
  530. # Filter out `REQUIRED` from the argument list, as the first call may fail
  531. set(_find_args_${package_name} "${ARGN}")
  532. list(REMOVE_ITEM _find_args_${package_name} "REQUIRED")
  533. if(NOT "MODULE" IN_LIST _find_args_${package_name})
  534. find_package(${package_name} ${_find_args_${package_name}} BYPASS_PROVIDER PATHS "${_conan_generators_folder}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
  535. unset(_find_args_${package_name})
  536. endif()
  537. # Invoke find_package a second time - if the first call succeeded,
  538. # this will simply reuse the result. If not, fall back to CMake default search
  539. # behaviour, also allowing modules to be searched.
  540. if(NOT ${package_name}_FOUND)
  541. list(FIND CMAKE_MODULE_PATH "${_conan_generators_folder}" _index)
  542. if(_index EQUAL -1)
  543. list(PREPEND CMAKE_MODULE_PATH "${_conan_generators_folder}")
  544. endif()
  545. unset(_index)
  546. find_package(${package_name} ${ARGN} BYPASS_PROVIDER)
  547. list(REMOVE_ITEM CMAKE_MODULE_PATH "${_conan_generators_folder}")
  548. endif()
  549. endmacro()
  550. cmake_language(
  551. SET_DEPENDENCY_PROVIDER conan_provide_dependency
  552. SUPPORTED_METHODS FIND_PACKAGE
  553. )
  554. macro(conan_provide_dependency_check)
  555. set(_CONAN_PROVIDE_DEPENDENCY_INVOKED FALSE)
  556. get_property(_CONAN_PROVIDE_DEPENDENCY_INVOKED GLOBAL PROPERTY CONAN_PROVIDE_DEPENDENCY_INVOKED)
  557. if(NOT _CONAN_PROVIDE_DEPENDENCY_INVOKED)
  558. message(WARNING "Conan is correctly configured as dependency provider, "
  559. "but Conan has not been invoked. Please add at least one "
  560. "call to `find_package()`.")
  561. if(DEFINED CONAN_COMMAND)
  562. # supress warning in case `CONAN_COMMAND` was specified but unused.
  563. set(_CONAN_COMMAND ${CONAN_COMMAND})
  564. unset(_CONAN_COMMAND)
  565. endif()
  566. endif()
  567. unset(_CONAN_PROVIDE_DEPENDENCY_INVOKED)
  568. endmacro()
  569. # Add a deferred call at the end of processing the top-level directory
  570. # to check if the dependency provider was invoked at all.
  571. cmake_language(DEFER DIRECTORY "${CMAKE_SOURCE_DIR}" CALL conan_provide_dependency_check)
  572. # Configurable variables for Conan profiles
  573. set(CONAN_HOST_PROFILE "default;auto-cmake" CACHE STRING "Conan host profile")
  574. set(CONAN_BUILD_PROFILE "default" CACHE STRING "Conan build profile")
  575. set(CONAN_INSTALL_ARGS "--build=missing" CACHE STRING "Command line arguments for conan install")
  576. 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)
  577. if(NOT _cmake_program)
  578. get_filename_component(PATH_TO_CMAKE_BIN "${CMAKE_COMMAND}" DIRECTORY)
  579. set(PATH_TO_CMAKE_BIN "${PATH_TO_CMAKE_BIN}" CACHE INTERNAL "Path where the CMake executable is")
  580. endif()
  581. cmake_policy(POP)