From 033e2e0031517cc625eb3249ab3516465d2bf475 Mon Sep 17 00:00:00 2001 From: PerseoGI Date: Mon, 13 May 2024 09:46:36 +0200 Subject: [PATCH] Added support for tools.cmake.cmaketoolchain:extra_variables (#16222) --- conan/tools/cmake/toolchain/blocks.py | 9 ++++++- conans/model/conf.py | 1 + .../toolchains/cmake/test_cmaketoolchain.py | 26 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/conan/tools/cmake/toolchain/blocks.py b/conan/tools/cmake/toolchain/blocks.py index adb338e5bea..b7e9547398c 100644 --- a/conan/tools/cmake/toolchain/blocks.py +++ b/conan/tools/cmake/toolchain/blocks.py @@ -795,6 +795,9 @@ class GenericSystemBlock(Block): message(STATUS "Conan toolchain: CMAKE_GENERATOR_TOOLSET={{ toolset }}") set(CMAKE_GENERATOR_TOOLSET "{{ toolset }}" CACHE STRING "" FORCE) {% endif %} + {% for key, value in extra_variables.items() %} + set({{ key }} "{{ value }}") + {% endfor %} """) @staticmethod @@ -955,6 +958,9 @@ def context(self): result = self._get_winsdk_version(system_version, generator_platform) system_version, winsdk_version, gen_platform_sdk_version = result + # Reading configuration from "tools.cmake.cmaketoolchain:extra_variables" + extra_variables = self._conanfile.conf.get("tools.cmake.cmaketoolchain:extra_variables", default={}, check_type=dict) + return {"toolset": toolset, "generator_platform": generator_platform, "cmake_system_name": system_name, @@ -962,7 +968,8 @@ def context(self): "cmake_system_processor": system_processor, "cmake_sysroot": cmake_sysroot, "winsdk_version": winsdk_version, - "gen_platform_sdk_version": gen_platform_sdk_version} + "gen_platform_sdk_version": gen_platform_sdk_version, + "extra_variables": extra_variables} class OutputDirsBlock(Block): diff --git a/conans/model/conf.py b/conans/model/conf.py index 5098608e9b4..5a7c102c558 100644 --- a/conans/model/conf.py +++ b/conans/model/conf.py @@ -73,6 +73,7 @@ "tools.cmake.cmaketoolchain:toolset_arch": "Toolset architecture to be used as part of CMAKE_GENERATOR_TOOLSET in CMakeToolchain", "tools.cmake.cmaketoolchain:toolset_cuda": "(Experimental) Path to a CUDA toolset to use, or version if installed at the system level", "tools.cmake.cmaketoolchain:presets_environment": "String to define wether to add or not the environment section to the CMake presets. Empty by default, will generate the environment section in CMakePresets. Can take values: 'disabled'.", + "tools.cmake.cmaketoolchain:extra_variables": "Dictionary with variables to be injected in CMakeToolchain", "tools.cmake.cmake_layout:build_folder_vars": "Settings and Options that will produce a different build folder and different CMake presets names", "tools.cmake.cmake_layout:build_folder": "(Experimental) Allow configuring the base folder of the build for local builds", "tools.cmake.cmake_layout:test_folder": "(Experimental) Allow configuring the base folder of the build for test_package", diff --git a/conans/test/integration/toolchains/cmake/test_cmaketoolchain.py b/conans/test/integration/toolchains/cmake/test_cmaketoolchain.py index ebaf4164929..d62732bbf0a 100644 --- a/conans/test/integration/toolchains/cmake/test_cmaketoolchain.py +++ b/conans/test/integration/toolchains/cmake/test_cmaketoolchain.py @@ -1414,3 +1414,29 @@ def build(self): }) client.run("export tool") client.run("create consumer -pr:h host -pr:b build --build=missing") + + +def test_toolchain_extra_variables(): + windows_profile = textwrap.dedent(""" + [settings] + os=Windows + arch=x86_64 + [conf] + tools.cmake.cmaketoolchain:extra_variables={'CMAKE_GENERATOR_INSTANCE': 'C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/', 'FOO': 'bar'} + """) + + client = TestClient(path_with_spaces=False) + client.save({"conanfile.txt": "[generators]\nCMakeToolchain", + "windows": windows_profile}) + + # Test passing extra_variables from profile + client.run("install . --profile:build=windows --profile:host=windows") + toolchain = client.load("conan_toolchain.cmake") + assert 'set(CMAKE_GENERATOR_INSTANCE "C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/")' in toolchain + assert 'set(FOO "bar")' in toolchain + + # Test input from command line passing dict between doble quotes + client.run("install . -c tools.cmake.cmaketoolchain:extra_variables=\"{'CMAKE_GENERATOR_INSTANCE': 'C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/', 'FOO': 'bar'}\"") + toolchain = client.load("conan_toolchain.cmake") + assert 'set(CMAKE_GENERATOR_INSTANCE "C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/")' in toolchain + assert 'set(FOO "bar")' in toolchain