forked from tobspr/P3DModuleBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
127 lines (103 loc) · 4.35 KB
/
setup.py
File metadata and controls
127 lines (103 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
setuptools entry-point that drives the existing CMake / interrogate build
and packages the resulting extension module into a wheel.
The package name and build options are read from config.ini (module_name).
Set the SETUPTOOLS_SCM_PRETEND_VERSION environment variable to control the
wheel version (the CI workflow does this automatically).
"""
import os
import platform
import shutil
import sys
from pathlib import Path
from types import SimpleNamespace
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
def _read_config():
"""Read the simple key=value config.ini next to this file."""
config = {}
config_path = Path(__file__).resolve().parent / "config.ini"
if config_path.exists():
for line in config_path.read_text().splitlines():
line = line.strip()
if line and "=" in line:
key, value = line.split("=", 1)
config[key.strip()] = value.strip()
return config
_CONFIG = _read_config()
MODULE_NAME = _CONFIG.get("module_name", "p3d_module")
DESCRIPTION = _CONFIG.get("description", "Panda3D C++ extension module")
# ---------------------------------------------------------------------------
# CMake-backed extension
# ---------------------------------------------------------------------------
class CMakeExtension(Extension):
"""Marker extension — no real source list; CMake handles everything."""
def __init__(self, name: str):
super().__init__(name, sources=[])
class CMakeBuild(build_ext):
"""build_ext that delegates to the existing scripts/ CMake pipeline."""
def build_extension(self, ext: Extension):
project_dir = Path(__file__).resolve().parent
# panda3d must be importable before we touch the scripts package.
import panda3d.core # noqa
# Ensure the project root is on sys.path so the scripts package
# resolves correctly regardless of cwd.
root_str = str(project_dir)
if root_str not in sys.path:
sys.path.insert(0, root_str)
# Change into the project directory (scripts expect it).
orig_cwd = os.getcwd()
os.chdir(root_str)
try:
from scripts.common import get_ini_conf
from scripts.setup import make_output_dir, run_cmake, run_cmake_build
config = get_ini_conf(str(project_dir / "config.ini"))
args = SimpleNamespace(optimize=None, clean=False)
make_output_dir(clean=False)
run_cmake(config, args)
run_cmake_build(config, args)
finally:
os.chdir(orig_cwd)
# build.py / finalize.py copy the binary to the project root.
if platform.system().lower() == "windows":
built = project_dir / f"{ext.name}.pyd"
else:
built = project_dir / f"{ext.name}.so"
if not built.exists():
raise FileNotFoundError(
f"Build did not produce expected binary: {built}"
)
# Place the binary where setuptools expects it for the wheel.
dest = Path(self.get_ext_fullpath(ext.name))
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(str(built), str(dest))
setup(
name=MODULE_NAME,
version=os.environ.get("SETUPTOOLS_SCM_PRETEND_VERSION", "0.0.0"),
description=DESCRIPTION,
long_description=Path("README.md").read_text(encoding="utf-8"),
long_description_content_type="text/markdown",
license="MIT",
license_files=["LICENSE"],
python_requires=">=3.10",
install_requires=["panda3d"],
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX :: Linux",
"Operating System :: MacOS",
"Programming Language :: C++",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Topic :: Games/Entertainment",
"Topic :: Software Development :: Libraries :: Python Modules",
],
ext_modules=[CMakeExtension(MODULE_NAME)],
cmdclass={"build_ext": CMakeBuild},
)