Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
153 lines (121 loc) · 4.84 KB
/
Copy pathsetup.py
File metadata and controls
153 lines (121 loc) · 4.84 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from __future__ import annotations
import os
import json
import shutil
import subprocess
import sys
from pathlib import Path
from setuptools import Distribution, Extension, setup
from setuptools.command.build_ext import build_ext
from setuptools.command.build_py import build_py
from wheel.bdist_wheel import bdist_wheel
ROOT = Path(__file__).parent.resolve()
PRESETS = {
"msvc-release": (ROOT / "build-vs18-release", "Release"),
"msvc-debug": (ROOT / "build-vs18-debug", "Debug"),
"linux-release": (ROOT / "build-linux-release", "Release"),
}
class CMakeExtension(Extension):
def __init__(self, name: str) -> None:
super().__init__(name, sources=[])
class CMakeBuildExt(build_ext):
user_options = build_ext.user_options + [
("cmake-preset=", None, "CMake configure preset"),
]
def initialize_options(self) -> None:
super().initialize_options()
self.cmake_preset = None
def finalize_options(self) -> None:
super().finalize_options()
self.cmake_preset = (self.cmake_preset or os.environ.get("FREEIMAGE_CMAKE_PRESET") or "msvc-release")
if self.cmake_preset not in PRESETS:
available = ", ".join(PRESETS)
raise RuntimeError(f"Unknown CMake preset {self.cmake_preset!r}. Available presets: {available}")
def run(self) -> None:
binary_dir, configuration = PRESETS[self.cmake_preset]
self._configure(binary_dir)
metadata = self._read_metadata(binary_dir)
self._build(binary_dir, configuration)
self._install(binary_dir, configuration, metadata)
def build_extension(self, ext: Extension) -> None:
del ext
def _configure(self, binary_dir: Path) -> None:
subprocess.check_call(["cmake", "--preset", self.cmake_preset, "-DFREEIMAGE_WITH_PYTHON_BINDINGS=ON"], cwd=ROOT)
def _build(self, binary_dir: Path, configuration: str) -> None:
command = ["cmake", "--build", str(binary_dir), "--target", "FreeImagePyStubs"]
if configuration:
command.extend(["--config", configuration])
subprocess.check_call(command, cwd=ROOT)
def _read_metadata(self, binary_dir: Path) -> dict:
metadata_path = binary_dir / "freeimagere-build.json"
if not metadata_path.is_file():
raise RuntimeError(f"CMake metadata file was not generated: {metadata_path}. Reconfigure the selected CMake preset.")
with metadata_path.open(encoding="utf-8") as metadata_file:
return json.load(metadata_file)
def _install(self, binary_dir: Path, configuration: str, metadata: dict) -> None:
command = ["cmake", "--build", str(binary_dir), "--target", "install"]
if configuration:
command.extend(["--config", configuration])
subprocess.check_call(command, cwd=ROOT)
output_dir = Path(self.get_ext_fullpath("freeimage._freeimage")).parent
output_dir.mkdir(parents=True, exist_ok=True)
paths = []
for target in metadata["targets"].values():
if "install_path" in target:
paths.append(target["install_path"])
copied = set()
for relative_path in paths:
source = Path(relative_path)
if source.is_file():
shutil.copy2(source, output_dir / source.name)
copied.add(source.resolve())
install_root = Path(metadata["targets"]["freeimage"]["install_path"]).parent.parent
for source in install_root.rglob("*"):
if source.is_file() and source.resolve() not in copied:
if source.suffix.lower() in {".pyd", ".dll", ".so", ".pyi"} or ".so." in source.name:
shutil.copy2(source, output_dir / source.name)
init_path = ROOT / "Source/Python/__init__.py"
if init_path.exists():
package_dir = Path(self.build_lib) / "freeimage"
package_dir.mkdir(parents=True, exist_ok=True)
shutil.copy2(init_path, package_dir / init_path.name)
class BuildPy(build_py):
def run(self) -> None:
super().run()
license_path = ROOT / "license-fi.txt"
if license_path.exists():
target = Path(self.build_lib) / license_path.name
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(license_path, target)
class CMakeBDistWheel(bdist_wheel):
user_options = bdist_wheel.user_options + [("cmake-preset=", None, "CMake configure preset")]
def initialize_options(self) -> None:
super().initialize_options()
self.cmake_preset = None
def finalize_options(self) -> None:
super().finalize_options()
self.cmake_preset = self.cmake_preset or os.environ.get("FREEIMAGE_CMAKE_PRESET")
def run(self) -> None:
build_ext_command = self.distribution.get_command_obj("build_ext")
if self.cmake_preset:
build_ext_command.cmake_preset = self.cmake_preset
super().run()
class FreeImageDistribution(Distribution):
def has_ext_modules(self) -> bool:
return True
setup(
name="FreeImageRe",
version="4.2.2",
description="Python bindings for FreeImageRe",
author="Alexey Gruzdev",
python_requires=">=3.12",
install_requires=["numpy>= 2.0.0"],
ext_modules=[CMakeExtension("freeimage._freeimage")],
cmdclass={
"build_ext": CMakeBuildExt,
"build_py": BuildPy,
"bdist_wheel": CMakeBDistWheel,
},
distclass=FreeImageDistribution,
py_modules=[],
)