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.

279 lines
13 KiB

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