Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ if(NOT PYTHON_EXECUTABLE)
endif()
announce_configured_options(PYTHON_EXECUTABLE)

# pybind11 >=3.0 uses find_package(Python) which looks for Python_EXECUTABLE,
# not the legacy PYTHON_EXECUTABLE variable. Bridge the two so pybind11 picks up
# the interpreter that setup.py (or the user) specified.
if(PYTHON_EXECUTABLE AND NOT Python_EXECUTABLE)
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}")
endif()

announce_configured_options(CMAKE_CXX_COMPILER_ID)
announce_configured_options(CMAKE_TOOLCHAIN_FILE)
announce_configured_options(BUILD_TESTING)
Expand Down Expand Up @@ -260,6 +267,27 @@ endif()

add_subdirectory(third-party)

# On Apple, pybind11 SHARED extension modules must not link libpython directly.
# When python_add_library creates a SHARED library, it links Python::Python (via
# pybind11::embed) which pulls in libpython. This conflicts with pybind11
# >=3.0's multi-phase module init — the duplicate Python runtime causes a GIL
# state crash at import time. This function replaces pybind11::embed with
# pybind11::module (which links Python::Module instead of Python::Python —
# headers and ABI only, no libpython) and adds -undefined dynamic_lookup for
# symbol resolution. No-op on non-Apple platforms.
function(strip_python_lib target)
if(NOT APPLE)
return()
endif()
get_target_property(_libs ${target} LINK_LIBRARIES)
if(_libs)
list(REMOVE_ITEM _libs Python::Python pybind11::embed)
list(APPEND _libs pybind11::module)
set_target_properties(${target} PROPERTIES LINK_LIBRARIES "${_libs}")
endif()
target_link_options(${target} PRIVATE "LINKER:-undefined,dynamic_lookup")
endfunction()

# Size-optimized builds disable exceptions, RTTI, and unwind tables.
# ExecuTorch's runtime uses Result<T>/Error instead of exceptions, so
# -fno-exceptions is safe. Suppressing .eh_frame via -fno-unwind-tables is safe
Expand Down Expand Up @@ -1114,6 +1142,7 @@ if(EXECUTORCH_BUILD_PYBIND)

# pybind portable_lib
pybind11_add_module(portable_lib SHARED extension/pybindings/pybindings.cpp)
strip_python_lib(portable_lib)
# The actual output file needs a leading underscore so it can coexist with
# portable_lib.py in the same python package. PyTorch requires C++20, so
# pybindings must be compiled with C++20.
Expand Down Expand Up @@ -1155,6 +1184,7 @@ if(EXECUTORCH_BUILD_PYBIND)
pybind11_add_module(
data_loader SHARED extension/pybindings/pybindings_data_loader.cpp
)
strip_python_lib(data_loader)
target_include_directories(data_loader PRIVATE ${_common_include_directories})
target_compile_options(data_loader PUBLIC ${_pybind_compile_options})
target_link_libraries(data_loader PRIVATE executorch)
Expand Down
1 change: 1 addition & 0 deletions backends/apple/coreml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ if(EXECUTORCH_BUILD_PYBIND)
executorchcoreml SHARED runtime/inmemoryfs/inmemory_filesystem_py.cpp
runtime/inmemoryfs/inmemory_filesystem_utils.cpp
)
strip_python_lib(executorchcoreml)
target_link_libraries(
executorchcoreml PRIVATE coreml_util coreml_inmemoryfs
nlohmann_json::nlohmann_json
Expand Down
1 change: 1 addition & 0 deletions codegen/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# Create the selective_build pybind11 module
pybind11_add_module(selective_build SHARED selective_build.cpp)
strip_python_lib(selective_build)

# Set the output name to match the module name
set_target_properties(selective_build PROPERTIES OUTPUT_NAME "selective_build")
Expand Down
1 change: 1 addition & 0 deletions extension/llm/runner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ if(EXECUTORCH_BUILD_PYBIND)
pybind11_add_module(
_llm_runner SHARED ${CMAKE_CURRENT_SOURCE_DIR}/pybindings.cpp
)
strip_python_lib(_llm_runner)

find_package_torch()
find_library(
Expand Down
1 change: 1 addition & 0 deletions extension/training/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ if(EXECUTORCH_BUILD_PYBIND)
_training_lib SHARED
${CMAKE_CURRENT_SOURCE_DIR}/pybindings/_training_lib.cpp
)
strip_python_lib(_training_lib)
set_target_properties(_training_lib PROPERTIES CXX_STANDARD 20)

target_include_directories(_training_lib PRIVATE ${TORCH_INCLUDE_DIRS})
Expand Down
2 changes: 1 addition & 1 deletion third-party/pybind11
Submodule pybind11 updated 290 files
Loading