From 223ca3b79531d2cac5343aa1f2dba567244f9236 Mon Sep 17 00:00:00 2001 From: memsharded Date: Mon, 28 Aug 2023 12:48:36 +0200 Subject: [PATCH 1/5] example for injecting cmake variables --- examples/tools/cmake/cmake.rst | 3 +- .../inject_cmake_variables.rst | 76 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst diff --git a/examples/tools/cmake/cmake.rst b/examples/tools/cmake/cmake.rst index 2fc71e2b13ba..d63d5ec18594 100644 --- a/examples/tools/cmake/cmake.rst +++ b/examples/tools/cmake/cmake.rst @@ -10,4 +10,5 @@ tools.cmake cmake_toolchain/build_project_cmake_presets - cmake_toolchain/extend_own_cmake_presets \ No newline at end of file + cmake_toolchain/extend_own_cmake_presets + cmake_toolchain/inject_cmake_variables \ No newline at end of file diff --git a/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst b/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst new file mode 100644 index 000000000000..6d526508ae21 --- /dev/null +++ b/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst @@ -0,0 +1,76 @@ +.. _examples-tools-cmake-toolchain-inject-variables: + +CMakeToolchain: Inject arbitrary CMake variables into dependencies +================================================================== + +In the general case, Conan package recipe provides the necessary abstractions via settings, confs, and options +to control different aspects of the build. Many recipes define ``options`` to activate or deactivate features, +optional dependencies, or binary characteristics. Configurations like ``tools.build:cxxflags`` can be used to +inject arbitrary C++ compile flags. + +In some exceptional cases, it might be desired to inject CMake variables directly into dependencies doing CMake +builds. This is possible when these dependencies use the ``CMakeToolchan`` integration. Let's check it in this +simple example. + +If we have the following package recipe, with a simple ``conanfile.py`` and a ``CMakeLists.txt`` printing a variable: + +.. code-block:: python + :caption: conanfile.py + + from conan import ConanFile + from conan.tools.cmake import CMake + + class AppConan(ConanFile): + name = "foo" + version = "1.0" + settings = "os", "compiler", "build_type", "arch" + exports_sources = "CMakeLists.txt" + + generators = "CMakeToolchain" + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + +.. code-block:: cmake + :caption: CMakeLists.txt + + cmake_minimum_required(VERSION 3.15) + project(foo LANGUAGES NONE) + message(STATUS "MYVAR1 ${MY_USER_VAR1}!!") + +We can define a profile file and a ``myvars.cmake`` file (both in the same folder) like the following: + +.. code-block:: ini + :caption: myprofile + + include(default) + [conf] + tools.cmake.cmaketoolchain:user_toolchain+={{profile_dir}}/myvars.cmake + +Note the ``{{profile_dir}}`` is a jinja template expression that evaluates to the current profile folder, allowing +to compute the necessary path to ``myvars.cmake`` file. The ``tools.cmake.cmaketoolchain:user_toolchain`` is a **list** +of files to inject to the generated ``conan_toolchain.cmake``, so the ``+=`` operator is used to append to it. + +The ``myvars.cmake`` can define as many variables as we want: + +.. code-block:: cmake + :caption: myvars.cmake + + set(MY_USER_VAR1 "MYVALUE1") + + +Applying this profile, we can see that the package CMake build effectively uses the variable provided in the +external ``myvars.cmake`` file: + +.. code-block:: bash + + $ conan create . -pr=myprofile + ... + -- MYVAR1 MYVALUE1!! + +The ``tools.cmake.cmaketoolchain:user_toolchain`` conf value might also be passed in the command line ``-c`` argument, +but the location of the ``myvars.cmake`` needs to be absolute to be found, as jinja replacement doesn't happen in the +command line. From df0426ffe3d1a4df4668918742e1ad0265e5387c Mon Sep 17 00:00:00 2001 From: Carlos Zoido Date: Mon, 28 Aug 2023 12:52:03 +0200 Subject: [PATCH 2/5] Update examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst --- examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst b/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst index 6d526508ae21..87d86cbc578d 100644 --- a/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst +++ b/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst @@ -9,7 +9,7 @@ optional dependencies, or binary characteristics. Configurations like ``tools.bu inject arbitrary C++ compile flags. In some exceptional cases, it might be desired to inject CMake variables directly into dependencies doing CMake -builds. This is possible when these dependencies use the ``CMakeToolchan`` integration. Let's check it in this +builds. This is possible when these dependencies use the ``CMakeToolchain`` integration. Let's check it in this simple example. If we have the following package recipe, with a simple ``conanfile.py`` and a ``CMakeLists.txt`` printing a variable: From cea9a062130d3596a7d382a7c78fab57120129f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Rinc=C3=B3n=20Blanco?= Date: Mon, 28 Aug 2023 16:59:59 +0200 Subject: [PATCH 3/5] Update examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst --- examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst b/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst index 87d86cbc578d..e0d7f4344b1d 100644 --- a/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst +++ b/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst @@ -3,7 +3,7 @@ CMakeToolchain: Inject arbitrary CMake variables into dependencies ================================================================== -In the general case, Conan package recipe provides the necessary abstractions via settings, confs, and options +In the general case, Conan package recipes provide the necessary abstractions via settings, confs, and options to control different aspects of the build. Many recipes define ``options`` to activate or deactivate features, optional dependencies, or binary characteristics. Configurations like ``tools.build:cxxflags`` can be used to inject arbitrary C++ compile flags. From 0b11c723b0658b292420e9bc036d2cd13f17748a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Rinc=C3=B3n=20Blanco?= Date: Mon, 28 Aug 2023 17:57:31 +0200 Subject: [PATCH 4/5] Add note about support for both user_toolchain and confs at the same time --- examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst b/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst index e0d7f4344b1d..ba22ea3c9109 100644 --- a/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst +++ b/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst @@ -71,6 +71,8 @@ external ``myvars.cmake`` file: ... -- MYVAR1 MYVALUE1!! +Note that using ``user_toolchain`` while defining values for confs like ``tools.cmake.cmaketoolchain:system_name`` is supported. + The ``tools.cmake.cmaketoolchain:user_toolchain`` conf value might also be passed in the command line ``-c`` argument, but the location of the ``myvars.cmake`` needs to be absolute to be found, as jinja replacement doesn't happen in the command line. From f4413988b327d21882c927ec1695af5798ea8d4d Mon Sep 17 00:00:00 2001 From: James Date: Tue, 29 Aug 2023 11:36:46 +0200 Subject: [PATCH 5/5] Update examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst Co-authored-by: Carlos Zoido --- .../cmake/cmake_toolchain/inject_cmake_variables.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst b/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst index ba22ea3c9109..5960cf62161a 100644 --- a/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst +++ b/examples/tools/cmake/cmake_toolchain/inject_cmake_variables.rst @@ -3,6 +3,15 @@ CMakeToolchain: Inject arbitrary CMake variables into dependencies ================================================================== +You can find the sources to recreate this project in the `examples2.0 repository +`_ in GitHub: + +.. code-block:: bash + + $ git clone https://github.com/conan-io/examples2.git + $ cd examples2/examples/tools/cmake/cmake_toolchain/user_toolchain_profile + + In the general case, Conan package recipes provide the necessary abstractions via settings, confs, and options to control different aspects of the build. Many recipes define ``options`` to activate or deactivate features, optional dependencies, or binary characteristics. Configurations like ``tools.build:cxxflags`` can be used to