joshua-auchincloss / hatch-cython

cython hooks for hatch
MIT License
27 stars 5 forks source link

External cpp files will be deleted when compilation failed #27

Closed ChingChuan-Chen closed 9 months ago

ChingChuan-Chen commented 11 months ago

My package is like below:

$ tree .
.
├── poc
│   ├── __init__.py
│   ├── cpp_helper.cpp
│   ├── cpp_helper.h
│   └── do_stuff.pyx
├── README.rst
├── MANIFEST.in
└── pyproject.toml

The cpp_helper.cpp will be deleted if my code failed to compile. It looks like that it clearn up every cpp and c files. Coud please you help fix it?

joshua-auchincloss commented 11 months ago

Apologies for the delayed reponse - you should be able to exclude the file from any actions (i.e preserve all cpp artifacts) if you do something like the following:

# hatch.toml

[build.hooks.cython.options]
# these might be NRQ
env = [{ env = "CPPFLAGS", arg = "-I./poc", platforms = "*", arch = "*" }]
extra_link_args = [{ arg = "-L./poc", platforms = "*", arch = "*" }]

[build.hooks.cython.options.files]
# this would be required
exclude = [{ matches = "*.cpp" }, { matches = "*/*.cpp" }]

You would then want to cimport the cpp_helper.h in your do_stuff.pyx. Let me know if this works and I'll tag / add an example appropriately :)

joshua-auchincloss commented 9 months ago

Added an example into src_structure to highlight using c/c++ helpers without the above workaround. Recommend to split into two source directories and utilize a custom include path to let cython resolve the path.