conan_support.cmake 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # https://github.com/conan-io/cmake-conan/blob/develop2/conan_support.cmake
  2. # commit: 3e088cd3e1d9d69e04b5250d565c1b8b55b0400b
  3. #
  4. # The MIT License (MIT)
  5. #
  6. # Copyright (c) 2019 JFrog
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining a copy
  9. # of this software and associated documentation files (the "Software"), to deal
  10. # in the Software without restriction, including without limitation the rights
  11. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. # copies of the Software, and to permit persons to whom the Software is
  13. # furnished to do so, subject to the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included in all
  16. # copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. # SOFTWARE.
  25. function(detect_os OS)
  26. # it could be cross compilation
  27. message(STATUS "Conan-cmake: cmake_system_name=${CMAKE_SYSTEM_NAME}")
  28. if(CMAKE_SYSTEM_NAME AND NOT CMAKE_SYSTEM_NAME STREQUAL "Generic")
  29. if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
  30. set(${OS} Macos PARENT_SCOPE)
  31. elseif(${CMAKE_SYSTEM_NAME} STREQUAL "QNX")
  32. set(${OS} Neutrino PARENT_SCOPE)
  33. else()
  34. set(${OS} ${CMAKE_SYSTEM_NAME} PARENT_SCOPE)
  35. endif()
  36. endif()
  37. endfunction()
  38. function(detect_cxx_standard CXX_STANDARD)
  39. set(${CXX_STANDARD} ${CMAKE_CXX_STANDARD} PARENT_SCOPE)
  40. if (CMAKE_CXX_EXTENSIONS)
  41. set(${CXX_STANDARD} "gnu${CMAKE_CXX_STANDARD}" PARENT_SCOPE)
  42. endif()
  43. endfunction()
  44. function(detect_compiler COMPILER COMPILER_VERSION)
  45. if(DEFINED CMAKE_CXX_COMPILER_ID)
  46. set(_COMPILER ${CMAKE_CXX_COMPILER_ID})
  47. set(_COMPILER_VERSION ${CMAKE_CXX_COMPILER_VERSION})
  48. else()
  49. if(NOT DEFINED CMAKE_C_COMPILER_ID)
  50. message(FATAL_ERROR "C or C++ compiler not defined")
  51. endif()
  52. set(_COMPILER ${CMAKE_C_COMPILER_ID})
  53. set(_COMPILER_VERSION ${CMAKE_C_COMPILER_VERSION})
  54. endif()
  55. message(STATUS "Conan-cmake: CMake compiler=${_COMPILER}")
  56. message(STATUS "Conan-cmake: CMake cmpiler version=${_COMPILER_VERSION}")
  57. if(_COMPILER MATCHES MSVC)
  58. set(_COMPILER "msvc")
  59. string(SUBSTRING ${MSVC_VERSION} 0 3 _COMPILER_VERSION)
  60. elseif(_COMPILER MATCHES AppleClang)
  61. set(_COMPILER "apple-clang")
  62. string(REPLACE "." ";" VERSION_LIST ${CMAKE_CXX_COMPILER_VERSION})
  63. list(GET VERSION_LIST 0 _COMPILER_VERSION)
  64. elseif(_COMPILER MATCHES Clang)
  65. set(_COMPILER "clang")
  66. string(REPLACE "." ";" VERSION_LIST ${CMAKE_CXX_COMPILER_VERSION})
  67. list(GET VERSION_LIST 0 _COMPILER_VERSION)
  68. elseif(_COMPILER MATCHES GNU)
  69. set(_COMPILER "gcc")
  70. string(REPLACE "." ";" VERSION_LIST ${CMAKE_CXX_COMPILER_VERSION})
  71. list(GET VERSION_LIST 0 _COMPILER_VERSION)
  72. endif()
  73. message(STATUS "Conan-cmake: [settings] compiler=${_COMPILER}")
  74. message(STATUS "Conan-cmake: [settings] compiler.version=${_COMPILER_VERSION}")
  75. set(${COMPILER} ${_COMPILER} PARENT_SCOPE)
  76. set(${COMPILER_VERSION} ${_COMPILER_VERSION} PARENT_SCOPE)
  77. endfunction()
  78. function(detect_build_type BUILD_TYPE)
  79. if(NOT CMAKE_CONFIGURATION_TYPES)
  80. # Only set when we know we are in a single-configuration generator
  81. # Note: we may want to fail early if `CMAKE_BUILD_TYPE` is not defined
  82. set(${BUILD_TYPE} ${CMAKE_BUILD_TYPE} PARENT_SCOPE)
  83. endif()
  84. endfunction()
  85. function(detect_host_profile output_file)
  86. detect_os(MYOS)
  87. detect_compiler(MYCOMPILER MYCOMPILER_VERSION)
  88. detect_cxx_standard(MYCXX_STANDARD)
  89. detect_build_type(MYBUILD_TYPE)
  90. set(PROFILE "")
  91. string(APPEND PROFILE "include(default)\n")
  92. string(APPEND PROFILE "[settings]\n")
  93. if(MYOS)
  94. string(APPEND PROFILE os=${MYOS} "\n")
  95. endif()
  96. if(MYCOMPILER)
  97. string(APPEND PROFILE compiler=${MYCOMPILER} "\n")
  98. endif()
  99. if(MYCOMPILER_VERSION)
  100. string(APPEND PROFILE compiler.version=${MYCOMPILER_VERSION} "\n")
  101. endif()
  102. if(MYCXX_STANDARD)
  103. string(APPEND PROFILE compiler.cppstd=${MYCXX_STANDARD} "\n")
  104. endif()
  105. if(MYBUILD_TYPE)
  106. string(APPEND PROFILE "build_type=${MYBUILD_TYPE}\n")
  107. endif()
  108. if(NOT DEFINED output_file)
  109. set(_FN "${CMAKE_BINARY_DIR}/profile")
  110. else()
  111. set(_FN ${output_file})
  112. endif()
  113. string(APPEND PROFILE "[conf]\n")
  114. string(APPEND PROFILE "tools.cmake.cmaketoolchain:generator=${CMAKE_GENERATOR}\n")
  115. message(STATUS "Conan-cmake: Creating profile ${_FN}")
  116. file(WRITE ${_FN} ${PROFILE})
  117. message(STATUS "Conan-cmake: Profile: \n${PROFILE}")
  118. endfunction()
  119. function(conan_profile_detect_default)
  120. message(STATUS "Conan-cmake: Checking if a default profile exists")
  121. execute_process(COMMAND conan profile path default
  122. RESULT_VARIABLE return_code
  123. OUTPUT_VARIABLE conan_stdout
  124. ERROR_VARIABLE conan_stderr
  125. ECHO_ERROR_VARIABLE # show the text output regardless
  126. ECHO_OUTPUT_VARIABLE
  127. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  128. if(NOT ${return_code} EQUAL "0")
  129. message(STATUS "Conan-cmake: The default profile doesn't exist, detecting it.")
  130. execute_process(COMMAND conan profile detect
  131. RESULT_VARIABLE return_code
  132. OUTPUT_VARIABLE conan_stdout
  133. ERROR_VARIABLE conan_stderr
  134. ECHO_ERROR_VARIABLE # show the text output regardless
  135. ECHO_OUTPUT_VARIABLE
  136. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  137. endif()
  138. endfunction()
  139. function(conan_install)
  140. cmake_parse_arguments(ARGS CONAN_ARGS ${ARGN})
  141. set(CONAN_OUTPUT_FOLDER ${CMAKE_BINARY_DIR}/conan)
  142. # Invoke "conan install" with the provided arguments
  143. set(CONAN_ARGS ${CONAN_ARGS} -of=${CONAN_OUTPUT_FOLDER})
  144. message(STATUS "CMake-conan: conan install ${CMAKE_SOURCE_DIR} ${CONAN_ARGS} ${ARGN}")
  145. execute_process(COMMAND conan install ${CMAKE_SOURCE_DIR} ${CONAN_ARGS} ${ARGN} --format=json
  146. RESULT_VARIABLE return_code
  147. OUTPUT_VARIABLE conan_stdout
  148. ERROR_VARIABLE conan_stderr
  149. ECHO_ERROR_VARIABLE # show the text output regardless
  150. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  151. if(NOT "${return_code}" STREQUAL "0")
  152. message(FATAL_ERROR "Conan install failed='${return_code}'")
  153. else()
  154. # the files are generated in a folder that depends on the layout used, if
  155. # one if specified, but we don't know a priori where this is.
  156. # TODO: this can be made more robust if Conan can provide this in the json output
  157. string(JSON CONAN_GENERATORS_FOLDER GET ${conan_stdout} graph nodes 0 generators_folder)
  158. # message("conan stdout: ${conan_stdout}")
  159. message(STATUS "CMake-conan: CONAN_GENERATORS_FOLDER=${CONAN_GENERATORS_FOLDER}")
  160. set(CONAN_GENERATORS_FOLDER "${CONAN_GENERATORS_FOLDER}" PARENT_SCOPE)
  161. set(CONAN_INSTALL_SUCCESS TRUE CACHE BOOL "Conan install has been invoked and was successful")
  162. endif()
  163. endfunction()
  164. function(conan_provide_dependency package_name)
  165. if(NOT CONAN_INSTALL_SUCCESS)
  166. message(STATUS "CMake-conan: first find_package() found. Installing dependencies with Conan")
  167. conan_profile_detect_default()
  168. detect_host_profile(${CMAKE_BINARY_DIR}/conan_host_profile)
  169. if(NOT CMAKE_CONFIGURATION_TYPES)
  170. message(STATUS "CMake-conan: Installing single configuration ${CMAKE_BUILD_TYPE}")
  171. conan_install(-pr ${CMAKE_BINARY_DIR}/conan_host_profile --build=missing -g CMakeDeps)
  172. else()
  173. message(STATUS "CMake-conan: Installing both Debug and Release")
  174. conan_install(-pr ${CMAKE_BINARY_DIR}/conan_host_profile -s build_type=Release --build=missing -g CMakeDeps)
  175. conan_install(-pr ${CMAKE_BINARY_DIR}/conan_host_profile -s build_type=Debug --build=missing -g CMakeDeps)
  176. endif()
  177. if (CONAN_INSTALL_SUCCESS)
  178. set(CONAN_GENERATORS_FOLDER "${CONAN_GENERATORS_FOLDER}" CACHE PATH "Conan generators folder")
  179. endif()
  180. else()
  181. message(STATUS "CMake-conan: find_package(${package_name}) found, 'conan install' aready ran")
  182. endif()
  183. if (CONAN_GENERATORS_FOLDER)
  184. list(PREPEND CMAKE_PREFIX_PATH "${CONAN_GENERATORS_FOLDER}")
  185. endif()
  186. find_package(${ARGN} BYPASS_PROVIDER)
  187. endfunction()