Repository: conan-io/conan-center-index

Issue:
[request] mpfr/4.1.0
### Package Details
  * Package Name/Version: **mpfr/4.1.0**
  * Changelog: **https://www.mpfr.org/mpfr-current/#changes**


The above mentioned version is newly released by the upstream project and not yet available as a recipe. Please add this version.



Discussion:

Resolving patch:
diff --git a/recipes/mpfr/all/conanfile.py b/recipes/mpfr/all/conanfile.py
--- a/recipes/mpfr/all/conanfile.py
+++ b/recipes/mpfr/all/conanfile.py
@@ -1,6 +1,9 @@
 from conans import ConanFile, AutoToolsBuildEnvironment, CMake, tools
+from conans.errors import ConanException, ConanInvalidConfiguration
+from contextlib import contextmanager
 import os
-import shutil
+import re
+import shlex
 
 
 class MpfrConan(ConanFile):
@@ -12,9 +15,17 @@ class MpfrConan(ConanFile):
     homepage = "https://www.mpfr.org/"
     license = "LGPL-3.0-or-later"
     settings = "os", "arch", "compiler", "build_type"
-    options = {"shared": [True, False], "fPIC": [True, False]}
-    default_options = {"shared": False, "fPIC": True}
-    exports_sources = "CMakeLists.txt"
+    options = {
+        "shared": [True, False],
+        "fPIC": [True, False],
+        "exact_int": ["mpir", "gmp",]
+    }
+    default_options = {
+        "shared": False,
+        "fPIC": True,
+        "exact_int": "mpir",
+    }
+    exports_sources = "CMakeLists.txt.in", "patches/**"
     generators = "cmake"
 
     _autotools = None
@@ -29,71 +40,131 @@ def config_options(self):
             del self.options.fPIC
 
     def configure(self):
-        if self.options.shared:
-            del self.options.fPIC
         del self.settings.compiler.libcxx
         del self.settings.compiler.cppstd
 
     def requirements(self):
-        self.requires("mpir/3.0.0")
+        if self.options.exact_int == "mpir":
+            self.requires("mpir/3.0.0")
+        elif self.options.exact_int == "gmp":
+            self.requires("gmp/6.2.0")
 
     def build_requirements(self):
-        if self.settings.os == "Windows" and self.settings.compiler != "Visual Studio" and \
-           "CONAN_BASH_PATH" not in os.environ and tools.os_info.detect_windows_subsystem() != "msys2":
+        if tools.os_info.is_windows and not tools.get_env("CONAN_BASH_PAH"):
             self.build_requires("msys2/20200517")
 
     def source(self):
         tools.get(**self.conan_data["sources"][self.version])
         extracted_dir = self.name + "-" + self.version
         os.rename(extracted_dir, self._source_subfolder)
-        if self.settings.compiler == "Visual Studio":
-            shutil.copy("CMakeLists.txt",
-                        os.path.join(self._source_subfolder, "CMakeLists.txt"))
 
     def _configure_autotools(self):
-        if not self._autotools:
-            args = ["--enable-thread-safe"]
-            if self.options.shared:
-                args.extend(["--disable-static", "--enable-shared"])
-            else:
-                args.extend(["--disable-shared", "--disable-static"])
-            if self.settings.compiler == "clang":
-                # warning: optimization flag '-ffloat-store' is not supported
-                args.append("mpfr_cv_gcc_floatconv_bug=no")
-            if self.settings.compiler == "clang" and self.settings.arch == "x86":
+        if self._autotools:
+            return self._autotools
+        self._autotools = AutoToolsBuildEnvironment(self, win_bash=tools.os_info.is_windows)
+        args = [
+            "--enable-thread-safe",
+            "--with-gmp-include={}".format(tools.unix_path(os.path.join(self.deps_cpp_info[str(self.options.exact_int)].rootpath, "include"))),
+            "--with-gmp-lib={}".format(tools.unix_path(os.path.join(self.deps_cpp_info[str(self.options.exact_int)].rootpath, "lib"))),
+        ]
+        if self.options.shared:
+            args.extend(["--disable-static", "--enable-shared"])
+        else:
+            args.extend(["--disable-shared", "--disable-static"])
+        if self.settings.compiler == "clang":
+            # warning: optimization flag '-ffloat-store' is not supported
+            args.append("mpfr_cv_gcc_floatconv_bug=no")
+            if self.settings.arch == "x86":
                 # fatal error: error in backend: Unsupported library call operation!
                 args.append("--disable-float128")
-            self._autotools = AutoToolsBuildEnvironment(self, win_bash=tools.os_info.is_windows)
-            self._autotools.configure(args=args, configure_dir=self._source_subfolder)
+        if self.options.exact_int == "mpir":
+            self._autotools.include_paths.append(self.build_folder)
+        if self.settings.compiler == "Visual Studio":
+            self._autotools.flags.append("-FS")
+        self._autotools.libs = []
+        self._autotools.configure(args=args, configure_dir=self._source_subfolder)
         return self._autotools
 
     def _configure_cmake(self):
-        if not self._cmake:
-            self._cmake = CMake(self)
-            self._cmake.configure(source_folder=self._source_subfolder)
+        if self._cmake:
+            return self._cmake
+        self._cmake = CMake(self)
+        self._cmake.configure(source_dir=os.path.join(self._source_subfolder, "src"))
         return self._cmake
 
-    def build(self):
+    def _extract_makefile_variable(self, makefile, variable):
+        makefile_contents = tools.load(makefile)
+        match = re.search("{}[ \t]*=[ \t]*((?:(?:[a-zA-Z0-9 \t.=/_-])|(?:\\\\\"))*(?:\\\\\n(?:(?:[a-zA-Z0-9 \t.=/_-])|(?:\\\"))*)*)\n".format(variable), makefile_contents)
+        if not match:
+            raise ConanException("Cannot extract variable {} from {}".format(variable, makefile_contents))
+        lines = [line.strip(" \t\\") for line in match.group(1).split()]
+        return [item for line in lines for item in shlex.split(line) if item]
+
+    def _extract_mpfr_autotools_variables(self):
+        makefile_am = os.path.join(self._source_subfolder, "src", "Makefile.am")
+        makefile = os.path.join("src", "Makefile")
+        sources = self._extract_makefile_variable(makefile_am, "libmpfr_la_SOURCES")
+        headers = self._extract_makefile_variable(makefile_am, "include_HEADERS")
+        defs = self._extract_makefile_variable(makefile, "DEFS")
+        return sources, headers, defs
+
+    @contextmanager
+    def _build_context(self):
+        env = {}
         if self.settings.compiler == "Visual Studio":
+            with tools.vcvars(self.settings):
+                env.update({
+                    "AR": "lib",
+                    "CC": "cl -nologo",
+                    "CXX": "cl -nologo",
+                    "LD": "link",
+                    "NM": "dumpbin -symbols",
+                    "OBJDUMP": ":",
+                    "RANLIB": ":",
+                    "STRIP": ":",
+                })
+                with tools.environment_append(env):
+                    yield
+        else:
+            yield
+
+    def build(self):
+        for patch in self.conan_data.get("patches", {}).get(self.version, []):
+            tools.patch(**patch)
+        if self.options.exact_int == "mpir":
+            tools.replace_in_file(os.path.join(self._source_subfolder, "configure"),
+                                       "-lgmp", "-lmpir")
+            tools.replace_in_file(os.path.join(self._source_subfolder, "src", "mpfr.h"),
+                                       "<gmp.h>", "<mpir.h>")
+            tools.save("gmp.h", "#pragma once\n#include <mpir.h>\n")
+        with self._build_context():
+            autotools = self._configure_autotools()
+        if self.settings.os == "Windows":
+            cmakelists_in = tools.load("CMakeLists.txt.in")
+            sources, headers, definitions = self._extract_mpfr_autotools_variables()
+            tools.save(os.path.join(self._source_subfolder, "src", "CMakeLists.txt"), cmakelists_in.format(
+                mpfr_sources = " ".join(sources),
+                mpfr_headers = " ".join(headers),
+                definitions = " ".join(definitions),
+            ))
             cmake = self._configure_cmake()
             cmake.build()
         else:
-            autotools = self._configure_autotools()
             autotools.make(args=["V=0"])
 
     def package(self):
         self.copy("COPYING", dst="licenses", src=self._source_subfolder)
-        if self.settings.compiler == "Visual Studio":
+        if self.settings.os == "Windows":
             cmake = self._configure_cmake()
             cmake.install()
         else:
             autotools = self._configure_autotools()
             autotools.install()
-            la = os.path.join(self.package_folder, "lib", "libmpfr.la")
-            if os.path.isfile(la):
-                os.unlink(la)
+            os.unlink(os.path.join(self.package_folder, "lib", "libmpfr.la"))
             tools.rmdir(os.path.join(self.package_folder, "share"))
             tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
 
     def package_info(self):
         self.cpp_info.libs = ["mpfr"]
+        if self.settings.os == "Windows" and self.options.shared:
+            self.cpp_info.defines = ["MPFR_DLL"]


Test patch:
diff --git a/recipes/mpfr/all/test_package/CMakeLists.txt b/recipes/mpfr/all/test_package/CMakeLists.txt
--- a/recipes/mpfr/all/test_package/CMakeLists.txt
+++ b/recipes/mpfr/all/test_package/CMakeLists.txt
@@ -1,8 +1,8 @@
 cmake_minimum_required(VERSION 3.3.0)
-project(test_package)
+project(test_package C)
 
-include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+include("${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
 conan_basic_setup()
 
-add_executable(${PROJECT_NAME} test_package.cpp)
+add_executable(${PROJECT_NAME} test_package.c)
 target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
diff --git a/recipes/mpfr/all/test_package/test_package.cpp b/recipes/mpfr/all/test_package/test_package.c
similarity index 97%
rename from recipes/mpfr/all/test_package/test_package.cpp
rename to recipes/mpfr/all/test_package/test_package.c
--- a/recipes/mpfr/all/test_package/test_package.cpp
+++ b/recipes/mpfr/all/test_package/test_package.c
@@ -2,7 +2,6 @@
 
 #include <stdio.h>
 
-#include <gmp.h>
 #include <mpfr.h>
 
 int main (void)