hatch-cpp 0.1.6__py3-none-any.whl → 0.1.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
hatch_cpp/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = "0.1.6"
1
+ __version__ = "0.1.7"
2
2
 
3
3
  from .hooks import hatch_register_build_hook
4
4
  from .plugin import HatchCppBuildHook
hatch_cpp/plugin.py CHANGED
@@ -1,10 +1,11 @@
1
1
  from __future__ import annotations
2
2
 
3
- import logging
4
- import os
5
- import platform as sysplatform
6
- import sys
7
- import typing as t
3
+ from logging import getLogger
4
+ from os import getenv
5
+ from pathlib import Path
6
+ from platform import machine as platform_machine
7
+ from sys import platform as sys_platform, version_info
8
+ from typing import Any
8
9
 
9
10
  from hatchling.builders.hooks.plugin.interface import BuildHookInterface
10
11
 
@@ -18,13 +19,14 @@ class HatchCppBuildHook(BuildHookInterface[HatchCppBuildConfig]):
18
19
  """The hatch-cpp build hook."""
19
20
 
20
21
  PLUGIN_NAME = "hatch-cpp"
21
- _logger = logging.getLogger(__name__)
22
+ _logger = getLogger(__name__)
22
23
 
23
- def initialize(self, version: str, build_data: dict[str, t.Any]) -> None:
24
+ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
24
25
  """Initialize the plugin."""
25
26
  # Log some basic information
27
+ project_name = self.metadata.config["project"]["name"]
26
28
  self._logger.info("Initializing hatch-cpp plugin version %s", version)
27
- self._logger.info("Running hatch-cpp")
29
+ self._logger.info(f"Running hatch-cpp: {project_name}")
28
30
 
29
31
  # Only run if creating wheel
30
32
  # TODO: Add support for specify sdist-plan
@@ -34,7 +36,7 @@ class HatchCppBuildHook(BuildHookInterface[HatchCppBuildConfig]):
34
36
 
35
37
  # Skip if SKIP_HATCH_CPP is set
36
38
  # TODO: Support CLI once https://github.com/pypa/hatch/pull/1743
37
- if os.getenv("SKIP_HATCH_CPP"):
39
+ if getenv("SKIP_HATCH_CPP"):
38
40
  self._logger.info("Skipping the build hook since SKIP_HATCH_CPP was set")
39
41
  return
40
42
 
@@ -42,17 +44,13 @@ class HatchCppBuildHook(BuildHookInterface[HatchCppBuildConfig]):
42
44
  build_config_class = import_string(self.config["build-config-class"]) if "build-config-class" in self.config else HatchCppBuildConfig
43
45
 
44
46
  # Instantiate build config
45
- config = build_config_class(**self.config)
46
-
47
- # Grab libraries and platform
48
- libraries = config.libraries
49
- platform = config.platform
47
+ config = build_config_class(name=project_name, **self.config)
50
48
 
51
49
  # Get build plan class or use default
52
50
  build_plan_class = import_string(self.config["build-plan-class"]) if "build-plan-class" in self.config else HatchCppBuildPlan
53
51
 
54
52
  # Instantiate builder
55
- build_plan = build_plan_class(libraries=libraries, platform=platform)
53
+ build_plan = build_plan_class(**config.model_dump())
56
54
 
57
55
  # Generate commands
58
56
  build_plan.generate()
@@ -68,24 +66,48 @@ class HatchCppBuildHook(BuildHookInterface[HatchCppBuildConfig]):
68
66
  # Perform any cleanup actions
69
67
  build_plan.cleanup()
70
68
 
71
- # force include libraries
72
- for library in libraries:
73
- name = library.get_qualified_name(build_plan.platform.platform)
74
- build_data["force_include"][name] = name
69
+ if build_plan.libraries:
70
+ # force include libraries
71
+ for library in build_plan.libraries:
72
+ name = library.get_qualified_name(build_plan.platform.platform)
73
+ build_data["force_include"][name] = name
75
74
 
76
- if libraries:
77
75
  build_data["pure_python"] = False
78
- machine = sysplatform.machine()
79
- version_major = sys.version_info.major
80
- version_minor = sys.version_info.minor
81
- # TODO abi3
82
- if "darwin" in sys.platform:
76
+ machine = platform_machine()
77
+ version_major = version_info.major
78
+ version_minor = version_info.minor
79
+ if "darwin" in sys_platform:
83
80
  os_name = "macosx_11_0"
84
- elif "linux" in sys.platform:
81
+ elif "linux" in sys_platform:
85
82
  os_name = "linux"
86
83
  else:
87
84
  os_name = "win"
88
- if all([lib.py_limited_api for lib in libraries]):
85
+ if all([lib.py_limited_api for lib in build_plan.libraries]):
89
86
  build_data["tag"] = f"cp{version_major}{version_minor}-abi3-{os_name}_{machine}"
90
87
  else:
91
88
  build_data["tag"] = f"cp{version_major}{version_minor}-cp{version_major}{version_minor}-{os_name}_{machine}"
89
+ else:
90
+ build_data["pure_python"] = False
91
+ machine = platform_machine()
92
+ version_major = version_info.major
93
+ version_minor = version_info.minor
94
+ # TODO abi3
95
+ if "darwin" in sys_platform:
96
+ os_name = "macosx_11_0"
97
+ elif "linux" in sys_platform:
98
+ os_name = "linux"
99
+ else:
100
+ os_name = "win"
101
+ build_data["tag"] = f"cp{version_major}{version_minor}-cp{version_major}{version_minor}-{os_name}_{machine}"
102
+
103
+ # force include libraries
104
+ for path in Path(".").rglob("*"):
105
+ if path.is_dir():
106
+ continue
107
+ if str(path).startswith(str(build_plan.cmake.build)) or str(path).startswith("dist"):
108
+ continue
109
+ if path.suffix in (".pyd", ".dll", ".so", ".dylib"):
110
+ build_data["force_include"][str(path)] = str(path)
111
+
112
+ for path in build_data["force_include"]:
113
+ self._logger.warning(f"Force include: {path}")
hatch_cpp/structs.py CHANGED
@@ -1,12 +1,12 @@
1
1
  from __future__ import annotations
2
2
 
3
- from os import environ, system
3
+ from os import environ, system as system_call
4
4
  from pathlib import Path
5
5
  from re import match
6
6
  from shutil import which
7
- from sys import executable, platform as sys_platform
7
+ from sys import executable, platform as sys_platform, version_info
8
8
  from sysconfig import get_path
9
- from typing import Any, List, Literal, Optional
9
+ from typing import Any, Dict, List, Literal, Optional
10
10
 
11
11
  from pydantic import AliasChoices, BaseModel, Field, field_validator, model_validator
12
12
 
@@ -20,7 +20,7 @@ __all__ = (
20
20
  BuildType = Literal["debug", "release"]
21
21
  CompilerToolchain = Literal["gcc", "clang", "msvc"]
22
22
  Language = Literal["c", "c++"]
23
- Binding = Literal["cpython", "pybind11", "nanobind"]
23
+ Binding = Literal["cpython", "pybind11", "nanobind", "generic"]
24
24
  Platform = Literal["linux", "darwin", "win32"]
25
25
  PlatformDefaults = {
26
26
  "linux": {"CC": "gcc", "CXX": "g++", "LD": "ld"},
@@ -65,9 +65,9 @@ class HatchCppLibrary(BaseModel, validate_assignment=True):
65
65
 
66
66
  def get_qualified_name(self, platform):
67
67
  if platform == "win32":
68
- suffix = "dll" if self.binding == "none" else "pyd"
68
+ suffix = "dll" if self.binding == "generic" else "pyd"
69
69
  elif platform == "darwin":
70
- suffix = "dylib" if self.binding == "none" else "so"
70
+ suffix = "dylib" if self.binding == "generic" else "so"
71
71
  else:
72
72
  suffix = "so"
73
73
  if self.py_limited_api and platform != "win32":
@@ -78,6 +78,8 @@ class HatchCppLibrary(BaseModel, validate_assignment=True):
78
78
  def check_binding_and_py_limited_api(self):
79
79
  if self.binding == "pybind11" and self.py_limited_api:
80
80
  raise ValueError("pybind11 does not support Py_LIMITED_API")
81
+ if self.binding == "generic" and self.py_limited_api:
82
+ raise ValueError("Generic binding can not support Py_LIMITED_API")
81
83
  return self
82
84
 
83
85
 
@@ -119,7 +121,8 @@ class HatchCppPlatform(BaseModel):
119
121
  flags = ""
120
122
 
121
123
  # Python.h
122
- library.include_dirs.append(get_path("include"))
124
+ if library.binding != "generic":
125
+ library.include_dirs.append(get_path("include"))
123
126
 
124
127
  if library.binding == "pybind11":
125
128
  import pybind11
@@ -217,36 +220,100 @@ class HatchCppPlatform(BaseModel):
217
220
  return flags
218
221
 
219
222
 
220
- class HatchCppBuildPlan(BaseModel):
221
- build_type: BuildType = "release"
223
+ class HatchCppCmakeConfiguration(BaseModel):
224
+ root: Path
225
+ build: Path = Field(default_factory=lambda: Path("build"))
226
+ install: Optional[Path] = Field(default=None)
227
+
228
+ cmake_arg_prefix: Optional[str] = Field(default=None)
229
+ cmake_args: Dict[str, str] = Field(default_factory=dict)
230
+ cmake_env_args: Dict[Platform, Dict[str, str]] = Field(default_factory=dict)
231
+
232
+ include_flags: Optional[Dict[str, Any]] = Field(default=None)
233
+
234
+
235
+ class HatchCppBuildConfig(BaseModel):
236
+ """Build config values for Hatch C++ Builder."""
237
+
238
+ verbose: Optional[bool] = Field(default=False)
239
+ name: Optional[str] = Field(default=None)
222
240
  libraries: List[HatchCppLibrary] = Field(default_factory=list)
223
- platform: HatchCppPlatform = Field(default_factory=HatchCppPlatform.default)
241
+ cmake: Optional[HatchCppCmakeConfiguration] = Field(default=None)
242
+ platform: Optional[HatchCppPlatform] = Field(default_factory=HatchCppPlatform.default)
243
+
244
+ @model_validator(mode="after")
245
+ def check_toolchain_matches_args(self):
246
+ if self.cmake and self.libraries:
247
+ raise ValueError("Must not provide libraries when using cmake toolchain.")
248
+ return self
249
+
250
+
251
+ class HatchCppBuildPlan(HatchCppBuildConfig):
252
+ build_type: BuildType = "release"
224
253
  commands: List[str] = Field(default_factory=list)
225
254
 
226
255
  def generate(self):
227
256
  self.commands = []
228
- for library in self.libraries:
229
- compile_flags = self.platform.get_compile_flags(library, self.build_type)
230
- link_flags = self.platform.get_link_flags(library, self.build_type)
231
- self.commands.append(
232
- f"{self.platform.cc if library.language == 'c' else self.platform.cxx} {' '.join(library.sources)} {compile_flags} {link_flags}"
233
- )
257
+ if self.libraries:
258
+ for library in self.libraries:
259
+ compile_flags = self.platform.get_compile_flags(library, self.build_type)
260
+ link_flags = self.platform.get_link_flags(library, self.build_type)
261
+ self.commands.append(
262
+ f"{self.platform.cc if library.language == 'c' else self.platform.cxx} {' '.join(library.sources)} {compile_flags} {link_flags}"
263
+ )
264
+ elif self.cmake:
265
+ # Derive prefix
266
+ if self.cmake.cmake_arg_prefix is None:
267
+ self.cmake.cmake_arg_prefix = f"{self.name.replace('.', '_').replace('-', '_').upper()}_"
268
+
269
+ # Append base command
270
+ self.commands.append(f"cmake {Path(self.cmake.root).parent} -DCMAKE_BUILD_TYPE={self.build_type} -B {self.cmake.build}")
271
+
272
+ # Setup install path
273
+ if self.cmake.install:
274
+ self.commands[-1] += f" -DCMAKE_INSTALL_PREFIX={self.cmake.install}"
275
+ else:
276
+ self.commands[-1] += f" -DCMAKE_INSTALL_PREFIX={Path(self.cmake.root).parent}"
277
+
278
+ # TODO: CMAKE_CXX_COMPILER
279
+ if self.platform.platform == "win32":
280
+ # TODO: prefix?
281
+ self.commands[-1] += f' -G "{environ.get("GENERATOR", "Visual Studio 17 2022")}"'
282
+
283
+ # Put in CMake flags
284
+ args = self.cmake.cmake_args.copy()
285
+ for platform, env_args in self.cmake.cmake_env_args.items():
286
+ if platform == self.platform.platform:
287
+ for key, value in env_args.items():
288
+ args[key] = value
289
+ for key, value in args.items():
290
+ self.commands[-1] += f" -D{self.cmake.cmake_arg_prefix}{key.upper()}={value}"
291
+
292
+ # Include customs
293
+ if self.cmake.include_flags:
294
+ if self.cmake.include_flags.get("python_version", False):
295
+ self.commands[-1] += f" -D{self.cmake.cmake_arg_prefix}PYTHON_VERSION={version_info.major}.{version_info.minor}"
296
+ if self.cmake.include_flags.get("manylinux", False) and self.platform.platform == "linux":
297
+ self.commands[-1] += f" -D{self.cmake.cmake_arg_prefix}MANYLINUX=ON"
298
+
299
+ # Include mac deployment target
300
+ if self.platform.platform == "darwin":
301
+ self.commands[-1] += f" -DCMAKE_OSX_DEPLOYMENT_TARGET={environ.get('OSX_DEPLOYMENT_TARGET', '11')}"
302
+
303
+ # Append build command
304
+ self.commands.append(f"cmake --build {self.cmake.build} --config {self.build_type}")
305
+
306
+ # Append install command
307
+ self.commands.append(f"cmake --install {self.cmake.build} --config {self.build_type}")
308
+
234
309
  return self.commands
235
310
 
236
311
  def execute(self):
237
312
  for command in self.commands:
238
- system(command)
313
+ system_call(command)
239
314
  return self.commands
240
315
 
241
316
  def cleanup(self):
242
317
  if self.platform.platform == "win32":
243
318
  for temp_obj in Path(".").glob("*.obj"):
244
319
  temp_obj.unlink()
245
-
246
-
247
- class HatchCppBuildConfig(BaseModel):
248
- """Build config values for Hatch C++ Builder."""
249
-
250
- verbose: Optional[bool] = Field(default=False)
251
- libraries: List[HatchCppLibrary] = Field(default_factory=list)
252
- platform: Optional[HatchCppPlatform] = Field(default_factory=HatchCppPlatform.default)
@@ -0,0 +1,92 @@
1
+ cmake_minimum_required(VERSION 3.20.0)
2
+ project(hatch-cpp-test-project-basic VERSION "0.1.0")
3
+ set(CMAKE_CXX_STANDARD 20)
4
+ include(CheckCCompilerFlag)
5
+ include(CheckLinkerFlag)
6
+
7
+ if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
8
+ set(WIN32 ON)
9
+ set(MACOS OFF)
10
+ set(LINUX OFF)
11
+ elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
12
+ set(WIN32 OFF)
13
+ set(MACOS ON)
14
+ set(LINUX OFF)
15
+ else()
16
+ set(WIN32 OFF)
17
+ set(MACOS OFF)
18
+ set(LINUX ON)
19
+ endif()
20
+
21
+ option(CMAKE_BUILD_TYPE "Release/Debug build" RELEASE)
22
+ option(HATCH_CPP_TEST_PROJECT_BASIC_BUILD_TESTS "Build tests" OFF)
23
+ option(HATCH_CPP_TEST_PROJECT_BASIC_MANYLINUX "Build for python's manylinux setup" OFF)
24
+
25
+ string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
26
+
27
+ set(BUILD_SHARED_LIBS TRUE)
28
+ set(CMAKE_MACOSX_RPATH TRUE)
29
+ set(CMAKE_SKIP_RPATH FALSE)
30
+ set(CMAKE_SKIP_BUILD_RPATH FALSE)
31
+ set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
32
+ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
33
+ set(CMAKE_INSTALL_NAME_DIR "@rpath")
34
+ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
35
+
36
+ string(REGEX REPLACE "[ ]*-O[^ ]+[ ]*" " " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
37
+ string(REGEX REPLACE "[ ]*-Wl,-O2 -Wl,[^ ]+[ ]*" " " CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
38
+ string(REGEX REPLACE "[ ]*-Wl,-O2 -Wl,[^ ]+[ ]*" " " CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
39
+
40
+
41
+ if(MACOS)
42
+ set(CMAKE_THREAD_LIBS_INIT "-lpthread")
43
+ set(CMAKE_HAVE_THREADS_LIBRARY 1)
44
+ set(CMAKE_USE_WIN32_THREADS_INIT 0)
45
+ set(CMAKE_USE_PTHREADS_INIT 1)
46
+ set(THREADS_PREFER_PTHREAD_FLAG ON)
47
+ set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined dynamic_lookup")
48
+ endif()
49
+
50
+
51
+ if(MACOS)
52
+ set(CMAKE_INSTALL_RPATH "@loader_path/")
53
+ elseif(LINUX)
54
+ set(CMAKE_INSTALL_RPATH "\$ORIGIN")
55
+ endif()
56
+
57
+ if(WIN32)
58
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP /bigobj")
59
+ foreach(warning 4244 4251 4267 4275 4290 4786 4305 4996)
60
+ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd${warning}")
61
+ endforeach(warning)
62
+ else()
63
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
64
+ -g \
65
+ -Wall \
66
+ -Werror \
67
+ -Wno-deprecated-declarations \
68
+ -Wno-deprecated \
69
+ ")
70
+ endif()
71
+
72
+
73
+ find_package(Python ${CSP_PYTHON_VERSION} EXACT REQUIRED COMPONENTS Interpreter Development.Module)
74
+ link_directories(${Python_LIBRARY_DIRS})
75
+ include_directories(${Python_INCLUDE_DIRS})
76
+
77
+ set(CMAKE_SHARED_LIBRARY_PREFIX "")
78
+ if(NOT WIN32)
79
+ set(CMAKE_SHARED_LIBRARY_SUFFIX .so)
80
+ else()
81
+ set(CMAKE_SHARED_LIBRARY_SUFFIX .pyd)
82
+ endif()
83
+
84
+ include_directories("${CMAKE_SOURCE_DIR}/cpp")
85
+
86
+ add_library(extension SHARED cpp/project/basic.cpp)
87
+ set_target_properties(extension PROPERTIES PUBLIC_HEADER cpp/project/basic.hpp)
88
+ install(TARGETS extension
89
+ PUBLIC_HEADER DESTINATION project/include/project
90
+ RUNTIME DESTINATION project/
91
+ LIBRARY DESTINATION project/
92
+ )
@@ -0,0 +1,140 @@
1
+ # CMAKE generated file: DO NOT EDIT!
2
+ # Generated by "Unix Makefiles" Generator, CMake Version 3.31
3
+
4
+ # Default target executed when no arguments are given to make.
5
+ default_target: all
6
+ .PHONY : default_target
7
+
8
+ # Allow only one "make -f Makefile2" at a time, but pass parallelism.
9
+ .NOTPARALLEL:
10
+
11
+ #=============================================================================
12
+ # Special targets provided by cmake.
13
+
14
+ # Disable implicit rules so canonical targets will work.
15
+ .SUFFIXES:
16
+
17
+ # Disable VCS-based implicit rules.
18
+ % : %,v
19
+
20
+ # Disable VCS-based implicit rules.
21
+ % : RCS/%
22
+
23
+ # Disable VCS-based implicit rules.
24
+ % : RCS/%,v
25
+
26
+ # Disable VCS-based implicit rules.
27
+ % : SCCS/s.%
28
+
29
+ # Disable VCS-based implicit rules.
30
+ % : s.%
31
+
32
+ .SUFFIXES: .hpux_make_needs_suffix_list
33
+
34
+ # Command-line flag to silence nested $(MAKE).
35
+ $(VERBOSE)MAKESILENT = -s
36
+
37
+ #Suppress display of executed commands.
38
+ $(VERBOSE).SILENT:
39
+
40
+ # A target that is always out of date.
41
+ cmake_force:
42
+ .PHONY : cmake_force
43
+
44
+ #=============================================================================
45
+ # Set environment variables for the build.
46
+
47
+ # The shell in which to execute make rules.
48
+ SHELL = /bin/sh
49
+
50
+ # The CMake executable.
51
+ CMAKE_COMMAND = /opt/homebrew/bin/cmake
52
+
53
+ # The command to remove a file.
54
+ RM = /opt/homebrew/bin/cmake -E rm -f
55
+
56
+ # Escaping for special characters.
57
+ EQUALS = =
58
+
59
+ # The top-level source directory on which CMake was run.
60
+ CMAKE_SOURCE_DIR = /Users/timkpaine/Developer/projects/templates/hatch-cpp/hatch_cpp/tests/test_project_cmake
61
+
62
+ # The top-level build directory on which CMake was run.
63
+ CMAKE_BINARY_DIR = /Users/timkpaine/Developer/projects/templates/hatch-cpp/hatch_cpp/tests/test_project_cmake
64
+
65
+ #=============================================================================
66
+ # Targets provided globally by CMake.
67
+
68
+ # Special rule for the target edit_cache
69
+ edit_cache:
70
+ @$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake cache editor..."
71
+ /opt/homebrew/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
72
+ .PHONY : edit_cache
73
+
74
+ # Special rule for the target edit_cache
75
+ edit_cache/fast: edit_cache
76
+ .PHONY : edit_cache/fast
77
+
78
+ # Special rule for the target rebuild_cache
79
+ rebuild_cache:
80
+ @$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake to regenerate build system..."
81
+ /opt/homebrew/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
82
+ .PHONY : rebuild_cache
83
+
84
+ # Special rule for the target rebuild_cache
85
+ rebuild_cache/fast: rebuild_cache
86
+ .PHONY : rebuild_cache/fast
87
+
88
+ # The main all target
89
+ all: cmake_check_build_system
90
+ $(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
91
+ $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all
92
+ $(CMAKE_COMMAND) -E cmake_progress_start /Users/timkpaine/Developer/projects/templates/hatch-cpp/hatch_cpp/tests/test_project_cmake/CMakeFiles 0
93
+ .PHONY : all
94
+
95
+ # The main clean target
96
+ clean:
97
+ $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 clean
98
+ .PHONY : clean
99
+
100
+ # The main clean target
101
+ clean/fast: clean
102
+ .PHONY : clean/fast
103
+
104
+ # Prepare targets for installation.
105
+ preinstall: all
106
+ $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
107
+ .PHONY : preinstall
108
+
109
+ # Prepare targets for installation.
110
+ preinstall/fast:
111
+ $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
112
+ .PHONY : preinstall/fast
113
+
114
+ # clear depends
115
+ depend:
116
+ $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
117
+ .PHONY : depend
118
+
119
+ # Help Target
120
+ help:
121
+ @echo "The following are some of the valid targets for this Makefile:"
122
+ @echo "... all (the default if no target is provided)"
123
+ @echo "... clean"
124
+ @echo "... depend"
125
+ @echo "... edit_cache"
126
+ @echo "... rebuild_cache"
127
+ .PHONY : help
128
+
129
+
130
+
131
+ #=============================================================================
132
+ # Special targets to cleanup operation of make.
133
+
134
+ # Special rule to run CMake to check the build system integrity.
135
+ # No rule that depends on this can have commands that come from listfiles
136
+ # because they might be regenerated.
137
+ cmake_check_build_system:
138
+ $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
139
+ .PHONY : cmake_check_build_system
140
+
@@ -0,0 +1,5 @@
1
+ #include "project/basic.hpp"
2
+
3
+ PyObject* hello(PyObject*, PyObject*) {
4
+ return PyUnicode_FromString("A string");
5
+ }
@@ -0,0 +1,17 @@
1
+ #pragma once
2
+ #include "Python.h"
3
+
4
+ PyObject* hello(PyObject*, PyObject*);
5
+
6
+ static PyMethodDef extension_methods[] = {
7
+ {"hello", (PyCFunction)hello, METH_NOARGS},
8
+ {nullptr, nullptr, 0, nullptr}
9
+ };
10
+
11
+ static PyModuleDef extension_module = {
12
+ PyModuleDef_HEAD_INIT, "extension", "extension", -1, extension_methods};
13
+
14
+ PyMODINIT_FUNC PyInit_extension(void) {
15
+ Py_Initialize();
16
+ return PyModule_Create(&extension_module);
17
+ }
File without changes
@@ -0,0 +1,39 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.20"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "hatch-cpp-test-project-basic"
7
+ description = "Basic test project for hatch-cpp"
8
+ version = "0.1.0"
9
+ requires-python = ">=3.9"
10
+ dependencies = [
11
+ "hatchling>=1.20",
12
+ "hatch-cpp",
13
+ ]
14
+
15
+ [tool.hatch.build]
16
+ artifacts = [
17
+ "project/*.dll",
18
+ "project/*.dylib",
19
+ "project/*.so",
20
+ ]
21
+
22
+ [tool.hatch.build.sources]
23
+ src = "/"
24
+
25
+ [tool.hatch.build.targets.sdist]
26
+ packages = ["project"]
27
+
28
+ [tool.hatch.build.targets.wheel]
29
+ packages = ["project"]
30
+
31
+ [tool.hatch.build.hooks.hatch-cpp]
32
+ verbose = true
33
+
34
+ [tool.hatch.build.hooks.hatch-cpp.cmake]
35
+ root = "CMakeLists.txt"
36
+ cmake_args = {"BUILD_TESTS" = "OFF"}
37
+ include_flags = {"python_version" = true}
38
+ [tool.hatch.build.hooks.hatch-cpp.cmake.cmake_env_args]
39
+ linux = {"MANYLINUX" = "ON"}
@@ -9,7 +9,15 @@ import pytest
9
9
 
10
10
  class TestProject:
11
11
  @pytest.mark.parametrize(
12
- "project", ["test_project_basic", "test_project_override_classes", "test_project_pybind", "test_project_nanobind", "test_project_limited_api"]
12
+ "project",
13
+ [
14
+ "test_project_basic",
15
+ "test_project_override_classes",
16
+ "test_project_pybind",
17
+ "test_project_nanobind",
18
+ "test_project_limited_api",
19
+ "test_project_cmake",
20
+ ],
13
21
  )
14
22
  def test_basic(self, project):
15
23
  # cleanup
@@ -1,7 +1,11 @@
1
+ from pathlib import Path
2
+ from sys import version_info
3
+
1
4
  import pytest
2
5
  from pydantic import ValidationError
6
+ from toml import loads
3
7
 
4
- from hatch_cpp.structs import HatchCppLibrary, HatchCppPlatform
8
+ from hatch_cpp.structs import HatchCppBuildConfig, HatchCppBuildPlan, HatchCppLibrary, HatchCppPlatform
5
9
 
6
10
 
7
11
  class TestStructs:
@@ -24,3 +28,21 @@ class TestStructs:
24
28
 
25
29
  with pytest.raises(ValidationError):
26
30
  library.binding = "pybind11"
31
+
32
+ def test_cmake_args(self):
33
+ txt = (Path(__file__).parent / "test_project_cmake" / "pyproject.toml").read_text()
34
+ toml = loads(txt)
35
+ hatch_build_config = HatchCppBuildConfig(name=toml["project"]["name"], **toml["tool"]["hatch"]["build"]["hooks"]["hatch-cpp"])
36
+ hatch_build_plan = HatchCppBuildPlan(**hatch_build_config.model_dump())
37
+ hatch_build_plan.generate()
38
+
39
+ assert hatch_build_plan.commands[0].startswith("cmake .")
40
+ assert hatch_build_plan.commands[1].startswith("cmake --build build")
41
+ assert hatch_build_plan.commands[2].startswith("cmake --install build")
42
+
43
+ assert "-DCMAKE_BUILD_TYPE=release" in hatch_build_plan.commands[0]
44
+ assert "-B build" in hatch_build_plan.commands[0]
45
+ assert "-DHATCH_CPP_TEST_PROJECT_BASIC_BUILD_TESTS=OFF" in hatch_build_plan.commands[0]
46
+ assert f"-DHATCH_CPP_TEST_PROJECT_BASIC_PYTHON_VERSION=3.{version_info.minor}" in hatch_build_plan.commands[0]
47
+ if hatch_build_plan.platform.platform == "darwin":
48
+ assert "-DCMAKE_OSX_DEPLOYMENT_TARGET=11" in hatch_build_plan.commands[0]
hatch_cpp/utils.py CHANGED
@@ -10,123 +10,3 @@ _import_string_adapter = TypeAdapter(ImportString)
10
10
  @lru_cache(maxsize=None)
11
11
  def import_string(input_string: str):
12
12
  return _import_string_adapter.validate_python(input_string)
13
-
14
-
15
- # import multiprocessing
16
- # import os
17
- # import os.path
18
- # import platform
19
- # import subprocess
20
- # import sys
21
- # from shutil import which
22
- # from skbuild import setup
23
-
24
- # CSP_USE_VCPKG = os.environ.get("CSP_USE_VCPKG", "1").lower() in ("1", "on")
25
- # # Allow arg to override default / env
26
- # if "--csp-no-vcpkg" in sys.argv:
27
- # CSP_USE_VCPKG = False
28
- # sys.argv.remove("--csp-no-vcpkg")
29
-
30
- # # CMake Options
31
- # CMAKE_OPTIONS = (
32
- # ("CSP_BUILD_NO_CXX_ABI", "0"),
33
- # ("CSP_BUILD_TESTS", "1"),
34
- # ("CSP_MANYLINUX", "0"),
35
- # ("CSP_BUILD_KAFKA_ADAPTER", "1"),
36
- # ("CSP_BUILD_PARQUET_ADAPTER", "1"),
37
- # ("CSP_BUILD_WS_CLIENT_ADAPTER", "1"),
38
- # # NOTE:
39
- # # - omit vcpkg, need to test for presence
40
- # # - omit ccache, need to test for presence
41
- # # - omit coverage/gprof, not implemented
42
- # )
43
-
44
- # if sys.platform == "linux":
45
- # VCPKG_TRIPLET = "x64-linux"
46
- # elif sys.platform == "win32":
47
- # VCPKG_TRIPLET = "x64-windows-static-md"
48
- # else:
49
- # VCPKG_TRIPLET = None
50
-
51
- # # This will be used for e.g. the sdist
52
- # if CSP_USE_VCPKG:
53
- # if not os.path.exists("vcpkg"):
54
- # subprocess.call(["git", "clone", "https://github.com/Microsoft/vcpkg.git"])
55
- # if not os.path.exists("vcpkg/ports"):
56
- # subprocess.call(["git", "submodule", "update", "--init", "--recursive"])
57
- # if not os.path.exists("vcpkg/buildtrees"):
58
- # subprocess.call(["git", "pull"], cwd="vcpkg")
59
- # args = ["install"]
60
- # if VCPKG_TRIPLET is not None:
61
- # args.append(f"--triplet={VCPKG_TRIPLET}")
62
-
63
- # if os.name == "nt":
64
- # subprocess.call(["bootstrap-vcpkg.bat"], cwd="vcpkg", shell=True)
65
- # subprocess.call(["vcpkg.bat"] + args, cwd="vcpkg", shell=True)
66
- # else:
67
- # subprocess.call(["./bootstrap-vcpkg.sh"], cwd="vcpkg")
68
- # subprocess.call(["./vcpkg"] + args, cwd="vcpkg")
69
-
70
-
71
- # python_version = f"{sys.version_info.major}.{sys.version_info.minor}"
72
- # cmake_args = [f"-DCSP_PYTHON_VERSION={python_version}"]
73
- # vcpkg_toolchain_file = os.path.abspath(
74
- # os.environ.get(
75
- # "CSP_VCPKG_PATH",
76
- # os.path.join("vcpkg/scripts/buildsystems/vcpkg.cmake"),
77
- # )
78
- # )
79
-
80
- # if CSP_USE_VCPKG and os.path.exists(vcpkg_toolchain_file):
81
- # cmake_args.extend(
82
- # [
83
- # "-DCMAKE_TOOLCHAIN_FILE={}".format(vcpkg_toolchain_file),
84
- # "-DCSP_USE_VCPKG=ON",
85
- # ]
86
- # )
87
-
88
- # if VCPKG_TRIPLET is not None:
89
- # cmake_args.append(f"-DVCPKG_TARGET_TRIPLET={VCPKG_TRIPLET}")
90
- # else:
91
- # cmake_args.append("-DCSP_USE_VCPKG=OFF")
92
-
93
- # if "CXX" in os.environ:
94
- # cmake_args.append(f"-DCMAKE_CXX_COMPILER={os.environ['CXX']}")
95
-
96
- # if "DEBUG" in os.environ:
97
- # cmake_args.append("-DCMAKE_BUILD_TYPE=Debug")
98
-
99
- # if platform.system() == "Windows":
100
- # import distutils.msvccompiler as dm
101
-
102
- # # https://wiki.python.org/moin/WindowsCompilers#Microsoft_Visual_C.2B-.2B-_14.0_with_Visual_Studio_2015_.28x86.2C_x64.2C_ARM.29
103
- # msvc = {
104
- # "12": "Visual Studio 12 2013",
105
- # "14": "Visual Studio 14 2015",
106
- # "14.0": "Visual Studio 14 2015",
107
- # "14.1": "Visual Studio 15 2017",
108
- # "14.2": "Visual Studio 16 2019",
109
- # "14.3": "Visual Studio 17 2022",
110
- # }.get(str(dm.get_build_version()), "Visual Studio 15 2017")
111
- # cmake_args.extend(
112
- # [
113
- # "-G",
114
- # os.environ.get("CSP_GENERATOR", msvc),
115
- # ]
116
- # )
117
-
118
- # for cmake_option, default in CMAKE_OPTIONS:
119
- # if os.environ.get(cmake_option, default).lower() in ("1", "on"):
120
- # cmake_args.append(f"-D{cmake_option}=ON")
121
- # else:
122
- # cmake_args.append(f"-D{cmake_option}=OFF")
123
-
124
- # if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
125
- # os.environ["CMAKE_BUILD_PARALLEL_LEVEL"] = str(multiprocessing.cpu_count())
126
-
127
- # if platform.system() == "Darwin":
128
- # os.environ["MACOSX_DEPLOYMENT_TARGET"] = os.environ.get("OSX_DEPLOYMENT_TARGET", "10.15")
129
- # cmake_args.append(f'-DCMAKE_OSX_DEPLOYMENT_TARGET={os.environ.get("OSX_DEPLOYMENT_TARGET", "10.15")}')
130
-
131
- # if which("ccache") and os.environ.get("CSP_USE_CCACHE", "") != "0":
132
- # cmake_args.append("-DCSP_USE_CCACHE=On")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hatch-cpp
3
- Version: 0.1.6
3
+ Version: 0.1.7
4
4
  Summary: Hatch plugin for C++ builds
5
5
  Project-URL: Repository, https://github.com/python-project-templates/hatch-cpp
6
6
  Project-URL: Homepage, https://github.com/python-project-templates/hatch-cpp
@@ -30,6 +30,7 @@ Requires-Dist: pybind11; extra == 'develop'
30
30
  Requires-Dist: pytest; extra == 'develop'
31
31
  Requires-Dist: pytest-cov; extra == 'develop'
32
32
  Requires-Dist: ruff<0.9,>=0.3; extra == 'develop'
33
+ Requires-Dist: toml; extra == 'develop'
33
34
  Requires-Dist: twine; extra == 'develop'
34
35
  Requires-Dist: wheel; extra == 'develop'
35
36
  Description-Content-Type: text/markdown
@@ -1,14 +1,20 @@
1
- hatch_cpp/__init__.py,sha256=GpnnPr3BgwAuiIJ5m9xconjyECZPRPhuZQ9Q7HzWR0o,129
1
+ hatch_cpp/__init__.py,sha256=iL_H3Zhh1jKzI3DB7IrRjibqnsFh0QPAQL1ERHAR7hI,129
2
2
  hatch_cpp/hooks.py,sha256=SQkF5WJIgzw-8rvlTzuQvBqdP6K3fHgnh6CZOZFag50,203
3
- hatch_cpp/plugin.py,sha256=iEDP6U7T_3G3m2ONCoYcMD3XNLq6ob_s_dn7AbtwnZ8,3238
4
- hatch_cpp/structs.py,sha256=o_XtXWVs0Zv0IARLY8R3NEbNqT2sAHQf7GNMIFggrKg,10681
5
- hatch_cpp/utils.py,sha256=topOiT6biVzisNAEKHvYzU-JdA56B3LA61jbU9fjAEQ,4444
6
- hatch_cpp/tests/test_projects.py,sha256=zE0XOPOuaQPfdvjgZRTPnsZ5gzK2shFPqKaPXkb5oSc,1509
7
- hatch_cpp/tests/test_structs.py,sha256=Qv2uBHwxf0edR8FIOnA2gj-AJgdsiDKt4vyK8HChAHg,851
3
+ hatch_cpp/plugin.py,sha256=AV5hcVcPPIOsalKKiGSXHcHrLlZ7mKIVc-g5_waK9fk,4403
4
+ hatch_cpp/structs.py,sha256=LDaAjzpAl4G7IysMIz6zUV1H5GcVBL2BAxA7DXjtxQw,13895
5
+ hatch_cpp/utils.py,sha256=lxd3U3aMYsTS6DYMc1y17MR16LzgRzv92f_xh87LSg8,297
6
+ hatch_cpp/tests/test_projects.py,sha256=vmJ218C_jP5nsen2gwjPtob0AoH4J_jNS5nOckHamvw,1623
7
+ hatch_cpp/tests/test_structs.py,sha256=5y_xi2Hsg5NwG34hWuQoycJZEOMRI384RjPjIY4skw4,2090
8
8
  hatch_cpp/tests/test_project_basic/pyproject.toml,sha256=eqM1UVpNmJWDfsuO18ZG_VOV9I4tAWgsM5Dhf49X8Nc,694
9
9
  hatch_cpp/tests/test_project_basic/cpp/project/basic.cpp,sha256=gQ2nmdLqIdgaqxSKvkN_vbp6Iv_pAoVIHETXPRnALb0,117
10
10
  hatch_cpp/tests/test_project_basic/cpp/project/basic.hpp,sha256=SO5GhPj8k3RzWrfH37lFSDc8w1Vf3yqTUhxmr1hnoko,422
11
11
  hatch_cpp/tests/test_project_basic/project/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
+ hatch_cpp/tests/test_project_cmake/CMakeLists.txt,sha256=J6ijT9x9uSuisNEVIdFVgc_AstU9hbmIUbwvdjn_4GU,2795
13
+ hatch_cpp/tests/test_project_cmake/Makefile,sha256=Vfr65pvnktWTUeuvpMWqIxDR-7V9MT4PQAyj-WHG_pI,4357
14
+ hatch_cpp/tests/test_project_cmake/pyproject.toml,sha256=pu9RWhm_FY7KlcdOg2nmp-obnStnPJ04jW-bxei704w,814
15
+ hatch_cpp/tests/test_project_cmake/cpp/project/basic.cpp,sha256=gQ2nmdLqIdgaqxSKvkN_vbp6Iv_pAoVIHETXPRnALb0,117
16
+ hatch_cpp/tests/test_project_cmake/cpp/project/basic.hpp,sha256=SO5GhPj8k3RzWrfH37lFSDc8w1Vf3yqTUhxmr1hnoko,422
17
+ hatch_cpp/tests/test_project_cmake/project/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
18
  hatch_cpp/tests/test_project_limited_api/pyproject.toml,sha256=k_2y7YpZP5I-KPvKOZLmnHqoKXVE2oVM8fjxtRKoTo0,726
13
19
  hatch_cpp/tests/test_project_limited_api/cpp/project/basic.cpp,sha256=gQ2nmdLqIdgaqxSKvkN_vbp6Iv_pAoVIHETXPRnALb0,117
14
20
  hatch_cpp/tests/test_project_limited_api/cpp/project/basic.hpp,sha256=SO5GhPj8k3RzWrfH37lFSDc8w1Vf3yqTUhxmr1hnoko,422
@@ -27,8 +33,8 @@ hatch_cpp/tests/test_project_pybind/cpp/project/basic.hpp,sha256=LZSfCfhLY_91MBO
27
33
  hatch_cpp/tests/test_project_pybind/project/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
28
34
  hatch_cpp/toolchains/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
29
35
  hatch_cpp/toolchains/cmake.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
30
- hatch_cpp-0.1.6.dist-info/METADATA,sha256=xxJ65xK090LytyqWJP48Gy4rQLOGJNIprM5UC2e8_gA,3003
31
- hatch_cpp-0.1.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
32
- hatch_cpp-0.1.6.dist-info/entry_points.txt,sha256=RgXfjpD4iwomJQK5n7FnPkXzb5fOnF5v3DI5hbkgVcw,30
33
- hatch_cpp-0.1.6.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
34
- hatch_cpp-0.1.6.dist-info/RECORD,,
36
+ hatch_cpp-0.1.7.dist-info/METADATA,sha256=z95QlwYagluSoKGVxGm9sdjCcVF_y93MCmxSc3yvnZ0,3043
37
+ hatch_cpp-0.1.7.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
38
+ hatch_cpp-0.1.7.dist-info/entry_points.txt,sha256=RgXfjpD4iwomJQK5n7FnPkXzb5fOnF5v3DI5hbkgVcw,30
39
+ hatch_cpp-0.1.7.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
40
+ hatch_cpp-0.1.7.dist-info/RECORD,,