Skip to content
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
19 changes: 9 additions & 10 deletions hatch_cpp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from os import environ, system as system_call
from pathlib import Path
from typing import List, Optional

from pkn import getSimpleLogger
from pydantic import BaseModel, Field, model_validator
Expand All @@ -21,13 +20,13 @@
class HatchCppBuildConfig(BaseModel):
"""Build config values for Hatch C++ Builder."""

verbose: Optional[bool] = Field(default=False)
skip: Optional[bool] = Field(default=False)
name: Optional[str] = Field(default=None)
libraries: List[HatchCppLibrary] = Field(default_factory=list)
cmake: Optional[HatchCppCmakeConfiguration] = Field(default=None)
platform: Optional[HatchCppPlatform] = Field(default_factory=HatchCppPlatform.default)
vcpkg: Optional[HatchCppVcpkgConfiguration] = Field(default_factory=HatchCppVcpkgConfiguration)
verbose: bool | None = Field(default=False)
skip: bool | None = Field(default=False)
name: str | None = Field(default=None)
libraries: list[HatchCppLibrary] = Field(default_factory=list)
cmake: HatchCppCmakeConfiguration | None = Field(default=None)
platform: HatchCppPlatform | None = Field(default_factory=HatchCppPlatform.default)
vcpkg: HatchCppVcpkgConfiguration | None = Field(default_factory=HatchCppVcpkgConfiguration)

@model_validator(mode="wrap")
@classmethod
Expand Down Expand Up @@ -56,9 +55,9 @@ def validate_model(cls, data, handler):

class HatchCppBuildPlan(HatchCppBuildConfig):
build_type: BuildType = "release"
commands: List[str] = Field(default_factory=list)
commands: list[str] = Field(default_factory=list)

_active_toolchains: List[Toolchain] = []
_active_toolchains: list[Toolchain] = []

def generate(self):
self.commands = []
Expand Down
4 changes: 1 addition & 3 deletions hatch_cpp/hooks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from typing import Type

from hatchling.plugin import hookimpl

from .plugin import HatchCppBuildHook


@hookimpl
def hatch_register_build_hook() -> Type[HatchCppBuildHook]:
def hatch_register_build_hook() -> type[HatchCppBuildHook]:
return HatchCppBuildHook
2 changes: 1 addition & 1 deletion hatch_cpp/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
os_name = "linux"
else:
os_name = "win"
if all([lib.py_limited_api for lib in build_plan.libraries]):
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}"
Expand Down
1 change: 0 additions & 1 deletion hatch_cpp/tests/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class TestProject:
[
"test_project_basic",
"test_project_override_classes",
"test_project_override_classes",
"test_project_override_toolchain",
"test_project_pybind",
"test_project_pybind_vcpkg",
Expand Down
16 changes: 8 additions & 8 deletions hatch_cpp/toolchains/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from os import environ
from pathlib import Path
from sys import version_info
from typing import Any, Dict, Optional, Union
from typing import Any

from pydantic import BaseModel, Field

Expand All @@ -23,17 +23,17 @@


class HatchCppCmakeConfiguration(BaseModel):
root: Optional[Path] = None
root: Path | None = None
build: Path = Field(default_factory=lambda: Path("build"))
install: Optional[Path] = Field(default=None)
install: Path | None = Field(default=None)

cmake_arg_prefix: Optional[str] = Field(default=None)
cmake_args: Dict[str, str] = Field(default_factory=dict)
cmake_env_args: Dict[Platform, Dict[str, str]] = Field(default_factory=dict)
cmake_arg_prefix: str | None = Field(default=None)
cmake_args: dict[str, str] = Field(default_factory=dict)
cmake_env_args: dict[Platform, dict[str, str]] = Field(default_factory=dict)

include_flags: Optional[Dict[str, Union[str, int, float, bool]]] = Field(default=None)
include_flags: dict[str, str | int | float | bool] | None = Field(default=None)

def generate(self, config) -> Dict[str, Any]:
def generate(self, config) -> dict[str, Any]:
commands = []

# Derive prefix
Expand Down
135 changes: 60 additions & 75 deletions hatch_cpp/toolchains/common.py

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions hatch_cpp/toolchains/vcpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path
from platform import machine as platform_machine
from sys import platform as sys_platform
from typing import Literal, Optional
from typing import Literal

from pydantic import BaseModel, Field

Expand Down Expand Up @@ -40,7 +40,7 @@
}


def _read_vcpkg_ref_from_gitmodules(vcpkg_root: Path) -> Optional[str]:
def _read_vcpkg_ref_from_gitmodules(vcpkg_root: Path) -> str | None:
"""Read the branch/ref for vcpkg from .gitmodules if it exists.

Looks for a submodule whose path matches ``vcpkg_root`` and returns
Expand All @@ -61,19 +61,19 @@ def _read_vcpkg_ref_from_gitmodules(vcpkg_root: Path) -> Optional[str]:


class HatchCppVcpkgConfiguration(BaseModel):
vcpkg: Optional[str] = Field(default="vcpkg.json")
vcpkg_root: Optional[Path] = Field(default=Path("vcpkg"))
vcpkg_repo: Optional[str] = Field(default="https://github.com/microsoft/vcpkg.git")
vcpkg_triplet: Optional[VcpkgTriplet] = Field(default=None)
vcpkg_ref: Optional[str] = Field(
vcpkg: str | None = Field(default="vcpkg.json")
vcpkg_root: Path | None = Field(default=Path("vcpkg"))
vcpkg_repo: str | None = Field(default="https://github.com/microsoft/vcpkg.git")
vcpkg_triplet: VcpkgTriplet | None = Field(default=None)
vcpkg_ref: str | None = Field(
default=None,
description="Branch, tag, or commit SHA to checkout after cloning vcpkg. "
"If not set, falls back to the branch specified in .gitmodules for the vcpkg submodule.",
)

# TODO: overlay

def _resolve_vcpkg_ref(self) -> Optional[str]:
def _resolve_vcpkg_ref(self) -> str | None:
"""Return the ref to checkout: explicit config takes priority, then .gitmodules."""
if self.vcpkg_ref is not None:
return self.vcpkg_ref
Expand Down
4 changes: 2 additions & 2 deletions hatch_cpp/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import annotations

from functools import lru_cache
from functools import cache

from pydantic import ImportString, TypeAdapter

_import_string_adapter = TypeAdapter(ImportString)


@lru_cache(maxsize=None)
@cache
def import_string(input_string: str):
return _import_string_adapter.validate_python(input_string)
Loading