The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

226 lines
10 KiB

2 months 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 carl version required
  13. carl_min_version = "17.12"
  14. carl_max_version = "19.12"
  15. carl_master14_version = "14."
  16. # Get the long description from the README file
  17. with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'README.md'), encoding='utf-8') as f:
  18. long_description = f.read()
  19. class CMakeExtension(Extension):
  20. def __init__(self, name, sourcedir='', subdir=''):
  21. Extension.__init__(self, name, sources=[])
  22. self.sourcedir = os.path.abspath(sourcedir)
  23. self.subdir = subdir
  24. class CMakeBuild(build_ext):
  25. user_options = build_ext.user_options + [
  26. ('carl-dir=', None, 'Path to carl root (binary) location'),
  27. ('carl-parser-dir=', None, 'Path to carl-parser root (binary) location'),
  28. ('disable-cln', None, 'Disable support for CLN'),
  29. ('disable-parser', None, 'Disable parsing support'),
  30. ('debug', None, 'Build in Debug mode'),
  31. ('jobs=', 'j', 'Number of jobs to use for compiling'),
  32. ]
  33. config = SetupConfig()
  34. def _extdir(self, extname):
  35. return os.path.abspath(os.path.dirname(self.get_ext_fullpath(extname)))
  36. def run(self):
  37. try:
  38. _ = subprocess.check_output(['cmake', '--version'])
  39. except OSError:
  40. raise RuntimeError("CMake must be installed to build the following extensions: " +
  41. ", ".join(e.name for e in self.extensions))
  42. # Build cmake version info
  43. build_temp_version = self.build_temp + "-version"
  44. setup_helper.ensure_dir_exists(build_temp_version)
  45. # Write config
  46. setup_helper.ensure_dir_exists("build")
  47. self.config.write_config("build/build_config.cfg")
  48. cmake_args = []
  49. carl_dir = os.path.expanduser(self.config.get_as_string("carl_dir"))
  50. carl_parser_dir = os.path.expanduser(self.config.get_as_string("carl_parser_dir"))
  51. if carl_dir:
  52. cmake_args += ['-Dcarl_DIR=' + carl_dir]
  53. if carl_parser_dir:
  54. cmake_args += ['-Dcarl_parser_DIR=' + carl_parser_dir]
  55. output = subprocess.check_output(['cmake', os.path.abspath("cmake")] + cmake_args, cwd=build_temp_version)
  56. cmake_conf = setup_helper.load_cmake_config(os.path.join(build_temp_version, 'generated/config.py'))
  57. # Set carl directory
  58. if carl_dir == "":
  59. carl_dir = cmake_conf.CARL_DIR
  60. if carl_dir != cmake_conf.CARL_DIR:
  61. print("Pycarl - Warning: Using different carl directory {} instead of given {}!".format(cmake_conf.CARL_DIR,
  62. carl_dir))
  63. carl_dir = cmake_conf.CARL_DIR
  64. # Set carl-parser directory
  65. if carl_parser_dir == "":
  66. carl_parser_dir = cmake_conf.CARL_PARSER_DIR
  67. if carl_parser_dir != cmake_conf.CARL_PARSER_DIR:
  68. print("Pycarl - Warning: Using different carl-parser directory {} instead of given {}!".format(
  69. cmake_conf.CARL_PARSER_DIR, carl_parser_dir))
  70. carl_parser_dir = cmake_conf.CARL_PARSER_DIR
  71. # Check version
  72. carl_version, carl_commit = setup_helper.parse_carl_version(cmake_conf.CARL_VERSION)
  73. if carl_version.startswith(carl_master14_version):
  74. print("Pycarl - Using carl with master14 branch.")
  75. elif StrictVersion(carl_version) < StrictVersion(carl_min_version):
  76. sys.exit("Pycarl - Error: carl version {} from '{}' is not supported anymore!".format(carl_version, carl_dir))
  77. elif StrictVersion(carl_version) > StrictVersion(carl_max_version):
  78. sys.exit("Pycarl - Error: carl version {} from '{}' is not supported!".format(carl_version, carl_dir))
  79. # Check additional support
  80. use_cln = cmake_conf.CARL_WITH_CLN and not self.config.get_as_bool("disable_cln")
  81. use_parser = cmake_conf.CARL_WITH_PARSER and not self.config.get_as_bool("disable_parser")
  82. # Print build info
  83. print("Pycarl - Using carl {} from {}".format(carl_version, carl_dir))
  84. if use_parser:
  85. print("Pycarl - carl parser extension from {} included.".format(carl_parser_dir))
  86. else:
  87. print("Pycarl - Warning: No parser support!")
  88. if use_cln:
  89. print("Pycarl - Support for CLN found and included.")
  90. else:
  91. print("Pycarl - Warning: No support for CLN!")
  92. # Set general cmake build options
  93. build_type = 'Debug' if self.config.get_as_bool("debug") else 'Release'
  94. cmake_args = ['-DPYTHON_EXECUTABLE=' + sys.executable]
  95. cmake_args += ['-DCMAKE_BUILD_TYPE=' + build_type]
  96. if carl_dir is not None:
  97. cmake_args += ['-Dcarl_DIR=' + carl_dir]
  98. if use_parser and carl_parser_dir:
  99. cmake_args += ['-Dcarl_parser_DIR=' + carl_parser_dir]
  100. build_args = ['--config', build_type]
  101. build_args += ['--', '-j{}'.format(self.config.get_as_int("jobs"))]
  102. # Build extensions
  103. for ext in self.extensions:
  104. setup_helper.ensure_dir_exists(os.path.join(self._extdir(ext.name), ext.subdir))
  105. if "core" in ext.name:
  106. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  107. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  108. f.write('CARL_VERSION = "{}"\n'.format(carl_version))
  109. f.write("CARL_WITH_PARSER = {}\n".format(use_parser))
  110. f.write("CARL_WITH_CLN = {}\n".format(use_cln))
  111. if "cln" in ext.name:
  112. with open(os.path.join(self._extdir(ext.name), ext.subdir, "_config.py"), "w") as f:
  113. f.write("# Generated from setup.py at {}\n".format(datetime.datetime.now()))
  114. f.write("CARL_WITH_CLN = {}\n".format(use_cln))
  115. if not use_cln:
  116. print("Pycarl - CLN bindings skipped")
  117. continue
  118. if "parse" in ext.name:
  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("CARL_WITH_PARSER = {}\n".format(use_parser))
  122. if not use_parser:
  123. print("Pycarl - Parser bindings skipped")
  124. continue
  125. self.build_extension(ext, cmake_args, build_args)
  126. def initialize_options(self):
  127. build_ext.initialize_options(self)
  128. # Load setup config
  129. self.config.load_from_file("build/build_config.cfg")
  130. # Set default values for custom cmdline flags
  131. self.carl_dir = None
  132. self.carl_parser_dir = None
  133. self.disable_cln = None
  134. self.disable_parser = None
  135. self.debug = None
  136. self.jobs = None
  137. def finalize_options(self):
  138. build_ext.finalize_options(self)
  139. # Update setup config
  140. self.config.update("carl_dir", self.carl_dir)
  141. self.config.update("carl_parser_dir", self.carl_parser_dir)
  142. self.config.update("disable_cln", self.disable_cln)
  143. self.config.update("disable_parser", self.disable_parser)
  144. self.config.update("debug", self.debug)
  145. self.config.update("jobs", self.jobs)
  146. def build_extension(self, ext, general_cmake_args, general_build_args):
  147. extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
  148. cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + os.path.join(extdir, ext.subdir)] + general_cmake_args
  149. build_args = general_build_args
  150. env = os.environ.copy()
  151. env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
  152. self.distribution.get_version())
  153. setup_helper.ensure_dir_exists(self.build_temp)
  154. print("Pycarl - CMake args={}".format(cmake_args))
  155. # Call cmake
  156. subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
  157. subprocess.check_call(['cmake', '--build', '.', '--target', ext.name] + build_args, cwd=self.build_temp)
  158. setup(
  159. name='pycarl',
  160. version=setup_helper.obtain_version(),
  161. author="S. Junges",
  162. author_email="sebastian.junges@cs.rwth-aachen.de",
  163. maintainer="M. Volk",
  164. maintainer_email="matthias.volk@cs.rwth-aachen.de",
  165. url="https://github.com/moves-rwth/pycarl/",
  166. description="pycarl - Python Bindings for CArL",
  167. long_description=long_description,
  168. long_description_content_type='text/markdown',
  169. project_urls={
  170. 'Documentation': 'https://moves-rwth.github.io/pycarl/',
  171. 'Source': 'https://github.com/moves-rwth/pycarl/',
  172. 'Bug reports': 'https://github.com/moves-rwth/pycarl/issues',
  173. },
  174. classifiers=[
  175. 'Intended Audience :: Science/Research',
  176. 'Topic :: Scientific/Engineering',
  177. 'Topic :: Software Development :: Libraries :: Python Modules',
  178. ],
  179. packages=find_packages('lib'),
  180. package_dir={'': 'lib'},
  181. include_package_data=True,
  182. ext_package='pycarl',
  183. ext_modules=[CMakeExtension('core', subdir=''),
  184. CMakeExtension('cln', subdir='cln'),
  185. CMakeExtension('gmp', subdir='gmp'),
  186. CMakeExtension('formula', subdir='formula'),
  187. CMakeExtension('formula-cln', subdir='cln/formula'),
  188. CMakeExtension('formula-gmp', subdir='gmp/formula'),
  189. CMakeExtension('parse', subdir='parse'),
  190. CMakeExtension('parse-gmp', subdir='gmp/parse'),
  191. CMakeExtension('parse-cln', subdir='cln/parse')],
  192. cmdclass={'build_ext': CMakeBuild},
  193. zip_safe=False,
  194. setup_requires=['pytest-runner'],
  195. tests_require=['pytest'],
  196. python_requires='>=3',
  197. )