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.

181 lines
7.5 KiB

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