-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add cmake_extra_variables
property for CMakeConfigDeps
and CMakeDeps
#18822
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
Merged
memsharded
merged 16 commits into
conan-io:develop2
from
AbrilRBS:ar/cmake-extra-variables-package_info
Sep 29, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
60b232f
Sketch cmake_extra_variables property
AbrilRBS 92e6ba1
Typo
AbrilRBS 4e48054
Move test, add cache check
AbrilRBS 7ae60dd
Add feature to old CMakeDeps too
AbrilRBS be7251c
Merge branch 'develop2' into ar/cmake-extra-variables-package_info
AbrilRBS e076a9d
Test for dependnecy overriding profile
AbrilRBS 8076b64
Merge branch 'ar/cmake-extra-variables-package_info' of github.com:Ab…
AbrilRBS 7a67db8
Merge branch 'develop2' into ar/cmake-extra-variables-package_info
AbrilRBS 22e67e4
Add missing CMake
AbrilRBS d062644
fix missing import
AbrilRBS 4959c6c
Fix precedence
AbrilRBS 6243867
Remove from dict
AbrilRBS 0af3898
Give access to conf to ConanFileInterface
AbrilRBS 9d977e0
Merge branch 'develop2' into ar/cmake-extra-variables-package_info
AbrilRBS e57f9f9
Fix testing for new cmakedeps2
AbrilRBS e67da91
Don't set them althogether if defined in conf
AbrilRBS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,41 @@ | ||
from conan.errors import ConanException | ||
|
||
|
||
def is_multi_configuration(generator): | ||
if not generator: | ||
return False | ||
return "Visual" in generator or "Xcode" in generator or "Multi-Config" in generator | ||
|
||
|
||
def parse_extra_variable(source, key, value): | ||
AbrilRBS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
CMAKE_CACHE_TYPES = ["BOOL", "FILEPATH", "PATH", "STRING", "INTERNAL"] | ||
if isinstance(value, str): | ||
return f"\"{value}\"" | ||
elif isinstance(value, (int, float)): | ||
return value | ||
elif isinstance(value, dict): | ||
var_value = parse_extra_variable(source, key, value.get("value")) | ||
is_force = value.get("force") | ||
if is_force: | ||
if not isinstance(is_force, bool): | ||
raise ConanException(f'{source} "{key}" "force" must be a boolean') | ||
is_cache = value.get("cache") | ||
if is_cache: | ||
if not isinstance(is_cache, bool): | ||
raise ConanException(f'{source} "{key}" "cache" must be a boolean') | ||
var_type = value.get("type") | ||
if not var_type: | ||
raise ConanException(f'{source} "{key}" needs "type" defined for cache variable') | ||
if var_type not in CMAKE_CACHE_TYPES: | ||
raise ConanException(f'{source} "{key}" invalid type "{var_type}" for cache variable.' | ||
f' Possible types: {", ".join(CMAKE_CACHE_TYPES)}') | ||
# Set docstring as variable name if not defined | ||
docstring = value.get("docstring") or key | ||
force_str = " FORCE" if is_force else "" # Support python < 3.11 | ||
return f"{var_value} CACHE {var_type} \"{docstring}\"{force_str}" | ||
else: | ||
if is_force: | ||
raise ConanException(f'{source} "{key}" "force" is only allowed for cache variables') | ||
return var_value | ||
raise ConanException(f'{source} "{key}" has invalid type. Allowed types: str, int, float, dict,' | ||
f' got {type(value)}') |
57 changes: 57 additions & 0 deletions
57
test/functional/toolchains/cmake/test_cmake_extra_variables.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import textwrap | ||
import pytest | ||
|
||
from conan.test.assets.genconanfile import GenConanfile | ||
from conan.test.utils.tools import TestClient | ||
new_value = "will_break_next" | ||
|
||
|
||
@pytest.mark.tool("cmake", "3.27") | ||
@pytest.mark.parametrize("generator", ["CMakeDeps", "CMakeConfigDeps"]) | ||
def test_package_info_extra_variables(generator): | ||
""" The dependencies can define extra variables to be used in CMake, | ||
but if the user is setting the cmake_extra_variables conf, | ||
those should have precedence. | ||
""" | ||
client = TestClient() | ||
dep_conanfile = textwrap.dedent(""" | ||
from conan import ConanFile | ||
|
||
class Pkg(ConanFile): | ||
name = "dep" | ||
version = "0.1" | ||
|
||
def package_info(self): | ||
self.cpp_info.set_property("cmake_extra_variables", {"FOO": 42}) | ||
""") | ||
client.save({"dep/conanfile.py": dep_conanfile}) | ||
client.run("create dep") | ||
|
||
cmakelists = textwrap.dedent(""" | ||
cmake_minimum_required(VERSION 3.27) | ||
project(myproject CXX) | ||
find_package(dep CONFIG REQUIRED) | ||
message(STATUS "FOO=${FOO}") | ||
""") | ||
|
||
|
||
conanfile = textwrap.dedent(f""" | ||
from conan import ConanFile | ||
from conan.tools.cmake import CMake | ||
|
||
class Pkg(ConanFile): | ||
settings = "os", "arch", "compiler", "build_type" | ||
generators = "{generator}", "CMakeToolchain" | ||
requires = "dep/0.1" | ||
def build(self): | ||
cmake = CMake(self) | ||
cmake.configure() | ||
""") | ||
client.save({"CMakeLists.txt": cmakelists, | ||
"conanfile.py": conanfile}) | ||
conf = f"-c tools.cmake.cmakedeps:new={new_value}" if generator == "CMakeConfigDeps" else "" | ||
client.run(f"build . {conf} " | ||
"""-c tools.cmake.cmaketoolchain:extra_variables="{'FOO': '9'}" """) | ||
|
||
assert "-- FOO=9" in client.out | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Note quite sure if the config is the best place for this, I write them here for symmetry with CMakeConfigDeps where they are written next to the
cmake_additional_variables_prefixes
too, maybe we want to move them elsewhere?