Skip to content

Commit d93a03f

Browse files
authored
Merge pull request #20 from python-project-templates/tkp/abi3
Add abi3 support via explicit flag
2 parents 9cd01d8 + 879ab0b commit d93a03f

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

hatch_rs/plugin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
9898
os_name = "linux"
9999
else:
100100
os_name = "win"
101-
build_data["tag"] = f"cp{version_major}{version_minor}-cp{version_major}{version_minor}-{os_name}_{machine}"
101+
if config.abi3:
102+
build_data["tag"] = f"cp{version_major}{version_minor}-abi3-{os_name}_{machine}"
103+
else:
104+
build_data["tag"] = f"cp{version_major}{version_minor}-cp{version_major}{version_minor}-{os_name}_{machine}"
102105

103106
# force include libraries
104107
for path in Path(".").rglob("*"):

hatch_rs/structs.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class HatchRustBuildConfig(BaseModel):
3030
module: str = Field(description="Python module name for the Rust extension.")
3131
path: Optional[Path] = Field(default=None, description="Path to the project root directory.")
3232

33+
abi3: bool = Field(
34+
default=False,
35+
description="If True, build the extension with Python's ABI3 compatibility.",
36+
)
37+
3338
target: Optional[str] = Field(
3439
default=None,
3540
description="Target platform for the build. If not specified, it will be determined automatically.",
@@ -147,13 +152,19 @@ def execute(self):
147152

148153
# Copy each file to the current directory
149154
if sys_platform == "win32":
150-
library_name = f"{self.module}\\{file_name}.pyd"
155+
if self.abi3:
156+
library_name = f"{self.module}\\{file_name}.abi3.pyd"
157+
else:
158+
library_name = f"{self.module}\\{file_name}.pyd"
151159
self._libraries.append(library_name)
152160
copy_command = f"copy {file} {cwd}\\{library_name}"
153161
else:
154162
if which("cp") is None:
155163
raise EnvironmentError("cp command not found. Ensure it is installed and available in PATH.")
156-
library_name = f"{self.module}/{file_name}.so"
164+
if self.abi3:
165+
library_name = f"{self.module}/{file_name}.abi3.so"
166+
else:
167+
library_name = f"{self.module}/{file_name}.so"
157168
self._libraries.append(library_name)
158169
copy_command = f"cp -f {file} {cwd}/{library_name}"
159170
system_call(copy_command)

hatch_rs/tests/test_project_basic/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ packages = ["project"]
3030

3131
[tool.hatch.build.hooks.hatch-rs]
3232
verbose = true
33+
abi3 = true
3334
path = "."
3435
module = "project"

hatch_rs/tests/test_projects.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def test_basic(self, project_folder):
3535

3636
# assert built
3737
if platform == "win32":
38-
assert "project.pyd" in listdir(f"hatch_rs/tests/{project_folder}/project")
38+
assert "project.abi3.pyd" in listdir(f"hatch_rs/tests/{project_folder}/project")
3939
else:
40-
assert "project.so" in listdir(f"hatch_rs/tests/{project_folder}/project")
40+
assert "project.abi3.so" in listdir(f"hatch_rs/tests/{project_folder}/project")
4141

4242
# dist
4343
check_call(
@@ -51,7 +51,7 @@ def test_basic(self, project_folder):
5151
cwd=f"hatch_rs/tests/{project_folder}",
5252
)
5353

54-
assert f"cp3{version_info.minor}-cp3{version_info.minor}" in listdir(f"hatch_rs/tests/{project_folder}/dist")[0]
54+
assert f"cp3{version_info.minor}-abi3" in listdir(f"hatch_rs/tests/{project_folder}/dist")[0]
5555

5656
# import
5757
here = Path(__file__).parent / project_folder

0 commit comments

Comments
 (0)