conan icon indicating copy to clipboard operation
conan copied to clipboard

[question] Include conan_provider.cmake inside of CMakeLists.txt

Open simon-rechermann opened this issue 1 year ago • 1 comments

Hi, is there a possibility to include the conan_provider.cmake script from inside of a CMakeLists.txt file like it was done with the conan.cmake file in conan v1, instead of passing -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=../conan_provider.cmake to the cmake CLI command like it is proposed in the README.md? In the example I tried to set CMAKE_PROJECT_TOP_LEVEL_INCLUDES and I tried to include the conan_provider.cmake file. Both did not work.

cmake_minimum_required(VERSION 3.24)

project(FormatOutput LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 14)

if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan_provider.cmake")
  message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
  file(DOWNLOAD "https://https://raw.githubusercontent.com/conan-io/cmake-conan/develop2/conan_provider.cmake"
                "${CMAKE_BINARY_DIR}/conan_provider.cmake"
                TLS_VERIFY ON)
endif()
# include(${CMAKE_BINARY_DIR}/conan_provider.cmake)
set(CMAKE_PROJECT_TOP_LEVEL_INCLUDES "${CMAKE_BINARY_DIR}/conan_provider.cmake")

find_package(fmt REQUIRED)

add_executable(main main.cpp)
target_link_libraries(main PRIVATE fmt::fmt)

cmake -B build -S . -DCMAKE_BUILD_TYPE=Release

Have you read the CONTRIBUTING guide?

  • [x] I've read the CONTRIBUTING guide

simon-rechermann avatar Apr 29 '24 09:04 simon-rechermann

Hi @simon-rechermann

Thanks for your question.

CMake provider files cannot be included in regular CMakeLists.txt. Same as with CMake toolchain files. Both files need to be passed in command line (or at the very least, before the project() call, but still this is not recommended).

It is generally not recommended to modify CMakeLists.txt to add such specific behavior. CMake providers and toolchains are designed to be external to the CMakeLists.txt, so they can be applied transparently and non-intrusively.

The recommended way to implement this is using the CMakePresets:

  • Define your own CMakePresets.json file
  • Add the CMAKE_PROJECT_TOP_LEVEL_INCLUDES=../conan_provider.cmake in the presets.
  • Call cmake --preset xxxx with your preset

Maybe the first part of this talk might help: https://youtu.be/s0q6s5XzIrA (linked from our docs https://docs.conan.io/2/knowledge/videos.html)

memsharded avatar Apr 29 '24 09:04 memsharded

Closing this ticket as responded, please create new tickets for any new question or issue, thanks!

memsharded avatar Jul 26 '24 16:07 memsharded