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

fix CMakeToolchain newline #15788

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ def get_rendered_content(self):
if context is None:
return

def cmake_value(value):
if isinstance(value, bool):
return "ON" if value else "OFF"
else:
return '"{}"'.format(value)

template = Template(self.template, trim_blocks=True, lstrip_blocks=True)
template.environment.filters["cmake_value"] = cmake_value
return template.render(**context)

def context(self):
Expand Down
8 changes: 4 additions & 4 deletions conan/tools/cmake/toolchain/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ class CMakeToolchain(object):
# Variables
{% for it, value in variables.items() %}
{% if value is boolean %}
set({{ it }} {{ value|cmake_value }} CACHE BOOL "Variable {{ it }} conan-toolchain defined")
set({{ it }} {{ "ON" if value else "OFF"}} CACHE BOOL "Variable {{ it }} conan-toolchain defined")
{% else %}
set({{ it }} {{ value|cmake_value }} CACHE STRING "Variable {{ it }} conan-toolchain defined")
set({{ it }} "{{ value }}" CACHE STRING "Variable {{ it }} conan-toolchain defined")
{% endif %}
{% endfor %}
# Variables per configuration
Expand Down Expand Up @@ -132,7 +132,6 @@ class CMakeToolchain(object):
{% endfor %}



if(CMAKE_POLICY_DEFAULT_CMP0091) # Avoid unused and not-initialized warnings
endif()
""")
Expand Down Expand Up @@ -198,7 +197,8 @@ def _context(self):
@property
def content(self):
context = self._context()
content = Template(self._template, trim_blocks=True, lstrip_blocks=True).render(**context)
content = Template(self._template, trim_blocks=True, lstrip_blocks=True,
keep_trailing_newline=True).render(**context)
content = relativize_generated_file(content, self._conanfile, "${CMAKE_CURRENT_LIST_DIR}")
return content

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1325,3 +1325,12 @@ def package(self):

presets = json.loads(c.load(presets_path))
assert presets["configurePresets"][0].get("cmakeExecutable") is None


def test_toolchain_ends_newline():
# https://github.com/conan-io/conan/issues/15785
client = TestClient()
client.save({"conanfile.py": GenConanfile()})
client.run("install . -g CMakeToolchain")
toolchain = client.load("conan_toolchain.cmake")
assert toolchain[-1] == "\n"