diff --git a/conan/tools/cmake/toolchain/blocks.py b/conan/tools/cmake/toolchain/blocks.py index 02d6c4723f5..4b8e9f991f2 100644 --- a/conan/tools/cmake/toolchain/blocks.py +++ b/conan/tools/cmake/toolchain/blocks.py @@ -834,6 +834,11 @@ class GenericSystemBlock(Block): message(STATUS "Conan toolchain: CMAKE_GENERATOR_TOOLSET={{ toolset }}") set(CMAKE_GENERATOR_TOOLSET "{{ toolset }}" CACHE STRING "" FORCE) {% endif %} + {% if extra_variables %} + {% for key, value in extra_variables.items() %} + set({{ key }} "{{ value }}") + {% endfor %} + {% endif %} """) @staticmethod @@ -994,6 +999,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, @@ -1001,7 +1009,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 ef758d342a7..274403f1ae6 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/test/integration/toolchains/cmake/test_cmaketoolchain.py b/test/integration/toolchains/cmake/test_cmaketoolchain.py index 81ad493a1ef..478b15de7b0 100644 --- a/test/integration/toolchains/cmake/test_cmaketoolchain.py +++ b/test/integration/toolchains/cmake/test_cmaketoolchain.py @@ -1501,7 +1501,6 @@ def layout(self): presets = json.loads(c.load(user_presets["include"][0])) assert os.path.isabs(presets["configurePresets"][0]["toolchainFile"]) - def test_output_dirs_gnudirs_local_default(): # https://github.com/conan-io/conan/issues/14733 c = TestClient() @@ -1575,3 +1574,30 @@ def _assert_install(out): c.run("build .") _assert_install(c.out) assert "CMAKE_INSTALL_PREFIX" not in c.out + + +def test_toolchain_extra_variables(): + windows_profile = textwrap.dedent(""" + [settings] + os=Windows + arch=x86_64 + [conf] + tools.cmake.cmaketoolchain:extra_variables={'CMAKE_GENERATOR_INSTANCE': '"${GENERATOR_INSTANCE}/buildTools/"', 'FOO': '42 CACHE' } + """) + + 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 "${GENERATOR_INSTANCE}/buildTools/")' in toolchain + assert 'set(FOO 42 CACHE)' in toolchain + + # Test input from command line passing dict between doble quotes + + client.run("install . -c tools.cmake.cmaketoolchain:extra_variables=\"{'CMAKE_GENERATOR_INSTANCE': '\"\${GENERATOR_INSTANCE}/buildTools/\"', 'FOO': '42 CACHE'}\"") + toolchain = client.load("conan_toolchain.cmake") + assert 'set(CMAKE_GENERATOR_INSTANCE "${GENERATOR_INSTANCE}/buildTools/")' in toolchain + assert 'set(FOO 42 CACHE)' in toolchain