|
2 | 2 |
|
3 | 3 | import os |
4 | 4 | import subprocess |
| 5 | +from typing import Any |
5 | 6 |
|
6 | 7 | import manygo |
7 | | -from hatchling.builders.hooks.plugin import interface |
| 8 | +from hatchling.builders.hooks.plugin.interface import BuildHookInterface |
8 | 9 |
|
9 | 10 |
|
10 | | -class GoBinaryBuildHook(interface.BuildHookInterface): |
11 | | - def initialize(self, version, build_data) -> None: # noqa: ANN001, ARG002 |
| 11 | +class CustomBuildHook(BuildHookInterface): |
| 12 | + """Build hook to compile the Go binary and include it in the wheel.""" |
| 13 | + |
| 14 | + def initialize(self, version: Any, build_data: Any) -> None: |
| 15 | + """Build the Go binary before packaging.""" |
12 | 16 | build_data["pure_python"] = False |
| 17 | + |
| 18 | + # Set platform tag for cross-compilation |
13 | 19 | goos = os.getenv("GOOS") |
14 | 20 | goarch = os.getenv("GOARCH") |
15 | 21 | if goos and goarch: |
16 | | - build_data["tag"] = "py3-none-" + manygo.get_platform_tag(goos=goos, goarch=goarch) # type: ignore[invalid-argument-type] |
17 | | - binary_name = self.config["binary_name"] |
18 | | - tag = os.getenv("GITHUB_REF_NAME", "dev").lstrip("gonyx/") |
| 22 | + build_data["tag"] = "py3-none-" + manygo.get_platform_tag( |
| 23 | + goos=goos, goarch=goarch |
| 24 | + ) |
| 25 | + else: |
| 26 | + build_data["infer_tag"] = True |
| 27 | + |
| 28 | + # Get config and environment |
| 29 | + binary_name = self.config.get("binary_name", "gonyx") |
19 | 30 | commit = os.getenv("GITHUB_SHA", "none") |
20 | 31 |
|
| 32 | + # Build the Go binary if it doesn't exist |
21 | 33 | if not os.path.exists(binary_name): |
22 | 34 | print(f"Building Go binary '{binary_name}'...") |
23 | 35 | subprocess.check_call( # noqa: S603 |
24 | 36 | [ |
25 | 37 | "go", |
26 | 38 | "build", |
27 | | - f"-ldflags=-X main.version={tag} -X main.commit={commit} -s -w", |
| 39 | + f"-ldflags=-X main.version={version} -X main.commit={commit} -s -w", |
28 | 40 | "-o", |
29 | 41 | binary_name, |
30 | 42 | ], |
31 | 43 | ) |
32 | 44 |
|
33 | | - build_data["shared_scripts"] = {binary_name: binary_name} |
| 45 | + # Include the binary in the wheel |
| 46 | + build_data["force_include"][binary_name] = binary_name |
0 commit comments