Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
20 changes: 17 additions & 3 deletions conan_provider.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,27 @@ macro(conan_provide_dependency method package_name)
construct_profile_argument(_host_profile_flags CONAN_HOST_PROFILE)
construct_profile_argument(_build_profile_flags CONAN_BUILD_PROFILE)
get_property(_MULTICONFIG_GENERATOR GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(EXISTS "${CMAKE_SOURCE_DIR}/conanfile.py")
file(READ "${CMAKE_SOURCE_DIR}/conanfile.py" outfile)
if(NOT "${outfile}" MATCHES ".*CMakeDeps.*")
juansblanco marked this conversation as resolved.
Show resolved Hide resolved
message(WARNING "Cmake-conan: CMakeDeps generator was not defined in the conanfile")
endif()
set(generator "")
else ()
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")
juansblanco marked this conversation as resolved.
Show resolved Hide resolved
endif()
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(_MULTICONFIG_GENERATOR)
else()
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()

8 changes: 8 additions & 0 deletions tests/resources/change_generators/no_generator/conanfile.py
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")
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 @@ -40,6 +40,8 @@ def run(cmd, check=True):
@contextmanager
def chdir(folder):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to remove this completely (will do on a separate PR) as it is no longer used

cwd = os.getcwd()
if os.path.exists(folder):
shutil.rmtree(folder)
os.makedirs(folder, exist_ok=True)
os.chdir(folder)
try:
Expand Down Expand Up @@ -609,3 +611,34 @@ 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:
# CMakeDeps generator is declared in the generate() function in conanfile.py
def test_one_generator(self, capfd, basic_cmake_project):
source_dir, binary_dir = change_conanfile(basic_cmake_project, 'single_generator')
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 = change_conanfile(basic_cmake_project, 'duplicate_generator')
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 either place
def test_no_generator(self, capfd, basic_cmake_project):
source_dir, binary_dir = change_conanfile(basic_cmake_project, 'no_generator')
run(f'cmake -S {source_dir} -B {binary_dir} -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES={conan_provider} -DCMAKE_BUILD_TYPE=Release', check=False)
out, _ = capfd.readouterr()
assert 'Could NOT find hello (missing: hello_DIR)' in out

def change_conanfile(basic_cmake_project, gen_resource):
juansblanco marked this conversation as resolved.
Show resolved Hide resolved
source_dir, binary_dir = basic_cmake_project
os.chdir(source_dir.as_posix())
juansblanco marked this conversation as resolved.
Show resolved Hide resolved
os.remove("conanfile.txt")
shutil.copy2(src_dir / 'tests' / 'resources' / 'change_generators' / gen_resource / 'conanfile.py', source_dir)
return source_dir, binary_dir