File indexing completed on 2024-07-14 07:17:18

0001 import os
0002 import sys
0003 import pathlib
0004 import subprocess
0005 import shutil
0006 from setuptools import setup, Extension
0007 from setuptools.command.build_ext import build_ext
0008 
0009 
0010 class CMakeExtension(Extension):
0011     def __init__(self, name, sourcedir=""):
0012         Extension.__init__(self, name, sources=[])
0013         self.sourcedir = os.path.abspath(sourcedir)
0014 
0015 
0016 class CMakeBuild(build_ext):
0017     def run(self):
0018         try:
0019             subprocess.check_output(["cmake", "--version"])
0020         except OSError:
0021             raise RuntimeError(
0022                 "CMake must be installed to build the following extensions: "
0023                 + ", ".join(e.name for e in self.extensions)
0024             )
0025 
0026         for ext in self.extensions:
0027             self.build_extension(ext)
0028 
0029     def build_extension(self, ext):
0030         extdir = os.path.dirname(self.get_ext_fullpath(ext.name))
0031 
0032         # required for auto-detection of auxiliary "native" libs
0033         if not extdir.endswith(os.path.sep):
0034             extdir += os.path.sep
0035 
0036         cmake_args = [
0037             "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir,
0038             "-DPYTHON_EXECUTABLE=" + sys.executable,
0039         ]
0040 
0041         cfg = "Debug" if self.debug else "Release"
0042         build_args = ["--config", cfg]
0043 
0044         cmake_args += ["-DCMAKE_BUILD_TYPE=" + cfg, "-DBUILD_PYKSTARS=YES", "-GNinja"]
0045         build_args += ["--"]
0046 
0047         env = os.environ.copy()
0048         env["CXXFLAGS"] = '{} -DVERSION_INFO=\\"{}\\"'.format(
0049             env.get("CXXFLAGS", ""), self.distribution.get_version()
0050         )
0051         if not os.path.exists(self.build_temp):
0052             os.makedirs(self.build_temp)
0053 
0054         if not os.path.exists(extdir):
0055             os.makedirs(extdir)
0056 
0057         subprocess.check_call(
0058             ["cmake", ext.sourcedir + "/../../"] + cmake_args,
0059             cwd=self.build_temp,
0060             env=env,
0061         )
0062 
0063         subprocess.check_call(
0064             ["cmake", "--build", ".", "--target", "pykstars"] + build_args,
0065             cwd=self.build_temp,
0066         )
0067 
0068         ext_file = subprocess.run(
0069             ["find", "-name", "pykstars*.so"],
0070             cwd=self.build_temp,
0071             check=True,
0072             capture_output=True,
0073         ).stdout.decode("utf-8")[:-1]
0074 
0075         shutil.copy2(os.path.join(self.build_temp, ext_file), extdir)
0076 
0077 
0078 # The directory containing this file
0079 HERE = pathlib.Path(__file__).parent
0080 
0081 # The text of the README file
0082 README = (HERE / "readme.md").read_text()
0083 
0084 setup(
0085     name="pykstars",
0086     version="0.0.2",
0087     author="Valentin Boettcher",
0088     author_email="hiro@protagon.space",
0089     description="Some python bindings for kstars. Primarily used to package DSO catalogs.",
0090     long_description_content_type="text/markdown",
0091     license="GPLv2+",
0092     long_description=README,
0093     include_package_data=True,
0094     ext_modules=[CMakeExtension("pykstars")],
0095     cmdclass=dict(build_ext=CMakeBuild),
0096     zip_safe=False,
0097 )