You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
9.4 KiB

8 years ago
  1. #!/usr/bin/env python
  2. import os
  3. import multiprocessing
  4. import sys
  5. import subprocess
  6. import datetime
  7. import re
  8. from setuptools import setup, Extension
  9. from setuptools.command.build_ext import build_ext
  10. from setuptools.command.test import test
  11. import importlib.util
  12. if sys.version_info[0] == 2:
  13. sys.exit('Sorry, Python 2.x is not supported')
  14. def check_storm_compatible(storm_v_major, storm_v_minor, storm_v_patch):
  15. if storm_v_major < 1 or (storm_v_major == 1 and storm_v_minor == 0 and storm_v_patch < 1):
  16. sys.exit('Sorry, Storm version {}.{}.{} is not supported anymore!'.format(storm_v_major, storm_v_minor,
  17. storm_v_patch))
  18. def parse_storm_version(version_string):
  19. """
  20. Parses the version of storm.
  21. :param version_string:
  22. :return: Version as three-tuple.
  23. """
  24. elems = version_string.split(".")
  25. if len(elems) != 3:
  26. sys.exit('Storm version string is ill-formed: "{}"'.format(version_string))
  27. return int(elems[0]), int(elems[1]), int(elems[2])
  28. def obtain_version():
  29. """
  30. Obtains the version as specified in stormpy.
  31. :return: Version of stormpy.
  32. """
  33. verstr = "unknown"
  34. try:
  35. verstrline = open('lib/stormpy/_version.py', "rt").read()
  36. except EnvironmentError:
  37. pass # Okay, there is no version file.
  38. else:
  39. VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
  40. mo = re.search(VSRE, verstrline, re.M)
  41. if mo:
  42. verstr = mo.group(1)
  43. else:
  44. raise RuntimeError("unable to find version in stormpy/_version.py")
  45. return verstr
  46. class CMakeExtension(Extension):
  47. def __init__(self, name, sourcedir='', subdir=''):
  48. Extension.__init__(self, name, sources=[])
  49. self.sourcedir = os.path.abspath(sourcedir)
  50. self.subdir = subdir
  51. class CMakeBuild(build_ext):
  52. user_options = build_ext.user_options + [
  53. ('storm-dir=', None, 'Path to storm root (binary) location'),
  54. ('jobs=', 'j', 'Number of jobs to use for compiling'),
  55. ('debug', None, 'Build in Debug mode'),
  56. ]
  57. def extdir(self, extname):
  58. return os.path.abspath(os.path.dirname(self.get_ext_fullpath(extname)))
  59. def run(self):
  60. self.conf = None
  61. try:
  62. _ = subprocess.check_output(['cmake', '--version'])
  63. except OSError:
  64. raise RuntimeError("CMake must be installed to build the following extensions: " +
  65. ", ".join(e.name for e in self.extensions))
  66. build_temp_version = self.build_temp + "-version"
  67. if not os.path.exists(build_temp_version):
  68. os.makedirs(build_temp_version)
  69. # Check cmake variable values
  70. cmake_args = []
  71. if self.storm_dir is not None:
  72. cmake_args = ['-Dstorm_DIR=' + self.storm_dir]
  73. output = subprocess.check_output(['cmake', os.path.abspath("cmake")] + cmake_args, cwd=build_temp_version)
  74. spec = importlib.util.spec_from_file_location("genconfig",
  75. os.path.join(build_temp_version, 'generated/config.py'))
  76. self.conf = importlib.util.module_from_spec(spec)
  77. spec.loader.exec_module(self.conf)
  78. # Check storm version
  79. storm_v_major, storm_v_minor, storm_v_patch = parse_storm_version(self.conf.STORM_VERSION)
  80. check_storm_compatible(storm_v_major, storm_v_minor, storm_v_patch)
  81. # Create dir
  82. lib_path = os.path.join(self.extdir("core"))
  83. if not os.path.exists(lib_path):
  84. os.makedirs(lib_path)
  85. for ext in self.extensions:
  86. if ext.name == "core":
  87. with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  88. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  89. f.write("import pycarl\n")
  90. if self.conf.STORM_CLN_EA or self.conf.STORM_CLN_RF:
  91. f.write("import pycarl.cln\n")
  92. if not self.conf.STORM_CLN_EA or not self.conf.STORM_CLN_RF:
  93. f.write("import pycarl.gmp\n")
  94. if self.conf.STORM_CLN_EA:
  95. f.write("Rational = pycarl.cln.Rational\n")
  96. else:
  97. f.write("Rational = pycarl.gmp.Rational\n")
  98. if self.conf.STORM_CLN_RF:
  99. rfpackage = "cln"
  100. else:
  101. rfpackage = "gmp"
  102. f.write("RationalRF = pycarl.{}.Rational\n".format(rfpackage))
  103. f.write("Polynomial = pycarl.{}.Polynomial\n".format(rfpackage))
  104. f.write("FactorizedPolynomial = pycarl.{}.FactorizedPolynomial\n".format(rfpackage))
  105. f.write("RationalFunction = pycarl.{}.RationalFunction\n".format(rfpackage))
  106. f.write("FactorizedRationalFunction = pycarl.{}.FactorizedRationalFunction\n".format(rfpackage))
  107. elif ext.name == "info":
  108. with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  109. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  110. f.write("storm_version = \"{}\"\n".format(self.conf.STORM_VERSION))
  111. f.write("storm_cln_ea = {}\n".format(self.conf.STORM_CLN_EA))
  112. f.write("storm_cln_rf = {}".format(self.conf.STORM_CLN_RF))
  113. elif ext.name == "pars":
  114. with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  115. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  116. f.write("has_storm_pars = {}".format(self.conf.HAVE_STORM_PARS))
  117. if not self.conf.HAVE_STORM_PARS:
  118. print("WARNING: storm-pars not found. No support for parametric analysis will be built.")
  119. continue
  120. elif ext.name == "dft":
  121. with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  122. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  123. f.write("has_storm_dft = {}".format(self.conf.HAVE_STORM_DFT))
  124. if not self.conf.HAVE_STORM_DFT:
  125. print("WARNING: storm-dft not found. No support for DFTs will be built.")
  126. continue
  127. self.build_extension(ext)
  128. def initialize_options(self):
  129. build_ext.initialize_options(self)
  130. self.storm_dir = None
  131. self.debug = False
  132. try:
  133. self.jobs = multiprocessing.cpu_count() if multiprocessing.cpu_count() is not None else 1
  134. except NotImplementedError:
  135. self.jobs = 1
  136. def finalize_options(self):
  137. if self.storm_dir is not None:
  138. print('The custom storm directory', self.storm_dir)
  139. build_ext.finalize_options(self)
  140. def build_extension(self, ext):
  141. extdir = self.extdir(ext.name)
  142. cmake_args = ['-DSTORMPY_LIB_DIR=' + extdir,
  143. '-DPYTHON_EXECUTABLE=' + sys.executable]
  144. build_type = 'Debug' if self.debug else 'Release'
  145. build_args = ['--config', build_type]
  146. build_args += ['--', '-j{}'.format(self.jobs)]
  147. cmake_args += ['-DCMAKE_BUILD_TYPE=' + build_type]
  148. if self.conf.STORM_DIR is not None:
  149. cmake_args += ['-Dstorm_DIR=' + self.conf.STORM_DIR]
  150. if self.conf.HAVE_STORM_PARS:
  151. cmake_args += ['-DHAVE_STORM_PARS=ON']
  152. if self.conf.HAVE_STORM_DFT:
  153. cmake_args += ['-DHAVE_STORM_DFT=ON']
  154. env = os.environ.copy()
  155. env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
  156. self.distribution.get_version())
  157. if not os.path.exists(self.build_temp):
  158. os.makedirs(self.build_temp)
  159. print("CMake args={}".format(cmake_args))
  160. # Call cmake
  161. subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
  162. subprocess.check_call(['cmake', '--build', '.', '--target', ext.name] + build_args, cwd=self.build_temp)
  163. class PyTest(test):
  164. def run_tests(self):
  165. # import here, cause outside the eggs aren't loaded
  166. import pytest
  167. errno = pytest.main(['tests'])
  168. sys.exit(errno)
  169. setup(
  170. name="stormpy",
  171. version=obtain_version(),
  172. author="M. Volk",
  173. author_email="matthias.volk@cs.rwth-aachen.de",
  174. maintainer="S. Junges",
  175. maintainer_email="sebastian.junges@cs.rwth-aachen.de",
  176. url="http://moves.rwth-aachen.de",
  177. description="stormpy - Python Bindings for Storm",
  178. packages=['stormpy', 'stormpy.info', 'stormpy.logic', 'stormpy.storage', 'stormpy.utility',
  179. 'stormpy.pars', 'stormpy.dft'],
  180. package_dir={'': 'lib'},
  181. ext_package='stormpy',
  182. ext_modules=[CMakeExtension('core', subdir=''),
  183. CMakeExtension('info', subdir='info'),
  184. CMakeExtension('logic', subdir='logic'),
  185. CMakeExtension('storage', subdir='storage'),
  186. CMakeExtension('utility', subdir='utility'),
  187. CMakeExtension('pars', subdir='pars'),
  188. CMakeExtension('dft', subdir='dft'),
  189. ],
  190. cmdclass={'build_ext': CMakeBuild, 'test': PyTest},
  191. zip_safe=False,
  192. install_requires=['pycarl>=2.0.0'],
  193. setup_requires=['pytest-runner'],
  194. tests_require=['pytest'],
  195. )