From 7efa4f7cf61cfdbefbfc761a911bb469ee261df5 Mon Sep 17 00:00:00 2001 From: wayneph01 <101033864+wayneph01@users.noreply.github.com> Date: Thu, 13 Jul 2023 13:46:55 -0500 Subject: [PATCH 1/2] use set to dedupe presets in user preset file --- conan/tools/cmake/presets.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/conan/tools/cmake/presets.py b/conan/tools/cmake/presets.py index 067e958ee00..b0476127082 100644 --- a/conan/tools/cmake/presets.py +++ b/conan/tools/cmake/presets.py @@ -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 From 55f63dea4c20200e04487a269ece6c61e37e0c6c Mon Sep 17 00:00:00 2001 From: wayneph01 <101033864+wayneph01@users.noreply.github.com> Date: Thu, 13 Jul 2023 13:50:21 -0500 Subject: [PATCH 2/2] unit test to verify creation of user presets --- .../toolchains/cmake/test_cmaketoolchain.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/conans/test/integration/toolchains/cmake/test_cmaketoolchain.py b/conans/test/integration/toolchains/cmake/test_cmaketoolchain.py index 7157f441870..5156bc1769e 100644 --- a/conans/test/integration/toolchains/cmake/test_cmaketoolchain.py +++ b/conans/test/integration/toolchains/cmake/test_cmaketoolchain.py @@ -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("""