forked from bbqsrc/pdf-strings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhatch_build.py
More file actions
73 lines (59 loc) · 2.59 KB
/
hatch_build.py
File metadata and controls
73 lines (59 loc) · 2.59 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
import os
import shutil
import subprocess
import sys
from pathlib import Path
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
class CustomBuildHook(BuildHookInterface):
def initialize(self, version, build_data):
"""
Build the Rust FFI library and copy it to the package directory.
"""
# Get paths - handle both dev and sdist scenarios
build_dir = Path(__file__).parent
package_dir = build_dir / "pdf_strings"
# Check if we're in an sdist or dev environment
# In sdist, the Rust code will be in the same directory
cargo_toml = build_dir / "Cargo.toml"
if not cargo_toml.exists():
# In dev mode, go up one level
cargo_toml = build_dir.parent / "Cargo.toml"
project_root = cargo_toml.parent
# Determine library name based on platform
if sys.platform == "darwin":
lib_name = "libpdf_strings_ffi.dylib"
elif sys.platform == "win32":
lib_name = "pdf_strings_ffi.dll"
else:
lib_name = "libpdf_strings_ffi.so"
print(f"Building Rust FFI library for {sys.platform}...")
# Build the Rust library
subprocess.run(
["cargo", "build", "-p", "pdf-strings-ffi", "--release"],
cwd=project_root,
check=True,
)
# Copy the built library to the package directory
target_lib = project_root / "target" / "release" / lib_name
dest_lib = package_dir / lib_name
print(f"Copying {target_lib} to {dest_lib}")
shutil.copy2(target_lib, dest_lib)
# Mark the wheel as platform-specific but Python-version-independent
# Since we use ctypes (pure Python), only the shared library is platform-specific
# Set tag to py3-none-{platform} instead of cp{ver}-cp{ver}-{platform}
import platform as plat
# Build a reasonable platform tag
if sys.platform == "darwin":
# macOS: use 10_12 as minimum (Rust's minimum)
machine = plat.machine()
platform_tag = f"macosx_10_12_{machine}"
elif sys.platform == "win32":
machine = plat.machine().lower()
platform_tag = f"win_{machine}"
else:
# Linux: use manylinux_2_17 (compatible with manylinux2014)
machine = plat.machine()
platform_tag = f"manylinux_2_17_{machine}"
# Set explicit tag: py3 (all Python 3), none (no ABI), platform-specific
build_data['tag'] = f'py3-none-{platform_tag}'
print("Rust FFI library built and copied successfully")