Skip to content

Commit 9074f0a

Browse files
committed
Enable the C ABI library option
Regenerate from the base template with add_c_abi. The sources split into cpp/python-template-cpp, which builds libpython_template_cpp with a C surface, and cpp/python, the pybind11 extension that links it. cpp/CMakeLists.txt builds and installs the library without Python. Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
1 parent 268627e commit 9074f0a

15 files changed

Lines changed: 300 additions & 25 deletions

File tree

‎.copier-answers.yaml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changes here will be overwritten by Copier
2-
_commit: 02f55bd
2+
_commit: c40b5eb
33
_src_path: https://github.com/python-project-templates/base.git
4+
add_c_abi: true
45
add_docs: true
56
add_extension: cpp
67
add_pyodide: true

‎README.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,28 @@ A C++-Python project template
99

1010
## Overview
1111

12+
## C ABI
13+
14+
`cpp/python-template-cpp` builds `libpython_template_cpp`, a C ABI over the C++ code, declared in
15+
`python_template_cpp/include/python_template_cpp/python_template_cpp.h`. Wheels ship the library under `python_template_cpp/lib` and
16+
the header under `python_template_cpp/include/python_template_cpp`, and the pybind11 extension in `cpp/python` links
17+
against the library. The tests compile a C program against the installed package. Other
18+
projects build against the installed wheel with `python_template_cpp.get_include()`,
19+
`get_library_dirs()`, `get_libraries()`, and `get_prefix()`.
20+
21+
`HATCH_CPP_LIBRARY_SOURCE` chooses where the library comes from at build time:
22+
`bundled` (the default, for wheels), `external` (a prebuilt library found
23+
through `HATCH_CPP_LIBRARY_ROOT`, pkg-config, or the conda prefix), or `direct`
24+
(compile the library sources into the extension).
25+
26+
`cpp/CMakeLists.txt` builds and installs the library without Python, with a CMake
27+
package config and a pkg-config file, for conda, vcpkg, and system packages:
28+
29+
```bash
30+
cmake -S cpp -B build -DCMAKE_BUILD_TYPE=Release
31+
cmake --build build
32+
cmake --install build --prefix /path/to/prefix
33+
```
34+
1235
> [!NOTE]
1336
> 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).

‎cpp/CMakeLists.txt‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Builds and installs libpython_template_cpp without Python, for conda, vcpkg, and system packages.
2+
cmake_minimum_required(VERSION 3.20)
3+
project(python_template_cpp VERSION 0.1.0 LANGUAGES CXX)
4+
5+
include(CMakePackageConfigHelpers)
6+
7+
add_library(python_template_cpp SHARED python-template-cpp/c_api.cpp python-template-cpp/example.cpp)
8+
add_library(python_template_cpp::python_template_cpp ALIAS python_template_cpp)
9+
target_compile_features(python_template_cpp PRIVATE cxx_std_17)
10+
target_compile_definitions(python_template_cpp PRIVATE PYTHON_TEMPLATE_CPP_BUILD)
11+
target_include_directories(
12+
python_template_cpp
13+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../python_template_cpp/include> $<INSTALL_INTERFACE:include>
14+
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
15+
)
16+
# SOVERSION is PYTHON_TEMPLATE_CPP_ABI_VERSION.
17+
set_target_properties(
18+
python_template_cpp
19+
PROPERTIES VERSION ${PROJECT_VERSION}
20+
SOVERSION 1
21+
CXX_VISIBILITY_PRESET hidden
22+
VISIBILITY_INLINES_HIDDEN ON
23+
INSTALL_NAME_DIR "@rpath"
24+
)
25+
26+
install(
27+
TARGETS python_template_cpp
28+
EXPORT python_template_cppTargets
29+
RUNTIME DESTINATION bin
30+
LIBRARY DESTINATION lib
31+
ARCHIVE DESTINATION lib
32+
)
33+
install(DIRECTORY ../python_template_cpp/include/ DESTINATION include)
34+
install(EXPORT python_template_cppTargets NAMESPACE python_template_cpp:: DESTINATION lib/cmake/python_template_cpp)
35+
36+
configure_package_config_file(
37+
cmake/python_template_cppConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/python_template_cppConfig.cmake
38+
INSTALL_DESTINATION lib/cmake/python_template_cpp
39+
)
40+
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/python_template_cppConfigVersion.cmake COMPATIBILITY SameMinorVersion)
41+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/python_template_cppConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/python_template_cppConfigVersion.cmake
42+
DESTINATION lib/cmake/python_template_cpp
43+
)
44+
45+
configure_file(cmake/python_template_cpp.pc.in ${CMAKE_CURRENT_BINARY_DIR}/python_template_cpp.pc @ONLY)
46+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/python_template_cpp.pc DESTINATION lib/pkgconfig)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
prefix=${pcfiledir}/../..
2+
libdir=${prefix}/lib
3+
includedir=${prefix}/include
4+
5+
Name: python_template_cpp
6+
Description: A C++-Python project template
7+
Version: @PROJECT_VERSION@
8+
Libs: -L${libdir} -lpython_template_cpp
9+
Cflags: -I${includedir}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/python_template_cppTargets.cmake")
4+
check_required_components(python_template_cpp)

‎cpp/python-template-cpp/c_api.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "python_template_cpp/python_template_cpp.h"
2+
3+
#include "python-template-cpp/example.hpp"
4+
5+
uint32_t python_template_cpp_abi_version(void) { return PYTHON_TEMPLATE_CPP_ABI_VERSION; }
6+
7+
int32_t python_template_cpp_add(int32_t i, int32_t j) { return python_template_cpp::add(i, j); }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "python-template-cpp/example.hpp"
2+
3+
namespace python_template_cpp {
4+
5+
int add(int i, int j) { return i + j; }
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
namespace python_template_cpp {
4+
5+
int add(int i, int j);
6+
7+
}

‎cpp/python-template-cpp/extension.cpp‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎cpp/python-template-cpp/extension.hpp‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)