This repository was archived by the owner on Jan 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathhatch_build.py
More file actions
165 lines (127 loc) · 4.97 KB
/
hatch_build.py
File metadata and controls
165 lines (127 loc) · 4.97 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
import os
import platform
import re
import shutil
from pathlib import Path
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
SYSTEM_LIB_EXTENSION = {
"darwin": "dylib",
"linux": "so",
"freebsd": "so",
}
# Architecture aliases for platform tags
ARCH_ALIASES = {
"x86_64": "x86_64",
"amd64": "x86_64",
"aarch64": "arm64", # Back to arm64 for macOS compatibility
"arm64": "arm64",
"arm64v8": "arm64",
}
# Architecture aliases for filenames
FILENAME_ARCH_ALIASES = {
"x86_64": "amd64" if platform.system().lower() == "linux" else "x86_64",
"amd64": "amd64" if platform.system().lower() == "linux" else "x86_64",
"aarch64": "arm64",
"arm64": "arm64",
"arm64v8": "arm64",
}
# Minimum OS versions for different platforms
PLATFORM_TAGS = {
("darwin", "arm64"): "macosx_11_0_arm64",
("darwin", "x86_64"): "macosx_10_9_x86_64",
("linux", "aarch64"): "manylinux_2_28_aarch64", # Now using aarch64 for Linux
("linux", "x86_64"): "manylinux_2_28_x86_64",
}
def get_macos_arch_from_env():
"""Get architecture from ARCHFLAGS environment variable for macOS."""
archflags = os.environ.get("ARCHFLAGS", "")
if not archflags:
return None
# Look for -arch xxx in ARCHFLAGS
match = re.search(r"-arch\s+(\w+)", archflags)
if not match:
return None
arch = match.group(1)
return ARCH_ALIASES.get(arch, arch)
def get_normalized_arch(machine_name: str) -> str:
"""Normalize architecture name to unified format."""
system = platform.system().lower()
arch = ARCH_ALIASES.get(machine_name.lower(), machine_name.lower())
# For Linux, convert arm64 to aarch64 for platform tags
if system == "linux" and arch == "arm64":
return "aarch64"
return arch
def get_filename_arch(machine_name: str) -> str:
"""Get architecture name for use in filename."""
return FILENAME_ARCH_ALIASES.get(machine_name.lower(), machine_name.lower())
def get_bundled_tdlib_filename():
uname = platform.uname()
system_name = uname.system.lower()
machine_name = uname.machine.lower()
# Check ARCHFLAGS for macOS
if system_name == "darwin":
env_arch = get_macos_arch_from_env()
if env_arch:
machine_name = env_arch
# Get architecture name for filename
machine_name = get_filename_arch(machine_name)
extension = SYSTEM_LIB_EXTENSION.get(system_name)
if not extension:
return None
# Generate filename in format libtdjson_{system}_{arch}.{ext}
binary_name = f"libtdjson_{system_name}_{machine_name}.{extension}"
return binary_name
class CustomHook(BuildHookInterface):
PLUGIN_NAME = "hatch"
def initialize(self, version, build_data):
build_data["pure_python"] = False
# Get platform according to specification
system = platform.system().lower()
machine = platform.machine().lower()
# Check ARCHFLAGS for macOS
if system == "darwin":
env_arch = get_macos_arch_from_env()
if env_arch:
machine = env_arch
# Normalize architecture for platform tag
machine = get_normalized_arch(machine)
# Get platform tag from predefined values
platform_tag = PLATFORM_TAGS.get((system, machine))
if not platform_tag:
# Use safer format for unknown platforms
platform_tag = f"{system.replace('-', '_')}_{machine.replace('-', '_')}"
# Set tag in format py3-none-{platform_tag}
build_data["tag"] = f"py3-none-{platform_tag}"
# Get binary filename for current platform
binary_filename = get_bundled_tdlib_filename()
if not binary_filename:
# Skip wheel build for unsupported platforms
print(f"Skipping wheel build for unsupported platform: {platform_tag}")
build_data["skip"] = True
return
# Create target directory if it doesn't exist
target_dir = Path("aiotdlib/tdlib")
target_dir.mkdir(parents=True, exist_ok=True)
# Clean target directory from binary files
for ext in SYSTEM_LIB_EXTENSION.values():
for f in target_dir.glob(f"*.{ext}"):
f.unlink()
# Look for binary file in possible locations
source_file = None
tdlib_dir = Path(__file__).parent / "tdlib"
if tdlib_dir.exists():
binary_path = tdlib_dir / binary_filename
if binary_path.exists():
source_file = binary_path
if not source_file:
# Skip wheel build if binary file not found
print(f"Binary file {binary_filename} not found, skipping wheel build")
build_data["skip"] = True
return
target_file = target_dir / binary_filename
print(f"Copying {source_file} to {target_file}")
shutil.copy2(source_file, target_file)
def finalize(self, version, build_data, artifact_path):
pass
def finalize(version, build_data, artifact_path):
pass