Skip to content

Commit 5221d89

Browse files
committed
cmake: build shared libexecutorch_portable_ops when EXECUTORCH_BUILD_SHARED=ON
Create a single libexecutorch_portable_ops.so that bundles portable_ops_lib and portable_kernels, allowing consumers to link portable op implementations via a shared library. portable_ops_lib already has --whole-archive as an INTERFACE link option from Codegen.cmake, so we do not wrap it in $<LINK_LIBRARY:WHOLE_ARCHIVE,...> to avoid pulling the registration constructor in twice. Link against executorch_shared (not executorch_core) to share a single operator registry.
1 parent 582abb5 commit 5221d89

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

kernels/portable/CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,38 @@ install(
106106
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/kernels/portable/
107107
)
108108

109+
# When building shared libraries, create a single
110+
# libexecutorch_portable_ops.so that bundles portable_ops_lib and
111+
# portable_kernels so consumers can link a shared library instead of
112+
# needing --whole-archive on the static archives.
113+
if(EXECUTORCH_BUILD_SHARED)
114+
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/portable_ops_shared_empty.cpp
115+
"// intentionally empty"
116+
)
117+
add_library(
118+
executorch_portable_ops SHARED
119+
${CMAKE_CURRENT_BINARY_DIR}/portable_ops_shared_empty.cpp
120+
)
121+
# portable_ops_lib already has --whole-archive as an INTERFACE link option
122+
# (from executorch_target_link_options_shared_lib in Codegen.cmake), so do
123+
# NOT wrap it in $<LINK_LIBRARY:WHOLE_ARCHIVE,...> to avoid pulling the
124+
# registration constructor in twice.
125+
target_link_libraries(
126+
executorch_portable_ops
127+
PRIVATE portable_ops_lib
128+
portable_kernels
129+
executorch_shared
130+
)
131+
set_target_properties(
132+
executorch_portable_ops PROPERTIES LINKER_LANGUAGE CXX
133+
)
134+
install(
135+
TARGETS executorch_portable_ops
136+
EXPORT ExecuTorchTargets
137+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
138+
)
139+
endif()
140+
109141
# Build the portable custom ops AOT library for registering custom ops into
110142
# PyTorch. Requires find_package(Torch), which must be called at root scope
111143
# before this subdirectory is processed. Not targeting ARM_BAREMETAL as aot_lib

0 commit comments

Comments
 (0)