Skip to content

Add support for limited abi, fixes #18 #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions hatch_cpp/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,6 @@ def initialize(self, version: str, build_data: dict[str, t.Any]) -> None:
self._logger.info("ignoring target name %s", self.target_name)
return

build_data["pure_python"] = False
machine = sysplatform.machine()
version_major = sys.version_info.major
version_minor = sys.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}"

# Skip if SKIP_HATCH_CPP is set
# TODO: Support CLI once https://github.com/pypa/hatch/pull/1743
if os.getenv("SKIP_HATCH_CPP"):
Expand Down Expand Up @@ -85,3 +72,20 @@ def initialize(self, version: str, build_data: dict[str, t.Any]) -> None:
for library in libraries:
name = library.get_qualified_name(build_plan.platform.platform)
build_data["force_include"][name] = name

if libraries:
build_data["pure_python"] = False
machine = sysplatform.machine()
version_major = sys.version_info.major
version_minor = sys.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"
if all([lib.py_limited_api for lib in 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}"
57 changes: 41 additions & 16 deletions hatch_cpp/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from os import environ, system
from pathlib import Path
from re import match
from shutil import which
from sys import executable, platform as sys_platform
from sysconfig import get_path
from typing import List, Literal, Optional
from typing import Any, List, Literal, Optional

from pydantic import BaseModel, Field
from pydantic import AliasChoices, BaseModel, Field, field_validator, model_validator

__all__ = (
"HatchCppBuildConfig",
Expand All @@ -28,7 +29,7 @@
}


class HatchCppLibrary(BaseModel):
class HatchCppLibrary(BaseModel, validate_assignment=True):
"""A C++ library."""

name: str
Expand All @@ -38,29 +39,47 @@
binding: Binding = "cpython"
std: Optional[str] = None

include_dirs: List[str] = Field(default_factory=list, alias="include-dirs")
library_dirs: List[str] = Field(default_factory=list, alias="library-dirs")
include_dirs: List[str] = Field(default_factory=list, alias=AliasChoices("include_dirs", "include-dirs"))
library_dirs: List[str] = Field(default_factory=list, alias=AliasChoices("library_dirs", "library-dirs"))
libraries: List[str] = Field(default_factory=list)

extra_compile_args: List[str] = Field(default_factory=list, alias="extra-compile-args")
extra_link_args: List[str] = Field(default_factory=list, alias="extra-link-args")
extra_objects: List[str] = Field(default_factory=list, alias="extra-objects")
extra_compile_args: List[str] = Field(default_factory=list, alias=AliasChoices("extra_compile_args", "extra-compile-args"))
extra_link_args: List[str] = Field(default_factory=list, alias=AliasChoices("extra_link_args", "extra-link-args"))
extra_objects: List[str] = Field(default_factory=list, alias=AliasChoices("extra_objects", "extra-objects"))

define_macros: List[str] = Field(default_factory=list, alias="define-macros")
undef_macros: List[str] = Field(default_factory=list, alias="undef-macros")
define_macros: List[str] = Field(default_factory=list, alias=AliasChoices("define_macros", "define-macros"))
undef_macros: List[str] = Field(default_factory=list, alias=AliasChoices("undef_macros", "undef-macros"))

export_symbols: List[str] = Field(default_factory=list, alias="export-symbols")
export_symbols: List[str] = Field(default_factory=list, alias=AliasChoices("export_symbols", "export-symbols"))
depends: List[str] = Field(default_factory=list)

py_limited_api: Optional[str] = Field(default="", alias=AliasChoices("py_limited_api", "py-limited-api"))

@field_validator("py_limited_api", mode="before")
@classmethod
def check_py_limited_api(cls, value: Any) -> Any:
if value:
if not match(r"cp3\d", value):
raise ValueError("py-limited-api must be in the form of cp3X")
return value

def get_qualified_name(self, platform):
if platform == "win32":
suffix = "dll" if self.binding == "none" else "pyd"
elif platform == "darwin" and self.binding == "none":
suffix = "dylib"
elif platform == "darwin":
suffix = "dylib" if self.binding == "none" else "so"
else:
suffix = "so"
if self.py_limited_api and platform != "win32":
return f"{self.name}.abi3.{suffix}"
return f"{self.name}.{suffix}"

@model_validator(mode="after")
def check_binding_and_py_limited_api(self):
if self.binding == "pybind11" and self.py_limited_api:
raise ValueError("pybind11 does not support Py_LIMITED_API")
return self


class HatchCppPlatform(BaseModel):
cc: str
Expand Down Expand Up @@ -117,6 +136,12 @@
library.sources.append(str(Path(nanobind.include_dir()).parent / "src" / "nb_combined.cpp"))
library.include_dirs.append(str((Path(nanobind.include_dir()).parent / "ext" / "robin_map" / "include")))

if library.py_limited_api:
if library.binding == "pybind11":
raise ValueError("pybind11 does not support Py_LIMITED_API")

Check warning on line 141 in hatch_cpp/structs.py

View check run for this annotation

Codecov / codecov/patch

hatch_cpp/structs.py#L141

Added line #L141 was not covered by tests
library.define_macros.append(f"Py_LIMITED_API=0x0{library.py_limited_api[2]}0{hex(int(library.py_limited_api[3:]))[2:]}00f0")

# Toolchain-specific flags
if self.toolchain == "gcc":
flags += " " + " ".join(f"-I{d}" for d in library.include_dirs)
flags += " -fPIC"
Expand Down Expand Up @@ -156,7 +181,7 @@
flags += " " + " ".join(library.extra_objects)
flags += " " + " ".join(f"-l{lib}" for lib in library.libraries)
flags += " " + " ".join(f"-L{lib}" for lib in library.library_dirs)
flags += f" -o {library.name}.so"
flags += f" -o {library.get_qualified_name(self.platform)}"
if self.platform == "darwin":
flags += " -undefined dynamic_lookup"
if "mold" in self.ld:
Expand All @@ -169,7 +194,7 @@
flags += " " + " ".join(library.extra_objects)
flags += " " + " ".join(f"-l{lib}" for lib in library.libraries)
flags += " " + " ".join(f"-L{lib}" for lib in library.library_dirs)
flags += f" -o {library.name}.so"
flags += f" -o {library.get_qualified_name(self.platform)}"
if self.platform == "darwin":
flags += " -undefined dynamic_lookup"
if "mold" in self.ld:
Expand All @@ -180,7 +205,7 @@
flags += " " + " ".join(library.extra_link_args)
flags += " " + " ".join(library.extra_objects)
flags += " /LD"
flags += f" /Fe:{library.name}.pyd"
flags += f" /Fe:{library.get_qualified_name(self.platform)}"
flags += " /link /DLL"
if (Path(executable).parent / "libs").exists():
flags += f" /LIBPATH:{str(Path(executable).parent / 'libs')}"
Expand Down
2 changes: 0 additions & 2 deletions hatch_cpp/tests/test_all.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "project/basic.hpp"

PyObject* hello(PyObject*, PyObject*) {
return PyUnicode_FromString("A string");
}
17 changes: 17 additions & 0 deletions hatch_cpp/tests/test_project_limited_api/cpp/project/basic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once
#include "Python.h"

PyObject* hello(PyObject*, PyObject*);

static PyMethodDef extension_methods[] = {
{"hello", (PyCFunction)hello, METH_NOARGS},
{nullptr, nullptr, 0, nullptr}
};

static PyModuleDef extension_module = {
PyModuleDef_HEAD_INIT, "extension", "extension", -1, extension_methods};

PyMODINIT_FUNC PyInit_extension(void) {
Py_Initialize();
return PyModule_Create(&extension_module);
}
Empty file.
35 changes: 35 additions & 0 deletions hatch_cpp/tests/test_project_limited_api/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[build-system]
requires = ["hatchling>=1.20"]
build-backend = "hatchling.build"

[project]
name = "hatch-cpp-test-project-limtied-api"
description = "Basic test project for hatch-cpp"
version = "0.1.0"
requires-python = ">=3.9"
dependencies = [
"hatchling>=1.20",
"hatch-cpp",
]

[tool.hatch.build]
artifacts = [
"project/*.dll",
"project/*.dylib",
"project/*.so",
]

[tool.hatch.build.sources]
src = "/"

[tool.hatch.build.targets.sdist]
packages = ["project"]

[tool.hatch.build.targets.wheel]
packages = ["project"]

[tool.hatch.build.hooks.hatch-cpp]
verbose = true
libraries = [
{name = "project/extension", sources = ["cpp/project/basic.cpp"], include-dirs = ["cpp"], py-limited-api = "cp39"},
]
2 changes: 1 addition & 1 deletion hatch_cpp/tests/test_project_nanobind/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["hatchling>=1.20"]
build-backend = "hatchling.build"

[project]
name = "hatch-cpp-test-project-basic"
name = "hatch-cpp-test-project-nanobind"
description = "Basic test project for hatch-cpp"
version = "0.1.0"
requires-python = ">=3.9"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["hatchling>=1.20"]
build-backend = "hatchling.build"

[project]
name = "hatch-cpp-test-project-basic"
name = "hatch-cpp-test-project-override-classes"
description = "Basic test project for hatch-cpp"
version = "0.1.0"
requires-python = ">=3.9"
Expand Down
2 changes: 1 addition & 1 deletion hatch_cpp/tests/test_project_pybind/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["hatchling>=1.20"]
build-backend = "hatchling.build"

[project]
name = "hatch-cpp-test-project-basic"
name = "hatch-cpp-test-project-pybind"
description = "Basic test project for hatch-cpp"
version = "0.1.0"
requires-python = ">=3.9"
Expand Down
13 changes: 9 additions & 4 deletions hatch_cpp/tests/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@


class TestProject:
@pytest.mark.parametrize("project", ["test_project_basic", "test_project_override_classes", "test_project_pybind", "test_project_nanobind"])
@pytest.mark.parametrize(
"project", ["test_project_basic", "test_project_override_classes", "test_project_pybind", "test_project_nanobind", "test_project_limited_api"]
)
def test_basic(self, project):
# cleanup
rmtree(f"hatch_cpp/tests/{project}/project/extension.so", ignore_errors=True)
Expand All @@ -28,10 +30,13 @@ def test_basic(self, project):

# assert built

if platform == "win32":
assert "extension.pyd" in listdir(f"hatch_cpp/tests/{project}/project")
if project == "test_project_limited_api" and platform != "win32":
assert "extension.abi3.so" in listdir(f"hatch_cpp/tests/{project}/project")
else:
assert "extension.so" in listdir(f"hatch_cpp/tests/{project}/project")
if platform == "win32":
assert "extension.pyd" in listdir(f"hatch_cpp/tests/{project}/project")
else:
assert "extension.so" in listdir(f"hatch_cpp/tests/{project}/project")

# import
here = Path(__file__).parent / project
Expand Down
26 changes: 26 additions & 0 deletions hatch_cpp/tests/test_structs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
from pydantic import ValidationError

from hatch_cpp.structs import HatchCppLibrary, HatchCppPlatform


class TestStructs:
def test_validate_py_limited_api(self):
with pytest.raises(ValidationError):
library = HatchCppLibrary(
name="test",
sources=["test.cpp"],
py_limited_api="42",
)
library = HatchCppLibrary(
name="test",
sources=["test.cpp"],
py_limited_api="cp39",
)
assert library.py_limited_api == "cp39"
platform = HatchCppPlatform.default()
flags = platform.get_compile_flags(library)
assert "-DPy_LIMITED_API=0x030900f0" in flags or "/DPy_LIMITED_API=0x030900f0" in flags

with pytest.raises(ValidationError):
library.binding = "pybind11"
Loading