-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.py
More file actions
113 lines (91 loc) · 4.3 KB
/
plugin.py
File metadata and controls
113 lines (91 loc) · 4.3 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
from __future__ import annotations
from logging import getLogger
from os import getenv
from pathlib import Path
from platform import machine as platform_machine
from sys import platform as sys_platform, version_info
from typing import Any
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
from .structs import HatchCppBuildConfig, HatchCppBuildPlan
from .utils import import_string
__all__ = ("HatchCppBuildHook",)
class HatchCppBuildHook(BuildHookInterface[HatchCppBuildConfig]):
"""The hatch-cpp build hook."""
PLUGIN_NAME = "hatch-cpp"
_logger = getLogger(__name__)
def initialize(self, version: str, build_data: dict[str, Any]) -> None:
"""Initialize the plugin."""
# Log some basic information
project_name = self.metadata.config["project"]["name"]
self._logger.info("Initializing hatch-cpp plugin version %s", version)
self._logger.info(f"Running hatch-cpp: {project_name}")
# Only run if creating wheel
# TODO: Add support for specify sdist-plan
if self.target_name != "wheel":
self._logger.info("ignoring target name %s", self.target_name)
return
# Skip if SKIP_HATCH_CPP is set
# TODO: Support CLI once https://github.com/pypa/hatch/pull/1743
if getenv("SKIP_HATCH_CPP"):
self._logger.info("Skipping the build hook since SKIP_HATCH_CPP was set")
return
# Get build config class or use default
build_config_class = import_string(self.config["build-config-class"]) if "build-config-class" in self.config else HatchCppBuildConfig
# Instantiate build config
config = build_config_class(name=project_name, **self.config)
# Get build plan class or use default
build_plan_class = import_string(self.config["build-plan-class"]) if "build-plan-class" in self.config else HatchCppBuildPlan
# Instantiate builder
build_plan = build_plan_class(**config.model_dump())
# Generate commands
build_plan.generate()
# Log commands if in verbose mode
if config.verbose:
for command in build_plan.commands:
self._logger.warning(command)
# Execute build plan
build_plan.execute()
# Perform any cleanup actions
build_plan.cleanup()
if build_plan.libraries:
# force include libraries
for library in build_plan.libraries:
name = library.get_qualified_name(build_plan.platform.platform)
build_data["force_include"][name] = name
build_data["pure_python"] = False
machine = platform_machine()
version_major = version_info.major
version_minor = version_info.minor
if "darwin" in sys_platform:
os_name = "macosx_11_0"
elif "linux" in sys_platform:
os_name = "linux"
else:
os_name = "win"
if all([lib.py_limited_api for lib in build_plan.libraries]):
build_data["tag"] = f"cp{version_major}{version_minor}-abi3-{os_name}_{machine}"
else:
build_data["tag"] = f"cp{version_major}{version_minor}-cp{version_major}{version_minor}-{os_name}_{machine}"
else:
build_data["pure_python"] = False
machine = platform_machine()
version_major = version_info.major
version_minor = version_info.minor
# TODO abi3
if "darwin" in sys_platform:
os_name = "macosx_11_0"
elif "linux" in sys_platform:
os_name = "linux"
else:
os_name = "win"
build_data["tag"] = f"cp{version_major}{version_minor}-cp{version_major}{version_minor}-{os_name}_{machine}"
# force include libraries
for path in Path(".").rglob("*"):
if path.is_dir():
continue
if str(path).startswith(str(build_plan.cmake.build)) or str(path).startswith("dist"):
continue
if path.suffix in (".pyd", ".dll", ".so", ".dylib"):
build_data["force_include"][str(path)] = str(path)
for path in build_data["force_include"]:
self._logger.warning(f"Force include: {path}")