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.

185 lines
7.9 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. for ext in self.extensions:
  56. if ext.name == "core":
  57. with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  58. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  59. f.write("import pycarl\n")
  60. if self.conf.STORM_CLN_EA or self.conf.STORM_CLN_RF:
  61. f.write("import pycarl.cln\n")
  62. if not self.conf.STORM_CLN_EA or not self.conf.STORM_CLN_RF:
  63. f.write("import pycarl.gmp\n")
  64. if self.conf.STORM_CLN_EA:
  65. f.write("Rational = pycarl.cln.Rational\n")
  66. else:
  67. f.write("Rational = pycarl.gmp.Rational\n")
  68. if self.conf.STORM_CLN_RF:
  69. rfpackage = "cln"
  70. else:
  71. rfpackage = "gmp"
  72. f.write("RationalRF = pycarl.{}.Rational\n".format(rfpackage))
  73. f.write("Polynomial = pycarl.{}.Polynomial\n".format(rfpackage))
  74. f.write("FactorizedPolynomial = pycarl.{}.FactorizedPolynomial\n".format(rfpackage))
  75. f.write("RationalFunction = pycarl.{}.RationalFunction\n".format(rfpackage))
  76. f.write("FactorizedRationalFunction = pycarl.{}.FactorizedRationalFunction\n".format(rfpackage))
  77. elif ext.name == "info":
  78. with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  79. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  80. f.write("storm_version = {}\n".format(self.conf.STORM_VERSION))
  81. f.write("storm_cln_ea = {}\n".format(self.conf.STORM_CLN_EA))
  82. f.write("storm_cln_rf = {}".format(self.conf.STORM_CLN_RF))
  83. elif ext.name == "dft":
  84. with open(os.path.join(self.extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  85. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  86. f.write("has_storm_dft = {}".format(self.conf.HAVE_STORM_DFT))
  87. if not self.conf.HAVE_STORM_DFT:
  88. print("WARNING: storm-dft not found. No support for DFTs will be built.")
  89. continue
  90. self.build_extension(ext)
  91. def initialize_options(self):
  92. build_ext.initialize_options(self)
  93. self.storm_dir = None
  94. self.debug = False
  95. try:
  96. self.jobs = multiprocessing.cpu_count() if multiprocessing.cpu_count() is not None else 1
  97. except NotImplementedError:
  98. self.jobs = 1
  99. def finalize_options(self):
  100. if self.storm_dir is not None:
  101. print('The custom storm directory', self.storm_dir)
  102. build_ext.finalize_options(self)
  103. def build_extension(self, ext):
  104. extdir = self.extdir(ext.name)
  105. cmake_args = ['-DSTORMPY_LIB_DIR=' + extdir,
  106. '-DPYTHON_EXECUTABLE=' + sys.executable]
  107. build_type = 'Debug' if self.debug else 'Release'
  108. build_args = ['--config', build_type]
  109. build_args += ['--', '-j{}'.format(self.jobs)]
  110. cmake_args += ['-DCMAKE_BUILD_TYPE=' + build_type]
  111. if self.conf.STORM_DIR is not None:
  112. cmake_args += ['-Dstorm_DIR=' + self.conf.STORM_DIR]
  113. if self.conf.HAVE_STORM_DFT:
  114. cmake_args += ['-DHAVE_STORM_DFT=ON']
  115. env = os.environ.copy()
  116. env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
  117. self.distribution.get_version())
  118. if not os.path.exists(self.build_temp):
  119. os.makedirs(self.build_temp)
  120. print("CMake args={}".format(cmake_args))
  121. # Call cmake
  122. subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
  123. subprocess.check_call(['cmake', '--build', '.', '--target', ext.name] + build_args, cwd=self.build_temp)
  124. class PyTest(test):
  125. def run_tests(self):
  126. # import here, cause outside the eggs aren't loaded
  127. import pytest
  128. errno = pytest.main(['tests'])
  129. sys.exit(errno)
  130. setup(
  131. name="stormpy",
  132. version="0.9.1",
  133. author="M. Volk",
  134. author_email="matthias.volk@cs.rwth-aachen.de",
  135. maintainer="S. Junges",
  136. maintainer_email="sebastian.junges@cs.rwth-aachen.de",
  137. url="http://moves.rwth-aachen.de",
  138. description="stormpy - Python Bindings for Storm",
  139. packages=['stormpy', 'stormpy.info', 'stormpy.expressions', 'stormpy.logic', 'stormpy.storage', 'stormpy.utility', 'stormpy.dft'],
  140. package_dir={'': 'lib'},
  141. ext_package='stormpy',
  142. ext_modules=[CMakeExtension('core', subdir=''),
  143. CMakeExtension('info', subdir='info'),
  144. CMakeExtension('expressions', subdir='expressions'),
  145. CMakeExtension('logic', subdir='logic'),
  146. CMakeExtension('storage', subdir='storage'),
  147. CMakeExtension('utility', subdir='utility'),
  148. CMakeExtension('dft', subdir='dft')
  149. ],
  150. cmdclass={'build_ext': CMakeBuild, 'test': PyTest},
  151. zip_safe=False,
  152. install_requires=['pycarl>=2.0.0'],
  153. tests_require=['pytest'],
  154. )