diff --git a/conan/tools/cmake/presets.py b/conan/tools/cmake/presets.py index c6fd71bd851..aa98be59c22 100644 --- a/conan/tools/cmake/presets.py +++ b/conan/tools/cmake/presets.py @@ -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) @@ -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), @@ -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: diff --git a/conan/tools/cmake/toolchain/toolchain.py b/conan/tools/cmake/toolchain/toolchain.py index 8391f93790c..4313495bb42 100644 --- a/conan/tools/cmake/toolchain/toolchain.py +++ b/conan/tools/cmake/toolchain/toolchain.py @@ -1,4 +1,5 @@ import os +from pathlib import Path import textwrap from collections import OrderedDict @@ -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