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 21, 2024
1 parent 3f8ae23 commit 21c4a86
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 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
32 changes: 29 additions & 3 deletions conans/test/integration/toolchains/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,13 @@ def test_runtime_lib_dirs_single_conf(lib_dir_setup):
is_windows = platform.system() == "Windows"
if is_windows:
generator = '-c tools.cmake.cmaketoolchain:generator=Ninja'

client.run(f'install . -s build_type=Release {generator}')
contents = client.load("conan_toolchain.cmake")
pattern_lib_path = r'list\(PREPEND CMAKE_LIBRARY_PATH (.*)\)'
pattern_lib_dirs = r'set\(CONAN_RUNTIME_LIB_DIRS (.*) \)'

# On *nix platforms: the list in `CMAKE_LIBRARY_PATH`
# On *nix platforms: the list in `CMAKE_LIBRARY_PATH`
# is the same as `CONAN_RUNTIME_LIB_DIRS`
# On windows, it's the same but with `bin` instead of `lib`
cmake_library_path = re.search(pattern_lib_path, contents).group(1)
Expand Down 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 21c4a86

Please sign in to comment.