@@ -423,6 +423,12 @@ if(MAX_KERNEL_NUM)
423423 )
424424endif ()
425425
426+ # Build devtools first if needed - some backends depend on protobuf from
427+ # devtools
428+ if (EXECUTORCH_BUILD_DEVTOOLS)
429+ add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR} /devtools)
430+ endif ()
431+
426432if (EXECUTORCH_BUILD_PYBIND AND APPLE )
427433 # shared version
428434 add_library (executorch_core_shared SHARED ${_executorch_core__srcs} )
@@ -588,10 +594,6 @@ if(EXECUTORCH_BUILD_CORTEX_M)
588594 add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR} /backends/cortex_m)
589595endif ()
590596
591- if (EXECUTORCH_BUILD_DEVTOOLS)
592- add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR} /devtools)
593- endif ()
594-
595597if (EXECUTORCH_BUILD_EXTENSION_APPLE)
596598 add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR} /extension/apple )
597599 list (APPEND _executorch_extensions apple_extension)
@@ -756,6 +758,30 @@ if(EXECUTORCH_BUILD_PYBIND)
756758 add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR} /devtools)
757759 endif ()
758760
761+ # Create bundled_module target only for pybindings when bundled_program exists
762+ # This target has hard dependencies on devtools generated headers
763+ if (TARGET bundled_program)
764+ add_library (
765+ bundled_module STATIC
766+ ${CMAKE_CURRENT_SOURCE_DIR} /extension/module/bundled_module.cpp
767+ )
768+
769+ # Ensure bundled_module waits for bundled_program's generated headers
770+ add_dependencies (bundled_module bundled_program)
771+
772+ target_link_libraries (bundled_module PRIVATE extension_data_loader)
773+ target_link_libraries (
774+ bundled_module PUBLIC extension_module_static bundled_program
775+ )
776+
777+ target_include_directories (
778+ bundled_module PUBLIC ${_common_include_directories}
779+ )
780+ target_compile_options (
781+ bundled_module PUBLIC -Wno-deprecated-declarations -fPIC
782+ )
783+ endif ()
784+
759785 # find pytorch lib, to allow pybind to take at::Tensor as input/output
760786 find_package_torch()
761787 find_library (
@@ -773,6 +799,16 @@ if(EXECUTORCH_BUILD_PYBIND)
773799 torch
774800 )
775801
802+ if (EXECUTORCH_BUILD_EXTENSION_MODULE)
803+ # Always use static linking for pybindings to avoid runtime symbol
804+ # resolution issues
805+ list (APPEND _dep_libs extension_module_static)
806+ # Add bundled_module if available
807+ if (TARGET bundled_module)
808+ list (APPEND _dep_libs bundled_module)
809+ endif ()
810+ endif ()
811+
776812 if (EXECUTORCH_BUILD_TESTS)
777813 list (APPEND _dep_libs test_backend_compiler_lib)
778814 endif ()
@@ -830,8 +866,18 @@ if(EXECUTORCH_BUILD_PYBIND)
830866 target_compile_definitions (
831867 portable_lib PUBLIC EXECUTORCH_PYTHON_MODULE_NAME=_portable_lib
832868 )
869+ # For portable_lib, do not define USE_ATEN_LIB (portable mode) This ensures it
870+ # uses the correct bundled_module namespace
833871 target_include_directories (portable_lib PRIVATE ${TORCH_INCLUDE_DIRS} )
834872 target_compile_options (portable_lib PUBLIC ${_pybind_compile_options} )
873+
874+ # Ensure bundled_module is linked to portable_lib since it uses bundled_module
875+ # functionality when USE_ATEN_LIB is not defined (which is the case for
876+ # portable_lib)
877+ if (TARGET bundled_module)
878+ list (APPEND _dep_libs bundled_module)
879+ endif ()
880+
835881 target_link_libraries (portable_lib PRIVATE ${_dep_libs} )
836882
837883 install (
0 commit comments