Skip to content

cmake: Use any available Python_add_library command#5606

Closed
dannf wants to merge 2 commits intopybind:masterfrom
dannf:python-add-library-fixes
Closed

cmake: Use any available Python_add_library command#5606
dannf wants to merge 2 commits intopybind:masterfrom
dannf:python-add-library-fixes

Conversation

@dannf
Copy link
Copy Markdown

@dannf dannf commented Apr 8, 2025

Description

python_add_library and python3_add_library are mis-cased. These commands are capitalized.

Even when corrected, this code will fail if Python_FOUND is set, but Python_add_library() is not available, which appears to be the issue described here:

#3996

This seems to be due to a mix of FindPython and FindPython3 usage. Both Python_add_library() and Python3_add_library() support Python 3.x, so use either one.

Suggested changelog entry:

pybind11NewTools.cmake: Use available Python(3)_add_library command

python_add_library and python3_add_library are mis-cased.
These commands are capitalized.

Even when corrected, this code will fail if Python_FOUND is
set, but Python_add_library() is not available, which appears
to be the issue described here:

 pybind#3996

This seems to be due to a mix of FindPython and FindPython3 usage.
Both Python_add_library() and Python3_add_library() support
Python 3.x, so use either one.

Signed-off-by: dann frazier <dann.frazier@chainguard.dev>
@dannf dannf requested a review from henryiii as a code owner April 8, 2025 18:57
@dannf
Copy link
Copy Markdown
Author

dannf commented Apr 8, 2025

Note: I'm not a cmake expert, this just seems to WFM.

@henryiii
Copy link
Copy Markdown
Collaborator

henryiii commented Apr 8, 2025

Commands in CMake are case insensitive, only variables are case sensitive. If Python_FOUND is set, Python_add_library should always be defined. Something is wrong if Python was found but only Python3_add_library is present. And Python3_add_library will expect the Python3_* variables to be set, so I think this would just be moving the error.

@henryiii
Copy link
Copy Markdown
Collaborator

henryiii commented Apr 8, 2025

Do you have an example of code that this fixes? I'd be curious to see it.

@dannf
Copy link
Copy Markdown
Author

dannf commented Apr 8, 2025

Commands in CMake are case insensitive, only variables are case sensitive.

TIL :)

If Python_FOUND is set, Python_add_library should always be defined. Something is wrong if Python was found but only Python3_add_library is present. And Python3_add_library will expect the Python3_* variables to be set, so I think this would just be moving the error.

I see - thanks!

@henryiii
Copy link
Copy Markdown
Collaborator

henryiii commented Apr 8, 2025

Command names are case-insensitive.

https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#command-invocations

(Most commands are actually defined in upper case, actually, including this one)

@henryiii
Copy link
Copy Markdown
Collaborator

henryiii commented Apr 8, 2025

If you have an example of a broken CMakeLists.txt, I can take a look.

@dannf
Copy link
Copy Markdown
Author

dannf commented Apr 8, 2025

Do you have an example of code that this fixes? I'd be curious to see it.

Sure. Here's the entry point:
https://github.com/NVIDIA/TensorRT-LLM/blob/28fb9aacaa5a05494635194a9cbb264da9a744bd/cpp/CMakeLists.txt

I generated a cmake trace to try and understand what is going on:
trace.txt

What I noticed there is a windy passage involving mixed FindPython3 and FindPython.

@henryiii
Copy link
Copy Markdown
Collaborator

henryiii commented Apr 8, 2025

I think this line: https://github.com/NVIDIA/TensorRT-LLM/blob/28fb9aacaa5a05494635194a9cbb264da9a744bd/cpp/CMakeLists.txt#L473 or https://github.com/NVIDIA/TensorRT-LLM/blob/28fb9aacaa5a05494635194a9cbb264da9a744bd/cpp/CMakeLists.txt#L431 is finding Python3, but pybind11 probably was added before this at https://github.com/NVIDIA/TensorRT-LLM/blob/5bdf997963365ff6ff167e21ab1f53631cf274eb/cpp/CMakeLists.txt#L391, so it found Python. Moving the pybind11 inclusion to after this line would fix it, or pybind11's discovery could be used without re-finding it. As it is, the two discoveries could find different Pythons.

Also it should not be calling python3 before searching for it at https://github.com/NVIDIA/TensorRT-LLM/blob/5bdf997963365ff6ff167e21ab1f53631cf274eb/cpp/CMakeLists.txt#L156.

@dannf
Copy link
Copy Markdown
Author

dannf commented Apr 9, 2025

I think this line: https://github.com/NVIDIA/TensorRT-LLM/blob/28fb9aacaa5a05494635194a9cbb264da9a744bd/cpp/CMakeLists.txt#L473 or https://github.com/NVIDIA/TensorRT-LLM/blob/28fb9aacaa5a05494635194a9cbb264da9a744bd/cpp/CMakeLists.txt#L431 is finding Python3, but pybind11 probably was added before this at https://github.com/NVIDIA/TensorRT-LLM/blob/5bdf997963365ff6ff167e21ab1f53631cf274eb/cpp/CMakeLists.txt#L391, so it found Python. Moving the pybind11 inclusion to after this line would fix it, or pybind11's discovery could be used without re-finding it. As it is, the two discoveries could find different Pythons.

Also it should not be calling python3 before searching for it at https://github.com/NVIDIA/TensorRT-LLM/blob/5bdf997963365ff6ff167e21ab1f53631cf274eb/cpp/CMakeLists.txt#L156.

Thanks @henryiii !

@dannf dannf closed this Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants