CMake toolchain for integration with Conan using cmake-conan, conan-setup.cmake
.
NOTE:
- Since CMakeDeps and CMakeToolchain will be the only two cmake-related generators supported in Conan 2.X, this project won't consider the situation using any other cmake-related generators although the demonstration is using Conan 1.X.
- This project is INCOMPATIBLE with Conan 2.x for now since there are some bugs about
conan_cmake_autodetect()
...etc. Theoretically, this project should work with Conan 2.X after those bugs are fixed, which is the main goal of this project.
This project aims to provide a NONINTRUSIVE way to integrate CMake with Conan using cmake-conan. It is because the typical usage of cmake-conan is intrusive, which uses APIs of cmake-conan inside the CMakeLists.txt
directly. For example:
Click to expand
cmake_minimum_required(VERSION 3.9)
project(FormatOutput CXX)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
add_definitions("-std=c++11")
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/0.18.1/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake"
TLS_VERIFY ON)
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_cmake_configure(REQUIRES fmt/6.1.2
GENERATORS cmake_find_package)
conan_cmake_autodetect(settings)
conan_cmake_install(PATH_OR_REFERENCE .
BUILD missing
REMOTE conancenter
SETTINGS ${settings})
find_package(fmt)
add_executable(main main.cpp)
target_link_libraries(main fmt::fmt)
Moreover, there's an important problem that made Conan 2.X decide to deprecate cmake-conan, which is the Chicken-And-Egg(CAE) problem with CMakeToolchain (See more details on this issue). Therefore, this project provides an alternative way to use cmake-conan, which can solve the CAE problem.
Users just need to do the following two things:
- Prepare a conan recipe (
conanfile.txt
orconanfile.py
) in the root source directory. - Specify cache variable CMAKE_PROJECT_INCLUDE_BEFORE with
conan-setup.cmake
.
And then, all the installation of dependencies will be DONE automatically when they configure their projects.
You can grab a specific version of conan.cmake
from cmake-conan to your project alongside with conan-setup.cmake
. Otherwise, conan-setup.cmake
will download the lastest version of conan.cmake
from the master branch of cmake-conan.
The idea of this project is inspired by this article of Qt Blog, which introduces a custom toolchain file called auto-setup.cmake
(You can check its content here). After simplifying the file and replacing the old APIs of cmake-conan with the new one into conan-setup.cmake
, there are 4 steps of the mechanism:
- Include
conan.cmake
and scan for conan recipes in the root source directory. - Create a new
CMakeLists.txt
, in which APIs of cmake-conan are used, by callingfile(WRITE)
. - Configure the generated
CMakeLists.txt
by callingexecute_process(COMMAND ${CMAKE_COMMAND})
. - Include
conan_toolchain.cmake
generated by CMakeToolchain at the end ofconan-setup.cmake
.
Therefore, we can include conan_toolchain.cmake
BEFORE the invocation of the first project()
command. As we can see, the mechanism successfully solves the CAE problem.
There are some default or custom variables that will take effect in conan-setup.cmake
.
Cache Variables | Default Value (if not specified) |
Effects |
---|---|---|
CMAKE_CONFIGURATION_TYPES | Debug;Release |
If you're using Multi-Config generators, then it will run conan install command with all the config types listed in this variable recursively. |
CMAKE_CONAN_OUPTUT_FOLDER | conan-deps |
The subfolder where conan install command generates files inside the build directory. |
CMAKE_CONAN_OUTPUT_QUIET | false |
Disable the output message of running conan install command if we set it to true . |
CMAKE_CONAN_ERROR_QUIET | false |
Disable the error message of running conan install command if we set it to true . |
- CMake (at least version 3.23 for using some features needed in
CMakePresets.json
) - Ninja (for using
Ninja
orNinja Multi-Config
generator) - C/C++ Compilers
There are some installation guides for your reference:
There are some configure presets in CMakePresets.json
. You can run cmake --list-presets
command to check all the available configure presets for your platform, and then run cmake --preset <preset-name>
to configure the project.
Click to expand
F:\GitRepo\cmake-conan-setup>cmake --list-presets
Available configure presets:
"win32-msvc-x64-ninja-debug" - Windows MSVC x64 (Ninja) Debug
"win32-msvc-x64-ninja-release" - Windows MSVC x64 (Ninja) Release
"win32-msvc-x64-nmake-debug" - Windows MSVC x64 (NMake Makefiles) Debug
"win32-msvc-x64-nmake-release" - Windows MSVC x64 (NMake Makefiles) Release
"win32-msvc-x64-ninja-multi" - Windows MSVC x64 (Ninja Multi-Config)
"win32-msvc-x64-vs2019-multi" - Windows MSVC x64 (Visual Studio 16 2019)
"win32-msvc-x86-ninja-debug" - Windows MSVC x86 (Ninja) Debug
"win32-msvc-x86-ninja-release" - Windows MSVC x86 (Ninja) Release
"win32-msvc-x86-nmake-debug" - Windows MSVC x86 (NMake Makefiles) Debug
"win32-msvc-x86-nmake-release" - Windows MSVC x86 (NMake Makefiles) Release
"win32-msvc-x86-ninja-multi" - Windows MSVC x86 (Ninja Multi-Config)
"win32-msvc-x86-vs2019-multi" - Windows MSVC x86 (Visual Studio 16 2019)
"win32-gcc-x64-ninja-debug" - Windows GCC x64 (Ninja) Debug
"win32-gcc-x64-ninja-release" - Windows GCC x64 (Ninja) Release
"win32-gcc-x64-mingw-debug" - Windows GCC x64 (MinGW Makefiles) Debug
"win32-gcc-x64-mingw-release" - Windows GCC x64 (MinGW Makefiles) Release
"win32-gcc-x64-ninja-multi" - Windows GCC x64 (Ninja Multi-Config)
"win32-gcc-x86-ninja-debug" - Windows GCC x86 (Ninja) Debug
"win32-gcc-x86-ninja-release" - Windows GCC x86 (Ninja) Release
"win32-gcc-x86-mingw-debug" - Windows GCC x86 (MinGW Makefiles) Debug
"win32-gcc-x86-mingw-release" - Windows GCC x86 (MinGW Makefiles) Release
"win32-gcc-x86-ninja-multi" - Windows GCC x86 (Ninja Multi-Config)
"win32-clangcl-x64-ninja-debug" - Windows ClangCL x64 (Ninja) Debug
"win32-clangcl-x64-ninja-release" - Windows ClangCL x64 (Ninja) Release
"win32-clangcl-x64-ninja-multi" - Windows ClangCL x64 (Ninja Multi-Config)
"win32-clangcl-x86-ninja-debug" - Windows ClangCL x86 (Ninja) Debug
"win32-clangcl-x86-ninja-release" - Windows ClangCL x86 (Ninja) Release
"win32-clangcl-x86-ninja-multi" - Windows ClangCL x86 (Ninja Multi-Config)
"win32-clang-x64-ninja-debug" - Windows Clang x64 (Ninja) Debug
"win32-clang-x64-ninja-release" - Windows Clang x64 (Ninja) Release
"win32-clang-x64-ninja-multi" - Windows Clang x64 (Ninja Multi-Config)
"win32-clang-x86-ninja-debug" - Windows Clang x86 (Ninja) Debug
"win32-clang-x86-ninja-release" - Windows Clang x86 (Ninja) Release
"win32-clang-x86-ninja-multi" - Windows Clang x86 (Ninja Multi-Config)
NOTE:
- You need to call
vcvarsall.bat <argument>
to setup environment before configuring the MSVC and ClangCL presets except those using Visaul Studio generator. (Only avaliable incmd.exe
not inpowershell.exe
)- You may need to modify the
"environment"
settings yourself if the Clang/LLVM or GCC/MinGW Compiler is installed on other locations.
- Open the project folder by Visual Studio Code.
- Install CMake Tools extension.
- Type "CMake: Select Configure Preset" on Command Palette (
Ctrl+Shift+P
). - Type "CMake: Configure" on Command Palette (
Ctrl+Shift+P
).