Skip to content

Commit ab9487f

Browse files
committed
Fix comments
1 parent 3ae459a commit ab9487f

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

conan/tools/cmake/toolchain/blocks.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,22 +1030,23 @@ class ExtraVariablesBlock(Block):
10301030
CMAKE_CACHE_TYPES = ["BOOL","FILEPATH", "PATH", "STRING", "INTERNAL"]
10311031

10321032
def get_exact_type(self, key, value):
1033-
if type(value) == str:
1033+
if isinstance(value, str):
10341034
return f"\"{value}\""
1035-
elif type(value) == int or type(value) == float:
1035+
elif isinstance(value, (int, float)):
10361036
return value
1037-
elif type(value) == dict:
1037+
elif isinstance(value, dict):
10381038
var_value = self.get_exact_type(key, value.get("value"))
10391039
is_cache = value.get("cache")
1040-
if is_cache == True or str(is_cache).lower() == 'true':
1040+
if is_cache:
1041+
if not isinstance(is_cache, bool):
1042+
raise ConanException(f'tools.cmake.cmaketoolchain:extra_variables "cache" must be a boolean (True/False)')
10411043
var_type = value.get("type")
10421044
if not var_type:
1043-
raise ConanException(f'CMakeToolchain needs type defined for cache variable "{key}"')
1045+
raise ConanException(f'tools.cmake.cmaketoolchain:extra_variables needs "type" defined for cache variable "{key}"')
10441046
if var_type not in self.CMAKE_CACHE_TYPES:
1045-
raise ConanException(f'CMakeToolchain invalid type "{var_type}" for cache variable "{key}". Possible types: {', '.join(self.CMAKE_CACHE_TYPES)}')
1046-
docstring = value.get("docstring")
1047-
if not docstring:
1048-
raise ConanException(f'CMakeToolchain needs docstring defined for cache variable "{key}"')
1047+
raise ConanException(f'tools.cmake.cmaketoolchain:extra_variables invalid type "{var_type}" for cache variable "{key}". Possible types: {", ".join(self.CMAKE_CACHE_TYPES)}')
1048+
# Set docstring as variable name if not defined
1049+
docstring = value.get("docstring") or key
10491050
return f"{var_value} CACHE {var_type} \"{docstring}\""
10501051
else:
10511052
return var_value

test/integration/toolchains/cmake/test_cmaketoolchain.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,19 +1585,19 @@ def test_toolchain_extra_variables():
15851585
tools.cmake.cmaketoolchain:extra_variables={'CMAKE_GENERATOR_INSTANCE': '${GENERATOR_INSTANCE}/buildTools/', 'FOO': '42' }
15861586
""")
15871587

1588-
client = TestClient(path_with_spaces=False)
1588+
client = TestClient()
15891589
client.save({"conanfile.txt": "[generators]\nCMakeToolchain",
15901590
"windows": windows_profile})
15911591

15921592
# Test passing extra_variables from pro ile
1593-
client.run("install . --profile:build=windows --profile:host=windows")
1593+
client.run("install . --profile:host=windows")
15941594
toolchain = client.load("conan_toolchain.cmake")
15951595
assert 'set(CMAKE_GENERATOR_INSTANCE "${GENERATOR_INSTANCE}/buildTools/")' in toolchain
15961596
assert 'set(FOO "42")' in toolchain
15971597

15981598
# Test input from command line passing dict between doble quotes
15991599
client.run(textwrap.dedent(r"""
1600-
install . -c tools.cmake.cmaketoolchain:extra_variables="{'CMAKE_GENERATOR_INSTANCE': '${GENERATOR_INSTANCE}/buildTools/', 'FOO': 42.2, 'DICT': {'value': 1}, 'CACHE_VAR': {'value': 'hello world', 'cache': 'true', 'type': 'BOOL', 'docstring': 'test variable'}}"
1600+
install . -c tools.cmake.cmaketoolchain:extra_variables="{'CMAKE_GENERATOR_INSTANCE': '${GENERATOR_INSTANCE}/buildTools/', 'FOO': 42.2, 'DICT': {'value': 1}, 'CACHE_VAR': {'value': 'hello world', 'cache': True, 'type': 'BOOL', 'docstring': 'test variable'}}"
16011601
""")
16021602
)
16031603

@@ -1608,19 +1608,25 @@ def test_toolchain_extra_variables():
16081608
assert 'set(CACHE_VAR "hello world" CACHE BOOL "test variable")' in toolchain
16091609

16101610

1611-
# Test invalid cache variable
16121611
client.run(textwrap.dedent("""
16131612
install . -c tools.cmake.cmaketoolchain:extra_variables="{'invalid': {'value': 'hello world', 'cache': 'true'}}"
16141613
""") , assert_error=True)
1615-
assert 'CMakeToolchain needs type defined for cache variable "invalid"' in client.out
1614+
assert 'tools.cmake.cmaketoolchain:extra_variables "cache" must be a boolean (True/False)' in client.out
16161615

1616+
# Test invalid cache variable
16171617
client.run(textwrap.dedent("""
1618-
install . -c tools.cmake.cmaketoolchain:extra_variables="{'invalid': {'value': 'hello world', 'cache': 'true', 'type': 'INVALID_TYPE'}}"
1618+
install . -c tools.cmake.cmaketoolchain:extra_variables="{'invalid': {'value': 'hello world', 'cache': True}}"
16191619
""") , assert_error=True)
1620-
assert 'CMakeToolchain invalid type "INVALID_TYPE" for cache variable "invalid". Possible types: BOOL, FILEPATH, PATH, STRING, INTERNAL' in client.out
1620+
assert 'tools.cmake.cmaketoolchain:extra_variables needs "type" defined for cache variable "invalid"' in client.out
16211621

16221622
client.run(textwrap.dedent("""
1623-
install . -c tools.cmake.cmaketoolchain:extra_variables="{'invalid': {'value': 'hello world', 'cache': 'true', 'type': 'PATH'}}"
1623+
install . -c tools.cmake.cmaketoolchain:extra_variables="{'invalid': {'value': 'hello world', 'cache': True, 'type': 'INVALID_TYPE'}}"
16241624
""") , assert_error=True)
1625-
assert 'CMakeToolchain needs docstring defined for cache variable "invalid"' in client.out
1625+
assert 'tools.cmake.cmaketoolchain:extra_variables invalid type "INVALID_TYPE" for cache variable "invalid". Possible types: BOOL, FILEPATH, PATH, STRING, INTERNAL' in client.out
1626+
1627+
client.run(textwrap.dedent("""
1628+
install . -c tools.cmake.cmaketoolchain:extra_variables="{'CACHE_VAR_DEFAULT_DOC': {'value': 'hello world', 'cache': True, 'type': 'PATH'}}"
1629+
"""))
1630+
toolchain = client.load("conan_toolchain.cmake")
1631+
assert 'set(CACHE_VAR_DEFAULT_DOC "hello world" CACHE PATH "CACHE_VAR_DEFAULT_DOC")' in toolchain
16261632

0 commit comments

Comments
 (0)