These tools are still experimental (so subject to breaking changes) but with very stable syntax. We encourage the usage of it to be prepared for Conan 2.0.
For example, this would implement the standard CMake project layout:
from conan.tools.cmake import cmake_layout def layout(self): cmake_layout(self)
To try it you can use the
conan new hello/0.1 --template=cmake_lib template.
cmake_layout() sets the
attributes described in the (layout reference).
The assigned values depend on the CMake generator that will be used.
It can be defined with the
tools.cmake.cmaketoolchain:generator [conf] entry or passing it in the recipe to the
cmake_layout(self, cmake_generator) function. The assigned values are different if it is a
multi-config generator (like Visual Studio or Xcode), or a single-config generator (like Unix Makefiles).
These are the values assigned by the
conanfile.folders.source: src_folder argument or
.if not specified.
build: if the cmake generator is multi-configuration.
cmake-build-debug: if the cmake generator is single-configuration, depending on the build_type.
["Debug"]for a multi-configuration cmake generator.
.for a single-configuration cmake generator.
def layout(self): cmake_layout(self, src_folder="subfolder")
conanfile.folders.generators can be customized to take into account the
options and not only the
build_type. Use the
conf to declare a list of settings or options:
conan install . -c tools.cmake.cmake_layout:build_folder_vars='["settings.compiler", "options.shared"]'
For the previous example, the values assigned by the
cmake_layout (installing the Release/static default
configuration) would be:
build-apple-clang-shared_false: if the cmake generator is multi-configuration.
cmake-build-debug-apple-clang-shared_false: if the cmake generator is single-configuration.
If we repeat the previous install with a different configuration:
conan install . -o shared=True -c tools.cmake.cmake_layout:build_folder_vars='["settings.compiler", "options.shared"]'
The values assigned by the
cmake_layout (installing the Release/shared configuration) would be:
build-apple-clang-shared_true: if the cmake generator is multi-configuration.
cmake-build-debug-apple-clang-shared_true: if the cmake generator is single-configuration.
So we can keep separated folders for any number of different configurations that we want to install.
CMakePresets.json file generated at the CMakeToolchain
generator, will also take this
tools.cmake.cmake_layout:build_folder_vars config into account to generate different
names for the presets, being very handy to install N configurations and building our project for any of them by
selecting the chosen preset.