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.

242 lines
11 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.3.1"
  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. ('debug', None, 'Build in Debug mode'),
  28. ('jobs=', 'j', 'Number of jobs to use for compiling')
  29. ]
  30. config = SetupConfig()
  31. def _extdir(self, extname):
  32. return os.path.abspath(os.path.dirname(self.get_ext_fullpath(extname)))
  33. def run(self):
  34. try:
  35. _ = subprocess.check_output(['cmake', '--version'])
  36. except OSError:
  37. raise RuntimeError("CMake must be installed to build the following extensions: " +
  38. ", ".join(e.name for e in self.extensions))
  39. # Build cmake version info
  40. print("Stormpy - Building into {}".format(self.build_temp))
  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(self.build_temp)
  45. self.config.write_config(os.path.join(self.build_temp, "build_config.cfg"))
  46. cmake_args = []
  47. storm_dir = os.path.expanduser(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. # Set default values for custom cmdline flags
  142. self.storm_dir = None
  143. self.disable_dft = None
  144. self.disable_pars = None
  145. self.debug = None
  146. self.jobs = None
  147. def finalize_options(self):
  148. build_ext.finalize_options(self)
  149. # Load setup config
  150. # This can only be done after the finalization step, because otherwise build_temp is not initialized yet.
  151. self.config.load_from_file(os.path.join(self.build_temp, "build_config.cfg"))
  152. # Update setup config
  153. self.config.update("storm_dir", self.storm_dir)
  154. self.config.update("disable_dft", self.disable_dft)
  155. self.config.update("disable_pars", self.disable_pars)
  156. self.config.update("debug", self.debug)
  157. self.config.update("jobs", self.jobs)
  158. def build_extension(self, ext, general_cmake_args, general_build_args):
  159. extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
  160. cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + os.path.join(extdir, ext.subdir)] + general_cmake_args
  161. build_args = general_build_args
  162. env = os.environ.copy()
  163. env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
  164. self.distribution.get_version())
  165. setup_helper.ensure_dir_exists(self.build_temp)
  166. print("Stormpy - CMake args={}".format(cmake_args))
  167. # Call cmake
  168. subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
  169. subprocess.check_call(['cmake', '--build', '.', '--target', ext.name] + build_args, cwd=self.build_temp)
  170. setup(
  171. name="stormpy",
  172. version=setup_helper.obtain_version(),
  173. author="M. Volk",
  174. author_email="matthias.volk@cs.rwth-aachen.de",
  175. maintainer="S. Junges",
  176. maintainer_email="sebastian.junges@cs.rwth-aachen.de",
  177. url="https://github.com/moves-rwth/stormpy/",
  178. description="stormpy - Python Bindings for Storm",
  179. long_description=long_description,
  180. long_description_content_type='text/markdown',
  181. project_urls={
  182. 'Documentation': 'https://moves-rwth.github.io/stormpy/',
  183. 'Source': 'https://github.com/moves-rwth/stormpy/',
  184. 'Bug reports': 'https://github.com/moves-rwth/stormpy/issues',
  185. },
  186. classifiers=[
  187. 'Intended Audience :: Science/Research',
  188. 'Topic :: Scientific/Engineering',
  189. 'Topic :: Software Development :: Libraries :: Python Modules',
  190. ],
  191. packages=find_packages('lib'),
  192. package_dir={'': 'lib'},
  193. include_package_data=True,
  194. package_data={'stormpy.examples': ['examples/files/*']},
  195. ext_package='stormpy',
  196. ext_modules=[CMakeExtension('core', subdir=''),
  197. CMakeExtension('info', subdir='info'),
  198. CMakeExtension('logic', subdir='logic'),
  199. CMakeExtension('storage', subdir='storage'),
  200. CMakeExtension('utility', subdir='utility'),
  201. CMakeExtension('dft', subdir='dft'),
  202. CMakeExtension('pars', subdir='pars')],
  203. cmdclass={'build_ext': CMakeBuild},
  204. zip_safe=False,
  205. install_requires=['pycarl>=2.0.3'],
  206. setup_requires=['pytest-runner'],
  207. tests_require=['pytest'],
  208. python_requires='>=3',
  209. )