Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
156 lines (124 loc) · 5.66 KB
/
Copy pathsetup.py
File metadata and controls
156 lines (124 loc) · 5.66 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
154
155
156
"""Setuptools build hooks for projecting OpenHCS MCP knowledge resources.
Dependency and package metadata comes exclusively from ``pyproject.toml``.
"""
import os
import runpy
import shutil
from abc import ABC, abstractmethod
from pathlib import Path
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools.command.build_py import build_py as _build_py
from setuptools.command.sdist import sdist as _sdist
_KNOWLEDGE_BUILD_HELPERS = runpy.run_path(
str(Path(__file__).resolve().parent / "scripts/build_mcp_knowledge_assets.py")
)
KNOWLEDGE_MANIFEST_RELATIVE_PATH = _KNOWLEDGE_BUILD_HELPERS[
"KNOWLEDGE_MANIFEST_RELATIVE_PATH"
]
PACKAGED_KNOWLEDGE_ROOT_RELATIVE_PATH = _KNOWLEDGE_BUILD_HELPERS[
"PACKAGED_KNOWLEDGE_ROOT_RELATIVE_PATH"
]
project_knowledge_assets = _KNOWLEDGE_BUILD_HELPERS["project_knowledge_assets"]
class BuildPyWithMcpKnowledge(_build_py):
"""Project a fresh OpenHCS package and its canonical MCP knowledge assets."""
def run(self):
package_build_root = Path(self.build_lib) / "openhcs"
if package_build_root.exists():
shutil.rmtree(package_build_root)
super().run()
project_root = Path(__file__).resolve().parent
if (project_root / KNOWLEDGE_MANIFEST_RELATIVE_PATH).is_file():
project_knowledge_assets(
project_root,
Path(self.build_lib) / PACKAGED_KNOWLEDGE_ROOT_RELATIVE_PATH,
)
class SdistWithMcpKnowledge(_sdist):
"""Include projected MCP knowledge resources in source distributions."""
def make_release_tree(self, base_dir, files):
super().make_release_tree(base_dir, files)
project_knowledge_assets(
Path(__file__).resolve().parent,
Path(base_dir) / PACKAGED_KNOWLEDGE_ROOT_RELATIVE_PATH,
)
class OpenHCSNativeExtension(Extension, ABC):
"""Build native module declarations with one stable-ABI compiler policy."""
@property
@abstractmethod
def qualified_module_name(self) -> str:
"""Declare the native module; its qualified name also owns its source path."""
def __init__(self) -> None:
module_name = self.qualified_module_name
source_path = Path(*module_name.split(".")).with_suffix(".cpp")
super().__init__(
module_name,
sources=[source_path.as_posix()],
language="c++",
define_macros=[("Py_LIMITED_API", "0x030B0000")],
py_limited_api=True,
extra_compile_args=["/O2"] if os.name == "nt" else ["-O3"],
)
def prepare_build_sources(self, build_root: Path) -> None:
"""Prepare any generated sources at the native package-build boundary."""
@classmethod
def declared_extensions(cls) -> list[Extension]:
"""Derive compilation targets from concrete declarations in this family."""
return [declaration() for declaration in cls.__subclasses__()]
class GranularityNativeExtension(OpenHCSNativeExtension):
@property
def qualified_module_name(self) -> str:
return "openhcs.processing.backends.cellprofiler._granularity_native"
class TabularNativeExtension(OpenHCSNativeExtension):
@property
def qualified_module_name(self) -> str:
return "openhcs.core._tabular_native"
class MedianNativeExtension(OpenHCSNativeExtension):
@property
def qualified_module_name(self) -> str:
return "openhcs.processing.backends.cellprofiler._median_native"
def prepare_build_sources(self, build_root: Path) -> None:
generator = runpy.run_path(
str(Path(__file__).resolve().parent / "scripts/build_median_network.py")
)
generated_root = build_root / "median-network"
generator["build_median_network_header"](
generated_root / "_median_network_generated.h"
)
self.include_dirs.append(str(generated_root.resolve()))
self.depends.extend([
str(Path(__file__).resolve().parent / "scripts/build_median_network.py"),
str(generated_root / "_median_network_generated.h"),
])
class FontRasterNativeExtension(OpenHCSNativeExtension):
"""Keep the CellProfiler-compatible font engine local to its native module."""
@property
def qualified_module_name(self) -> str:
return "openhcs.processing.backends.cellprofiler._font_raster_native"
def prepare_build_sources(self, build_root: Path) -> None:
project_root = Path(__file__).resolve().parent
helper = project_root / "scripts/build_freetype_sources.py"
vendor = project_root / "vendor/freetype-2.6.1"
declarations = runpy.run_path(str(helper))
self.sources.append(str(Path(*self.qualified_module_name.split(".")).with_name("_font_raster.c")))
self.sources.extend(
Path(source).relative_to(project_root).as_posix()
for source in declarations["freetype_sources"](vendor)
)
self.include_dirs.append(str(vendor / "include"))
self.define_macros.append(("FT2_BUILD_LIBRARY", None))
self.depends.extend([str(helper), *map(str, vendor.rglob("*.h"))])
if os.name != "nt":
self.extra_compile_args.append("-fvisibility=hidden")
class BuildNativeExtensions(_build_ext):
"""Prepare declared native sources without executing runtime compilation."""
def build_extension(self, extension):
extension.prepare_build_sources(Path(self.build_temp))
super().build_extension(extension)
setup(
ext_modules=OpenHCSNativeExtension.declared_extensions(),
cmdclass={
"build_ext": BuildNativeExtensions,
"build_py": BuildPyWithMcpKnowledge,
"sdist": SdistWithMcpKnowledge,
},
)