Some of the features used in this section are still under development, while they are recommended and usable and we will try not to break them in future releases, some breaking changes might still happen if necessary to prepare for the Conan 2.0 release.
Available since: 1.45.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.
build/Release: if the cmake generator is single-configuration, depending on the build_type.
"build"string, can be defined to other value by the
["Debug"]for a multi-configuration cmake generator.
.for a single-configuration cmake generator.
def layout(self): cmake_layout(self, src_folder="subfolder", build_folder="build")
".") internally defines
conanfile.folders.subprojectis not defined, otherwise it will define the value relative to
"build") defines the base name for the folder containing the build artifacts.
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.
build/apple-clang-shared_false/Debug: 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.
build/apple-clang-shared_true/Debug: 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.
settings.build_type value is forbidden in
tools.cmake.cmake_layout:build_folder_vars because the
build_type is already managed automatically with multi-config support in