Skip to content

Commit

Permalink
fix: quote template CMake variables (#17156)
Browse files Browse the repository at this point in the history
* fix: quote template CMake variables

This fix addresses an issue where "cppstd" is undefined for pure C
recipes. Consequently, "set" sometimes leaves the
"conan_watched_std_variable" unset, leading to an error in the
subsequent string comparison:

```
CMake Error at .../conan_toolchain.cmake:64 (if):
  if given arguments:

    "READ_ACCESS" "STREQUAL" "MODIFIED_ACCESS" "AND" "NOT" "11" "STREQUAL"

  Unknown arguments specified
```

* Fix error reporting for cstd

* fix Windows test

---------

Co-authored-by: Abril Rincón Blanco <git@rinconblanco.es>
Co-authored-by: memsharded <james@conan.io>
  • Loading branch information
3 people authored Oct 14, 2024
1 parent 2aa8e08 commit e85aaa1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion conan/internal/api/profile/profile_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _error(compiler, cstd, min_version, version):
mver = {"17": "192",
"11": "192"}.get(cstd)
if mver and version < mver:
_error(compiler, cppstd, mver, version)
_error(compiler, cstd, mver, version)
"""


Expand Down
6 changes: 3 additions & 3 deletions conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,11 @@ class CppStdBlock(Block):
# Define the C++ and C standards from 'compiler.cppstd' and 'compiler.cstd'
function(conan_modify_std_watch variable access value current_list_file stack)
set(conan_watched_std_variable {{ cppstd }})
set(conan_watched_std_variable "{{ cppstd }}")
if (${variable} STREQUAL "CMAKE_C_STANDARD")
set(conan_watched_std_variable {{ cstd }})
set(conan_watched_std_variable "{{ cstd }}")
endif()
if (${access} STREQUAL "MODIFIED_ACCESS" AND NOT ${value} STREQUAL ${conan_watched_std_variable})
if ("${access}" STREQUAL "MODIFIED_ACCESS" AND NOT "${value}" STREQUAL "${conan_watched_std_variable}")
message(STATUS "Warning: Standard ${variable} value defined in conan_toolchain.cmake to ${conan_watched_std_variable} has been modified to ${value} by ${current_list_file}")
endif()
unset(conan_watched_std_variable)
Expand Down
45 changes: 45 additions & 0 deletions test/functional/toolchains/cmake/test_cmake_toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2112,3 +2112,48 @@ def build(self):
c.run('build . --profile:host=android')
assert 'VERSION ".0" format invalid.' not in c.out
assert 'sdk: 1.0.0' in c.out


@pytest.mark.tool("cmake")
def test_cmake_toolchain_language_c():
client = TestClient()

conanfile = textwrap.dedent(r"""
from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
class Pkg(ConanFile):
settings = "os", "compiler", "arch", "build_type"
generators = "CMakeToolchain"
languages = "C"
def layout(self):
cmake_layout(self)
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
""")

cmakelists = textwrap.dedent("""
cmake_minimum_required(VERSION 3.15)
project(pkg C)
""")

client.save(
{
"conanfile.py": conanfile,
"CMakeLists.txt": cmakelists,
},
clean_first=True,
)

if platform.system() == "Windows":
# compiler.version=191 is already the default now
client.run("build . -s compiler.cstd=11 -s compiler.version=191", assert_error=True)
assert "The provided compiler.cstd=11 requires at least msvc>=192 but version 191 provided" \
in client.out
else:
client.run("build . -s compiler.cppstd=11")
# It doesn't fail

0 comments on commit e85aaa1

Please sign in to comment.