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

it was difficult to change just a value of a CMakeToolchain block #9109

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions conan/tools/cmake/toolchain.py
Expand Up @@ -51,9 +51,20 @@ class Block(object):
def __init__(self, conanfile, toolchain):
self._conanfile = conanfile
self._toolchain = toolchain
self._context_values = None

def get_block(self):
context = self.context()
@property
def values(self):
if self._context_values is None:
self._context_values = self.context()
return self._context_values

@values.setter
def values(self, context_values):
self._context_values = context_values

def get_rendered_content(self):
context = self.values
if context is None:
return
return Template(self.template, trim_blocks=True, lstrip_blocks=True).render(**context)
Expand Down Expand Up @@ -544,9 +555,9 @@ def __getitem__(self, name):
def process_blocks(self):
result = []
for b in self._blocks.values():
block = b.get_block()
if block:
result.append(block)
content = b.get_rendered_content()
if content:
result.append(content)
return result


Expand Down
15 changes: 15 additions & 0 deletions conans/test/unittests/tools/cmake/test_cmaketoolchain.py
Expand Up @@ -69,6 +69,21 @@ def context(self):
assert 'set(CMAKE_BUILD_TYPE "SuperRelease"' in content


def test_context_update(conanfile):
toolchain = CMakeToolchain(conanfile)
build_type = toolchain.blocks["generic_system"].values["build_type"]
toolchain.blocks["generic_system"].values["build_type"] = "Super" + build_type
content = toolchain.content
assert 'set(CMAKE_BUILD_TYPE "SuperRelease"' in content


def test_context_replace(conanfile):
toolchain = CMakeToolchain(conanfile)
toolchain.blocks["generic_system"].values = {"build_type": "SuperRelease"}
content = toolchain.content
assert 'set(CMAKE_BUILD_TYPE "SuperRelease"' in content


def test_replace_block(conanfile):
toolchain = CMakeToolchain(conanfile)

Expand Down