Skip to content

Commit

Permalink
Added support for tools.cmake.cmaketoolchain:extra_variables (#16222)
Browse files Browse the repository at this point in the history
  • Loading branch information
perseoGI committed May 13, 2024
1 parent 80fee05 commit 033e2e0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
9 changes: 8 additions & 1 deletion conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -955,14 +958,18 @@ 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,
"cmake_system_version": system_version,
"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):
Expand Down
1 change: 1 addition & 0 deletions conans/model/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions conans/test/integration/toolchains/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 033e2e0

Please sign in to comment.