Skip to content

Commit 41bab7a

Browse files
committed
Add the before_build script
1 parent 4da6ab6 commit 41bab7a

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python3
2+
3+
from __future__ import annotations
4+
5+
import shlex
6+
import sys
7+
import sysconfig
8+
from pathlib import Path
9+
10+
11+
def _normalize_path(path: Path | str) -> str:
12+
return str(path).replace('\\', '/')
13+
14+
15+
def _normalize_dll_name(name: str) -> str:
16+
if name.endswith('.dll'):
17+
return name
18+
if name.startswith('lib') and name.endswith('.dll.a'):
19+
return name[3:-2]
20+
raise SystemExit(f'Unexpected Python library name: {name}')
21+
22+
23+
def _find_dll(dll_name: str) -> Path:
24+
candidates: list[Path] = []
25+
libdir = sysconfig.get_config_var('LIBDIR')
26+
if libdir:
27+
candidates.append(Path(libdir) / dll_name)
28+
exe_dir = Path(sys.executable).resolve().parent
29+
candidates.append(exe_dir / dll_name)
30+
base_prefix = Path(getattr(sys, 'base_prefix', sys.prefix))
31+
candidates.append(base_prefix / dll_name)
32+
candidates.append(base_prefix / 'DLLs' / dll_name)
33+
candidates.append(base_prefix / 'libs' / dll_name)
34+
35+
for candidate in candidates:
36+
if candidate.exists():
37+
return candidate.resolve()
38+
39+
paths = '\n'.join(_normalize_path(path) for path in candidates)
40+
raise SystemExit(f'Could not find {dll_name} in:\n{paths}')
41+
42+
43+
def main() -> None:
44+
repo_root = Path.cwd().resolve()
45+
lib_dir = repo_root / '.local' / 'lib'
46+
pkgconfig_dir = lib_dir / 'pkgconfig'
47+
lib_dir.mkdir(parents=True, exist_ok=True)
48+
pkgconfig_dir.mkdir(parents=True, exist_ok=True)
49+
50+
raw_name = sysconfig.get_config_var('DLLLIBRARY') or sysconfig.get_config_var('LDLIBRARY')
51+
if not raw_name:
52+
raise SystemExit('Could not determine Python DLL name')
53+
dll_name = _normalize_dll_name(raw_name)
54+
include_dir = sysconfig.get_config_var('INCLUDEPY')
55+
if not include_dir:
56+
raise SystemExit('Could not determine Python include dir')
57+
pkg_version = sysconfig.get_config_var('LDVERSION') or sysconfig.get_python_version()
58+
dll_path = _find_dll(dll_name)
59+
60+
values = {
61+
'REPO_ROOT': _normalize_path(repo_root),
62+
'LIB_DIR': _normalize_path(lib_dir),
63+
'PKGCONFIG_DIR': _normalize_path(pkgconfig_dir),
64+
'DLL_NAME': dll_name,
65+
'DLL_PATH': _normalize_path(dll_path),
66+
'PKG_VERSION': pkg_version,
67+
'INCLUDE_DIR': _normalize_path(include_dir),
68+
}
69+
70+
env_path = repo_root / '.local' / 'cibw_before_build_windows_arm64.env'
71+
env_text = ''.join(f'{key}={shlex.quote(value)}\n' for key, value in values.items())
72+
env_path.write_text(env_text, encoding='utf-8')
73+
print(f'Generated {env_path}')
74+
75+
76+
if __name__ == '__main__':
77+
main()

0 commit comments

Comments
 (0)