Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better CMakeToolchain blocks filter #14731

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,8 +840,23 @@ def __init__(self, conanfile, toolchain, items=None):
for name, block in items:
self._blocks[name] = block(conanfile, toolchain)

def remove(self, name):
def keys(self):
return self._blocks.keys()

def items(self):
return self._blocks.items()

def remove(self, name, *args):
del self._blocks[name]
for arg in args:
del self._blocks[arg]

def select(self, name, *args):
"""
keep the blocks provided as arguments, remove the others
"""
to_keep = [name] + list(args)
self._blocks = OrderedDict((k, v) for k, v in self._blocks.items() if k in to_keep)
memsharded marked this conversation as resolved.
Show resolved Hide resolved

def __setitem__(self, name, block_type):
# Create a new class inheriting Block with the elements of the provided one
Expand Down
34 changes: 34 additions & 0 deletions conans/test/unittests/tools/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,40 @@ def test_remove(conanfile):
toolchain.blocks.remove("generic_system")
content = toolchain.content
assert 'CMAKE_SYSTEM_NAME' not in content
assert "CMAKE_CXX_FLAGS_INIT" in content
assert "_CMAKE_IN_TRY_COMPILE" in content

# remove multiple
toolchain = CMakeToolchain(conanfile)
toolchain.blocks.remove("generic_system", "cmake_flags_init")
content = toolchain.content
assert 'CMAKE_SYSTEM_NAME' not in content
assert "CMAKE_CXX_FLAGS_INIT" not in content
assert "_CMAKE_IN_TRY_COMPILE" in content


def test_filter(conanfile):
toolchain = CMakeToolchain(conanfile)
toolchain.blocks.select("generic_system")
content = toolchain.content
assert 'CMAKE_SYSTEM_NAME' in content
assert "CMAKE_CXX_FLAGS_INIT" not in content
assert "_CMAKE_IN_TRY_COMPILE" not in content

# remove multiple
toolchain = CMakeToolchain(conanfile)
toolchain.blocks.select("generic_system", "cmake_flags_init")
content = toolchain.content
assert 'CMAKE_SYSTEM_NAME' in content
assert "CMAKE_CXX_FLAGS_INIT" in content
assert "_CMAKE_IN_TRY_COMPILE" not in content


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


def test_template_remove(conanfile):
Expand Down