From d07c97a306a45930147f6bfbfff25f50fdfb7067 Mon Sep 17 00:00:00 2001 From: Luis Martinez de Bartolome Izquierdo Date: Mon, 6 Sep 2021 10:36:53 +0200 Subject: [PATCH] CMakeDeps and CMakeToolchain improvements (#2209) --- reference/conanfile/tools/cmake/cmakedeps.rst | 25 +++++++++++----- .../conanfile/tools/cmake/cmaketoolchain.rst | 29 +++++++++++++++++++ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/reference/conanfile/tools/cmake/cmakedeps.rst b/reference/conanfile/tools/cmake/cmakedeps.rst index 14956308f5d..70a9e1d8340 100644 --- a/reference/conanfile/tools/cmake/cmakedeps.rst +++ b/reference/conanfile/tools/cmake/cmakedeps.rst @@ -163,14 +163,20 @@ The following properties affect the CMakeDeps generator: - **cmake_file_name**: The config file generated for the current package will follow the ``-config.cmake`` pattern, so to find the package you write ``find_package()``. -- **cmake_target_name**: Name of the target to be consumed. When set on the root ``cpp_info``, - it changes the namespace of the target. When set to a component, it changes the name of the target - (see the example below). +- **cmake_target_name**: Name of the target to be consumed. +- **cmake_target_namespace**: Namespace of the target to be consumed. If not specified, it will use **cmake_target_name**. This is only read when set on the root ``cpp_info`` (see the example below). +- **cmake_find_mode**: Defaulted to ``config``. Possible values are: + + - ``config``: The CMakeDeps generator will create config scripts for the dependency. + - ``module``: Will create module config (FindXXX.cmake) scripts for the dependency. + - ``both``: Will generate both config and modules. + - ``none``: Won't generate any file. It can be used, for instance, to create a system wrapper package so the consumers find the config files in the CMake installation config path and not in the generated by Conan (because it has been skipped). + +- **cmake_module_file_name**: Same as **cmake_file_name** but when generating modules with ``cmake_find_mode=module/both``. If not specified it will default to **cmake_file_name**. +- **cmake_module_target_name**: Same as **cmake_target_name** but when generating modules with ``cmake_find_mode=module/both``. If not specified it will default to **cmake_target_name**. +- **cmake_module_target_namespace**: Same as **cmake_target_namespace** but when generating modules with ``cmake_find_mode=module/both``. This is only read when set on the root ``cpp_info``. If not specified it will default to **cmake_target_namespace**. - **cmake_build_modules**: List of ``.cmake`` files (route relative to root package folder) that are automatically included when the consumer run the ``find_package()``. -- **skip_deps_file**: It tells the ``CMakeDeps`` generator to skip the creation of files for the package declaring - this property. It can be used, for instance, to create a system wrapper package so the consumers find - the config files in the CMake installation config path and not in the generated by Conan (because it has been skipped). Example: @@ -182,6 +188,8 @@ Example: self.cpp_info.set_property("cmake_file_name", "MyFileName") # Foo:: namespace for the targets (Foo::Foo if no components) self.cpp_info.set_property("cmake_target_name", "Foo") + # self.cpp_info.set_property("cmake_target_namespace", "Foo") # This can be omitted as the value is the same + # Foo::Var target name for the component "mycomponent" self.cpp_info.components["mycomponent"].set_property("cmake_target_name", "Var") # Automatically include the lib/mypkg.cmake file when calling find_package() @@ -189,4 +197,7 @@ Example: # Skip this package when generating the files for the whole dependency tree in the consumer # note: it will make useless the previous adjustements. - self.cpp_info.set_property("skip_deps_file", True) + # self.cpp_info.set_property("cmake_find_mode", "none") + + # Generate both MyFileNameConfig.cmake and FindMyFileName.cmake + self.cpp_info.set_property("cmake_find_mode", "both") diff --git a/reference/conanfile/tools/cmake/cmaketoolchain.rst b/reference/conanfile/tools/cmake/cmaketoolchain.rst index e12a2cc6ef6..cf2cf9e3b36 100644 --- a/reference/conanfile/tools/cmake/cmaketoolchain.rst +++ b/reference/conanfile/tools/cmake/cmaketoolchain.rst @@ -126,6 +126,35 @@ This will be translated to: - One ``set()`` definition, using a cmake generator expression in ``conan_toolchain.cmake`` file, using the different values for different configurations. +The booleans assigned to a variable will be translated to ``ON`` and ``OFF`` symbols in CMake: + +.. code:: python + + def generate(self): + tc = CMakeToolchain(self) + tc.variables["FOO"] = True + tc.variables["VAR"] = False + tc.generate() + + +Will generate the sentences: ``set(FOO ON ...)`` and ``set(VAR OFF ...)``. + + +find_builddirs +++++++++++++++ + +Defaulted to ``True``. If ``True`` Conan adds the ``cpp_info.builddirs`` from the requirements to the +``CMAKE_PREFIX_PATH`` and ``CMAKE_MODULE_PATH`` variables. That would allow finding the config files or modules +packaged in the dependencies and also including them from the consumer CMakeLists.txt. + +.. code:: python + + def generate(self): + tc = CMakeToolchain(self) + tc.find_builddirs = False + tc.generate() + + Generators ++++++++++