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.

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