Skip to content

Commit

Permalink
Fix for CMakeDeps generator being duplicated when using a conanfile.py (
Browse files Browse the repository at this point in the history
#584)

* Fix for CMakeDeps generator being duplicated when using a conanfile.py

---------

Co-authored-by: Luis Caro Campos <[email protected]>
  • Loading branch information
juansblanco and jcar87 authored Nov 14, 2023
1 parent 9376d81 commit bd279af
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 3 deletions.
20 changes: 17 additions & 3 deletions conan_provider.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,28 @@ macro(conan_provide_dependency method package_name)
endif()
construct_profile_argument(_host_profile_flags CONAN_HOST_PROFILE)
construct_profile_argument(_build_profile_flags CONAN_BUILD_PROFILE)
if(EXISTS "${CMAKE_SOURCE_DIR}/conanfile.py")
file(READ "${CMAKE_SOURCE_DIR}/conanfile.py" outfile)
if(NOT "${outfile}" MATCHES ".*CMakeDeps.*")
message(WARNING "Cmake-conan: CMakeDeps generator was not defined in the conanfile")
endif()
set(generator "")
elseif (EXISTS "${CMAKE_SOURCE_DIR}/conanfile.txt")
file(READ "${CMAKE_SOURCE_DIR}/conanfile.txt" outfile)
if(NOT "${outfile}" MATCHES ".*CMakeDeps.*")
message(WARNING "Cmake-conan: CMakeDeps generator was not defined in the conanfile. "
"Please define the generator as it will be mandatory in the future")
endif()
set(generator "-g;CMakeDeps")
endif()
get_property(_multiconfig_generator GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(NOT _multiconfig_generator)
message(STATUS "CMake-Conan: Installing single configuration ${CMAKE_BUILD_TYPE}")
conan_install(${_host_profile_flags} ${_build_profile_flags} --build=missing -g CMakeDeps)
conan_install(${_host_profile_flags} ${_build_profile_flags} --build=missing ${generator})
else()
message(STATUS "CMake-Conan: Installing both Debug and Release")
conan_install(${_host_profile_flags} ${_build_profile_flags} -s build_type=Release --build=missing -g CMakeDeps)
conan_install(${_host_profile_flags} ${_build_profile_flags} -s build_type=Debug --build=missing -g CMakeDeps)
conan_install(${_host_profile_flags} ${_build_profile_flags} -s build_type=Release --build=missing ${generator})
conan_install(${_host_profile_flags} ${_build_profile_flags} -s build_type=Debug --build=missing ${generator})
endif()
unset(_host_profile_flags)
unset(_build_profile_flags)
Expand Down
15 changes: 15 additions & 0 deletions tests/resources/change_generators/duplicate_generator/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from conan import ConanFile
from conan.tools.cmake import CMakeDeps

class testRecipe(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeToolchain", "CMakeDeps"

def requirements(self):
self.requires("hello/0.1")
self.requires("bye/0.1")

def generate(self):
deps = CMakeDeps(self)
deps.generate()

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from conan import ConanFile

class testRecipe(ConanFile):
settings = "os", "compiler", "build_type", "arch"

def requirements(self):
self.requires("hello/0.1")
self.requires("bye/0.1")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[requires]
hello/0.1
bye/0.1
13 changes: 13 additions & 0 deletions tests/resources/change_generators/single_generator/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from conan import ConanFile
from conan.tools.cmake import CMakeDeps

class testRecipe(ConanFile):
settings = "os", "compiler", "build_type", "arch"

def requirements(self):
self.requires("hello/0.1")
self.requires("bye/0.1")

def generate(self):
deps = CMakeDeps(self)
deps.generate()
33 changes: 33 additions & 0 deletions tests/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,3 +636,36 @@ def test_msvc_x86(self, capfd, basic_cmake_project):
run(f'cmake -S {source_dir} -B {binary_dir} -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES={conan_provider} -G "Visual Studio 16 2019" -A Win32')
out, _ = capfd.readouterr()
assert "arch=x86" in out


class TestCMakeDepsGenerators:
@staticmethod
def copy_resource(gen_resource, source_dir):
os.remove(source_dir / "conanfile.txt")
shutil.copytree(src_dir / 'tests' / 'resources' / 'change_generators' / gen_resource, source_dir, dirs_exist_ok=True)

# CMakeDeps generator is declared in the generate() function in conanfile.py
def test_single_generator(self, capfd, basic_cmake_project):
source_dir, binary_dir = basic_cmake_project
self.copy_resource('single_generator', source_dir)
run(f'cmake -S {source_dir} -B {binary_dir} -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES={conan_provider} -DCMAKE_BUILD_TYPE=Release')
out, _ = capfd.readouterr()
assert 'Generating done' in out

# CMakeDeps generator is declared both in generators attribute and generate() function in conanfile.py
def test_duplicate_generator(self, capfd, basic_cmake_project):
source_dir, binary_dir = basic_cmake_project
self.copy_resource('duplicate_generator', source_dir)
run(f'cmake -S {source_dir} -B {binary_dir} -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES={conan_provider} -DCMAKE_BUILD_TYPE=Release', check=False)
_, err = capfd.readouterr()
assert ('ConanException: CMakeDeps is declared in the generators attribute, but was instantiated in the '
'generate() method too') in err

# CMakeDeps generator is not declared in the conanfile
@pytest.mark.parametrize("resource_path", ["no_generator_py", "no_generator_txt"])
def test_no_generator_py(self, capfd, basic_cmake_project, resource_path):
source_dir, binary_dir = basic_cmake_project
self.copy_resource(resource_path, source_dir)
run(f'cmake -S {source_dir} -B {binary_dir} -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES={conan_provider} -DCMAKE_BUILD_TYPE=Release', check=False)
_, err = capfd.readouterr()
assert 'Cmake-conan: CMakeDeps generator was not defined in the conanfile' in err

0 comments on commit bd279af

Please sign in to comment.