Skip to content

Commit 6bcd2b4

Browse files
tomeuvclaude
andcommitted
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. Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5793da4 commit 6bcd2b4

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

kernels/portable/CMakeLists.txt

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

109+
if(EXECUTORCH_BUILD_SHARED)
110+
executorch_add_shared_library(
111+
executorch_portable_ops
112+
portable_ops_lib
113+
portable_kernels
114+
executorch_shared
115+
)
116+
endif()
117+
109118
# Build the portable custom ops AOT library for registering custom ops into
110119
# PyTorch. Requires find_package(Torch), which must be called at root scope
111120
# before this subdirectory is processed. Not targeting ARM_BAREMETAL as aot_lib

tools/cmake/Utils.cmake

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,29 @@ function(executorch_target_copy_mlx_metallib target)
211211
endif()
212212
endif()
213213
endfunction()
214+
215+
# Create and install a shared library composed from dependency
216+
# libraries. The target links the provided dependencies and carries
217+
# VERSION/SOVERSION.
218+
function(executorch_add_shared_library target_name)
219+
set(_empty_source_name "${target_name}_empty.cpp")
220+
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${_empty_source_name}
221+
"// intentionally empty"
222+
)
223+
add_library(
224+
${target_name} SHARED
225+
${CMAKE_CURRENT_BINARY_DIR}/${_empty_source_name}
226+
)
227+
target_link_libraries(${target_name} PRIVATE ${ARGN})
228+
set_target_properties(
229+
${target_name}
230+
PROPERTIES VERSION "${PROJECT_VERSION}"
231+
SOVERSION "${PROJECT_VERSION_MAJOR}"
232+
LINKER_LANGUAGE CXX
233+
)
234+
install(
235+
TARGETS ${target_name}
236+
EXPORT ExecuTorchTargets
237+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
238+
)
239+
endfunction()

0 commit comments

Comments
 (0)