Skip to content

Commit 033e2e0

Browse files
committed
Added support for tools.cmake.cmaketoolchain:extra_variables (#16222)
1 parent 80fee05 commit 033e2e0

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

conan/tools/cmake/toolchain/blocks.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,9 @@ class GenericSystemBlock(Block):
795795
message(STATUS "Conan toolchain: CMAKE_GENERATOR_TOOLSET={{ toolset }}")
796796
set(CMAKE_GENERATOR_TOOLSET "{{ toolset }}" CACHE STRING "" FORCE)
797797
{% endif %}
798+
{% for key, value in extra_variables.items() %}
799+
set({{ key }} "{{ value }}")
800+
{% endfor %}
798801
""")
799802

800803
@staticmethod
@@ -955,14 +958,18 @@ def context(self):
955958
result = self._get_winsdk_version(system_version, generator_platform)
956959
system_version, winsdk_version, gen_platform_sdk_version = result
957960

961+
# Reading configuration from "tools.cmake.cmaketoolchain:extra_variables"
962+
extra_variables = self._conanfile.conf.get("tools.cmake.cmaketoolchain:extra_variables", default={}, check_type=dict)
963+
958964
return {"toolset": toolset,
959965
"generator_platform": generator_platform,
960966
"cmake_system_name": system_name,
961967
"cmake_system_version": system_version,
962968
"cmake_system_processor": system_processor,
963969
"cmake_sysroot": cmake_sysroot,
964970
"winsdk_version": winsdk_version,
965-
"gen_platform_sdk_version": gen_platform_sdk_version}
971+
"gen_platform_sdk_version": gen_platform_sdk_version,
972+
"extra_variables": extra_variables}
966973

967974

968975
class OutputDirsBlock(Block):

conans/model/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"tools.cmake.cmaketoolchain:toolset_arch": "Toolset architecture to be used as part of CMAKE_GENERATOR_TOOLSET in CMakeToolchain",
7474
"tools.cmake.cmaketoolchain:toolset_cuda": "(Experimental) Path to a CUDA toolset to use, or version if installed at the system level",
7575
"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'.",
76+
"tools.cmake.cmaketoolchain:extra_variables": "Dictionary with variables to be injected in CMakeToolchain",
7677
"tools.cmake.cmake_layout:build_folder_vars": "Settings and Options that will produce a different build folder and different CMake presets names",
7778
"tools.cmake.cmake_layout:build_folder": "(Experimental) Allow configuring the base folder of the build for local builds",
7879
"tools.cmake.cmake_layout:test_folder": "(Experimental) Allow configuring the base folder of the build for test_package",

conans/test/integration/toolchains/cmake/test_cmaketoolchain.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,3 +1414,29 @@ def build(self):
14141414
})
14151415
client.run("export tool")
14161416
client.run("create consumer -pr:h host -pr:b build --build=missing")
1417+
1418+
1419+
def test_toolchain_extra_variables():
1420+
windows_profile = textwrap.dedent("""
1421+
[settings]
1422+
os=Windows
1423+
arch=x86_64
1424+
[conf]
1425+
tools.cmake.cmaketoolchain:extra_variables={'CMAKE_GENERATOR_INSTANCE': 'C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/', 'FOO': 'bar'}
1426+
""")
1427+
1428+
client = TestClient(path_with_spaces=False)
1429+
client.save({"conanfile.txt": "[generators]\nCMakeToolchain",
1430+
"windows": windows_profile})
1431+
1432+
# Test passing extra_variables from profile
1433+
client.run("install . --profile:build=windows --profile:host=windows")
1434+
toolchain = client.load("conan_toolchain.cmake")
1435+
assert 'set(CMAKE_GENERATOR_INSTANCE "C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/")' in toolchain
1436+
assert 'set(FOO "bar")' in toolchain
1437+
1438+
# Test input from command line passing dict between doble quotes
1439+
client.run("install . -c tools.cmake.cmaketoolchain:extra_variables=\"{'CMAKE_GENERATOR_INSTANCE': 'C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/', 'FOO': 'bar'}\"")
1440+
toolchain = client.load("conan_toolchain.cmake")
1441+
assert 'set(CMAKE_GENERATOR_INSTANCE "C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/")' in toolchain
1442+
assert 'set(FOO "bar")' in toolchain

0 commit comments

Comments
 (0)