SpaceInvaders

mpdecimal 2.5.0 migration to conan 2.0 conanfile.py #16934

Apr 6th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.85 KB | None | 0 0
  1. # Warning!!!!
  2. # Before using this file or any part of this file please review:
  3. # https://github.com/conan-io/conan-center-index/issues/16934
  4. # or search https://github.com/worldforge/conan-packages/issues
  5. # for details or a more current version
  6.  
  7. from conan import ConanFile, conan_version
  8. from conan.errors import ConanInvalidConfiguration
  9. from conan.tools.apple import is_apple_os
  10. from conan.tools.files import (
  11. apply_conandata_patches, chdir, copy, export_conandata_patches,
  12. get, load, rename, replace_in_file, rm, rmdir, save
  13. )
  14. from conan.tools.gnu import Autotools, AutotoolsToolchain, AutotoolsDeps
  15. from conan.tools.layout import basic_layout
  16. from conan.tools.scm import Version
  17. import os
  18. import shutil
  19.  
  20. required_conan_version = ">=1.53.0"
  21.  
  22.  
  23. class MpdecimalConan(ConanFile):
  24. name = "mpdecimal"
  25. description = "mpdecimal is a package for correctly-rounded arbitrary precision decimal floating point arithmetic."
  26. license = "BSD-2-Clause"
  27. topics = ("mpdecimal", "multiprecision", "library")
  28. url = "https://github.com/conan-io/conan-center-index"
  29. homepage = "http://www.bytereef.org/mpdecimal"
  30. settings = "os", "compiler", "build_type", "arch"
  31. options = {
  32. "shared": [True, False],
  33. "fPIC": [True, False],
  34. "cxx": [True, False],
  35. }
  36. default_options = {
  37. "shared": False,
  38. "fPIC": True,
  39. "cxx": True,
  40. }
  41.  
  42. _autotools = None
  43.  
  44. @property
  45. def _source_subfolder(self):
  46. return "source_subfolder"
  47.  
  48. @property
  49. def _settings_build(self):
  50. return getattr(self, "setings_build", self.settings)
  51.  
  52. @property
  53. def _is_msvc(self):
  54. return str(self.settings.compiler) in ["Visual Studio", "msvc"]
  55.  
  56. def export_sources(self):
  57. for patch in self.conan_data.get("patches", {}).get(self.version, []):
  58. self.copy(patch["patch_file"])
  59.  
  60. def config_options(self):
  61. if self.settings.os == "Windows":
  62. del self.options.fPIC
  63.  
  64. def configure(self):
  65. if self.options.shared:
  66. del self.options.fPIC
  67. if not self.options.cxx:
  68. del self.settings.compiler.libcxx
  69. del self.settings.compiler.cppstd
  70.  
  71. def build_requirements(self):
  72. if self._is_msvc:
  73. self.build_requires("automake/1.16.4")
  74. if self._settings_build.os == "Windows" and not get_env("CONAN_BASH_PATH"):
  75. self.build_requires("msys2/cci.latest")
  76.  
  77. def validate(self):
  78. if self._is_msvc and self.settings.arch not in ("x86", "x86_64"):
  79. raise ConanInvalidConfiguration("Arch is unsupported")
  80. if self.options.cxx:
  81. if self.options.shared and self.settings.os == "Windows":
  82. raise ConanInvalidConfiguration("A shared libmpdec++ is not possible on Windows (due to non-exportable thread local storage)")
  83.  
  84. def source(self):
  85. get(**self.conan_data["sources"][self.version],
  86. destination=self._source_subfolder, strip_root=True)
  87.  
  88. def _patch_sources(self):
  89. for patch in self.conan_data.get("patches", {}).get(self.version, []):
  90. patch(**patch)
  91.  
  92. def _build_msvc(self):
  93. libmpdec_folder = os.path.join(self.build_folder, self._source_subfolder, "libmpdec")
  94. libmpdecpp_folder = os.path.join(self.build_folder, self._source_subfolder, "libmpdec++")
  95. vcbuild_folder = os.path.join(self.build_folder, self._source_subfolder, "vcbuild")
  96. arch_ext = "{}".format(32 if self.settings.arch == "x86" else 64)
  97. dist_folder = os.path.join(vcbuild_folder, "dist{}".format(arch_ext))
  98. os.mkdir(dist_folder)
  99.  
  100. shutil.copy(os.path.join(libmpdec_folder, "Makefile.vc"), os.path.join(libmpdec_folder, "Makefile"))
  101.  
  102. autotools = AutoToolsBuildEnvironment(self)
  103. mpdec_extra_flags = []
  104. mpdecxx_extra_flags = []
  105. if Version(self.version) >= "2.5.1":
  106. if self.options.shared:
  107. mpdec_extra_flags = ["-DMPDECIMAL_DLL"]
  108. mpdecxx_extra_flags = ["-DLIBMPDECXX_DLL"]
  109.  
  110. mpdec_target = "libmpdec-{}.{}".format(self.version, "dll" if self.options.shared else "lib")
  111. mpdecpp_target = "libmpdec++-{}.{}".format(self.version, "dll" if self.options.shared else "lib")
  112.  
  113. builds = [[libmpdec_folder, mpdec_target, mpdec_extra_flags] ]
  114. if self.options.cxx:
  115. builds.append([libmpdecpp_folder, mpdecpp_target, mpdecxx_extra_flags])
  116. with vcvars(self):
  117. for build_dir, target, extra_flags in builds:
  118. with chdir(build_dir):
  119. self.run("""nmake /nologo /f Makefile.vc {target} MACHINE={machine} DEBUG={debug} DLL={dll} CONAN_CFLAGS="{cflags}" CONAN_CXXFLAGS="{cxxflags}" CONAN_LDFLAGS="{ldflags}" """.format(
  120. target=target,
  121. machine={"x86": "ppro", "x86_64": "x64"}[str(self.settings.arch)], # FIXME: else, use ansi32 and ansi64
  122. debug="1" if self.settings.build_type == "Debug" else "0",
  123. dll="1" if self.options.shared else "0",
  124. cflags=" ".join(autotools.flags + extra_flags),
  125. cxxflags=" ".join(autotools.cxx_flags + extra_flags),
  126. ldflags=" ".join(autotools.link_flags),
  127. ))
  128.  
  129. with chdir(libmpdec_folder):
  130. shutil.copy("mpdecimal.h", dist_folder)
  131. if self.options.shared:
  132. shutil.copy("libmpdec-{}.dll".format(self.version), os.path.join(dist_folder, "libmpdec-{}.dll".format(self.version)))
  133. shutil.copy("libmpdec-{}.dll.exp".format(self.version), os.path.join(dist_folder, "libmpdec-{}.exp".format(self.version)))
  134. shutil.copy("libmpdec-{}.dll.lib".format(self.version), os.path.join(dist_folder, "libmpdec-{}.lib".format(self.version)))
  135. else:
  136. shutil.copy("libmpdec-{}.lib".format(self.version), os.path.join(dist_folder, "libmpdec-{}.lib".format(self.version)))
  137. if self.options.cxx:
  138. with chdir(libmpdecpp_folder):
  139. shutil.copy("decimal.hh", dist_folder)
  140. shutil.copy("libmpdec++-{}.lib".format(self.version), os.path.join(dist_folder, "libmpdec++-{}.lib".format(self.version)))
  141.  
  142. def _configure_autotools(self):
  143. if self._autotools:
  144. return self._autotools
  145. self._autotools = AutoToolsBuildEnvironment(self, win_bash=tools.os_info.is_windows)
  146. conf_vars = self._autotools.vars
  147. if self.settings.os == "Macos" and self.settings.arch == "armv8":
  148. conf_vars["LDFLAGS"] += " -arch arm64"
  149. conf_vars["LDXXFLAGS"] = "-arch arm64"
  150. conf_args = [
  151. "--enable-cxx" if self.options.cxx else "--disable-cxx"
  152. ]
  153. self._autotools.configure(args=conf_args, vars=conf_vars)
  154. return self._autotools
  155.  
  156. @property
  157. def _shared_suffix(self):
  158. if is_apple_os(self):
  159. return ".dylib"
  160. return {
  161. "Windows": ".dll",
  162. }.get(str(self.settings.os), ".so")
  163.  
  164. @property
  165. def _target_names(self):
  166. libsuffix = self._shared_suffix if self.options.shared else ".a"
  167. versionsuffix = ".{}".format(self.version) if self.options.shared else ""
  168. suffix = "{}{}".format(versionsuffix, libsuffix) if is_apple_os(self) or self.settings.os == "Windows" else "{}{}".format(libsuffix, versionsuffix)
  169. return "libmpdec{}".format(suffix), "libmpdec++{}".format(suffix)
  170.  
  171. def build(self):
  172. self._patch_sources()
  173. if self._is_msvc:
  174. self._build_msvc()
  175. else:
  176. with chdir(self._source_subfolder):
  177. self.run("autoreconf -fiv", win_bash=tools.os_info.is_windows)
  178. autotools = self._configure_autotools()
  179. self.output.info(tools.load(os.path.join("libmpdec", "Makefile")))
  180. libmpdec, libmpdecpp = self._target_names
  181. with chdir("libmpdec"):
  182. autotools.make(target=libmpdec)
  183. if self.options.cxx:
  184. with chdir("libmpdec++"):
  185. autotools.make(target=libmpdecpp)
  186.  
  187. def package(self):
  188. self.copy("LICENSE.txt", src=self._source_subfolder, dst="licenses")
  189. if self._is_msvc:
  190. distfolder = os.path.join(self.build_folder, self._source_subfolder, "vcbuild", "dist{}".format(32 if self.settings.arch == "x86" else 64))
  191. self.copy("vc*.h", src=os.path.join(self.build_folder, self._source_subfolder, "libmpdec"), dst="include")
  192. self.copy("*.h", src=distfolder, dst="include")
  193. if self.options.cxx:
  194. self.copy("*.hh", src=distfolder, dst="include")
  195. self.copy("*.lib", src=distfolder, dst="lib")
  196. self.copy("*.dll", src=distfolder, dst="bin")
  197. else:
  198. mpdecdir = os.path.join(self._source_subfolder, "libmpdec")
  199. mpdecppdir = os.path.join(self._source_subfolder, "libmpdec++")
  200. self.copy("mpdecimal.h", src=mpdecdir, dst="include")
  201. if self.options.cxx:
  202. self.copy("decimal.hh", src=mpdecppdir, dst="include")
  203. builddirs = [mpdecdir]
  204. if self.options.cxx:
  205. builddirs.append(mpdecppdir)
  206. for builddir in builddirs:
  207. self.copy("*.a", src=builddir, dst="lib")
  208. self.copy("*.so", src=builddir, dst="lib")
  209. self.copy("*.so.*", src=builddir, dst="lib")
  210. self.copy("*.dylib", src=builddir, dst="lib")
  211. self.copy("*.dll", src=builddir, dst="bin")
  212.  
  213. def package_info(self):
  214. lib_pre_suf = ("", "")
  215. if self._is_msvc:
  216. lib_pre_suf = ("lib", "-{}".format(self.version))
  217. elif self.settings.os == "Windows":
  218. if self.options.shared:
  219. lib_pre_suf = ("", ".dll")
  220.  
  221. self.cpp_info.components["libmpdecimal"].libs = ["{}mpdec{}".format(*lib_pre_suf)]
  222. if self.options.shared:
  223. if self._is_msvc:
  224. if Version(self.version) >= "2.5.1":
  225. self.cpp_info.components["libmpdecimal"].defines = ["MPDECIMAL_DLL"]
  226. else:
  227. self.cpp_info.components["libmpdecimal"].defines = ["USE_DLL"]
  228. else:
  229. if self.settings.os in ["Linux", "FreeBSD"]:
  230. self.cpp_info.components["libmpdecimal"].system_libs = ["m"]
  231.  
  232. if self.options.cxx:
  233. self.cpp_info.components["libmpdecimal++"].libs = ["{}mpdec++{}".format(*lib_pre_suf)]
  234. self.cpp_info.components["libmpdecimal++"].requires = ["libmpdecimal"]
  235. if self.options.shared:
  236. if Version(self.version) >= "2.5.1":
  237. self.cpp_info.components["libmpdecimal"].defines = ["MPDECIMALXX_DLL"]
  238.  
Advertisement
Add Comment
Please, Sign In to add comment