-
Notifications
You must be signed in to change notification settings - Fork 989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #16078 - add type to CMakePresets cacheVariables #16079
Conversation
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"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace cache_variables with an object with both value
and type
where appropriate
Test example:
tc = CMakeToolchain(self)
tc.cache_variables["MY_BOOL"] = False
tc.cache_variables["MY_FILE_PATH"] = Path("/path/to/file") # assert Path("/path/to/file").is_file()
tc.cache_variables["MY_FILE_PATH"] = Path("/path/to/dir") # assert Path("/path/to/file").is_dir()
tc.generate()
conan/tools/cmake/presets.py
Outdated
@@ -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} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also add it to a few vars in presets.py itself
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} " \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Properly format the message that is printed as well:
conanfile.py: If your CMake version is not compatible
with CMakePresets (<3.23) call cmake like:
'cmake <path> -G Ninja
-DCMAKE_TOOLCHAIN_FILE:FILEPATH=/media/path/to/conan_toolchain.cmake
-DMY_BOOL:BOOL=ON
-DMY_FILE_PATH:FILEPATH=/path/to/file
-DMY_DIR_PATH:PATH=/path/to/dir
6c06148
to
dbbb1c3
Compare
I have done a commit to fix one broken "integration" test, but then a lot of "functional" tests are failing: logs.txt (Linux logs, but other OSs are failing as well) And it is not failing in obvious ways. So this is a big concern, that this change might be breaking users in unexpected ways. |
Hi @jmarrec Have you been able to check my comment above?
The changes you are proposing are not risk-free, even if the broken tests might be fixed, the risk of breaking users is still there. So it would be necessary to provide some broken/bug cases in which the proposed changes solve the issue. Can you please try to provide such a "red" test to re-consider this PR? Otherwise I think we will have to discard this PR, even if the typing is a nice-to-have, if it is not solving some actual user issues, it might not be worth the risk of breaking users. |
I can't think of a way to produce a bug, so if you don't want to pursue it that's fine. |
Yes, I think that unless it is solving some specific issue that we can reproduce, it would be better not to add this extra complexity, as it doesn't seem completely risk-free. Thanks for understanding. |
Changelog: Feature: add type to CMakePresets cacheVariables
Close #16078
pathlib.Path
to thetc.cache_variables
)develop
branch, documenting this one.