Skip to content
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

Closed
wants to merge 4 commits into from

Conversation

jmarrec
Copy link
Contributor

@jmarrec jmarrec commented Apr 15, 2024

Changelog: Feature: add type to CMakePresets cacheVariables

Close #16078

  • Refer to the issue that supports this Pull Request.
  • If the issue has missing info, explain the purpose/use case/pain/need that covers this Pull Request.
  • I've read the Contributing guide.
  • I've followed the PEP8 style guides for Python code.
  • I've opened another PR in the Conan docs repo to the develop branch, documenting this one.

@CLAassistant
Copy link

CLAassistant commented Apr 15, 2024

CLA assistant check
All committers have signed the CLA.

Comment on lines +244 to +258
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"}
Copy link
Contributor Author

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()

@@ -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}
Copy link
Contributor Author

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

Comment on lines +161 to +176
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} " \
Copy link
Contributor Author

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

@memsharded
Copy link
Member

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.
To move this forward it will be necessary a very compelling example of something that clearly fails (and can be marked as a bug) when the type of the cache variable is not specified, because so far it has been working quite smoothly.

@memsharded
Copy link
Member

Hi @jmarrec

Have you been able to check my comment above?

To move this forward it will be necessary a very compelling example of something that clearly fails (and can be marked as a bug) when the type of the cache variable is not specified, because so far it has been working quite smoothly.

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.

@memsharded memsharded added the staled The issue has been inactive for a while and will be closed soon label Apr 27, 2024
@jmarrec
Copy link
Contributor Author

jmarrec commented May 3, 2024

I can't think of a way to produce a bug, so if you don't want to pursue it that's fine.

@jmarrec jmarrec closed this May 3, 2024
@memsharded
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
staled The issue has been inactive for a while and will be closed soon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature] CMakeToolchain cacheVariables should have a type
3 participants