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.

232 lines
10 KiB

8 years ago
  1. import os
  2. import sys
  3. import subprocess
  4. import datetime
  5. from setuptools import setup, Extension
  6. from setuptools.command.build_ext import build_ext
  7. from setuptools.command.test import test
  8. from distutils.version import StrictVersion
  9. import setup.helper as setup_helper
  10. from setup.config import SetupConfig
  11. if sys.version_info[0] == 2:
  12. sys.exit('Sorry, Python 2.x is not supported')
  13. # Minimal storm version required
  14. storm_min_version = "1.1.0"
  15. class CMakeExtension(Extension):
  16. def __init__(self, name, sourcedir='', subdir=''):
  17. Extension.__init__(self, name, sources=[])
  18. self.sourcedir = os.path.abspath(sourcedir)
  19. self.subdir = subdir
  20. class CMakeBuild(build_ext):
  21. user_options = build_ext.user_options + [
  22. ('storm-dir=', None, 'Path to storm root (binary) location'),
  23. ('disable-dft', None, 'Disable support for DFTs'),
  24. ('disable-pars', None, 'Disable support for parametric models'),
  25. ('debug', None, 'Build in Debug mode'),
  26. ('jobs=', 'j', 'Number of jobs to use for compiling'),
  27. ]
  28. config = SetupConfig()
  29. def _extdir(self, extname):
  30. return os.path.abspath(os.path.dirname(self.get_ext_fullpath(extname)))
  31. def run(self):
  32. try:
  33. _ = subprocess.check_output(['cmake', '--version'])
  34. except OSError:
  35. raise RuntimeError("CMake must be installed to build the following extensions: " +
  36. ", ".join(e.name for e in self.extensions))
  37. # Build cmake version info
  38. build_temp_version = self.build_temp + "-version"
  39. setup_helper.ensure_dir_exists(build_temp_version)
  40. # Write config
  41. setup_helper.ensure_dir_exists("build")
  42. self.config.write_config("build/build_config.cfg")
  43. cmake_args = []
  44. storm_dir = self.config.get_as_string("storm_dir")
  45. if storm_dir:
  46. cmake_args += ['-Dstorm_DIR=' + storm_dir]
  47. _ = subprocess.check_output(['cmake', os.path.abspath("cmake")] + cmake_args, cwd=build_temp_version)
  48. cmake_conf = setup_helper.load_cmake_config(os.path.join(build_temp_version, 'generated/config.py'))
  49. # Set storm directory
  50. if storm_dir == "":
  51. storm_dir = cmake_conf.STORM_DIR
  52. if storm_dir != cmake_conf.STORM_DIR:
  53. print("Stormpy - Warning: Using different storm directory {} instead of given {}!".format(
  54. cmake_conf.STORM_DIR,
  55. storm_dir))
  56. storm_dir = cmake_conf.STORM_DIR
  57. # Check version
  58. storm_version, storm_commit = setup_helper.parse_storm_version(cmake_conf.STORM_VERSION)
  59. if StrictVersion(storm_version) < StrictVersion(storm_min_version):
  60. sys.exit(
  61. 'Stormpy - Error: Storm version {} from \'{}\' is not supported anymore!'.format(storm_version,
  62. storm_dir))
  63. # Check additional support
  64. use_dft = cmake_conf.HAVE_STORM_DFT and not self.config.get_as_bool("disable_dft")
  65. use_pars = cmake_conf.HAVE_STORM_PARS and not self.config.get_as_bool("disable_pars")
  66. # Print build info
  67. print("Stormpy - Using storm {} from {}".format(storm_version, storm_dir))
  68. if use_dft:
  69. print("Stormpy - Support for DFTs found and included.")
  70. else:
  71. print("Stormpy - Warning: No support for DFTs!")
  72. if use_pars:
  73. print("Stormpy - Support for parametric models found and included.")
  74. else:
  75. print("Stormpy - Warning: No support for parametric models!")
  76. # Set general cmake build options
  77. build_type = 'Debug' if self.config.get_as_bool("debug") else 'Release'
  78. cmake_args = ['-DPYTHON_EXECUTABLE=' + sys.executable]
  79. cmake_args += ['-DCMAKE_BUILD_TYPE=' + build_type]
  80. if storm_dir is not None:
  81. cmake_args += ['-Dstorm_DIR=' + storm_dir]
  82. if use_dft:
  83. cmake_args += ['-DHAVE_STORM_DFT=ON']
  84. if use_pars:
  85. cmake_args += ['-DHAVE_STORM_PARS=ON']
  86. build_args = ['--config', build_type]
  87. build_args += ['--', '-j{}'.format(self.config.get_as_int("jobs"))]
  88. # Build extensions
  89. for ext in self.extensions:
  90. setup_helper.ensure_dir_exists(os.path.join(self._extdir(ext.name), ext.subdir))
  91. if ext.name == "core":
  92. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  93. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  94. f.write("import pycarl\n")
  95. if cmake_conf.STORM_CLN_EA or cmake_conf.STORM_CLN_RF:
  96. f.write("import pycarl.cln\n")
  97. if not cmake_conf.STORM_CLN_EA or not cmake_conf.STORM_CLN_RF:
  98. f.write("import pycarl.gmp\n")
  99. if cmake_conf.STORM_CLN_EA:
  100. f.write("Rational = pycarl.cln.Rational\n")
  101. else:
  102. f.write("Rational = pycarl.gmp.Rational\n")
  103. if cmake_conf.STORM_CLN_RF:
  104. rfpackage = "cln"
  105. else:
  106. rfpackage = "gmp"
  107. f.write("RationalRF = pycarl.{}.Rational\n".format(rfpackage))
  108. f.write("Polynomial = pycarl.{}.Polynomial\n".format(rfpackage))
  109. f.write("FactorizedPolynomial = pycarl.{}.FactorizedPolynomial\n".format(rfpackage))
  110. f.write("RationalFunction = pycarl.{}.RationalFunction\n".format(rfpackage))
  111. f.write("FactorizedRationalFunction = pycarl.{}.FactorizedRationalFunction\n".format(rfpackage))
  112. f.write("\n")
  113. f.write("storm_with_dft = {}\n".format(use_dft))
  114. f.write("storm_with_pars = {}\n".format(use_pars))
  115. elif ext.name == "info":
  116. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  117. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  118. f.write("storm_version = \"{}\"\n".format(storm_version))
  119. f.write("storm_cln_ea = {}\n".format(cmake_conf.STORM_CLN_EA))
  120. f.write("storm_cln_rf = {}".format(cmake_conf.STORM_CLN_RF))
  121. elif ext.name == "dft":
  122. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  123. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  124. f.write("storm_with_dft = {}".format(use_dft))
  125. if not use_dft:
  126. print("Stormpy - DFT bindings skipped")
  127. continue
  128. elif ext.name == "pars":
  129. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  130. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  131. f.write("storm_with_pars = {}".format(use_pars))
  132. if not use_pars:
  133. print("Stormpy - Bindings for parametric models skipped")
  134. continue
  135. self.build_extension(ext, cmake_args, build_args)
  136. def initialize_options(self):
  137. build_ext.initialize_options(self)
  138. # Load setup config
  139. self.config.load_from_file("build/build_config.cfg")
  140. # Set default values for custom cmdline flags
  141. self.storm_dir = None
  142. self.disable_dft = None
  143. self.disable_pars = None
  144. self.debug = None
  145. self.jobs = None
  146. def finalize_options(self):
  147. build_ext.finalize_options(self)
  148. # Update setup config
  149. self.config.update("storm_dir", self.storm_dir)
  150. self.config.update("disable_dft", self.disable_dft)
  151. self.config.update("disable_pars", self.disable_pars)
  152. self.config.update("debug", self.debug)
  153. self.config.update("jobs", self.jobs)
  154. def build_extension(self, ext, general_cmake_args, general_build_args):
  155. extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
  156. cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + os.path.join(extdir, ext.subdir)] + general_cmake_args
  157. build_args = general_build_args
  158. env = os.environ.copy()
  159. env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
  160. self.distribution.get_version())
  161. setup_helper.ensure_dir_exists(self.build_temp)
  162. print("Pycarl - 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=setup_helper.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. long_description='',
  182. packages=['stormpy', 'stormpy.info', 'stormpy.logic', 'stormpy.storage', 'stormpy.utility',
  183. 'stormpy.pars', 'stormpy.dft'],
  184. package_dir={'': 'lib'},
  185. ext_package='stormpy',
  186. ext_modules=[CMakeExtension('core', subdir=''),
  187. CMakeExtension('info', subdir='info'),
  188. CMakeExtension('logic', subdir='logic'),
  189. CMakeExtension('storage', subdir='storage'),
  190. CMakeExtension('utility', subdir='utility'),
  191. CMakeExtension('dft', subdir='dft'),
  192. CMakeExtension('pars', subdir='pars'),
  193. ],
  194. cmdclass={'build_ext': CMakeBuild, 'test': PyTest},
  195. zip_safe=False,
  196. install_requires=['pycarl>=2.0.1'],
  197. setup_requires=['pytest-runner'],
  198. tests_require=['pytest'],
  199. python_requires='>=3',
  200. )