Skip to content

Commit

Permalink
fix cmakedeps multi conditional (#15853)
Browse files Browse the repository at this point in the history
* fix cmakedeps multi conditional

* fix test

* wip

* fix

* tried test faster, didn't work
  • Loading branch information
memsharded authored Mar 13, 2024
1 parent df77297 commit ea07cc3
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 10 deletions.
12 changes: 6 additions & 6 deletions conan/tools/cmake/cmakedeps/templates/target_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ 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 %}
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 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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()
# 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=")

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 <iostream>
#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"
assert "conanfile.py (pkgc/0.1): Running CMake.build()" in c.out
2 changes: 1 addition & 1 deletion conans/test/integration/test_components_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

0 comments on commit ea07cc3

Please sign in to comment.