From 7d2a28a4960d0e5b51fac6e2427414bc3ab7a716 Mon Sep 17 00:00:00 2001 From: memsharded Date: Mon, 11 Mar 2024 22:45:43 +0100 Subject: [PATCH 1/5] fix cmakedeps multi conditional --- .../cmake/cmakedeps/templates/target_data.py | 4 - .../cmakedeps/test_conditional_build_type.py | 101 ++++++++++++++++++ 2 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 conans/test/functional/toolchains/cmake/cmakedeps/test_conditional_build_type.py diff --git a/conan/tools/cmake/cmakedeps/templates/target_data.py b/conan/tools/cmake/cmakedeps/templates/target_data.py index 2bc8c52094f..c4592d0c912 100644 --- a/conan/tools/cmake/cmakedeps/templates/target_data.py +++ b/conan/tools/cmake/cmakedeps/templates/target_data.py @@ -93,12 +93,8 @@ def template(self): {% else %} set({{ pkg_name }}_COMPONENT_NAMES "") {% endif %} - {% if dependency_filenames %} list(APPEND {{ pkg_name }}_FIND_DEPENDENCY_NAMES {{ dependency_filenames }}) list(REMOVE_DUPLICATES {{ pkg_name }}_FIND_DEPENDENCY_NAMES) - {% else %} - set({{ pkg_name }}_FIND_DEPENDENCY_NAMES "") - {% endif %} {% for dep_name, mode in dependency_find_modes.items() %} set({{ dep_name }}_FIND_MODE "{{ mode }}") {% endfor %} diff --git a/conans/test/functional/toolchains/cmake/cmakedeps/test_conditional_build_type.py b/conans/test/functional/toolchains/cmake/cmakedeps/test_conditional_build_type.py new file mode 100644 index 00000000000..150a803c627 --- /dev/null +++ b/conans/test/functional/toolchains/cmake/cmakedeps/test_conditional_build_type.py @@ -0,0 +1,101 @@ +import textwrap + +from conans.test.utils.tools import TestClient + + +def test_conditional_build_type(): + # https://github.com/conan-io/conan/issues/15851 + c = TestClient() + c.run("new cmake_lib -d name=pkga -d version=0.1") + c.run("create . -s build_type=Debug -tf=") + + c.save({}, clean_first=True) + c.run("new cmake_lib -d name=pkgb -d version=0.1 -d requires=pkga/0.1") + conanfile = textwrap.dedent(""" + from conan import ConanFile + from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps + + class pkgbRecipe(ConanFile): + name = "pkgb" + version = "0.1" + package_type = "static-library" + settings = "os", "compiler", "build_type", "arch" + exports_sources = "CMakeLists.txt", "src/*", "include/*" + + def layout(self): + cmake_layout(self) + + def generate(self): + deps = CMakeDeps(self) + deps.generate() + tc = CMakeToolchain(self) + if self.settings.build_type == "Debug": + tc.cache_variables["USE_PKGA"] = 1 + tc.preprocessor_definitions["USE_PKGA"] = 1 + tc.generate() + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def package(self): + cmake = CMake(self) + cmake.install() + + def package_info(self): + self.cpp_info.libs = ["pkgb"] + + def requirements(self): + if self.settings.build_type == "Debug": + self.requires("pkga/0.1") + """) + cmake = textwrap.dedent("""\ + cmake_minimum_required(VERSION 3.15) + project(pkgb CXX) + + add_library(pkgb src/pkgb.cpp) + target_include_directories(pkgb PUBLIC include) + + if(USE_PKGA) + find_package(pkga CONFIG REQUIRED) + target_link_libraries(pkgb PRIVATE pkga::pkga) + endif() + + set_target_properties(pkgb PROPERTIES PUBLIC_HEADER "include/pkgb.h") + install(TARGETS pkgb) + """) + pkgb_cpp = textwrap.dedent(r""" + #include + #include "pkgb.h" + #ifdef USE_PKGA + #include "pkga.h" + #endif + + void pkgb(){ + #ifdef USE_PKGA + pkga(); + #endif + + #ifdef NDEBUG + std::cout << "pkgb/0.1: Hello World Release!\n"; + #else + std::cout << "pkgb/0.1: Hello World Debug!\n"; + #endif + } + """) + c.save({"conanfile.py": conanfile, + "CMakeLists.txt": cmake, + "src/pkgb.cpp": pkgb_cpp}) + c.run("create . -s build_type=Debug -tf=") + assert "pkga/0.1" in c.out + c.run("create . -s build_type=Release -tf=") # without dep to pkga + assert "pkga" not in c.out + + c.save({}, clean_first=True) + c.run("new cmake_lib -d name=pkgc -d version=0.1 -d requires=pkgb/0.1") + c.run("build . -s build_type=Debug") + c.run("build . -s build_type=Release") + # This used to crash because "pkga::pkga" + print(c.out) + print(c.current_folder) From fe1d5d3e2e6c8dc8f521c8eac32c139f4d1605ae Mon Sep 17 00:00:00 2001 From: memsharded Date: Mon, 11 Mar 2024 23:23:21 +0100 Subject: [PATCH 2/5] fix test --- conans/test/integration/test_components_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conans/test/integration/test_components_error.py b/conans/test/integration/test_components_error.py index 51bf8911ca1..2e5818bd89f 100644 --- a/conans/test/integration/test_components_error.py +++ b/conans/test/integration/test_components_error.py @@ -51,5 +51,5 @@ class t3Conan(ConanFile): c.run("install t3") arch = c.get_default_host_profile().settings['arch'] - assert 'set(t2_FIND_DEPENDENCY_NAMES "")' in c.load(f"t3/t2-release-{arch}-data.cmake") + assert 'list(APPEND t2_FIND_DEPENDENCY_NAMES )' in c.load(f"t3/t2-release-{arch}-data.cmake") assert not os.path.exists(os.path.join(c.current_folder, "t3/t1-config.cmake")) From 8c82cdc0d1ca376144438c7cf06bb125437652b2 Mon Sep 17 00:00:00 2001 From: memsharded Date: Tue, 12 Mar 2024 00:10:40 +0100 Subject: [PATCH 3/5] wip --- conan/tools/cmake/cmakedeps/templates/target_data.py | 8 ++++++-- .../cmake/cmakedeps/test_cmakedeps_transitivity.py | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/conan/tools/cmake/cmakedeps/templates/target_data.py b/conan/tools/cmake/cmakedeps/templates/target_data.py index c4592d0c912..1d4c8b96d3f 100644 --- a/conan/tools/cmake/cmakedeps/templates/target_data.py +++ b/conan/tools/cmake/cmakedeps/templates/target_data.py @@ -93,8 +93,12 @@ def template(self): {% else %} set({{ pkg_name }}_COMPONENT_NAMES "") {% endif %} - list(APPEND {{ pkg_name }}_FIND_DEPENDENCY_NAMES {{ dependency_filenames }}) - list(REMOVE_DUPLICATES {{ pkg_name }}_FIND_DEPENDENCY_NAMES) + if(DEFINED {{ pkg_name }}_FIND_DEPENDENCY_NAMES) + list(APPEND {{ pkg_name }}_FIND_DEPENDENCY_NAMES {{ dependency_filenames }}) + list(REMOVE_DUPLICATES {{ pkg_name }}_FIND_DEPENDENCY_NAMES) + else() + set({{ pkg_name }}_FIND_DEPENDENCY_NAMES {{ dependency_filenames }}) + endif() {% for dep_name, mode in dependency_find_modes.items() %} set({{ dep_name }}_FIND_MODE "{{ mode }}") {% endfor %} diff --git a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_transitivity.py b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_transitivity.py index 79a18786d71..f6b63b7403e 100644 --- a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_transitivity.py +++ b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_transitivity.py @@ -147,9 +147,9 @@ class Pkg(ConanFile): c.run("install . -o engine*:shared=True") assert not os.path.exists(os.path.join(c.current_folder, f"matrix-release-{arch}-data.cmake")) cmake = c.load(f"engine-release-{arch}-data.cmake") - assert 'set(engine_FIND_DEPENDENCY_NAMES "")' in cmake + assert 'list(APPEND engine_FIND_DEPENDENCY_NAMES )' in cmake c.run("install . -o engine*:shared=True --build=engine*") assert not os.path.exists(os.path.join(c.current_folder, f"matrix-release-{arch}-data.cmake")) cmake = c.load(f"engine-release-{arch}-data.cmake") - assert 'set(engine_FIND_DEPENDENCY_NAMES "")' in cmake + assert 'list(APPEND engine_FIND_DEPENDENCY_NAMES )' in cmake From d44aff82fea0d9d9389db583401a661a5137c2a2 Mon Sep 17 00:00:00 2001 From: memsharded Date: Tue, 12 Mar 2024 00:25:44 +0100 Subject: [PATCH 4/5] fix --- .../functional/toolchains/cmake/cmakedeps/test_cmakedeps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py index a6b06deb0c0..bcb82dc3338 100644 --- a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py +++ b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps.py @@ -521,7 +521,7 @@ def test_private_transitive(): client.run("install consumer -g CMakeDeps -s arch=x86_64 -s build_type=Release -of=. -v") client.assert_listed_binary({"dep/0.1": (NO_SETTINGS_PACKAGE_ID, "Skip")}) data_cmake = client.load("pkg-release-x86_64-data.cmake") - assert 'set(pkg_FIND_DEPENDENCY_NAMES "")' in data_cmake + assert 'list(APPEND pkg_FIND_DEPENDENCY_NAMES )' in data_cmake @pytest.mark.tool("cmake") From 84f8633ac4ed0af4d2454461aafb3f3132da22ab Mon Sep 17 00:00:00 2001 From: memsharded Date: Tue, 12 Mar 2024 19:50:38 +0100 Subject: [PATCH 5/5] tried test faster, didn't work --- .../toolchains/cmake/cmakedeps/test_conditional_build_type.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conans/test/functional/toolchains/cmake/cmakedeps/test_conditional_build_type.py b/conans/test/functional/toolchains/cmake/cmakedeps/test_conditional_build_type.py index 150a803c627..27b7369a62e 100644 --- a/conans/test/functional/toolchains/cmake/cmakedeps/test_conditional_build_type.py +++ b/conans/test/functional/toolchains/cmake/cmakedeps/test_conditional_build_type.py @@ -6,6 +6,7 @@ def test_conditional_build_type(): # https://github.com/conan-io/conan/issues/15851 c = TestClient() + # A header-only library can't be used for testing, it doesn't fail c.run("new cmake_lib -d name=pkga -d version=0.1") c.run("create . -s build_type=Debug -tf=") @@ -97,5 +98,4 @@ def requirements(self): c.run("build . -s build_type=Debug") c.run("build . -s build_type=Release") # This used to crash because "pkga::pkga" - print(c.out) - print(c.current_folder) + assert "conanfile.py (pkgc/0.1): Running CMake.build()" in c.out