-
Notifications
You must be signed in to change notification settings - Fork 198
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow stubgen recursively from CMake #463
base: master
Are you sure you want to change the base?
Allow stubgen recursively from CMake #463
Conversation
As always, pypy3.9 decides to randomly fail during test ¯\(ツ)/¯ Looks like pypy has newer release (v7.3.15), perhaps updating it in |
Good idea, I will try 🤞 Regarding the PR: |
But what if user specify both |
That combination doesn't make sense. And output file with recursive also doesn't make sense. (Indeed, stubgen also has some checks for this inside). I was just referring to a part of the text in the docs you wrote that seemed to suggest that RECURSIVE requires OUTPUT_DIR. |
Please check! |
I'm still pondering the best API to expose stub generation in Python. For example, I think that there is a way of getting nanobind_add_stub(
...
MODULE my_ext
OUTPUT_FILE my_ext/__init__.pyi
) and nanobind_add_stub(
...
MODULE my_ext
RECURSIVE
OUTPUT_FILES my_ext/__init__.pyi my_ext/submodule/__init__.pyi
) If Once things are tracked at that level of granularity, here is nothing against allowing more modules as input even in non-recursive mode, e.g.: nanobind_add_stub(
...
MODULES my_ext_1 my_ext_2
OUTPUT_FILES my_ext_1/__init__.pyi my_ext_2/__init__.pyi
) |
You have mentioned that...
Do you have any examples of those projects? I am not sure how those kind of project work, but maybe calling a python script from |
56d7e93
to
e80edb1
Compare
In my opinion,
is less clean than nanobind_add_stub(
...
MODULES my_ext my_ext.submodule
) If we cannot figure out how to find out what output files need to be generated for non-INSTALL_TIME, I think it is better for us to not implement set(OUTPUT_FILES "")
foreach(M ARG_MODULES)
string(REPLACE "." "/" M_NEW1)
string(CONCAT ${M_NEW1} "/__init__.pyi" M_NEW2)
set(OUTPUT_FILES "${OUTPUT_FILES} ${M_NEW2}")
endforeach() |
There is a problem I see with the loop you posted:
In a submodule You were interested in knowing about a project that creates a build-time importable library. See the |
Two ideas. Idea 1I think we actually don't need to know the stubgen output files path in order to let CMake only regenerate stub files if source was changed. From add_custom_command(
OUTPUT ${NB_STUBGEN_OUTPUTS}
COMMAND ${NB_STUBGEN_CMD}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
DEPENDS ${ARG_DEPENDS} "${NB_STUBGEN}" "${ARG_PATTERN_FILE}"
${NB_STUBGEN_EXTRA}
)
add_custom_target(${name} ALL DEPENDS ${NB_STUBGEN_OUTPUTS}) What actually determines if This means this should work: add_custom_command(
OUTPUT "whatever_file.txt"
COMMAND ${NB_STUBGEN_CMD} # stubgen.py also creates whatever_file.txt
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
DEPENDS ${ARG_DEPENDS} "${NB_STUBGEN}" "${ARG_PATTERN_FILE}"
${NB_STUBGEN_EXTRA}
)
add_custom_target(${name} ALL DEPENDS "whatever_file.txt") Or even better (Less confident about this though): add_custom_command(
OUTPUT whatever
COMMAND ${NB_STUBGEN_CMD}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
DEPENDS ${ARG_DEPENDS} "${NB_STUBGEN}" "${ARG_PATTERN_FILE}"
${NB_STUBGEN_EXTRA}
)
set_source_files_properties(whatever PROPERTIES SYMBOLIC true)
add_custom_target(${name} ALL DEPENDS whatever) CMake should not panic if the command generates more file than expected, and probably won't delete those extra files? Idea 2 (EDIT: This should NOT work)If above does not work, this shall work: I searched "cmake add_custom_command with unknown output name" in google, found:
What if we:
Yes, this means we technically need to run |
The first idea worked! Now it should be possible to recursively generate stub even for non-install time, and only generated if the source was changed. I have made a new commit. To test, clone my fork, change Lines 70 to 76 in df8996a
To the following: nanobind_add_stub(
py_stub
MODULE py_stub_test
# OUTPUT ${PYI_PREFIX}py_stub_test.pyi
OUTPUT_DIR ${PYI_PREFIX}
RECURSIVE
VERBOSE
PYTHON_PATH $<TARGET_FILE_DIR:test_stl_ext>
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/py_stub_test.py
) Now cmake build nanobind with test. Only catch for this method is if Note: workflow should be successful if change |
Interesting, I guess that makes sense and is reminiscent of dummy timestamp files used in |
One way is to use If there is no way we can mark the |
This article provides a semi-working solution: https://discourse.cmake.org/t/how-to-cleanup-random-byproducts/3154 Please try the latest commit. Recursively generated files are cleaned reliably (Ninja) or from second time onwards (Unix Makefile) However, note that Given that using |
After some thought, seems like the second method of calling Seems like the first idea is the best we can do... |
dce91b4
to
183f039
Compare
c30294a
to
af57451
Compare
d7117a4
to
983d6c0
Compare
Hi @laggykiller, sorry about leaving this dormant for a long time. This PR went through a few different phases and I was wondering:
|
Currently this PR allow The downsides are:
Dummy files only used for non-INSTALL_TIME.
Yes it is part of the PR (5982f36)
Yes, if user uses
I forgot to remove that sentence in the PR, fixed. I was very busy and many things happened after my last comment here, so I may not remember all the details about the PR. |
f9e5e0b
to
30e96b7
Compare
f3e2796
to
bff96e2
Compare
What is the status for this feature? This would be very useful for me and I hope you can find time to add it soon. |
No description provided.