Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Allow for toolchain tweaks in pyproject.toml#67

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
timkpaine merged 1 commit intomainfromtkp/tc
Oct 28, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
39 changes: 34 additions & 5 deletionshatch_cpp/structs.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,8 +102,15 @@ def default() -> HatchCppPlatform:
toolchain = "clang"
elif "cl" in CC and "cl" in CXX:
toolchain = "msvc"
# Fallback to platform defaults
elif platform == "linux":
toolchain = "gcc"
elif platform == "darwin":
toolchain = "clang"
elif platform == "win32":
toolchain = "msvc"
else:
raise Exception(f"Unrecognizedtoolchain: {CC}, {CXX}")
toolchain = "gcc"

# Customizations
if which("ccache") and not environ.get("HATCH_CPP_DISABLE_CCACHE"):
Expand All@@ -117,6 +124,12 @@ def default() -> HatchCppPlatform:
# LD = which("ld.lld")
return HatchCppPlatform(cc=CC, cxx=CXX, ld=LD, platform=platform, toolchain=toolchain)

@staticmethod
def platform_for_toolchain(toolchain: CompilerToolchain) -> HatchCppPlatform:
platform = HatchCppPlatform.default()
platform.toolchain = toolchain
return platform

def get_compile_flags(self, library: HatchCppLibrary, build_type: BuildType = "release") -> str:
flags = ""

Expand DownExpand Up@@ -241,11 +254,27 @@ class HatchCppBuildConfig(BaseModel):
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:
@model_validator(mode="wrap")
@classmethod
def validate_model(cls, data, handler):
if "toolchain" in data:
data["platform"] = HatchCppPlatform.platform_for_toolchain(data["toolchain"])
data.pop("toolchain")
elif "platform" not in data:
data["platform"] = HatchCppPlatform.default()
if "cc" in data:
data["platform"].cc = data["cc"]
data.pop("cc")
if "cxx" in data:
data["platform"].cxx = data["cxx"]
data.pop("cxx")
if "ld" in data:
data["platform"].ld = data["ld"]
data.pop("ld")
model = handler(data)
if model.cmake and model.libraries:
raise ValueError("Must not provide libraries when using cmake toolchain.")
returnself
returnmodel


class HatchCppBuildPlan(HatchCppBuildConfig):
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff 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);
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
#include "project/basic.hpp"

PyObject* hello(PyObject*, PyObject*) {
return PyUnicode_FromString("A string");
}
View file
Open in desktop
Original file line numberDiff line numberDiff 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.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
[build-system]
requires = ["hatchling>=1.20"]
build-backend = "hatchling.build"

[project]
name = "hatch-cpp-test-project-toolchain"
description = "Toolchain override 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"]}
]
toolchain = "gcc"
cc = "clang"
cxx = "clang++"
5 changes: 3 additions & 2 deletionshatch_cpp/tests/test_projects.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,8 @@ class TestProject:
[
"test_project_basic",
"test_project_override_classes",
"test_project_override_classes",
"test_project_override_toolchain",
"test_project_pybind",
"test_project_nanobind",
"test_project_limited_api",
Expand All@@ -29,8 +31,7 @@ def test_basic(self, project):
# compile
check_call(
[
"hatchling",
"build",
"hatch-build",
"--hooks-only",
],
cwd=f"hatch_cpp/tests/{project}",
Expand Down
8 changes: 8 additions & 0 deletionshatch_cpp/tests/test_structs.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,3 +46,11 @@ def test_cmake_args(self):
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]

def test_platform_toolchain_override(self):
txt = (Path(__file__).parent / "test_project_override_toolchain" / "pyproject.toml").read_text()
toml = loads(txt)
hatch_build_config = HatchCppBuildConfig(name=toml["project"]["name"], **toml["tool"]["hatch"]["build"]["hooks"]["hatch-cpp"])
assert "clang" in hatch_build_config.platform.cc
assert "clang++" in hatch_build_config.platform.cxx
assert hatch_build_config.platform.toolchain == "gcc"
1 change: 1 addition & 0 deletionspyproject.toml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -45,6 +45,7 @@ develop = [
"check-manifest",
"codespell>=2.4,<2.5",
"hatchling",
"hatch-build",
"mdformat>=0.7.22,<0.8",
"mdformat-tables>=1",
"pytest",
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp