Skip to content

Commit

Permalink
fix: quote template CMake variables
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
skycaptain committed Oct 13, 2024
1 parent 890f000 commit dccf0e8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
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
39 changes: 39 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,42 @@ 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,
)

client.run("install . -s compiler.cstd=11")
client.run("build . -s compiler.cstd=11")

0 comments on commit dccf0e8

Please sign in to comment.