-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
242 lines (209 loc) · 7.99 KB
/
Copy pathsetup.py
File metadata and controls
242 lines (209 loc) · 7.99 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from __future__ import annotations
import os
import platform
import re
import shlex
import shutil
import subprocess
import sys
import sysconfig
from pathlib import Path
from setuptools import Distribution, setup
from setuptools.command.build_py import build_py as _build_py
from setuptools.command.sdist import sdist as _sdist
sys.path.insert(0, str(Path(__file__).resolve().parent))
from _reproducible_sdist import make_reproducible_gztar, parse_source_date_epoch
try:
from setuptools.command.bdist_wheel import bdist_wheel as _bdist_wheel
except ImportError:
from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
ROOT = Path(__file__).resolve().parent
def _project_version() -> str:
text = (ROOT / "pyproject.toml").read_text(encoding="utf-8")
match = re.search(r'^version\s*=\s*"([^"]+)"', text, flags=re.MULTILINE)
if match is None:
raise RuntimeError("pyproject.toml version not found")
return match.group(1)
def _macos_arch_flags() -> list[str]:
flags = shlex.split(sysconfig.get_config_var("CFLAGS") or "")
arch_flags: list[str] = []
i = 0
while i < len(flags):
if flags[i] == "-arch" and i + 1 < len(flags):
arch_flags.extend(flags[i : i + 2])
i += 2
continue
i += 1
return arch_flags
def _macos_deployment_target(arch_flags: list[str]) -> str:
configured = sysconfig.get_config_var("MACOSX_DEPLOYMENT_TARGET") or "10.9"
major_text, _sep, minor_text = configured.partition(".")
major = int(major_text)
minor = int(minor_text or 0)
if "arm64" in arch_flags and major < 11:
return "11.0"
return f"{major}.{minor}"
if platform.system() == "Darwin":
_initial_arch_flags = _macos_arch_flags()
os.environ.setdefault("MACOSX_DEPLOYMENT_TARGET", _macos_deployment_target(_initial_arch_flags))
class sdist(_sdist):
def make_archive(
self,
base_name,
format,
root_dir=None,
base_dir=None,
owner=None,
group=None,
):
epoch_text = os.environ.get("SOURCE_DATE_EPOCH")
if epoch_text is not None and format == "gztar":
if base_dir is None:
raise RuntimeError("gztar source distribution requires a release-tree directory")
return make_reproducible_gztar(
base_name,
root_dir=root_dir,
base_dir=base_dir,
epoch=parse_source_date_epoch(epoch_text),
)
return super().make_archive(
base_name,
format,
root_dir=root_dir,
base_dir=base_dir,
owner=owner,
group=group,
)
class BinaryDistribution(Distribution):
def has_ext_modules(self) -> bool:
return True
class build_py(_build_py):
def run(self) -> None:
super().run()
self.build_native_library()
self.build_native_cli()
self.copy_assay_evidence()
self.copy_agent_assets()
def copy_assay_evidence(self) -> None:
out_dir = Path(self.build_lib) / "dotmatch" / "data"
out_dir.mkdir(parents=True, exist_ok=True)
self.copy_file(str(ROOT / "docs" / "assay-evidence.json"), str(out_dir / "assay-evidence.json"))
def copy_agent_assets(self) -> None:
out_dir = Path(self.build_lib) / "dotmatch" / "data"
out_dir.mkdir(parents=True, exist_ok=True)
for name in (
"agent-capabilities.json",
"agent-capabilities.schema.json",
"agent-tools.json",
"agent-tools.schema.json",
):
self.copy_file(str(ROOT / name), str(out_dir / name))
source_skill = ROOT / "extensions" / "codex" / "dotmatch-agent"
target_skill = out_dir / "codex-skill"
if target_skill.exists():
shutil.rmtree(target_skill)
shutil.copytree(source_skill, target_skill)
def build_native_library(self) -> None:
system = platform.system()
arch_flags: list[str] = []
if system == "Darwin":
lib_name = "libdotmatch.dylib"
link_flags = ["-dynamiclib", "-install_name", "@rpath/libdotmatch.dylib"]
arch_flags = _macos_arch_flags()
build_env = os.environ.copy()
build_env.setdefault("MACOSX_DEPLOYMENT_TARGET", _macos_deployment_target(arch_flags))
elif system == "Linux":
lib_name = "libdotmatch.so"
link_flags = ["-shared"]
build_env = os.environ.copy()
else:
raise RuntimeError(f"DotMatch Python wheels are not yet supported on {system}")
build_cmd = self.get_finalized_command("build")
build_temp = Path(build_cmd.build_temp)
build_temp.mkdir(parents=True, exist_ok=True)
object_path = build_temp / "qdalign.package.o"
out_dir = Path(self.build_lib) / "dotmatch"
out_dir.mkdir(parents=True, exist_ok=True)
out_path = out_dir / lib_name
cc = shlex.split(os.environ.get("CC") or sysconfig.get_config_var("CC") or "cc")
cppflags = shlex.split(os.environ.get("CPPFLAGS", ""))
cflags = shlex.split(os.environ.get("CFLAGS", ""))
ldflags = shlex.split(os.environ.get("LDFLAGS", ""))
compile_cmd = [
*cc,
*arch_flags,
"-O3",
"-std=c11",
"-Wall",
"-Wextra",
"-Wpedantic",
"-fPIC",
"-I",
str(ROOT / "include"),
*cppflags,
*cflags,
"-c",
str(ROOT / "src" / "qdalign.c"),
"-o",
str(object_path),
]
link_cmd = [*cc, *arch_flags, *link_flags, str(object_path), "-o", str(out_path), *ldflags]
subprocess.run(compile_cmd, check=True, env=build_env)
subprocess.run(link_cmd, check=True, env=build_env)
def build_native_cli(self) -> None:
system = platform.system()
arch_flags: list[str] = []
if system == "Darwin":
arch_flags = _macos_arch_flags()
build_env = os.environ.copy()
build_env.setdefault("MACOSX_DEPLOYMENT_TARGET", _macos_deployment_target(arch_flags))
elif system == "Linux":
build_env = os.environ.copy()
else:
raise RuntimeError(f"DotMatch Python wheels are not yet supported on {system}")
build_cmd = self.get_finalized_command("build")
build_temp = Path(build_cmd.build_temp)
build_temp.mkdir(parents=True, exist_ok=True)
out_dir = Path(self.build_lib) / "dotmatch"
out_dir.mkdir(parents=True, exist_ok=True)
out_path = out_dir / "dotmatch-native"
cc = shlex.split(os.environ.get("CC") or sysconfig.get_config_var("CC") or "cc")
cppflags = shlex.split(os.environ.get("CPPFLAGS", ""))
cflags = shlex.split(os.environ.get("CFLAGS", ""))
ldflags = shlex.split(os.environ.get("LDFLAGS", ""))
compile_cmd = [
*cc,
*arch_flags,
"-O3",
"-std=c11",
"-Wall",
"-Wextra",
"-Wpedantic",
"-I",
str(ROOT / "include"),
f'-DDOTMATCH_VERSION="{_project_version()}"',
*cppflags,
*cflags,
str(ROOT / "src/qda.c"),
str(ROOT / "src/qdalign.c"),
str(ROOT / "src/qdmetal_stub.c"),
"-o",
str(out_path),
*ldflags,
"-lz",
"-pthread",
]
subprocess.run(compile_cmd, check=True, env=build_env)
out_path.chmod(0o755)
class bdist_wheel(_bdist_wheel):
def finalize_options(self) -> None:
super().finalize_options()
self.root_is_pure = False
def get_tag(self) -> tuple[str, str, str]:
_python, _abi, platform_tag = super().get_tag()
return "py3", "none", platform_tag
setup(
cmdclass={"build_py": build_py, "bdist_wheel": bdist_wheel, "sdist": sdist},
distclass=BinaryDistribution,
package_data={"dotmatch": ["libdotmatch.*", "dotmatch-native", "data/*.json"]},
)