Skip to content

Commit

Permalink
Fix conan-io#16078 - add type to cmakepresets cacheVariables
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarrec committed Apr 15, 2024
1 parent f1ef6e4 commit 6c06148
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
20 changes: 15 additions & 5 deletions conan/tools/cmake/presets.py
Expand Up @@ -44,7 +44,7 @@ def generate(conanfile, toolchain_file, generator, cache_variables, preset_prefi

if "BUILD_TESTING" not in cache_variables:
if conanfile.conf.get("tools.build:skip_test", check_type=bool):
cache_variables["BUILD_TESTING"] = "OFF"
cache_variables["BUILD_TESTING"] = {"value": "OFF", "type": bool}

preset_path = os.path.join(conanfile.generators_folder, "CMakePresets.json")
multiconfig = is_multi_configuration(generator)
Expand Down Expand Up @@ -116,7 +116,7 @@ def _configure_preset(conanfile, generator, cache_variables, toolchain_file, mul
if preset_prefix:
name = f"{preset_prefix}-{name}"
if not multiconfig and build_type:
cache_variables["CMAKE_BUILD_TYPE"] = build_type
cache_variables["CMAKE_BUILD_TYPE"] = {"value": build_type, "type": "STRING"}
ret = {
"name": name,
"displayName": "'{}' config".format(name),
Expand Down Expand Up @@ -158,12 +158,22 @@ def _configure_preset(conanfile, generator, cache_variables, toolchain_file, mul
ret["binaryDir"] = conanfile.build_folder

def _format_val(val):
return f'"{val}"' if type(val) == str and " " in val else f"{val}"
return f'"{val}"' if isinstance(val, str) and " " in val else f"{val}"

def _format_var(var, value_or_dict):
type_str = ""
if isinstance(value_or_dict, dict):
value = value_or_dict["value"]
type_str = f":{value_or_dict['type']}"
else:
value = value_or_dict

return f"-D{var}{type_str}={_format_val(value)}"

# https://github.com/conan-io/conan/pull/12034#issuecomment-1253776285
cache_variables_info = " ".join(
[f"-D{var}={_format_val(value)}" for var, value in cache_variables.items()])
add_toolchain_cache = f"-DCMAKE_TOOLCHAIN_FILE={toolchain_file} " \
[_format_var(var, value) for var, value in cache_variables.items()])
add_toolchain_cache = f"-DCMAKE_TOOLCHAIN_FILE:FILEPATH={toolchain_file} " \
if "CMAKE_TOOLCHAIN_FILE" not in cache_variables_info else ""

try:
Expand Down
14 changes: 11 additions & 3 deletions conan/tools/cmake/toolchain/toolchain.py
@@ -1,4 +1,5 @@
import os
from pathlib import Path
import textwrap
from collections import OrderedDict

Expand Down Expand Up @@ -240,14 +241,21 @@ def generate(self):
cache_variables = {}
for name, value in self.cache_variables.items():
if isinstance(value, bool):
cache_variables[name] = "ON" if value else "OFF"
cache_variables[name] = {"value": "ON" if value else "OFF", "type": "BOOL"}
elif isinstance(value, Path):
cache_variables[name] = {
"value": str(value),
"type": "FILEPATH" if value.is_file() else 'PATH'
}
elif isinstance(value, str):
cache_variables[name] = {"value": value, "type": "STRING"}
elif isinstance(value, _PackageOption):
if str(value).lower() in ["true", "false", "none"]:
cache_variables[name] = "ON" if bool(value) else "OFF"
cache_variables[name] = {"value": "ON" if bool(value) else "OFF", "type": "BOOL"}
elif str(value).isdigit():
cache_variables[name] = int(value)
else:
cache_variables[name] = str(value)
cache_variables[name] = {"value": str(value), "type": "STRING"}
else:
cache_variables[name] = value

Expand Down

0 comments on commit 6c06148

Please sign in to comment.