Skip to content

Commit

Permalink
Added support for tools.cmake.cmaketoolchain:extra_variables (conan-i…
Browse files Browse the repository at this point in the history
  • Loading branch information
perseoGI authored and AbrilRBS committed May 21, 2024
1 parent 0597c50 commit 9b53abd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
11 changes: 10 additions & 1 deletion conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -994,14 +999,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
28 changes: 27 additions & 1 deletion test/integration/toolchains/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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

0 comments on commit 9b53abd

Please sign in to comment.