Repository: conan-io/conan-center-index

Issue:
[package] MPC/1.2.0 lacks support for Conan 2.0
### Description

mpc has yet to be ported to Conan 2.0

### Package and Environment Details

Package Name/Version: mpc/1.2.0
Operating System+version: Debian 10
Compiler+version: gcc 8
Conan version: conan 2.0.0-beta6
Python version: Python 3.7.3

### Conan profile

Profile host:
[settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.version=8
os=Linux
[options]
cmake*:bootstrap=True
[tool_requires]
!openssl*: cmake/3.24.2


### Steps to reproduce

conan-2.0 build -pr:b tools.jinja -pr:h tools.jinja --name mpc--version 1.2.0 .

### Logs

<details><summary>Click to expand log</summary>

```
conan-2.0 source --name mpc --version 1.2.0 .
ERROR: Error loading conanfile at '/tmp/tmp.R02tApJCqY/mpc/conanfile.py': Unable to load conanfile in /tmp/tmp.R02tApJCqY/mpc/conanfile.py
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/tmp/tmp.R02tApJCqY/mpc/conanfile.py", line 1, in <module>
    from conans import ConanFile, AutoToolsBuildEnvironment, tools
ImportError: cannot import name 'ConanFile' from 'conans' (/mathworks/devel/sandbox/marc/conan-2.0/conan-io/conans/__init__.py)```

</details>



Discussion:
Will submit proposed compatibility changes shortly

Resolving patch:
diff --git a/recipes/mpc/all/conanfile.py b/recipes/mpc/all/conanfile.py
--- a/recipes/mpc/all/conanfile.py
+++ b/recipes/mpc/all/conanfile.py
@@ -1,11 +1,20 @@
-from conans import ConanFile, AutoToolsBuildEnvironment, tools
-from conans.errors import ConanInvalidConfiguration
+from conan import ConanFile
+from conan.tools.build import cross_building
+from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
+from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, rm, rmdir
+from conan.tools.layout import basic_layout
+from conan.tools.gnu import Autotools, AutotoolsToolchain, AutotoolsDeps
+from conan.tools.microsoft import is_msvc, unix_path
+from conan.tools.apple import fix_apple_shared_install_name
+from conan.errors import ConanInvalidConfiguration
 import os
 
-required_conan_version = ">=1.33.0"
+
+required_conan_version = ">=1.54.0"
 
 class MpcConan(ConanFile):
     name = "mpc"
+    package_type = "library"
     description = "GNU MPC is a C library for the arithmetic of complex numbers with arbitrarily high precision " \
                   "and correct rounding of the result"
     topics = ("conan", "mpc", "multiprecision", "math", "mathematics")
@@ -15,66 +24,78 @@ class MpcConan(ConanFile):
     settings = "os", "arch", "compiler", "build_type"
     options = {"shared": [True, False], "fPIC": [True, False]}
     default_options = {"shared": False, "fPIC": True}
-    exports_sources = "patches/**"
-    _autotools = None
 
-    @property
-    def _source_subfolder(self):
-        return "source_subfolder"
+    def export_sources(self):
+        export_conandata_patches(self)
 
     def config_options(self):
         if self.settings.os == 'Windows':
-            del self.options.fPIC
+            self.options.rm_safe("fPIC")
 
     def configure(self):
         if self.options.shared:
-            del self.options.fPIC
-        del self.settings.compiler.libcxx
-        del self.settings.compiler.cppstd
+            self.options.rm_safe("fPIC")
+        self.settings.rm_safe("compiler.libcxx")
+        self.settings.rm_safe("compiler.cppstd")
 
     def requirements(self):
-        self.requires("mpfr/4.1.0")
+        self.requires("gmp/6.2.1", transitive_headers=True)
+        self.requires("mpfr/4.1.0", transitive_headers=True)
 
     def validate(self):
-        # FIXME: add Visual Studio support, upstream has a makefile.vc
-        if self.settings.compiler == "Visual Studio":
-            raise ConanInvalidConfiguration("mpc can be built with Visual Studio, but it's not supported yet in this recipe.")
+        # FIXME: add msvc support, upstream has a makefile.vc
+        if is_msvc(self):
+            raise ConanInvalidConfiguration("mpc can be built with msvc, but it's not supported yet in this recipe.")
 
     @property
     def _settings_build(self):
         return getattr(self, "settings_build", self.settings)
 
     def build_requirements(self):
-        if self._settings_build.os == "Windows" and not tools.get_env("CONAN_BASH_PATH"):
-            self.build_requires("msys2/cci.latest")
+        if self._settings_build.os == "Windows":
+            self.win_bash = True
+            if not self.conf.get("tools.microsoft.bash:path", check_type=str):
+                self.tool_requires("msys2/cci.latest")
+
+    def layout(self):
+        basic_layout(self, src_folder="src")
 
     def source(self):
-        tools.get(**self.conan_data["sources"][self.version], strip_root=True, destination=self._source_subfolder)
+        get(self, **self.conan_data["sources"][self.version],
+            destination=self.source_folder, strip_root=True)
 
-    def _configure_autotools(self):
-        if self._autotools:
-            return self._autotools
-        self._autotools = AutoToolsBuildEnvironment(self, win_bash=tools.os_info.is_windows)
-        args = []
-        if self.options.shared:
-            args.extend(["--disable-static", "--enable-shared"])
-        else:
-            args.extend(["--disable-shared", "--enable-static"])
-        self._autotools.configure(args=args, configure_dir=self._source_subfolder)
-        return self._autotools
+    def generate(self):
+        env = VirtualBuildEnv(self)
+        env.generate()
+        if not cross_building(self):
+            env = VirtualRunEnv(self)
+            env.generate(scope="build")
+
+        tc = AutotoolsToolchain(self)
+        tc.configure_args.append(f'--with-gmp={unix_path(self, self.dependencies["gmp"].package_folder)}')
+        tc.configure_args.append(f'--with-mpfr={unix_path(self, self.dependencies["mpfr"].package_folder)}')
+        tc.generate()
+
+        tc = AutotoolsDeps(self)
+        tc.generate()
 
     def build(self):
-        for patch in self.conan_data.get("patches", {}).get(self.version, []):
-            tools.patch(**patch)
-        autotools = self._configure_autotools()
+        apply_conandata_patches(self)
+        autotools = Autotools(self)
+        if not os.path.exists(os.path.join(self.source_folder, "configure")):
+            autotools.autoreconf(["-i"])
+        autotools.configure()
         autotools.make()
 
     def package(self):
-        self.copy(pattern="COPYING.LESSER", dst="licenses", src=self._source_subfolder)
-        autotools = self._configure_autotools()
+        copy(self, "COPYING.LESSER", self.source_folder, os.path.join(self.package_folder, "licenses"), keep_path=False)
+        autotools = Autotools(self)
         autotools.install()
-        tools.rmdir(os.path.join(self.package_folder, "share"))
-        tools.remove_files_by_mask(os.path.join(self.package_folder, "lib"), "*.la")
+        fix_apple_shared_install_name(self)
+        rmdir(self, os.path.join(self.package_folder, "share"))
+        rm(self, "*.la", os.path.join(self.package_folder, "lib"), recursive=True)
 
     def package_info(self):
         self.cpp_info.libs = ["mpc"]
+        if self.settings.os in ["Linux", "FreeBSD"]:
+            self.cpp_info.system_libs = ["m"]


Test patch:
diff --git a/recipes/mpc/all/test_package/CMakeLists.txt b/recipes/mpc/all/test_package/CMakeLists.txt
--- a/recipes/mpc/all/test_package/CMakeLists.txt
+++ b/recipes/mpc/all/test_package/CMakeLists.txt
@@ -1,8 +1,7 @@
 cmake_minimum_required(VERSION 3.1)
 project(test_package C)
 
-include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
-conan_basic_setup()
+find_package(mpc REQUIRED CONFIG)
 
 add_executable(${PROJECT_NAME} test_package.c)
-target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
+target_link_libraries(${PROJECT_NAME} PRIVATE mpc::mpc)
diff --git a/recipes/mpc/all/test_package/conanfile.py b/recipes/mpc/all/test_package/conanfile.py
--- a/recipes/mpc/all/test_package/conanfile.py
+++ b/recipes/mpc/all/test_package/conanfile.py
@@ -1,10 +1,18 @@
-from conans import ConanFile, CMake, tools
+from conan import ConanFile
+from conan.tools.cmake import CMake, cmake_layout
+from conan.tools.build import cross_building
 import os
 
 
 class TestPackageConan(ConanFile):
     settings = "os", "compiler", "build_type", "arch"
-    generators = "cmake"
+    generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"
+
+    def layout(self):
+        cmake_layout(self)
+
+    def requirements(self):
+        self.requires(self.tested_reference_str)
 
     def build(self):
         cmake = CMake(self)
@@ -12,6 +20,6 @@ def build(self):
         cmake.build()
 
     def test(self):
-        if not tools.cross_building(self.settings):
-            bin_path = os.path.join("bin", "test_package")
-            self.run(bin_path, run_environment=True)
+        if not cross_building(self):
+            bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
+            self.run(bin_path, env="conanrun")
diff --git a/recipes/mpc/all/test_v1_package/CMakeLists.txt b/recipes/mpc/all/test_v1_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/mpc/all/test_v1_package/CMakeLists.txt
@@ -0,0 +1,8 @@
+cmake_minimum_required(VERSION 3.1)
+project(test_package)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup(TARGETS)
+
+add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package
+                 ${CMAKE_CURRENT_BINARY_DIR}/test_package)
diff --git a/recipes/mpc/all/test_v1_package/conanfile.py b/recipes/mpc/all/test_v1_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/mpc/all/test_v1_package/conanfile.py
@@ -0,0 +1,17 @@
+from conans import ConanFile, CMake, tools
+import os
+
+
+class TestPackageConan(ConanFile):
+    settings = "os", "compiler", "build_type", "arch"
+    generators = "cmake", "cmake_find_package_multi"
+
+    def build(self):
+        cmake = CMake(self)
+        cmake.configure()
+        cmake.build()
+
+    def test(self):
+        if not tools.cross_building(self.settings):
+            bin_path = os.path.join("bin", "test_package")
+            self.run(bin_path, run_environment=True)