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.

253 lines
11 KiB

7 years ago
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.2.2"
  15. # Get the long description from the README file
  16. with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'README.md'), encoding='utf-8') as f:
  17. long_description = f.read()
  18. class CMakeExtension(Extension):
  19. def __init__(self, name, sourcedir='', subdir=''):
  20. Extension.__init__(self, name, sources=[])
  21. self.sourcedir = os.path.abspath(sourcedir)
  22. self.subdir = subdir
  23. class CMakeBuild(build_ext):
  24. user_options = build_ext.user_options + [
  25. ('storm-dir=', None, 'Path to storm root (binary) location'),
  26. ('disable-dft', None, 'Disable support for DFTs'),
  27. ('disable-pars', None, 'Disable support for parametric models'),
  28. ('debug', None, 'Build in Debug mode'),
  29. ('jobs=', 'j', 'Number of jobs to use for compiling'),
  30. ]
  31. config = SetupConfig()
  32. def _extdir(self, extname):
  33. return os.path.abspath(os.path.dirname(self.get_ext_fullpath(extname)))
  34. def run(self):
  35. try:
  36. _ = subprocess.check_output(['cmake', '--version'])
  37. except OSError:
  38. raise RuntimeError("CMake must be installed to build the following extensions: " +
  39. ", ".join(e.name for e in self.extensions))
  40. # Build cmake version info
  41. build_temp_version = self.build_temp + "-version"
  42. setup_helper.ensure_dir_exists(build_temp_version)
  43. # Write config
  44. setup_helper.ensure_dir_exists("build")
  45. self.config.write_config("build/build_config.cfg")
  46. cmake_args = []
  47. storm_dir = self.config.get_as_string("storm_dir")
  48. if storm_dir:
  49. cmake_args += ['-Dstorm_DIR=' + storm_dir]
  50. _ = subprocess.check_output(['cmake', os.path.abspath("cmake")] + cmake_args, cwd=build_temp_version)
  51. cmake_conf = setup_helper.load_cmake_config(os.path.join(build_temp_version, 'generated/config.py'))
  52. # Set storm directory
  53. if storm_dir == "":
  54. storm_dir = cmake_conf.STORM_DIR
  55. if storm_dir != cmake_conf.STORM_DIR:
  56. print("Stormpy - Warning: Using different storm directory {} instead of given {}!".format(
  57. cmake_conf.STORM_DIR,
  58. storm_dir))
  59. storm_dir = cmake_conf.STORM_DIR
  60. # Check version
  61. storm_version, storm_commit = setup_helper.parse_storm_version(cmake_conf.STORM_VERSION)
  62. if StrictVersion(storm_version) < StrictVersion(storm_min_version):
  63. sys.exit(
  64. 'Stormpy - Error: Storm version {} from \'{}\' is not supported anymore!'.format(storm_version,
  65. storm_dir))
  66. # Check additional support
  67. use_dft = cmake_conf.HAVE_STORM_DFT and not self.config.get_as_bool("disable_dft")
  68. use_pars = cmake_conf.HAVE_STORM_PARS and not self.config.get_as_bool("disable_pars")
  69. # Print build info
  70. print("Stormpy - Using storm {} from {}".format(storm_version, storm_dir))
  71. if use_dft:
  72. print("Stormpy - Support for DFTs found and included.")
  73. else:
  74. print("Stormpy - Warning: No support for DFTs!")
  75. if use_pars:
  76. print("Stormpy - Support for parametric models found and included.")
  77. else:
  78. print("Stormpy - Warning: No support for parametric models!")
  79. # Set general cmake build options
  80. build_type = 'Debug' if self.config.get_as_bool("debug") else 'Release'
  81. cmake_args = ['-DPYTHON_EXECUTABLE=' + sys.executable]
  82. cmake_args += ['-DCMAKE_BUILD_TYPE=' + build_type]
  83. if storm_dir is not None:
  84. cmake_args += ['-Dstorm_DIR=' + storm_dir]
  85. if use_dft:
  86. cmake_args += ['-DHAVE_STORM_DFT=ON']
  87. if use_pars:
  88. cmake_args += ['-DHAVE_STORM_PARS=ON']
  89. build_args = ['--config', build_type]
  90. build_args += ['--', '-j{}'.format(self.config.get_as_int("jobs"))]
  91. # Build extensions
  92. for ext in self.extensions:
  93. setup_helper.ensure_dir_exists(os.path.join(self._extdir(ext.name), ext.subdir))
  94. if ext.name == "core":
  95. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  96. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  97. f.write("import pycarl\n")
  98. if cmake_conf.STORM_CLN_EA or cmake_conf.STORM_CLN_RF:
  99. f.write("import pycarl.cln\n")
  100. if not cmake_conf.STORM_CLN_EA or not cmake_conf.STORM_CLN_RF:
  101. f.write("import pycarl.gmp\n")
  102. if cmake_conf.STORM_CLN_EA:
  103. f.write("Rational = pycarl.cln.Rational\n")
  104. else:
  105. f.write("Rational = pycarl.gmp.Rational\n")
  106. if cmake_conf.STORM_CLN_RF:
  107. rfpackage = "cln"
  108. else:
  109. rfpackage = "gmp"
  110. f.write("RationalRF = pycarl.{}.Rational\n".format(rfpackage))
  111. f.write("Polynomial = pycarl.{}.Polynomial\n".format(rfpackage))
  112. f.write("FactorizedPolynomial = pycarl.{}.FactorizedPolynomial\n".format(rfpackage))
  113. f.write("RationalFunction = pycarl.{}.RationalFunction\n".format(rfpackage))
  114. f.write("FactorizedRationalFunction = pycarl.{}.FactorizedRationalFunction\n".format(rfpackage))
  115. f.write("\n")
  116. f.write("storm_with_dft = {}\n".format(use_dft))
  117. f.write("storm_with_pars = {}\n".format(use_pars))
  118. elif ext.name == "info":
  119. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  120. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  121. f.write("storm_version = \"{}\"\n".format(storm_version))
  122. f.write("storm_cln_ea = {}\n".format(cmake_conf.STORM_CLN_EA))
  123. f.write("storm_cln_rf = {}".format(cmake_conf.STORM_CLN_RF))
  124. elif ext.name == "dft":
  125. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  126. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  127. f.write("storm_with_dft = {}".format(use_dft))
  128. if not use_dft:
  129. print("Stormpy - DFT bindings skipped")
  130. continue
  131. elif ext.name == "pars":
  132. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  133. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  134. f.write("storm_with_pars = {}".format(use_pars))
  135. if not use_pars:
  136. print("Stormpy - Bindings for parametric models skipped")
  137. continue
  138. self.build_extension(ext, cmake_args, build_args)
  139. def initialize_options(self):
  140. build_ext.initialize_options(self)
  141. # Load setup config
  142. self.config.load_from_file("build/build_config.cfg")
  143. # Set default values for custom cmdline flags
  144. self.storm_dir = None
  145. self.disable_dft = None
  146. self.disable_pars = None
  147. self.debug = None
  148. self.jobs = None
  149. def finalize_options(self):
  150. build_ext.finalize_options(self)
  151. # Update setup config
  152. self.config.update("storm_dir", self.storm_dir)
  153. self.config.update("disable_dft", self.disable_dft)
  154. self.config.update("disable_pars", self.disable_pars)
  155. self.config.update("debug", self.debug)
  156. self.config.update("jobs", self.jobs)
  157. def build_extension(self, ext, general_cmake_args, general_build_args):
  158. extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
  159. cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + os.path.join(extdir, ext.subdir)] + general_cmake_args
  160. build_args = general_build_args
  161. env = os.environ.copy()
  162. env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
  163. self.distribution.get_version())
  164. setup_helper.ensure_dir_exists(self.build_temp)
  165. print("Pycarl - CMake args={}".format(cmake_args))
  166. # Call cmake
  167. subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
  168. subprocess.check_call(['cmake', '--build', '.', '--target', ext.name] + build_args, cwd=self.build_temp)
  169. class PyTest(test):
  170. def run_tests(self):
  171. # import here, cause outside the eggs aren't loaded
  172. import pytest
  173. errno = pytest.main(['tests'])
  174. sys.exit(errno)
  175. setup(
  176. name="stormpy",
  177. version=setup_helper.obtain_version(),
  178. author="M. Volk",
  179. author_email="matthias.volk@cs.rwth-aachen.de",
  180. maintainer="S. Junges",
  181. maintainer_email="sebastian.junges@cs.rwth-aachen.de",
  182. url="http://moves.rwth-aachen.de",
  183. description="stormpy - Python Bindings for Storm",
  184. long_description=long_description,
  185. long_description_content_type='text/markdown',
  186. project_urls={
  187. 'Documentation': 'https://moves-rwth.github.io/stormpy/',
  188. 'Source': 'https://github.com/moves-rwth/stormpy/',
  189. 'Bug reports': 'https://github.com/moves-rwth/stormpy/issues',
  190. },
  191. classifiers=[
  192. 'Intended Audience :: Science/Research',
  193. 'Topic :: Scientific/Engineering',
  194. 'Topic :: Software Development :: Libraries :: Python Modules',
  195. ],
  196. packages=['stormpy',
  197. 'stormpy.info',
  198. 'stormpy.logic',
  199. 'stormpy.storage',
  200. 'stormpy.utility',
  201. 'stormpy.dft',
  202. 'stormpy.pars'],
  203. package_dir={'': 'lib'},
  204. ext_package='stormpy',
  205. ext_modules=[CMakeExtension('core', subdir=''),
  206. CMakeExtension('info', subdir='info'),
  207. CMakeExtension('logic', subdir='logic'),
  208. CMakeExtension('storage', subdir='storage'),
  209. CMakeExtension('utility', subdir='utility'),
  210. CMakeExtension('dft', subdir='dft'),
  211. CMakeExtension('pars', subdir='pars')],
  212. cmdclass={'build_ext': CMakeBuild, 'test': PyTest},
  213. zip_safe=False,
  214. install_requires=['pycarl>=2.0.2'],
  215. setup_requires=['pytest-runner'],
  216. tests_require=['pytest'],
  217. python_requires='>=3',
  218. )