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.

201 lines
8.7 KiB

8 years ago
  1. #!/usr/bin/env python
  2. import os
  3. import multiprocessing
  4. import sys
  5. import subprocess
  6. import datetime
  7. from setuptools import setup, Extension
  8. from setuptools.command.build_ext import build_ext
  9. from setuptools.command.test import test
  10. import importlib.util
  11. if sys.version_info[0] == 2:
  12. sys.exit('Sorry, Python 2.x is not supported')
  13. def check_storm_compatible(storm_v_major, storm_v_minor, storm_v_patch):
  14. if storm_v_major < 1 or (storm_v_major == 1 and storm_v_minor == 0 and storm_v_patch < 1):
  15. sys.exit('Sorry, Storm version {}.{}.{} is not supported anymore!'.format(storm_v_major, storm_v_minor, storm_v_patch))
  16. def parse_storm_version(version_string):
  17. elems = version_string.split(".")
  18. if len(elems) != 3:
  19. sys.exit('Storm version string is ill-formed: "{}"'.format(version_string))
  20. return int(elems[0]), int(elems[1]), int(elems[2])
  21. class CMakeExtension(Extension):
  22. def __init__(self, name, sourcedir='', subdir=''):
  23. Extension.__init__(self, name, sources=[])
  24. self.sourcedir = os.path.abspath(sourcedir)
  25. self.subdir = subdir
  26. class CMakeBuild(build_ext):
  27. user_options = build_ext.user_options + [
  28. ('storm-dir=', None, 'Path to storm root (binary) location'),
  29. ('jobs=', 'j', 'Number of jobs to use for compiling'),
  30. ('debug', None, 'Build in Debug mode'),
  31. ]
  32. def extdir(self, extname):
  33. return os.path.abspath(os.path.dirname(self.get_ext_fullpath(extname)))
  34. def run(self):
  35. self.conf = None
  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_temp_version = self.build_temp + "-version"
  42. if not os.path.exists(build_temp_version):
  43. os.makedirs(build_temp_version)
  44. # Check cmake variable values
  45. cmake_args = []
  46. if self.storm_dir is not None:
  47. cmake_args = ['-Dstorm_DIR=' + self.storm_dir]
  48. output = subprocess.check_output(['cmake', os.path.abspath("cmake")] + cmake_args, cwd=build_temp_version)
  49. spec = importlib.util.spec_from_file_location("genconfig", os.path.join(build_temp_version, 'generated/config.py'))
  50. self.conf = importlib.util.module_from_spec(spec)
  51. spec.loader.exec_module(self.conf)
  52. # Check storm version
  53. storm_v_major, storm_v_minor, storm_v_patch = parse_storm_version(self.conf.STORM_VERSION)
  54. check_storm_compatible(storm_v_major, storm_v_minor, storm_v_patch)
  55. # Create dir
  56. lib_path = os.path.join(self.extdir("core"))
  57. if not os.path.exists(lib_path):
  58. os.makedirs(lib_path)
  59. for ext in self.extensions:
  60. if ext.name == "core":
  61. with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  62. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  63. f.write("import pycarl\n")
  64. if self.conf.STORM_CLN_EA or self.conf.STORM_CLN_RF:
  65. f.write("import pycarl.cln\n")
  66. if not self.conf.STORM_CLN_EA or not self.conf.STORM_CLN_RF:
  67. f.write("import pycarl.gmp\n")
  68. if self.conf.STORM_CLN_EA:
  69. f.write("Rational = pycarl.cln.Rational\n")
  70. else:
  71. f.write("Rational = pycarl.gmp.Rational\n")
  72. if self.conf.STORM_CLN_RF:
  73. rfpackage = "cln"
  74. else:
  75. rfpackage = "gmp"
  76. f.write("RationalRF = pycarl.{}.Rational\n".format(rfpackage))
  77. f.write("Polynomial = pycarl.{}.Polynomial\n".format(rfpackage))
  78. f.write("FactorizedPolynomial = pycarl.{}.FactorizedPolynomial\n".format(rfpackage))
  79. f.write("RationalFunction = pycarl.{}.RationalFunction\n".format(rfpackage))
  80. f.write("FactorizedRationalFunction = pycarl.{}.FactorizedRationalFunction\n".format(rfpackage))
  81. elif ext.name == "info":
  82. with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  83. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  84. f.write("storm_version = {}\n".format(self.conf.STORM_VERSION))
  85. f.write("storm_cln_ea = {}\n".format(self.conf.STORM_CLN_EA))
  86. f.write("storm_cln_rf = {}".format(self.conf.STORM_CLN_RF))
  87. elif ext.name == "pars":
  88. with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  89. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  90. f.write("has_storm_pars = {}".format(self.conf.HAVE_STORM_PARS))
  91. if not self.conf.HAVE_STORM_PARS:
  92. print("WARNING: storm-pars not found. No support for parametric analysis will be built.")
  93. continue
  94. elif ext.name == "dft":
  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("has_storm_dft = {}".format(self.conf.HAVE_STORM_DFT))
  98. if not self.conf.HAVE_STORM_DFT:
  99. print("WARNING: storm-dft not found. No support for DFTs will be built.")
  100. continue
  101. self.build_extension(ext)
  102. def initialize_options(self):
  103. build_ext.initialize_options(self)
  104. self.storm_dir = None
  105. self.debug = False
  106. try:
  107. self.jobs = multiprocessing.cpu_count() if multiprocessing.cpu_count() is not None else 1
  108. except NotImplementedError:
  109. self.jobs = 1
  110. def finalize_options(self):
  111. if self.storm_dir is not None:
  112. print('The custom storm directory', self.storm_dir)
  113. build_ext.finalize_options(self)
  114. def build_extension(self, ext):
  115. extdir = self.extdir(ext.name)
  116. cmake_args = ['-DSTORMPY_LIB_DIR=' + extdir,
  117. '-DPYTHON_EXECUTABLE=' + sys.executable]
  118. build_type = 'Debug' if self.debug else 'Release'
  119. build_args = ['--config', build_type]
  120. build_args += ['--', '-j{}'.format(self.jobs)]
  121. cmake_args += ['-DCMAKE_BUILD_TYPE=' + build_type]
  122. if self.conf.STORM_DIR is not None:
  123. cmake_args += ['-Dstorm_DIR=' + self.conf.STORM_DIR]
  124. if self.conf.HAVE_STORM_PARS:
  125. cmake_args += ['-DHAVE_STORM_PARS=ON']
  126. if self.conf.HAVE_STORM_DFT:
  127. cmake_args += ['-DHAVE_STORM_DFT=ON']
  128. env = os.environ.copy()
  129. env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
  130. self.distribution.get_version())
  131. if not os.path.exists(self.build_temp):
  132. os.makedirs(self.build_temp)
  133. print("CMake args={}".format(cmake_args))
  134. # Call cmake
  135. subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
  136. subprocess.check_call(['cmake', '--build', '.', '--target', ext.name] + build_args, cwd=self.build_temp)
  137. class PyTest(test):
  138. def run_tests(self):
  139. # import here, cause outside the eggs aren't loaded
  140. import pytest
  141. errno = pytest.main(['tests'])
  142. sys.exit(errno)
  143. setup(
  144. name="stormpy",
  145. version="0.9.1",
  146. author="M. Volk",
  147. author_email="matthias.volk@cs.rwth-aachen.de",
  148. maintainer="S. Junges",
  149. maintainer_email="sebastian.junges@cs.rwth-aachen.de",
  150. url="http://moves.rwth-aachen.de",
  151. description="stormpy - Python Bindings for Storm",
  152. packages=['stormpy', 'stormpy.info', 'stormpy.expressions', 'stormpy.logic', 'stormpy.storage', 'stormpy.utility',
  153. 'stormpy.pars', 'stormpy.dft'],
  154. package_dir={'': 'lib'},
  155. ext_package='stormpy',
  156. ext_modules=[CMakeExtension('core', subdir=''),
  157. CMakeExtension('info', subdir='info'),
  158. CMakeExtension('expressions', subdir='expressions'),
  159. CMakeExtension('logic', subdir='logic'),
  160. CMakeExtension('storage', subdir='storage'),
  161. CMakeExtension('utility', subdir='utility'),
  162. CMakeExtension('pars', subdir='pars'),
  163. CMakeExtension('dft', subdir='dft'),
  164. ],
  165. cmdclass={'build_ext': CMakeBuild, 'test': PyTest},
  166. zip_safe=False,
  167. install_requires=['pycarl>=2.0.0'],
  168. tests_require=['pytest'],
  169. )