Include .py file migration in build_py packaging step; perform build_clib and build_ext before build_py packaging step
This commit is contained in:
@@ -6,6 +6,7 @@ See: https://stackoverflow.com/questions/60073711/how-to-build-c-extensions-via-
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
from distutils.command.build_py import build_py
|
||||
from distutils.command.build_ext import build_ext
|
||||
from distutils.errors import DistutilsPlatformError, CCompilerError, DistutilsExecError
|
||||
from distutils.extension import Extension
|
||||
@@ -52,6 +53,7 @@ class LibztModule(Extension):
|
||||
"libzt._libzt",
|
||||
sources=sources,
|
||||
include_dirs=INCLUDE_DIRS,
|
||||
# depends = ... # TODO: specify dependencies for recompile
|
||||
extra_compile_args=[
|
||||
"-std=c++11",
|
||||
"-DZTS_ENABLE_PYTHON=1",
|
||||
@@ -95,21 +97,42 @@ def cstuff():
|
||||
})
|
||||
|
||||
|
||||
def copy_python_files(src_dir: str, dst_dir: str):
|
||||
""" Copy all Python files from `src_dir` to `dst_dir`"""
|
||||
for filename in {os.path.basename(filepath) for filepath in glob(os.path.join(src_dir, "*.py"))}:
|
||||
shutil.copy(os.path.join(src_dir, filename), os.path.join(dst_dir, filename))
|
||||
|
||||
|
||||
class BuildFailed(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class PyBuilder(build_py):
|
||||
|
||||
def run(self):
|
||||
# Build extension module first since libzt.py is generated by SWIG
|
||||
self.run_command("build_clib")
|
||||
self.run_command("build_ext")
|
||||
return super().run()
|
||||
|
||||
def build_packages(self):
|
||||
# Copy Python files into packaging directory
|
||||
copy_python_files(src_dir=os.path.join(ROOT_DIR, "src/bindings/python"),
|
||||
dst_dir=os.path.join(DIR, "libzt"))
|
||||
super().build_packages()
|
||||
|
||||
|
||||
class ExtBuilder(build_ext):
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
build_ext.run(self)
|
||||
super().run()
|
||||
except (DistutilsPlatformError, FileNotFoundError):
|
||||
raise BuildFailed('File not found. Could not compile C extension.')
|
||||
|
||||
def build_extension(self, ext):
|
||||
try:
|
||||
build_ext.build_extension(self, ext)
|
||||
super().build_extension(ext)
|
||||
except (CCompilerError, DistutilsExecError, DistutilsPlatformError, ValueError):
|
||||
raise BuildFailed('Could not compile C extension.')
|
||||
|
||||
@@ -122,18 +145,10 @@ def build(setup_kwargs):
|
||||
# Ensure git submodules are loaded
|
||||
subprocess.run(["git", "submodule", "update", "--init"])
|
||||
|
||||
# Copy Python files into packaging directory
|
||||
src_dir = os.path.join(ROOT_DIR, "src", "bindings", "python")
|
||||
dst_dir = os.path.join(DIR, "libzt")
|
||||
for filename in os.listdir(src_dir):
|
||||
if os.path.isfile(os.path.join(src_dir, filename)) and filename.endswith(".py"):
|
||||
shutil.copy(os.path.join(src_dir, filename), os.path.join(dst_dir, filename))
|
||||
|
||||
# LICENSE file
|
||||
shutil.copy(os.path.join(ROOT_DIR, "LICENSE.txt"), os.path.join(DIR, "LICENSE"))
|
||||
|
||||
# Pass up extensions information
|
||||
setup_kwargs["libraries"] = [cstuff()]
|
||||
setup_kwargs["ext_modules"] = [LibztModule()]
|
||||
setup_kwargs["py_modules"] = ["libzt"]
|
||||
setup_kwargs["cmdclass"] = {"build_ext": ExtBuilder}
|
||||
setup_kwargs["cmdclass"] = {"build_py": PyBuilder, "build_ext": ExtBuilder}
|
||||
|
||||
Reference in New Issue
Block a user