Skip to content

Commit

Permalink
New tools.cmake.cmaketoolchain:enabled_blocks (#16563)
Browse files Browse the repository at this point in the history
* CMakeToolchain block select

* better tests
  • Loading branch information
memsharded authored Jul 1, 2024
1 parent e8d50c6 commit 2b84864
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,15 @@ def __getitem__(self, name):
return self._blocks[name]

def process_blocks(self):
blocks = self._conanfile.conf.get("tools.cmake.cmaketoolchain:enabled_blocks",
check_type=list)
if blocks is not None:
try:
new_blocks = OrderedDict((b, self._blocks[b]) for b in blocks)
except KeyError as e:
raise ConanException(f"Block {e} defined in tools.cmake.cmaketoolchain"
f":enabled_blocks doesn't exist in {list(self._blocks.keys())}")
self._blocks = new_blocks
result = []
for b in self._blocks.values():
content = b.get_rendered_content()
Expand Down
1 change: 1 addition & 0 deletions conans/model/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"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 (potential override of CMakeToolchain defined variables)",
"tools.cmake.cmaketoolchain:enabled_blocks": "Select the specific blocks to use in the conan_toolchain.cmake",
"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: 28 additions & 0 deletions test/unittests/tools/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,34 @@ def test_select_blocks(conanfile):
assert "########## 'preprocessor' block #############" not in content


def test_enabled_blocks_conf(conanfile):
conanfile.conf.define("tools.cmake.cmaketoolchain:enabled_blocks", ["generic_system"])
toolchain = CMakeToolchain(conanfile)
content = toolchain.content
assert "########## 'generic_system' block #############" in content
assert "########## 'cmake_flags_init' block #############" not in content
assert "########## 'libcxx' block #############" not in content
assert "########## 'variables' block #############" not in content
assert "########## 'preprocessor' block #############" not in content

# remove multiple
conanfile.conf.define("tools.cmake.cmaketoolchain:enabled_blocks",
["generic_system", "cmake_flags_init"])
toolchain = CMakeToolchain(conanfile)
content = toolchain.content
assert "########## 'generic_system' block #############" in content
assert "########## 'cmake_flags_init' block #############" in content
assert "########## 'libcxx' block #############" not in content
assert "########## 'variables' block #############" not in content
assert "########## 'preprocessor' block #############" not in content

conanfile.conf.define("tools.cmake.cmaketoolchain:enabled_blocks", ["potato"])
toolchain = CMakeToolchain(conanfile)
with pytest.raises(ConanException) as e:
_ = toolchain.content
assert "Block 'potato' defined in tools.cmake.cmaketoolchain:enabled_blocks doesn't" in str(e)


def test_dict_keys(conanfile):
toolchain = CMakeToolchain(conanfile)
assert "generic_system" in toolchain.blocks.keys()
Expand Down

0 comments on commit 2b84864

Please sign in to comment.