diff --git a/.copier-answers.yml b/.copier-answers.yaml similarity index 76% rename from .copier-answers.yml rename to .copier-answers.yaml index 3e3d361..1ad91e7 100644 --- a/.copier-answers.yml +++ b/.copier-answers.yaml @@ -1,6 +1,6 @@ # Changes here will be overwritten by Copier -_commit: 81e8acd -_src_path: git@github.com:python-project-templates/base.git +_commit: 5c63cb2 +_src_path: https://github.com/python-project-templates/base.git add_extension: python email: t.paine154@gmail.com github: python-project-templates diff --git a/.gitattributes b/.gitattributes index 092719d..8e05465 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,4 +3,11 @@ docs/* linguist-documentation *.ipynb linguist-documentation Makefile linguist-documentation +<<<<<<< before updating * text=auto eol=lf +======= +*.md text=auto eol=lf +*.py text=auto eol=lf +*.toml text=auto eol=lf +*.yaml text=auto eol=lf +>>>>>>> after updating diff --git a/.github/dependabot.yml b/.github/dependabot.yaml similarity index 66% rename from .github/dependabot.yml rename to .github/dependabot.yaml index 4d17b20..42cac77 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yaml @@ -14,11 +14,3 @@ updates: labels: - "lang: python" - "part: dependencies" - - - package-ecosystem: "npm" - directory: "/" - schedule: - interval: "monthly" - labels: - - "lang: javascript" - - "part: dependencies" diff --git a/.github/workflows/build.yml b/.github/workflows/build.yaml similarity index 78% rename from .github/workflows/build.yml rename to .github/workflows/build.yaml index d9a8ac7..109d0d0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yaml @@ -29,18 +29,15 @@ jobs: strategy: matrix: - os: [ubuntu-latest, macos-latest] - python-version: ["3.9"] + os: [ubuntu-latest] + python-version: ["3.11"] steps: - uses: actions/checkout@v4 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 + - uses: actions-ext/python/setup@main with: - python-version: ${{ matrix.python-version }} - cache: 'pip' - cache-dependency-path: 'pyproject.toml' + version: ${{ matrix.python-version }} - name: Install dependencies run: make develop @@ -68,7 +65,6 @@ jobs: uses: EnricoMi/publish-unit-test-result-action@v2 with: files: '**/junit.xml' - if: matrix.os == 'ubuntu-latest' - name: Upload coverage uses: codecov/codecov-action@v5 @@ -82,4 +78,3 @@ jobs: with: name: dist-${{matrix.os}} path: dist - if: matrix.os == 'ubuntu-latest' diff --git a/.github/workflows/copier.yml b/.github/workflows/copier.yaml similarity index 100% rename from .github/workflows/copier.yml rename to .github/workflows/copier.yaml diff --git a/.gitignore b/.gitignore index a090202..5e96e1e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,12 @@ __pycache__/ *$py.class # C extensions +*.a *.so +*.obj +*.dll +*.exp +*.lib # Distribution / packaging .Python @@ -15,6 +20,7 @@ dist/ downloads/ eggs/ .eggs/ +include/ lib/ lib64/ parts/ diff --git a/LICENSE b/LICENSE index 261eeb9..95b5e23 100644 --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,11 @@ same "printed page" as the copyright notice for easier identification within third-party archives. +<<<<<<< before updating Copyright [yyyy] [name of copyright owner] +======= + Copyright 2025 the hatch-cpp authors +>>>>>>> after updating Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/Makefile b/Makefile index 627b57c..adac7f6 100644 --- a/Makefile +++ b/Makefile @@ -4,13 +4,13 @@ .PHONY: develop build install develop: ## install dependencies and build library - python -m pip install -e .[develop] + uv pip install -e .[develop] build: ## build the python library python -m build -n install: ## install library - python -m pip install . + uv pip install . ######### # LINTS # @@ -86,7 +86,7 @@ dist-build: # build python dists dist-check: ## run python dist checker with twine python -m twine check dist/* -dist: clean build dist-build dist-check ## build all dists +dist: clean dist-build dist-check ## build all dists publish: dist # publish python assets @@ -99,7 +99,7 @@ deep-clean: ## clean everything from the repository git clean -fdx clean: ## clean the repository - rm -rf .coverage coverage cover htmlcov logs build dist *.egg-info + rm -rf .coverage coverage cover htmlcov logs build dist *.egg-info hatch_cpp/tests/*/dist hatch_cpp/tests/*/build hatch_cpp/tests/*/*/*.so hatch_cpp/tests/*/*/*.pyd ############################################################################################ diff --git a/README.md b/README.md index d47e19c..2d254b9 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,34 @@ Hatch plugin for C++ builds -[![Build Status](https://github.com/python-project-templates/hatch-cpp/actions/workflows/build.yml/badge.svg?branch=main&event=push)](https://github.com/python-project-templates/hatch-cpp/actions/workflows/build.yml) +[![Build Status](https://github.com/python-project-templates/hatch-cpp/actions/workflows/build.yaml/badge.svg?branch=main&event=push)](https://github.com/python-project-templates/hatch-cpp/actions/workflows/build.yaml) [![codecov](https://codecov.io/gh/python-project-templates/hatch-cpp/branch/main/graph/badge.svg)](https://codecov.io/gh/python-project-templates/hatch-cpp) [![License](https://img.shields.io/github/license/python-project-templates/hatch-cpp)](https://github.com/python-project-templates/hatch-cpp) [![PyPI](https://img.shields.io/pypi/v/hatch-cpp.svg)](https://pypi.python.org/pypi/hatch-cpp) ## Overview +A simple, extensible C++ build plugin for [hatch](https://hatch.pypa.io/latest/). + +```toml +[tool.hatch.build.hooks.hatch-cpp] +libraries = [ + {name = "project/extension", sources = ["cpp/project/basic.cpp"], include-dirs = ["cpp"]} +] +``` + +For more complete systems, see: +- [scikit-build-core](https://github.com/scikit-build/scikit-build-core) +- [setuptools](https://setuptools.pypa.io/en/latest/userguide/ext_modules.html) + +## Environment Variables +| Name | Default | Description | +|:-----|:--------|:------------| +|`CC`| | | +|`CXX`| | | +|`LD`| | | +|`HATCH_CPP_PLATFORM`| | | +|`HATCH_CPP_DISABLE_CCACHE`| | | + > [!NOTE] > This library was generated using [copier](https://copier.readthedocs.io/en/stable/) from the [Base Python Project Template repository](https://github.com/python-project-templates/base). diff --git a/hatch_cpp/__init__.py b/hatch_cpp/__init__.py index 3dc1f76..d3dfbab 100644 --- a/hatch_cpp/__init__.py +++ b/hatch_cpp/__init__.py @@ -1 +1,5 @@ -__version__ = "0.1.0" +__version__ = "0.1.7" + +from .hooks import hatch_register_build_hook +from .plugin import HatchCppBuildHook +from .structs import * diff --git a/hatch_cpp/__main__.py b/hatch_cpp/__main__.py deleted file mode 100644 index 9ae637f..0000000 --- a/hatch_cpp/__main__.py +++ /dev/null @@ -1,4 +0,0 @@ -from .cli import main - -if __name__ == "__main__": - main() diff --git a/hatch_cpp/plugin.py b/hatch_cpp/plugin.py index fb88301..ab6ebf8 100644 --- a/hatch_cpp/plugin.py +++ b/hatch_cpp/plugin.py @@ -1,13 +1,16 @@ from __future__ import annotations -import logging -import os -import typing as t -from dataclasses import fields +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, HatchCppLibrary, HatchCppPlatform +from .structs import HatchCppBuildConfig, HatchCppBuildPlan +from .utils import import_string __all__ = ("HatchCppBuildHook",) @@ -16,70 +19,95 @@ class HatchCppBuildHook(BuildHookInterface[HatchCppBuildConfig]): """The hatch-cpp build hook.""" PLUGIN_NAME = "hatch-cpp" - _logger = logging.getLogger(__name__) + _logger = getLogger(__name__) - def initialize(self, version: str, _: dict[str, t.Any]) -> None: + def initialize(self, version: str, build_data: dict[str, Any]) -> None: """Initialize the plugin.""" - self._logger.info("Running hatch-cpp") + # 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 - if os.getenv("SKIP_HATCH_CPP"): + # 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 - kwargs = {k.replace("-", "_"): v if not isinstance(v, bool) else str(v) for k, v in self.config.items()} - available_fields = [f.name for f in fields(HatchCppBuildConfig)] - for key in list(kwargs): - if key not in available_fields: - del kwargs[key] - config = HatchCppBuildConfig(**kwargs) - - library_kwargs = [ - {k.replace("-", "_"): v if not isinstance(v, bool) else str(v) for k, v in library_kwargs.items()} for library_kwargs in config.libraries - ] - libraries = [HatchCppLibrary(**library_kwargs) for library_kwargs in library_kwargs] - platform = HatchCppPlatform.default() - if config.toolchain == "raw": - # g++ basic-project/basic.cpp -I. -I/opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/include/python3.11/ -undefined dynamic_lookup -fPIC -shared -o extension.so - build_plan = HatchCppBuildPlan(libraries=libraries, platform=platform) - build_plan.generate() - build_plan.execute(verbose=config.verbose) - # build_kwargs = config.build_kwargs - # if version == "editable": - # build_kwargs = config.editable_build_kwargs or build_kwargs - - # should_skip_build = False - # if not config.build_function: - # log.warning("No build function found") - # should_skip_build = True - - # elif config.skip_if_exists and version == "standard": - # should_skip_build = should_skip(config.skip_if_exists) - # if should_skip_build: - # log.info("Skip-if-exists file(s) found") - - # # Get build function and call it with normalized parameter names. - # if not should_skip_build and config.build_function: - # build_func = get_build_func(config.build_function) - # build_kwargs = normalize_kwargs(build_kwargs) - # log.info("Building with %s", config.build_function) - # log.info("With kwargs: %s", build_kwargs) - # try: - # build_func(self.target_name, version, **build_kwargs) - # except Exception as e: - # if version == "editable" and config.optional_editable_build.lower() == "true": - # warnings.warn(f"Encountered build error:\n{e}", stacklevel=2) - # else: - # raise e - # else: - # log.info("Skipping build") - - # # Ensure targets in distributable dists. - # if version == "standard": - # ensure_targets(config.ensured_targets) - - self._logger.info("Finished running hatch-cpp") - 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}") diff --git a/hatch_cpp/structs.py b/hatch_cpp/structs.py index ccfab16..75df2d8 100644 --- a/hatch_cpp/structs.py +++ b/hatch_cpp/structs.py @@ -1,12 +1,14 @@ from __future__ import annotations -from dataclasses import dataclass, field -from os import environ, system -from sys import platform as sys_platform +from os import environ, system as system_call +from pathlib import Path +from re import match +from shutil import which +from sys import executable, platform as sys_platform, version_info from sysconfig import get_path -from typing import Literal +from typing import Any, Dict, List, Literal, Optional -from hatchling.builders.config import BuilderConfig +from pydantic import AliasChoices, BaseModel, Field, field_validator, model_validator __all__ = ( "HatchCppBuildConfig", @@ -15,53 +17,76 @@ "HatchCppBuildPlan", ) -Platform = Literal["linux", "darwin", "win32"] +BuildType = Literal["debug", "release"] CompilerToolchain = Literal["gcc", "clang", "msvc"] +Language = Literal["c", "c++"] +Binding = Literal["cpython", "pybind11", "nanobind", "generic"] +Platform = Literal["linux", "darwin", "win32"] PlatformDefaults = { - "linux": {"CC": "gcc", "CXX": "g++"}, - "darwin": {"CC": "clang", "CXX": "clang++"}, - "win32": {"CC": "cl", "CXX": "cl"}, + "linux": {"CC": "gcc", "CXX": "g++", "LD": "ld"}, + "darwin": {"CC": "clang", "CXX": "clang++", "LD": "ld"}, + "win32": {"CC": "cl", "CXX": "cl", "LD": "link"}, } -@dataclass -class HatchCppBuildConfig(BuilderConfig): - """Build config values for Hatch C++ Builder.""" +class HatchCppLibrary(BaseModel, validate_assignment=True): + """A C++ library.""" - toolchain: str | None = field(default="raw") - libraries: list[dict[str, str]] = field(default_factory=list) - verbose: bool | None = field(default=False) - # build_function: str | None = None - # build_kwargs: t.Mapping[str, str] = field(default_factory=dict) - # editable_build_kwargs: t.Mapping[str, str] = field(default_factory=dict) - # ensured_targets: list[str] = field(default_factory=list) - # skip_if_exists: list[str] = field(default_factory=list) + name: str + sources: List[str] + language: Language = "c++" + binding: Binding = "cpython" + std: Optional[str] = None -@dataclass -class HatchCppLibrary(object): - """A C++ library.""" + 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) - name: str - sources: list[str] + 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=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=AliasChoices("export_symbols", "export-symbols")) + depends: List[str] = Field(default_factory=list) - include_dirs: list[str] = field(default_factory=list) - library_dirs: list[str] = field(default_factory=list) - libraries: list[str] = field(default_factory=list) - extra_compile_args: list[str] = field(default_factory=list) - extra_link_args: list[str] = field(default_factory=list) - extra_objects: list[str] = field(default_factory=list) - define_macros: list[str] = field(default_factory=list) - undef_macros: list[str] = field(default_factory=list) + py_limited_api: Optional[str] = Field(default="", alias=AliasChoices("py_limited_api", "py-limited-api")) - export_symbols: list[str] = field(default_factory=list) - depends: list[str] = field(default_factory=list) + @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 == "generic" else "pyd" + elif platform == "darwin": + suffix = "dylib" if self.binding == "generic" 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") + if self.binding == "generic" and self.py_limited_api: + raise ValueError("Generic binding can not support Py_LIMITED_API") + return self -@dataclass -class HatchCppPlatform(object): +class HatchCppPlatform(BaseModel): cc: str cxx: str + ld: str platform: Platform toolchain: CompilerToolchain @@ -70,6 +95,7 @@ def default() -> HatchCppPlatform: platform = environ.get("HATCH_CPP_PLATFORM", sys_platform) CC = environ.get("CC", PlatformDefaults[platform]["CC"]) CXX = environ.get("CXX", PlatformDefaults[platform]["CXX"]) + LD = environ.get("LD", PlatformDefaults[platform]["LD"]) if "gcc" in CC and "g++" in CXX: toolchain = "gcc" elif "clang" in CC and "clang++" in CXX: @@ -78,69 +104,216 @@ def default() -> HatchCppPlatform: toolchain = "msvc" else: raise Exception(f"Unrecognized toolchain: {CC}, {CXX}") - return HatchCppPlatform(cc=CC, cxx=CXX, platform=platform, toolchain=toolchain) - def get_flags(self, library: HatchCppLibrary) -> str: + # Customizations + if which("ccache") and not environ.get("HATCH_CPP_DISABLE_CCACHE"): + CC = f"ccache {CC}" + CXX = f"ccache {CXX}" + + # https://github.com/rui314/mold/issues/647 + # if which("ld.mold"): + # LD = which("ld.mold") + # elif which("ld.lld"): + # LD = which("ld.lld") + return HatchCppPlatform(cc=CC, cxx=CXX, ld=LD, platform=platform, toolchain=toolchain) + + def get_compile_flags(self, library: HatchCppLibrary, build_type: BuildType = "release") -> str: flags = "" + + # Python.h + if library.binding != "generic": + library.include_dirs.append(get_path("include")) + + if library.binding == "pybind11": + import pybind11 + + library.include_dirs.append(pybind11.get_include()) + if not library.std: + library.std = "c++11" + elif library.binding == "nanobind": + import nanobind + + library.include_dirs.append(nanobind.include_dir()) + if not library.std: + library.std = "c++17" + 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") + 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 = f"-I{get_path('include')}" flags += " " + " ".join(f"-I{d}" for d in library.include_dirs) - flags += " -fPIC -shared" + flags += " -fPIC" flags += " " + " ".join(library.extra_compile_args) - flags += " " + " ".join(library.extra_link_args) - 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 += " " + " ".join(f"-D{macro}" for macro in library.define_macros) flags += " " + " ".join(f"-U{macro}" for macro in library.undef_macros) - flags += f" -o {library.name}.so" + if library.std: + flags += f" -std={library.std}" elif self.toolchain == "clang": - flags = f"-I{get_path('include')} " flags += " ".join(f"-I{d}" for d in library.include_dirs) - flags += " -undefined dynamic_lookup -fPIC -shared" + flags += " -fPIC" flags += " " + " ".join(library.extra_compile_args) - flags += " " + " ".join(library.extra_link_args) - 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 += " " + " ".join(f"-D{macro}" for macro in library.define_macros) flags += " " + " ".join(f"-U{macro}" for macro in library.undef_macros) - flags += f" -o {library.name}.so" + if library.std: + flags += f" -std={library.std}" elif self.toolchain == "msvc": - flags = f"/I{get_path('include')} " flags += " ".join(f"/I{d}" for d in library.include_dirs) - flags += " /LD" flags += " " + " ".join(library.extra_compile_args) flags += " " + " ".join(library.extra_link_args) flags += " " + " ".join(library.extra_objects) - flags += " " + " ".join(f"{lib}.lib" for lib in library.libraries) - flags += " " + " ".join(f"/LIBPATH:{lib}" for lib in library.library_dirs) flags += " " + " ".join(f"/D{macro}" for macro in library.define_macros) flags += " " + " ".join(f"/U{macro}" for macro in library.undef_macros) - flags += f" /Fo{library.name}.obj" - flags += f" /Fe{library.name}.pyd" + flags += " /EHsc /DWIN32" + if library.std: + flags += f" /std:{library.std}" + # clean + while flags.count(" "): + flags = flags.replace(" ", " ") + return flags + + def get_link_flags(self, library: HatchCppLibrary, build_type: BuildType = "release") -> str: + flags = "" + if self.toolchain == "gcc": + flags += " -shared" + flags += " " + " ".join(library.extra_link_args) + 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.get_qualified_name(self.platform)}" + if self.platform == "darwin": + flags += " -undefined dynamic_lookup" + if "mold" in self.ld: + flags += f" -fuse-ld={self.ld}" + elif "lld" in self.ld: + flags += " -fuse-ld=lld" + elif self.toolchain == "clang": + flags += " -shared" + flags += " " + " ".join(library.extra_link_args) + 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.get_qualified_name(self.platform)}" + if self.platform == "darwin": + flags += " -undefined dynamic_lookup" + if "mold" in self.ld: + flags += f" -fuse-ld={self.ld}" + elif "lld" in self.ld: + flags += " -fuse-ld=lld" + elif self.toolchain == "msvc": + flags += " " + " ".join(library.extra_link_args) + flags += " " + " ".join(library.extra_objects) + flags += " /LD" + 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')}" + flags += " " + " ".join(f"{lib}.lib" for lib in library.libraries) + flags += " " + " ".join(f"/LIBPATH:{lib}" for lib in library.library_dirs) # clean while flags.count(" "): flags = flags.replace(" ", " ") return flags -@dataclass -class HatchCppBuildPlan(object): - libraries: list[HatchCppLibrary] = field(default_factory=list) - platform: HatchCppPlatform = field(default_factory=HatchCppPlatform.default) - commands: list[str] = field(default_factory=list) +class HatchCppCmakeConfiguration(BaseModel): + root: Path + build: Path = Field(default_factory=lambda: Path("build")) + install: Optional[Path] = 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) + + include_flags: Optional[Dict[str, Any]] = Field(default=None) + + +class HatchCppBuildConfig(BaseModel): + """Build config values for Hatch C++ Builder.""" + + verbose: 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) + + @model_validator(mode="after") + def check_toolchain_matches_args(self): + if self.cmake and self.libraries: + raise ValueError("Must not provide libraries when using cmake toolchain.") + return self + + +class HatchCppBuildPlan(HatchCppBuildConfig): + build_type: BuildType = "release" + commands: List[str] = Field(default_factory=list) def generate(self): self.commands = [] - for library in self.libraries: - flags = self.platform.get_flags(library) - self.commands.append(f"{self.platform.cc} {' '.join(library.sources)} {flags}") + if self.libraries: + for library in self.libraries: + compile_flags = self.platform.get_compile_flags(library, self.build_type) + link_flags = self.platform.get_link_flags(library, self.build_type) + self.commands.append( + f"{self.platform.cc if library.language == 'c' else self.platform.cxx} {' '.join(library.sources)} {compile_flags} {link_flags}" + ) + elif self.cmake: + # Derive prefix + if self.cmake.cmake_arg_prefix is None: + self.cmake.cmake_arg_prefix = f"{self.name.replace('.', '_').replace('-', '_').upper()}_" + + # Append base command + self.commands.append(f"cmake {Path(self.cmake.root).parent} -DCMAKE_BUILD_TYPE={self.build_type} -B {self.cmake.build}") + + # Setup install path + if self.cmake.install: + self.commands[-1] += f" -DCMAKE_INSTALL_PREFIX={self.cmake.install}" + else: + self.commands[-1] += f" -DCMAKE_INSTALL_PREFIX={Path(self.cmake.root).parent}" + + # TODO: CMAKE_CXX_COMPILER + if self.platform.platform == "win32": + # TODO: prefix? + self.commands[-1] += f' -G "{environ.get("GENERATOR", "Visual Studio 17 2022")}"' + + # Put in CMake flags + args = self.cmake.cmake_args.copy() + for platform, env_args in self.cmake.cmake_env_args.items(): + if platform == self.platform.platform: + for key, value in env_args.items(): + args[key] = value + for key, value in args.items(): + self.commands[-1] += f" -D{self.cmake.cmake_arg_prefix}{key.upper()}={value}" + + # Include customs + if self.cmake.include_flags: + if self.cmake.include_flags.get("python_version", False): + self.commands[-1] += f" -D{self.cmake.cmake_arg_prefix}PYTHON_VERSION={version_info.major}.{version_info.minor}" + if self.cmake.include_flags.get("manylinux", False) and self.platform.platform == "linux": + self.commands[-1] += f" -D{self.cmake.cmake_arg_prefix}MANYLINUX=ON" + + # Include mac deployment target + if self.platform.platform == "darwin": + self.commands[-1] += f" -DCMAKE_OSX_DEPLOYMENT_TARGET={environ.get('OSX_DEPLOYMENT_TARGET', '11')}" + + # Append build command + self.commands.append(f"cmake --build {self.cmake.build} --config {self.build_type}") + + # Append install command + self.commands.append(f"cmake --install {self.cmake.build} --config {self.build_type}") + return self.commands - def execute(self, verbose: bool = True): + def execute(self): for command in self.commands: - if verbose: - print(f"Running command: {command}") - system(command) + system_call(command) return self.commands + + def cleanup(self): + if self.platform.platform == "win32": + for temp_obj in Path(".").glob("*.obj"): + temp_obj.unlink() diff --git a/hatch_cpp/tests/test_all.py b/hatch_cpp/tests/test_all.py deleted file mode 100644 index 82959de..0000000 --- a/hatch_cpp/tests/test_all.py +++ /dev/null @@ -1,2 +0,0 @@ -def test_import(): - pass diff --git a/hatch_cpp/tests/test_project_basic.py b/hatch_cpp/tests/test_project_basic.py deleted file mode 100644 index b70fff5..0000000 --- a/hatch_cpp/tests/test_project_basic.py +++ /dev/null @@ -1,22 +0,0 @@ -from os import listdir -from shutil import rmtree -from subprocess import check_output -from sys import platform - - -class TestProject: - def test_basic(self): - rmtree("hatch_cpp/tests/test_project_basic/basic_project/extension.so", ignore_errors=True) - rmtree("hatch_cpp/tests/test_project_basic/basic_project/extension.pyd", ignore_errors=True) - check_output( - [ - "hatchling", - "build", - "--hooks-only", - ], - cwd="hatch_cpp/tests/test_project_basic", - ) - if platform == "win32": - assert "extension.pyd" in listdir("hatch_cpp/tests/test_project_basic/basic_project") - else: - assert "extension.so" in listdir("hatch_cpp/tests/test_project_basic/basic_project") diff --git a/hatch_cpp/tests/test_project_basic/cpp/basic-project/basic.cpp b/hatch_cpp/tests/test_project_basic/cpp/project/basic.cpp similarity index 71% rename from hatch_cpp/tests/test_project_basic/cpp/basic-project/basic.cpp rename to hatch_cpp/tests/test_project_basic/cpp/project/basic.cpp index a7e840e..db4432a 100644 --- a/hatch_cpp/tests/test_project_basic/cpp/basic-project/basic.cpp +++ b/hatch_cpp/tests/test_project_basic/cpp/project/basic.cpp @@ -1,4 +1,4 @@ -#include "basic-project/basic.hpp" +#include "project/basic.hpp" PyObject* hello(PyObject*, PyObject*) { return PyUnicode_FromString("A string"); diff --git a/hatch_cpp/tests/test_project_basic/cpp/basic-project/basic.hpp b/hatch_cpp/tests/test_project_basic/cpp/project/basic.hpp similarity index 100% rename from hatch_cpp/tests/test_project_basic/cpp/basic-project/basic.hpp rename to hatch_cpp/tests/test_project_basic/cpp/project/basic.hpp diff --git a/hatch_cpp/tests/test_project_basic/basic_project/__init__.py b/hatch_cpp/tests/test_project_basic/project/__init__.py similarity index 100% rename from hatch_cpp/tests/test_project_basic/basic_project/__init__.py rename to hatch_cpp/tests/test_project_basic/project/__init__.py diff --git a/hatch_cpp/tests/test_project_basic/pyproject.toml b/hatch_cpp/tests/test_project_basic/pyproject.toml index aea842d..d51683e 100644 --- a/hatch_cpp/tests/test_project_basic/pyproject.toml +++ b/hatch_cpp/tests/test_project_basic/pyproject.toml @@ -14,50 +14,22 @@ dependencies = [ [tool.hatch.build] artifacts = [ - "basic_project/*.dll", - "basic_project/*.dylib", - "basic_project/*.so", + "project/*.dll", + "project/*.dylib", + "project/*.so", ] [tool.hatch.build.sources] src = "/" [tool.hatch.build.targets.sdist] -packages = ["basic_project"] +packages = ["project"] [tool.hatch.build.targets.wheel] -packages = ["basic_project"] +packages = ["project"] [tool.hatch.build.hooks.hatch-cpp] verbose = true libraries = [ - {name = "basic_project/extension", sources = ["cpp/basic-project/basic.cpp"], include-dirs = ["cpp"]} + {name = "project/extension", sources = ["cpp/project/basic.cpp"], include-dirs = ["cpp"]} ] - -# build-function = "hatch_cpp.cpp_builder" - -# [tool.hatch.build.hooks.defaults] -# build-type = "release" - -# [tool.hatch.build.hooks.env-vars] -# TODO: these will all be available via -# CLI after https://github.com/pypa/hatch/pull/1743 -# e.g. --hatch-cpp-build-type=debug -# build-type = "BUILD_TYPE" -# ccache = "USE_CCACHE" -# manylinux = "MANYLINUX" -# vcpkg = "USE_VCPKG" - -# [tool.hatch.build.hooks.cmake] - -# [tool.hatch.build.hooks.vcpkg] -# triplets = {linux="x64-linux", macos="x64-osx", windows="x64-windows-static-md"} -# clone = true -# update = true - -# [tool.hatch.build.hooks.hatch-cpp.build-kwargs] -# path = "cpp" - -[tool.pytest.ini_options] -asyncio_mode = "strict" -testpaths = "basic_project/tests" diff --git a/hatch_cpp/tests/test_project_cmake/CMakeLists.txt b/hatch_cpp/tests/test_project_cmake/CMakeLists.txt new file mode 100644 index 0000000..6344c70 --- /dev/null +++ b/hatch_cpp/tests/test_project_cmake/CMakeLists.txt @@ -0,0 +1,92 @@ +cmake_minimum_required(VERSION 3.20.0) +project(hatch-cpp-test-project-basic VERSION "0.1.0") +set(CMAKE_CXX_STANDARD 20) +include(CheckCCompilerFlag) +include(CheckLinkerFlag) + +if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") + set(WIN32 ON) + set(MACOS OFF) + set(LINUX OFF) +elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + set(WIN32 OFF) + set(MACOS ON) + set(LINUX OFF) +else() + set(WIN32 OFF) + set(MACOS OFF) + set(LINUX ON) +endif() + +option(CMAKE_BUILD_TYPE "Release/Debug build" RELEASE) +option(HATCH_CPP_TEST_PROJECT_BASIC_BUILD_TESTS "Build tests" OFF) +option(HATCH_CPP_TEST_PROJECT_BASIC_MANYLINUX "Build for python's manylinux setup" OFF) + +string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER) + +set(BUILD_SHARED_LIBS TRUE) +set(CMAKE_MACOSX_RPATH TRUE) +set(CMAKE_SKIP_RPATH FALSE) +set(CMAKE_SKIP_BUILD_RPATH FALSE) +set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) +set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) +set(CMAKE_INSTALL_NAME_DIR "@rpath") +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +string(REGEX REPLACE "[ ]*-O[^ ]+[ ]*" " " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") +string(REGEX REPLACE "[ ]*-Wl,-O2 -Wl,[^ ]+[ ]*" " " CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") +string(REGEX REPLACE "[ ]*-Wl,-O2 -Wl,[^ ]+[ ]*" " " CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") + + +if(MACOS) + set(CMAKE_THREAD_LIBS_INIT "-lpthread") + set(CMAKE_HAVE_THREADS_LIBRARY 1) + set(CMAKE_USE_WIN32_THREADS_INIT 0) + set(CMAKE_USE_PTHREADS_INIT 1) + set(THREADS_PREFER_PTHREAD_FLAG ON) + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined dynamic_lookup") +endif() + + +if(MACOS) + set(CMAKE_INSTALL_RPATH "@loader_path/") +elseif(LINUX) + set(CMAKE_INSTALL_RPATH "\$ORIGIN") +endif() + +if(WIN32) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP /bigobj") + foreach(warning 4244 4251 4267 4275 4290 4786 4305 4996) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd${warning}") + endforeach(warning) +else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \ + -g \ + -Wall \ + -Werror \ + -Wno-deprecated-declarations \ + -Wno-deprecated \ + ") +endif() + + +find_package(Python ${CSP_PYTHON_VERSION} EXACT REQUIRED COMPONENTS Interpreter Development.Module) +link_directories(${Python_LIBRARY_DIRS}) +include_directories(${Python_INCLUDE_DIRS}) + +set(CMAKE_SHARED_LIBRARY_PREFIX "") +if(NOT WIN32) + set(CMAKE_SHARED_LIBRARY_SUFFIX .so) +else() + set(CMAKE_SHARED_LIBRARY_SUFFIX .pyd) +endif() + +include_directories("${CMAKE_SOURCE_DIR}/cpp") + +add_library(extension SHARED cpp/project/basic.cpp) +set_target_properties(extension PROPERTIES PUBLIC_HEADER cpp/project/basic.hpp) +install(TARGETS extension + PUBLIC_HEADER DESTINATION project/include/project + RUNTIME DESTINATION project/ + LIBRARY DESTINATION project/ + ) diff --git a/hatch_cpp/tests/test_project_cmake/Makefile b/hatch_cpp/tests/test_project_cmake/Makefile new file mode 100644 index 0000000..c265da9 --- /dev/null +++ b/hatch_cpp/tests/test_project_cmake/Makefile @@ -0,0 +1,140 @@ +# CMAKE generated file: DO NOT EDIT! +# Generated by "Unix Makefiles" Generator, CMake Version 3.31 + +# Default target executed when no arguments are given to make. +default_target: all +.PHONY : default_target + +# Allow only one "make -f Makefile2" at a time, but pass parallelism. +.NOTPARALLEL: + +#============================================================================= +# Special targets provided by cmake. + +# Disable implicit rules so canonical targets will work. +.SUFFIXES: + +# Disable VCS-based implicit rules. +% : %,v + +# Disable VCS-based implicit rules. +% : RCS/% + +# Disable VCS-based implicit rules. +% : RCS/%,v + +# Disable VCS-based implicit rules. +% : SCCS/s.% + +# Disable VCS-based implicit rules. +% : s.% + +.SUFFIXES: .hpux_make_needs_suffix_list + +# Command-line flag to silence nested $(MAKE). +$(VERBOSE)MAKESILENT = -s + +#Suppress display of executed commands. +$(VERBOSE).SILENT: + +# A target that is always out of date. +cmake_force: +.PHONY : cmake_force + +#============================================================================= +# Set environment variables for the build. + +# The shell in which to execute make rules. +SHELL = /bin/sh + +# The CMake executable. +CMAKE_COMMAND = /opt/homebrew/bin/cmake + +# The command to remove a file. +RM = /opt/homebrew/bin/cmake -E rm -f + +# Escaping for special characters. +EQUALS = = + +# The top-level source directory on which CMake was run. +CMAKE_SOURCE_DIR = /Users/timkpaine/Developer/projects/templates/hatch-cpp/hatch_cpp/tests/test_project_cmake + +# The top-level build directory on which CMake was run. +CMAKE_BINARY_DIR = /Users/timkpaine/Developer/projects/templates/hatch-cpp/hatch_cpp/tests/test_project_cmake + +#============================================================================= +# Targets provided globally by CMake. + +# Special rule for the target edit_cache +edit_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake cache editor..." + /opt/homebrew/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : edit_cache + +# Special rule for the target edit_cache +edit_cache/fast: edit_cache +.PHONY : edit_cache/fast + +# Special rule for the target rebuild_cache +rebuild_cache: + @$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake to regenerate build system..." + /opt/homebrew/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) +.PHONY : rebuild_cache + +# Special rule for the target rebuild_cache +rebuild_cache/fast: rebuild_cache +.PHONY : rebuild_cache/fast + +# The main all target +all: cmake_check_build_system + $(CMAKE_COMMAND) -E cmake_progress_start /Users/timkpaine/Developer/projects/templates/hatch-cpp/hatch_cpp/tests/test_project_cmake/CMakeFiles /Users/timkpaine/Developer/projects/templates/hatch-cpp/hatch_cpp/tests/test_project_cmake//CMakeFiles/progress.marks + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all + $(CMAKE_COMMAND) -E cmake_progress_start /Users/timkpaine/Developer/projects/templates/hatch-cpp/hatch_cpp/tests/test_project_cmake/CMakeFiles 0 +.PHONY : all + +# The main clean target +clean: + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 clean +.PHONY : clean + +# The main clean target +clean/fast: clean +.PHONY : clean/fast + +# Prepare targets for installation. +preinstall: all + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall + +# Prepare targets for installation. +preinstall/fast: + $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall +.PHONY : preinstall/fast + +# clear depends +depend: + $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 +.PHONY : depend + +# Help Target +help: + @echo "The following are some of the valid targets for this Makefile:" + @echo "... all (the default if no target is provided)" + @echo "... clean" + @echo "... depend" + @echo "... edit_cache" + @echo "... rebuild_cache" +.PHONY : help + + + +#============================================================================= +# Special targets to cleanup operation of make. + +# Special rule to run CMake to check the build system integrity. +# No rule that depends on this can have commands that come from listfiles +# because they might be regenerated. +cmake_check_build_system: + $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 +.PHONY : cmake_check_build_system + diff --git a/hatch_cpp/tests/test_project_cmake/cpp/project/basic.cpp b/hatch_cpp/tests/test_project_cmake/cpp/project/basic.cpp new file mode 100644 index 0000000..db4432a --- /dev/null +++ b/hatch_cpp/tests/test_project_cmake/cpp/project/basic.cpp @@ -0,0 +1,5 @@ +#include "project/basic.hpp" + +PyObject* hello(PyObject*, PyObject*) { + return PyUnicode_FromString("A string"); +} diff --git a/hatch_cpp/tests/test_project_cmake/cpp/project/basic.hpp b/hatch_cpp/tests/test_project_cmake/cpp/project/basic.hpp new file mode 100644 index 0000000..65cb62e --- /dev/null +++ b/hatch_cpp/tests/test_project_cmake/cpp/project/basic.hpp @@ -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); +} diff --git a/hatch_cpp/tests/test_project_cmake/project/__init__.py b/hatch_cpp/tests/test_project_cmake/project/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hatch_cpp/tests/test_project_cmake/pyproject.toml b/hatch_cpp/tests/test_project_cmake/pyproject.toml new file mode 100644 index 0000000..8ad530e --- /dev/null +++ b/hatch_cpp/tests/test_project_cmake/pyproject.toml @@ -0,0 +1,39 @@ +[build-system] +requires = ["hatchling>=1.20"] +build-backend = "hatchling.build" + +[project] +name = "hatch-cpp-test-project-basic" +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 + +[tool.hatch.build.hooks.hatch-cpp.cmake] +root = "CMakeLists.txt" +cmake_args = {"BUILD_TESTS" = "OFF"} +include_flags = {"python_version" = true} +[tool.hatch.build.hooks.hatch-cpp.cmake.cmake_env_args] +linux = {"MANYLINUX" = "ON"} diff --git a/hatch_cpp/tests/test_project_limited_api/cpp/project/basic.cpp b/hatch_cpp/tests/test_project_limited_api/cpp/project/basic.cpp new file mode 100644 index 0000000..db4432a --- /dev/null +++ b/hatch_cpp/tests/test_project_limited_api/cpp/project/basic.cpp @@ -0,0 +1,5 @@ +#include "project/basic.hpp" + +PyObject* hello(PyObject*, PyObject*) { + return PyUnicode_FromString("A string"); +} diff --git a/hatch_cpp/tests/test_project_limited_api/cpp/project/basic.hpp b/hatch_cpp/tests/test_project_limited_api/cpp/project/basic.hpp new file mode 100644 index 0000000..65cb62e --- /dev/null +++ b/hatch_cpp/tests/test_project_limited_api/cpp/project/basic.hpp @@ -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); +} diff --git a/hatch_cpp/tests/test_project_limited_api/project/__init__.py b/hatch_cpp/tests/test_project_limited_api/project/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hatch_cpp/tests/test_project_limited_api/pyproject.toml b/hatch_cpp/tests/test_project_limited_api/pyproject.toml new file mode 100644 index 0000000..e1157e3 --- /dev/null +++ b/hatch_cpp/tests/test_project_limited_api/pyproject.toml @@ -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"}, +] diff --git a/hatch_cpp/tests/test_project_nanobind/cpp/project/basic.cpp b/hatch_cpp/tests/test_project_nanobind/cpp/project/basic.cpp new file mode 100644 index 0000000..2ac7d56 --- /dev/null +++ b/hatch_cpp/tests/test_project_nanobind/cpp/project/basic.cpp @@ -0,0 +1,2 @@ +#include "project/basic.hpp" + diff --git a/hatch_cpp/tests/test_project_nanobind/cpp/project/basic.hpp b/hatch_cpp/tests/test_project_nanobind/cpp/project/basic.hpp new file mode 100644 index 0000000..1afa022 --- /dev/null +++ b/hatch_cpp/tests/test_project_nanobind/cpp/project/basic.hpp @@ -0,0 +1,7 @@ +#pragma once +#include +#include + +NB_MODULE(extension, m) { + m.def("hello", []() { return "A string"; }); +} diff --git a/hatch_cpp/tests/test_project_nanobind/project/__init__.py b/hatch_cpp/tests/test_project_nanobind/project/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hatch_cpp/tests/test_project_nanobind/pyproject.toml b/hatch_cpp/tests/test_project_nanobind/pyproject.toml new file mode 100644 index 0000000..bd03189 --- /dev/null +++ b/hatch_cpp/tests/test_project_nanobind/pyproject.toml @@ -0,0 +1,35 @@ +[build-system] +requires = ["hatchling>=1.20"] +build-backend = "hatchling.build" + +[project] +name = "hatch-cpp-test-project-nanobind" +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"], binding = "nanobind"}, +] diff --git a/hatch_cpp/tests/test_project_override_classes/cpp/project/basic.cpp b/hatch_cpp/tests/test_project_override_classes/cpp/project/basic.cpp new file mode 100644 index 0000000..db4432a --- /dev/null +++ b/hatch_cpp/tests/test_project_override_classes/cpp/project/basic.cpp @@ -0,0 +1,5 @@ +#include "project/basic.hpp" + +PyObject* hello(PyObject*, PyObject*) { + return PyUnicode_FromString("A string"); +} diff --git a/hatch_cpp/tests/test_project_override_classes/cpp/project/basic.hpp b/hatch_cpp/tests/test_project_override_classes/cpp/project/basic.hpp new file mode 100644 index 0000000..65cb62e --- /dev/null +++ b/hatch_cpp/tests/test_project_override_classes/cpp/project/basic.hpp @@ -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); +} diff --git a/hatch_cpp/tests/test_project_override_classes/project/__init__.py b/hatch_cpp/tests/test_project_override_classes/project/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hatch_cpp/tests/test_project_override_classes/pyproject.toml b/hatch_cpp/tests/test_project_override_classes/pyproject.toml new file mode 100644 index 0000000..90e3215 --- /dev/null +++ b/hatch_cpp/tests/test_project_override_classes/pyproject.toml @@ -0,0 +1,37 @@ +[build-system] +requires = ["hatchling>=1.20"] +build-backend = "hatchling.build" + +[project] +name = "hatch-cpp-test-project-override-classes" +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] +build-config-class = "hatch_cpp.HatchCppBuildConfig" +build-plan-class = "hatch_cpp.HatchCppBuildPlan" +verbose = true +libraries = [ + {name = "project/extension", sources = ["cpp/project/basic.cpp"], include-dirs = ["cpp"]} +] diff --git a/hatch_cpp/tests/test_project_pybind/cpp/project/basic.cpp b/hatch_cpp/tests/test_project_pybind/cpp/project/basic.cpp new file mode 100644 index 0000000..ebe96f8 --- /dev/null +++ b/hatch_cpp/tests/test_project_pybind/cpp/project/basic.cpp @@ -0,0 +1,6 @@ +#include "project/basic.hpp" + +std::string hello() { + return "A string"; +} + diff --git a/hatch_cpp/tests/test_project_pybind/cpp/project/basic.hpp b/hatch_cpp/tests/test_project_pybind/cpp/project/basic.hpp new file mode 100644 index 0000000..86053b2 --- /dev/null +++ b/hatch_cpp/tests/test_project_pybind/cpp/project/basic.hpp @@ -0,0 +1,9 @@ +#pragma once +#include +#include + +std::string hello(); + +PYBIND11_MODULE(extension, m) { + m.def("hello", &hello); +} \ No newline at end of file diff --git a/hatch_cpp/tests/test_project_pybind/project/__init__.py b/hatch_cpp/tests/test_project_pybind/project/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hatch_cpp/tests/test_project_pybind/pyproject.toml b/hatch_cpp/tests/test_project_pybind/pyproject.toml new file mode 100644 index 0000000..38e279e --- /dev/null +++ b/hatch_cpp/tests/test_project_pybind/pyproject.toml @@ -0,0 +1,35 @@ +[build-system] +requires = ["hatchling>=1.20"] +build-backend = "hatchling.build" + +[project] +name = "hatch-cpp-test-project-pybind" +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"], binding="pybind11"}, +] diff --git a/hatch_cpp/tests/test_projects.py b/hatch_cpp/tests/test_projects.py new file mode 100644 index 0000000..7553c04 --- /dev/null +++ b/hatch_cpp/tests/test_projects.py @@ -0,0 +1,54 @@ +from os import listdir +from pathlib import Path +from shutil import rmtree +from subprocess import check_call +from sys import modules, path, platform + +import pytest + + +class TestProject: + @pytest.mark.parametrize( + "project", + [ + "test_project_basic", + "test_project_override_classes", + "test_project_pybind", + "test_project_nanobind", + "test_project_limited_api", + "test_project_cmake", + ], + ) + def test_basic(self, project): + # cleanup + rmtree(f"hatch_cpp/tests/{project}/project/extension.so", ignore_errors=True) + rmtree(f"hatch_cpp/tests/{project}/project/extension.pyd", ignore_errors=True) + modules.pop("project", None) + modules.pop("project.extension", None) + + # compile + check_call( + [ + "hatchling", + "build", + "--hooks-only", + ], + cwd=f"hatch_cpp/tests/{project}", + ) + + # assert built + + if project == "test_project_limited_api" and platform != "win32": + assert "extension.abi3.so" in listdir(f"hatch_cpp/tests/{project}/project") + else: + 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 + path.insert(0, str(here)) + import project.extension + + assert project.extension.hello() == "A string" diff --git a/hatch_cpp/tests/test_structs.py b/hatch_cpp/tests/test_structs.py new file mode 100644 index 0000000..fc36d9a --- /dev/null +++ b/hatch_cpp/tests/test_structs.py @@ -0,0 +1,48 @@ +from pathlib import Path +from sys import version_info + +import pytest +from pydantic import ValidationError +from toml import loads + +from hatch_cpp.structs import HatchCppBuildConfig, HatchCppBuildPlan, 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" + + def test_cmake_args(self): + txt = (Path(__file__).parent / "test_project_cmake" / "pyproject.toml").read_text() + toml = loads(txt) + hatch_build_config = HatchCppBuildConfig(name=toml["project"]["name"], **toml["tool"]["hatch"]["build"]["hooks"]["hatch-cpp"]) + hatch_build_plan = HatchCppBuildPlan(**hatch_build_config.model_dump()) + hatch_build_plan.generate() + + assert hatch_build_plan.commands[0].startswith("cmake .") + assert hatch_build_plan.commands[1].startswith("cmake --build build") + assert hatch_build_plan.commands[2].startswith("cmake --install build") + + assert "-DCMAKE_BUILD_TYPE=release" in hatch_build_plan.commands[0] + assert "-B build" in hatch_build_plan.commands[0] + assert "-DHATCH_CPP_TEST_PROJECT_BASIC_BUILD_TESTS=OFF" in hatch_build_plan.commands[0] + assert f"-DHATCH_CPP_TEST_PROJECT_BASIC_PYTHON_VERSION=3.{version_info.minor}" in hatch_build_plan.commands[0] + if hatch_build_plan.platform.platform == "darwin": + assert "-DCMAKE_OSX_DEPLOYMENT_TARGET=11" in hatch_build_plan.commands[0] diff --git a/hatch_cpp/utils.py b/hatch_cpp/utils.py index f95bf5e..0efb237 100644 --- a/hatch_cpp/utils.py +++ b/hatch_cpp/utils.py @@ -1,120 +1,12 @@ from __future__ import annotations -# import multiprocessing -# import os -# import os.path -# import platform -# import subprocess -# import sys -# from shutil import which -# from skbuild import setup +from functools import lru_cache -# CSP_USE_VCPKG = os.environ.get("CSP_USE_VCPKG", "1").lower() in ("1", "on") -# # Allow arg to override default / env -# if "--csp-no-vcpkg" in sys.argv: -# CSP_USE_VCPKG = False -# sys.argv.remove("--csp-no-vcpkg") +from pydantic import ImportString, TypeAdapter -# # CMake Options -# CMAKE_OPTIONS = ( -# ("CSP_BUILD_NO_CXX_ABI", "0"), -# ("CSP_BUILD_TESTS", "1"), -# ("CSP_MANYLINUX", "0"), -# ("CSP_BUILD_KAFKA_ADAPTER", "1"), -# ("CSP_BUILD_PARQUET_ADAPTER", "1"), -# ("CSP_BUILD_WS_CLIENT_ADAPTER", "1"), -# # NOTE: -# # - omit vcpkg, need to test for presence -# # - omit ccache, need to test for presence -# # - omit coverage/gprof, not implemented -# ) +_import_string_adapter = TypeAdapter(ImportString) -# if sys.platform == "linux": -# VCPKG_TRIPLET = "x64-linux" -# elif sys.platform == "win32": -# VCPKG_TRIPLET = "x64-windows-static-md" -# else: -# VCPKG_TRIPLET = None -# # This will be used for e.g. the sdist -# if CSP_USE_VCPKG: -# if not os.path.exists("vcpkg"): -# subprocess.call(["git", "clone", "https://github.com/Microsoft/vcpkg.git"]) -# if not os.path.exists("vcpkg/ports"): -# subprocess.call(["git", "submodule", "update", "--init", "--recursive"]) -# if not os.path.exists("vcpkg/buildtrees"): -# subprocess.call(["git", "pull"], cwd="vcpkg") -# args = ["install"] -# if VCPKG_TRIPLET is not None: -# args.append(f"--triplet={VCPKG_TRIPLET}") - -# if os.name == "nt": -# subprocess.call(["bootstrap-vcpkg.bat"], cwd="vcpkg", shell=True) -# subprocess.call(["vcpkg.bat"] + args, cwd="vcpkg", shell=True) -# else: -# subprocess.call(["./bootstrap-vcpkg.sh"], cwd="vcpkg") -# subprocess.call(["./vcpkg"] + args, cwd="vcpkg") - - -# python_version = f"{sys.version_info.major}.{sys.version_info.minor}" -# cmake_args = [f"-DCSP_PYTHON_VERSION={python_version}"] -# vcpkg_toolchain_file = os.path.abspath( -# os.environ.get( -# "CSP_VCPKG_PATH", -# os.path.join("vcpkg/scripts/buildsystems/vcpkg.cmake"), -# ) -# ) - -# if CSP_USE_VCPKG and os.path.exists(vcpkg_toolchain_file): -# cmake_args.extend( -# [ -# "-DCMAKE_TOOLCHAIN_FILE={}".format(vcpkg_toolchain_file), -# "-DCSP_USE_VCPKG=ON", -# ] -# ) - -# if VCPKG_TRIPLET is not None: -# cmake_args.append(f"-DVCPKG_TARGET_TRIPLET={VCPKG_TRIPLET}") -# else: -# cmake_args.append("-DCSP_USE_VCPKG=OFF") - -# if "CXX" in os.environ: -# cmake_args.append(f"-DCMAKE_CXX_COMPILER={os.environ['CXX']}") - -# if "DEBUG" in os.environ: -# cmake_args.append("-DCMAKE_BUILD_TYPE=Debug") - -# if platform.system() == "Windows": -# import distutils.msvccompiler as dm - -# # https://wiki.python.org/moin/WindowsCompilers#Microsoft_Visual_C.2B-.2B-_14.0_with_Visual_Studio_2015_.28x86.2C_x64.2C_ARM.29 -# msvc = { -# "12": "Visual Studio 12 2013", -# "14": "Visual Studio 14 2015", -# "14.0": "Visual Studio 14 2015", -# "14.1": "Visual Studio 15 2017", -# "14.2": "Visual Studio 16 2019", -# "14.3": "Visual Studio 17 2022", -# }.get(str(dm.get_build_version()), "Visual Studio 15 2017") -# cmake_args.extend( -# [ -# "-G", -# os.environ.get("CSP_GENERATOR", msvc), -# ] -# ) - -# for cmake_option, default in CMAKE_OPTIONS: -# if os.environ.get(cmake_option, default).lower() in ("1", "on"): -# cmake_args.append(f"-D{cmake_option}=ON") -# else: -# cmake_args.append(f"-D{cmake_option}=OFF") - -# if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ: -# os.environ["CMAKE_BUILD_PARALLEL_LEVEL"] = str(multiprocessing.cpu_count()) - -# if platform.system() == "Darwin": -# os.environ["MACOSX_DEPLOYMENT_TARGET"] = os.environ.get("OSX_DEPLOYMENT_TARGET", "10.15") -# cmake_args.append(f'-DCMAKE_OSX_DEPLOYMENT_TARGET={os.environ.get("OSX_DEPLOYMENT_TARGET", "10.15")}') - -# if which("ccache") and os.environ.get("CSP_USE_CCACHE", "") != "0": -# cmake_args.append("-DCSP_USE_CCACHE=On") +@lru_cache(maxsize=None) +def import_string(input_string: str): + return _import_string_adapter.validate_python(input_string) diff --git a/pyproject.toml b/pyproject.toml index c9344a7..f727b50 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,7 @@ authors = [{name = "the hatch-cpp authors", email = "t.paine154@gmail.com"}] description = "Hatch plugin for C++ builds" readme = "README.md" license = { text = "Apache-2.0" } -version = "0.1.0" +version = "0.1.7" requires-python = ">=3.9" keywords = [ "hatch", @@ -28,11 +28,13 @@ classifiers = [ "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", "License :: OSI Approved :: Apache Software License", ] dependencies = [ "hatchling>=1.20", + "pydantic", ] [project.optional-dependencies] @@ -40,26 +42,30 @@ develop = [ "build", "bump-my-version", "check-manifest", - "ruff>=0.3,<0.9", + "ruff>=0.3,<0.12", "twine", + "uv", "wheel", # test + "nanobind<2.8.0", # https://github.com/wjakob/nanobind/commit/abd27e3b5565bc95f5091321f0f863fce8b5b95b + "pybind11", "pytest", "pytest-cov", + "toml", ] [project.entry-points.hatch] cpp = "hatch_cpp.hooks" -[project.scripts] -hatch-cpp = "hatch_cpp.cli:main" +# [project.scripts] +# hatch-cpp = "hatch_cpp.cli:main" [project.urls] Repository = "https://github.com/python-project-templates/hatch-cpp" Homepage = "https://github.com/python-project-templates/hatch-cpp" [tool.bumpversion] -current_version = "0.1.0" +current_version = "0.1.7" commit = true tag = false @@ -75,9 +81,8 @@ replace = 'version = "{new_version}"' [tool.check-manifest] ignore = [ - ".copier-answers.yml", + ".copier-answers.yaml", "Makefile", - "setup.py", "docs/**/*", ] @@ -93,7 +98,7 @@ exclude_also = [ "@(abc\\.)?abstractmethod", ] ignore_errors = true -fail_under = 75 +fail_under = 70 [tool.hatch.build] artifacts = [] @@ -109,7 +114,6 @@ packages = ["hatch_cpp"] [tool.pytest.ini_options] addopts = ["-vvv", "--junitxml=junit.xml"] -asyncio_mode = "strict" testpaths = "hatch_cpp/tests" [tool.ruff]