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.

231 lines
9.6 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. f.write("\n")
  108. f.write("storm_with_pars = {}\n".format(self.conf.HAVE_STORM_PARS))
  109. f.write("storm_with_dft = {}\n".format(self.conf.HAVE_STORM_DFT))
  110. elif ext.name == "info":
  111. with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  112. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  113. f.write("storm_version = \"{}\"\n".format(self.conf.STORM_VERSION))
  114. f.write("storm_cln_ea = {}\n".format(self.conf.STORM_CLN_EA))
  115. f.write("storm_cln_rf = {}".format(self.conf.STORM_CLN_RF))
  116. elif ext.name == "pars":
  117. with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  118. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  119. f.write("storm_with_pars = {}".format(self.conf.HAVE_STORM_PARS))
  120. if not self.conf.HAVE_STORM_PARS:
  121. print("WARNING: storm-pars not found. No support for parametric analysis will be built.")
  122. continue
  123. elif ext.name == "dft":
  124. with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  125. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  126. f.write("storm_with_dft = {}".format(self.conf.HAVE_STORM_DFT))
  127. if not self.conf.HAVE_STORM_DFT:
  128. print("WARNING: storm-dft not found. No support for DFTs will be built.")
  129. continue
  130. self.build_extension(ext)
  131. def initialize_options(self):
  132. build_ext.initialize_options(self)
  133. self.storm_dir = None
  134. self.debug = False
  135. try:
  136. self.jobs = multiprocessing.cpu_count() if multiprocessing.cpu_count() is not None else 1
  137. except NotImplementedError:
  138. self.jobs = 1
  139. def finalize_options(self):
  140. if self.storm_dir is not None:
  141. print('The custom storm directory', self.storm_dir)
  142. build_ext.finalize_options(self)
  143. def build_extension(self, ext):
  144. extdir = self.extdir(ext.name)
  145. cmake_args = ['-DSTORMPY_LIB_DIR=' + extdir,
  146. '-DPYTHON_EXECUTABLE=' + sys.executable]
  147. build_type = 'Debug' if self.debug else 'Release'
  148. build_args = ['--config', build_type]
  149. build_args += ['--', '-j{}'.format(self.jobs)]
  150. cmake_args += ['-DCMAKE_BUILD_TYPE=' + build_type]
  151. if self.conf.STORM_DIR is not None:
  152. cmake_args += ['-Dstorm_DIR=' + self.conf.STORM_DIR]
  153. if self.conf.HAVE_STORM_PARS:
  154. cmake_args += ['-DHAVE_STORM_PARS=ON']
  155. if self.conf.HAVE_STORM_DFT:
  156. cmake_args += ['-DHAVE_STORM_DFT=ON']
  157. env = os.environ.copy()
  158. env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
  159. self.distribution.get_version())
  160. if not os.path.exists(self.build_temp):
  161. os.makedirs(self.build_temp)
  162. print("CMake args={}".format(cmake_args))
  163. # Call cmake
  164. subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
  165. subprocess.check_call(['cmake', '--build', '.', '--target', ext.name] + build_args, cwd=self.build_temp)
  166. class PyTest(test):
  167. def run_tests(self):
  168. # import here, cause outside the eggs aren't loaded
  169. import pytest
  170. errno = pytest.main(['tests'])
  171. sys.exit(errno)
  172. setup(
  173. name="stormpy",
  174. version=obtain_version(),
  175. author="M. Volk",
  176. author_email="matthias.volk@cs.rwth-aachen.de",
  177. maintainer="S. Junges",
  178. maintainer_email="sebastian.junges@cs.rwth-aachen.de",
  179. url="http://moves.rwth-aachen.de",
  180. description="stormpy - Python Bindings for Storm",
  181. packages=['stormpy', 'stormpy.info', 'stormpy.logic', 'stormpy.storage', 'stormpy.utility',
  182. 'stormpy.pars', 'stormpy.dft'],
  183. package_dir={'': 'lib'},
  184. ext_package='stormpy',
  185. ext_modules=[CMakeExtension('core', subdir=''),
  186. CMakeExtension('info', subdir='info'),
  187. CMakeExtension('logic', subdir='logic'),
  188. CMakeExtension('storage', subdir='storage'),
  189. CMakeExtension('utility', subdir='utility'),
  190. CMakeExtension('pars', subdir='pars'),
  191. CMakeExtension('dft', subdir='dft'),
  192. ],
  193. cmdclass={'build_ext': CMakeBuild, 'test': PyTest},
  194. zip_safe=False,
  195. install_requires=['pycarl>=2.0.0'],
  196. setup_requires=['pytest-runner'],
  197. tests_require=['pytest'],
  198. )