Repository navigation
Add a C ABI library option to the cpp template - #220
Merged
Merged
Conversation
add_c_abi now also applies to the cpp template. It splits the sources into cpp/<name>, which builds lib<module> with an extern "C" surface over the C++ code, and cpp/python, the pybind11 extension. hatch-cpp builds the library as a generic entry under <module>/lib, checks its exports against the header, and links the extension to it through depends. The header is checked in under <module>/include/<module> so editable installs and wheels find it in the same place. The package exposes get_prefix, get_include, get_library_dirs, and get_libraries, and adds its lib directory to the Windows DLL search path. Wheel repair keeps the bundled library, and Pyodide builds compile the library sources into the extension. cpp/CMakeLists.txt builds and installs the library without Python, with an exported target, a relocatable package config, and a pkg-config file. The tests compile a C program against the installed package and build a find_package consumer against the CMake install. A cpp-capi example and gen/test targets exercise the option in CI. Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
5 of 11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
add_c_abinow also applies to thecpptemplate. With it, the generated project has:cpp/<name>/: C++ code plusc_api.cpp, built by hatch-cpp as thegenericlibrary<module>/lib/lib<module>with hidden visibility. hatch-cpp checks its exports against the header and the<MODULE>_ABI_VERSIONstring.<module>/include/<module>/<module>.h: the C header with the export macro. It is checked in inside the package, because hatch-cpp does not copy headers and an editable install has to find it in the same place as a wheel does.cpp/python/extension.cpp: the pybind11 extension, linked against the library throughdepends.cpp/CMakeLists.txt: builds and installs the library without Python, with an exported target, a relocatable package config, a pkg-config file, an@rpathinstall name, andVERSION/SOVERSION.bump-my-versionupdates its version.get_prefix,get_include,get_library_dirs, andget_librariesin the package, andos.add_dll_directoryfor the bundledlibon Windows.auditwheelanddelocaterepair commands that leave the bundled library in place, andHATCH_CPP_LIBRARY_SOURCE=directfor Pyodide.find_packageconsumer against the CMake install.The option requires
hatch-cpp>=0.6.0. Withoutadd_c_abithe generated project is unchanged apart from the new answer in.copier-answers.yaml.Checked on macOS with the
cpp-capiexample:make lint,make lint-docs,make checks, andmake testpass. A wheel repaired with thedelocatecommand passes the tests in a clean environment, and the package builds and imports withHATCH_CPP_LIBRARY_SOURCEset toexternal(against the CMake install) anddirect. The Windows and Pyodide parts have not been run.Type of Change
Checklist
make lint)make test)