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 duplicate presets #14296

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions conan/tools/cmake/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,14 @@ def _collect_user_inherits(output_dir, preset_prefix):
if os.path.exists(user_file):
user_json = json.loads(load(user_file))
for preset_type in types:
conan_inherits = []
for preset in user_json.get(preset_type, []):
inherits = preset.get("inherits", [])
if isinstance(inherits, str):
inherits = [inherits]
conan_inherits = [i for i in inherits if i.startswith(preset_prefix)]
if conan_inherits:
collected_targets.setdefault(preset_type, []).extend(conan_inherits)
conan_inherits.extend([i for i in inherits if i.startswith(preset_prefix)])
if len(conan_inherits):
collected_targets.setdefault(preset_type, []).extend(list(set(conan_inherits)))
return collected_targets

@staticmethod
Expand Down
61 changes: 61 additions & 0 deletions conans/test/integration/toolchains/cmake/test_cmaketoolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,67 @@ def layout(self):
assert presets["configurePresets"][0]["binaryDir"] == build_dir


def test_cmake_presets_shared_preset():
"""valid user preset file is created when multiple project presets inherit
from the same conan presets.
"""
client = TestClient()
project_presets = textwrap.dedent("""
{
"version": 4,
"cmakeMinimumRequired": {
"major": 3,
"minor": 23,
"patch": 0
},
"include":["ConanPresets.json"],
"configurePresets": [
{
"name": "debug1",
"inherits": ["conan-debug"]
},
{
"name": "debug2",
"inherits": ["conan-debug"]
},
{
"name": "release1",
"inherits": ["conan-release"]
},
{
"name": "release2",
"inherits": ["conan-release"]
}
]
}""")
conanfile = textwrap.dedent("""
from conan import ConanFile
from conan.tools.cmake import cmake_layout, CMakeToolchain

class TestPresets(ConanFile):
generators = ["CMakeDeps"]
settings = "build_type"

def layout(self):
cmake_layout(self)

def generate(self):
tc = CMakeToolchain(self)
tc.user_presets_path = 'ConanPresets.json'
tc.generate()
""")

client.save({"CMakePresets.json": project_presets,
"conanfile.py": conanfile,
"CMakeLists.txt": "" }) #File must exist for Conan to do Preset things.

client.run("install . -s build_type=Debug")

conan_presets = json.loads(client.load("ConanPresets.json"))
assert len(conan_presets["configurePresets"]) == 1
assert conan_presets["configurePresets"][0]["name"] == "conan-release"


def test_cmake_presets_multiconfig():
client = TestClient()
profile = textwrap.dedent("""
Expand Down