Skip to content

Commit af1ab5f

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 6902161 commit af1ab5f

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

kernels/portable/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ 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 portable_ops_lib portable_kernels executorch_shared
112+
)
113+
endif()
114+
109115
# Build the portable custom ops AOT library for registering custom ops into
110116
# PyTorch. Requires find_package(Torch), which must be called at root scope
111117
# before this subdirectory is processed. Not targeting ARM_BAREMETAL as aot_lib

tools/cmake/Utils.cmake

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

0 commit comments

Comments
 (0)