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.

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