1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import os
16+ import platform
1517import sys
1618
1719from hatchling .builders .hooks .plugin .interface import BuildHookInterface
@@ -29,17 +31,55 @@ def initialize(self, version, build_data):
2931 build_data ["pure_python" ] = False
3032 build_data ["infer_tag" ] = False
3133
32- # Get the platform tag using hatchling's logic (handles MACOSX_DEPLOYMENT_TARGET, etc.)
33- from packaging .tags import sys_tags
34+ if sys .platform == "darwin" :
35+ plat_tag = self ._get_macos_platform_tag ()
36+ else :
37+ from packaging .tags import sys_tags
3438
35- tag = next (
36- t for t in sys_tags () if "manylinux" not in t .platform and "musllinux" not in t .platform
37- )
38- platform = tag .platform
39+ tag = next (
40+ t
41+ for t in sys_tags ()
42+ if "manylinux" not in t .platform and "musllinux" not in t .platform
43+ )
44+ plat_tag = tag .platform
3945
40- if sys .platform == "darwin" :
41- from hatchling .builders .macos import process_macos_plat_tag
46+ build_data ["tag" ] = f"py3-none-{ plat_tag } "
47+
48+ def _get_macos_platform_tag (self ):
49+ """Build macOS platform tag from MACOSX_DEPLOYMENT_TARGET env var."""
50+ deployment_target = os .environ .get ("MACOSX_DEPLOYMENT_TARGET" )
51+ if not deployment_target :
52+ # Fall back to current macOS version
53+ deployment_target = platform .mac_ver ()[0 ]
54+ # Use only major.minor
55+ parts = deployment_target .split ("." )
56+ deployment_target = f"{ parts [0 ]} .{ parts [1 ] if len (parts ) > 1 else '0' } "
57+
58+ # Convert version to wheel tag format (e.g., "11.0" -> "11_0")
59+ version_tag = deployment_target .replace ("." , "_" )
4260
43- platform = process_macos_plat_tag (platform , compat = True )
61+ # Get target architecture from ARCHFLAGS (set by cibuildwheel for cross-compilation)
62+ # or fall back to host machine architecture
63+ arch = self ._get_macos_target_arch ()
64+
65+ return f"macosx_{ version_tag } _{ arch } "
66+
67+ def _get_macos_target_arch (self ):
68+ """Detect target architecture for macOS builds.
69+
70+ Cibuildwheel sets ARCHFLAGS for cross-compilation (e.g., "-arch x86_64").
71+ Falls back to host machine architecture if not set.
72+ """
73+ archflags = os .environ .get ("ARCHFLAGS" , "" )
74+ if "-arch arm64" in archflags :
75+ return "arm64"
76+ elif "-arch x86_64" in archflags :
77+ return "x86_64"
4478
45- build_data ["tag" ] = f"py3-none-{ platform } "
79+ # Fall back to host architecture
80+ machine = platform .machine ()
81+ if machine == "x86_64" :
82+ return "x86_64"
83+ elif machine == "arm64" :
84+ return "arm64"
85+ return machine
0 commit comments