The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

286 lines
13 KiB

2 months 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.3"
  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_xerces = {}\n".format(cmake_conf.STORM_XERCES))
  133. f.write("storm_with_dft = {}\n".format(use_dft))
  134. f.write("storm_with_gspn = {}\n".format(use_gspn))
  135. f.write("storm_with_pars = {}\n".format(use_pars))
  136. elif ext.name == "info":
  137. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  138. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  139. f.write("storm_version = \"{}\"\n".format(storm_version))
  140. f.write("storm_cln_ea = {}\n".format(cmake_conf.STORM_CLN_EA))
  141. f.write("storm_cln_rf = {}".format(cmake_conf.STORM_CLN_RF))
  142. elif ext.name == "dft":
  143. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  144. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  145. f.write("storm_with_dft = {}".format(use_dft))
  146. if not use_dft:
  147. print("Stormpy - DFT bindings skipped")
  148. continue
  149. elif ext.name == "gspn":
  150. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  151. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  152. f.write("storm_with_gspn = {}".format(use_gspn))
  153. if not use_gspn:
  154. print("Stormpy - GSPN bindings skipped")
  155. continue
  156. elif ext.name == "pars":
  157. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  158. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  159. f.write("storm_with_pars = {}".format(use_pars))
  160. if not use_pars:
  161. print("Stormpy - Bindings for parametric models skipped")
  162. continue
  163. elif ext.name == "pomdp":
  164. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  165. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  166. f.write("storm_with_pomdp = {}".format(use_pomdp))
  167. if not use_pomdp:
  168. print("Stormpy - Bindings for POMDP analysis skipped")
  169. continue
  170. self.build_extension(ext, cmake_args, build_args)
  171. def initialize_options(self):
  172. build_ext.initialize_options(self)
  173. # Set default values for custom cmdline flags
  174. self.storm_dir = None
  175. self.disable_dft = None
  176. self.disable_gspn = None
  177. self.disable_pars = None
  178. self.disable_pomdp = None
  179. self.debug = None
  180. self.jobs = None
  181. def finalize_options(self):
  182. build_ext.finalize_options(self)
  183. # Load setup config
  184. # This can only be done after the finalization step, because otherwise build_temp is not initialized yet.
  185. self.config.load_from_file(os.path.join(self.build_temp, "build_config.cfg"))
  186. # Update setup config
  187. self.config.update("storm_dir", self.storm_dir)
  188. self.config.update("disable_dft", self.disable_dft)
  189. self.config.update("disable_gspn", self.disable_gspn)
  190. self.config.update("disable_pars", self.disable_pars)
  191. self.config.update("disable_pomdp", self.disable_pomdp)
  192. self.config.update("debug", self.debug)
  193. self.config.update("jobs", self.jobs)
  194. def build_extension(self, ext, general_cmake_args, general_build_args):
  195. extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
  196. cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + os.path.join(extdir, ext.subdir)] + general_cmake_args
  197. build_args = general_build_args
  198. env = os.environ.copy()
  199. env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
  200. self.distribution.get_version())
  201. setup_helper.ensure_dir_exists(self.build_temp)
  202. print("Stormpy - CMake args={}".format(cmake_args))
  203. # Call cmake
  204. subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
  205. subprocess.check_call(['cmake', '--build', '.', '--target', ext.name] + build_args, cwd=self.build_temp)
  206. setup(
  207. name="stormpy",
  208. version=setup_helper.obtain_version(),
  209. author="M. Volk",
  210. author_email="matthias.volk@cs.rwth-aachen.de",
  211. maintainer="S. Junges",
  212. maintainer_email="sebastian.junges@cs.rwth-aachen.de",
  213. url="https://github.com/moves-rwth/stormpy/",
  214. description="stormpy - Python Bindings for Storm",
  215. long_description=long_description,
  216. long_description_content_type='text/markdown',
  217. project_urls={
  218. 'Documentation': 'https://moves-rwth.github.io/stormpy/',
  219. 'Source': 'https://github.com/moves-rwth/stormpy/',
  220. 'Bug reports': 'https://github.com/moves-rwth/stormpy/issues',
  221. },
  222. classifiers=[
  223. 'Intended Audience :: Science/Research',
  224. 'Topic :: Scientific/Engineering',
  225. 'Topic :: Software Development :: Libraries :: Python Modules',
  226. ],
  227. packages=find_packages('lib'),
  228. package_dir={'': 'lib'},
  229. include_package_data=True,
  230. package_data={'stormpy.examples': ['examples/files/*']},
  231. ext_package='stormpy',
  232. ext_modules=[CMakeExtension('core', subdir=''),
  233. CMakeExtension('info', subdir='info'),
  234. CMakeExtension('logic', subdir='logic'),
  235. CMakeExtension('storage', subdir='storage'),
  236. CMakeExtension('utility', subdir='utility'),
  237. CMakeExtension('dft', subdir='dft'),
  238. CMakeExtension('gspn', subdir='gspn'),
  239. CMakeExtension('pars', subdir='pars'),
  240. CMakeExtension('pomdp', subdir='pomdp'),
  241. CMakeExtension('shields', subdir='shields')
  242. ],
  243. cmdclass={'build_ext': CMakeBuild},
  244. zip_safe=False,
  245. install_requires=['pycarl>=2.0.4', 'dtcontrol'],
  246. setup_requires=['pytest-runner'],
  247. tests_require=['pytest', 'nbval'],
  248. extras_require={
  249. "numpy": ["numpy"],
  250. "doc": ["Sphinx", "sphinx-bootstrap-theme", "nbsphinx", "ipython", "ipykernel"], # also requires pandoc to be installed
  251. },
  252. python_requires='>=3',
  253. )