Skip to content

Commit

Permalink
(#9135) mpir: relocatable shared lib on macOS + modernize
Browse files Browse the repository at this point in the history
* modernize

* relocatable shared lib on macOS

* mpir provides gmp API if enable_gmpcompat enabled

* proper runtime mapping

Co-authored-by: Anonymous Maarten <madebr@users.noreply.github.com>

* improve vc runtime handling

Co-authored-by: Anonymous Maarten <madebr@users.noreply.github.com>
  • Loading branch information
SpaceIm and madebr committed Feb 4, 2022
1 parent 3bffc38 commit cb4b025
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
61 changes: 40 additions & 21 deletions recipes/mpir/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from conan.tools.microsoft import msvc_runtime_flag
from conans import ConanFile, tools, AutoToolsBuildEnvironment, MSBuild
from conans.errors import ConanInvalidConfiguration
import contextlib
import os
import glob

required_conan_version = ">=1.33.0"


class MpirConan(ConanFile):
name = "mpir"
description = "MPIR is a highly optimised library for bignum arithmetic" \
Expand All @@ -14,18 +15,21 @@ class MpirConan(ConanFile):
url = "https://github.com/conan-io/conan-center-index"
homepage = "http://mpir.org/"
license = "LGPL-3.0-or-later"
settings = "os", "compiler", "arch", "build_type"

provides = []

settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
"enable_cxx": [True, False],
"enable_gmpcompat": [True, False]
"enable_gmpcompat": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"enable_cxx": True,
"enable_gmpcompat": True
"enable_gmpcompat": True,
}

_autotools = None
Expand All @@ -34,26 +38,36 @@ class MpirConan(ConanFile):
def _source_subfolder(self):
return "source_subfolder"

@property
def _settings_build(self):
return getattr(self, "settings_build", self.settings)

@property
def _is_msvc(self):
return str(self.settings.compiler) in ["Visual Studio", "msvc"]

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def configure(self):
if self.options.shared:
del self.options.fPIC
if self.settings.compiler == "Visual Studio" and self.options.shared:
if self._is_msvc and self.options.shared:
del self.options.enable_cxx
if not self.options.get_safe("enable_cxx", False):
del self.settings.compiler.libcxx
del self.settings.compiler.cppstd
if self.options.enable_gmpcompat:
self.provides.append("gmp")

@property
def _settings_build(self):
return getattr(self, "settings_build", self.settings)
def validate(self):
if hasattr(self, "settings_build") and tools.cross_building(self, skip_x64_x86=True):
raise ConanInvalidConfiguration("Cross-building doesn't work (yet)")

def build_requirements(self):
self.build_requires("yasm/1.3.0")
if self.settings.compiler != "Visual Studio":
if not self._is_msvc:
self.build_requires("m4/1.4.19")
if self._settings_build.os == "Windows" and not tools.get_env("CONAN_BASH_PATH"):
self.build_requires("msys2/cci.latest")
Expand Down Expand Up @@ -85,15 +99,18 @@ def _vcxproj_paths(self):
return vcxproj_paths

def _build_visual_studio(self):
if "MD" in self.settings.compiler.runtime and not self.options.shared: # RuntimeLibrary only defined in lib props files
props_path = os.path.join(self._source_subfolder, "build.vc",
"mpir_{}_{}.props".format(str(self.settings.build_type).lower(), self._dll_or_lib))
if self.settings.build_type == "Debug":
tools.replace_in_file(props_path, "<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>",
"<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>")
else:
tools.replace_in_file(props_path, "<RuntimeLibrary>MultiThreaded</RuntimeLibrary>",
"<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>")
if not self.options.shared: # RuntimeLibrary only defined in lib props files
build_type = "debug" if self.settings.build_type == "Debug" else "release"
props_path = os.path.join(self._source_subfolder, "build.vc",
"mpir_{}_lib.props".format(build_type))
old_runtime = "MultiThreaded{}".format(
"Debug" if build_type == "debug" else "",
)
new_runtime = "MultiThreaded{}{}".format(
"Debug" if "d" in msvc_runtime_flag(self) else "",
"DLL" if "MD" in msvc_runtime_flag(self) else "",
)
tools.replace_in_file(props_path, old_runtime, new_runtime)
msbuild = MSBuild(self)
for vcxproj_path in self._vcxproj_paths:
msbuild.build(vcxproj_path, platforms=self._platforms, upgrade_project=False)
Expand Down Expand Up @@ -134,16 +151,18 @@ def _configure_autotools(self):
return self._autotools

def build(self):
if self.settings.compiler == "Visual Studio":
if self._is_msvc:
self._build_visual_studio()
else:
with tools.chdir(self._source_subfolder), self._build_context():
# relocatable shared lib on macOS
tools.replace_in_file("configure", "-install_name \\$rpath/", "-install_name @rpath/")
autotools = self._configure_autotools()
autotools.make()

def package(self):
self.copy("COPYING*", dst="licenses", src=self._source_subfolder)
if self.settings.compiler == "Visual Studio":
if self._is_msvc:
lib_folder = os.path.join(self._source_subfolder, self._dll_or_lib,
self._platforms.get(str(self.settings.arch)),
str(self.settings.build_type))
Expand All @@ -167,7 +186,7 @@ def package_info(self):
if self.options.get_safe("enable_cxx"):
self.cpp_info.libs.append("mpirxx")
self.cpp_info.libs.append("mpir")
if self.options.enable_gmpcompat and self.settings.compiler != "Visual Studio":
if self.options.enable_gmpcompat and not self._is_msvc:
if self.options.get_safe("enable_cxx"):
self.cpp_info.libs.append("gmpxx")
self.cpp_info.libs.append("gmp")
Expand Down
4 changes: 2 additions & 2 deletions recipes/mpir/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class TestPackageConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
settings = "os", "arch", "compiler", "build_type"
generators = "cmake"

def build(self):
Expand All @@ -12,6 +12,6 @@ def build(self):
cmake.build()

def test(self):
if not tools.cross_building(self.settings):
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)

0 comments on commit cb4b025

Please sign in to comment.