Skip to content

Commit

Permalink
Fix duplicate presets (#14296)
Browse files Browse the repository at this point in the history
* use set to dedupe presets in user preset file

* unit test to verify creation of user presets
  • Loading branch information
wayneph01 authored Jul 18, 2023
1 parent a26104a commit 9075b95
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
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

0 comments on commit 9075b95

Please sign in to comment.