diff --git a/conan_provider.cmake b/conan_provider.cmake index 19c4c492..aa0b439a 100644 --- a/conan_provider.cmake +++ b/conan_provider.cmake @@ -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) diff --git a/tests/resources/change_generators/duplicate_generator/conanfile.py b/tests/resources/change_generators/duplicate_generator/conanfile.py new file mode 100644 index 00000000..34d5c493 --- /dev/null +++ b/tests/resources/change_generators/duplicate_generator/conanfile.py @@ -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() + diff --git a/tests/resources/change_generators/no_generator_py/conanfile.py b/tests/resources/change_generators/no_generator_py/conanfile.py new file mode 100644 index 00000000..411f44bb --- /dev/null +++ b/tests/resources/change_generators/no_generator_py/conanfile.py @@ -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") \ No newline at end of file diff --git a/tests/resources/change_generators/no_generator_txt/conanfile.txt b/tests/resources/change_generators/no_generator_txt/conanfile.txt new file mode 100644 index 00000000..bda2e24e --- /dev/null +++ b/tests/resources/change_generators/no_generator_txt/conanfile.txt @@ -0,0 +1,3 @@ +[requires] +hello/0.1 +bye/0.1 diff --git a/tests/resources/change_generators/single_generator/conanfile.py b/tests/resources/change_generators/single_generator/conanfile.py new file mode 100644 index 00000000..ea543210 --- /dev/null +++ b/tests/resources/change_generators/single_generator/conanfile.py @@ -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() diff --git a/tests/test_smoke.py b/tests/test_smoke.py index a682fc9a..fbed59ae 100644 --- a/tests/test_smoke.py +++ b/tests/test_smoke.py @@ -612,3 +612,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