|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from logging import getLogger |
| 4 | +from os import getenv |
| 5 | +from pathlib import Path |
| 6 | +from platform import machine as platform_machine |
| 7 | +from sys import platform as sys_platform, version_info |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +from hatchling.builders.hooks.plugin.interface import BuildHookInterface |
| 11 | + |
| 12 | +from .structs import HatchGoBuildConfig, HatchGoBuildPlan |
| 13 | +from .utils import import_string |
| 14 | + |
| 15 | +__all__ = ("HatchGoBuildHook",) |
| 16 | + |
| 17 | +log = getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +class HatchGoBuildHook(BuildHookInterface[HatchGoBuildConfig]): |
| 21 | + """The hatch-go build hook.""" |
| 22 | + |
| 23 | + PLUGIN_NAME = "hatch-go" |
| 24 | + _logger = log |
| 25 | + |
| 26 | + def initialize(self, version: str, build_data: dict[str, Any]) -> None: |
| 27 | + """Initialize the plugin.""" |
| 28 | + # Log some basic information |
| 29 | + project_name = self.metadata.config["project"]["name"] |
| 30 | + self._logger.info("Initializing hatch-go plugin version %s", version) |
| 31 | + self._logger.info(f"Running hatch-go: {project_name}") |
| 32 | + |
| 33 | + # Only run if creating wheel |
| 34 | + if self.target_name != "wheel": |
| 35 | + self._logger.info("ignoring target name %s", self.target_name) |
| 36 | + return |
| 37 | + |
| 38 | + # Skip if SKIP_HATCH_GO is set |
| 39 | + if getenv("SKIP_HATCH_GO"): |
| 40 | + self._logger.info("Skipping the build hook since SKIP_HATCH_GO was set") |
| 41 | + return |
| 42 | + |
| 43 | + # Get build config class or use default |
| 44 | + build_config_class = import_string(self.config["build-config-class"]) if "build-config-class" in self.config else HatchGoBuildConfig |
| 45 | + |
| 46 | + # Instantiate build config |
| 47 | + config = build_config_class(name=project_name, **self.config) |
| 48 | + |
| 49 | + # Get build plan class or use default |
| 50 | + build_plan_class = import_string(self.config["build-plan-class"]) if "build-plan-class" in self.config else HatchGoBuildPlan |
| 51 | + |
| 52 | + # Instantiate builder |
| 53 | + build_plan = build_plan_class(**config.model_dump()) |
| 54 | + |
| 55 | + # Generate commands |
| 56 | + build_plan.generate() |
| 57 | + |
| 58 | + # Log commands if in verbose mode |
| 59 | + if build_plan.verbose: |
| 60 | + for command in build_plan.commands: |
| 61 | + self._logger.warning(command) |
| 62 | + |
| 63 | + if build_plan.skip: |
| 64 | + self._logger.warning("Skipping build") |
| 65 | + return |
| 66 | + |
| 67 | + # Execute build plan |
| 68 | + build_plan.execute() |
| 69 | + |
| 70 | + # Perform any cleanup actions |
| 71 | + build_plan.cleanup() |
| 72 | + |
| 73 | + if not build_plan._libraries: |
| 74 | + raise ValueError("No libraries were created by the build.") |
| 75 | + |
| 76 | + build_data["pure_python"] = False |
| 77 | + machine = platform_machine() |
| 78 | + version_major = version_info.major |
| 79 | + version_minor = version_info.minor |
| 80 | + |
| 81 | + if "darwin" in sys_platform: |
| 82 | + os_name = "macosx_11_0" |
| 83 | + elif "linux" in sys_platform: |
| 84 | + os_name = "linux" |
| 85 | + else: |
| 86 | + os_name = "win" |
| 87 | + |
| 88 | + build_data["tag"] = f"cp{version_major}{version_minor}-cp{version_major}{version_minor}-{os_name}_{machine}" |
| 89 | + |
| 90 | + # Force include libraries |
| 91 | + for path in Path(".").rglob("*"): |
| 92 | + if path.is_dir(): |
| 93 | + continue |
| 94 | + if str(path).startswith("dist") or not str(path).startswith(config.module): |
| 95 | + continue |
| 96 | + if path.suffix in (".pyd", ".dll", ".so", ".dylib"): |
| 97 | + build_data["force_include"][str(path)] = str(path) |
| 98 | + |
| 99 | + for path in build_data["force_include"]: |
| 100 | + self._logger.warning(f"Force include: {path}") |
0 commit comments