Skip to content

Commit 80072c7

Browse files
tomeuvclaudeGregoryComer
authored
Build and install shared library for consumers and backends (pytorch#18560)
### Summary These are the changes that I needed so that both my Mesa backend (tentatively named Torx), and my test suite can link to Executorch. This should be also what Linux distributions such as Debian and Fedora would need to package Executorch. There are some fixes as well for recent toolchains, whichi will be needed as well by distributions. I also install the headers for runtime/backend/, which weren't installed before, to make out-of-tree backends to build cleanly. ### Test plan Build with: ``` $ CMAKE_ARGS="-DEXECUTORCH_BUILD_SHARED=ON \ -DEXECUTORCH_BUILD_EXTENSION_LLM=OFF \ -DEXECUTORCH_BUILD_EXTENSION_LLM_RUNNER=OFF" \ pip install --break-system-packages --no-build-isolation . ``` Then run: ``` $ cmake --install pip-out/temp.linux-$(uname -m)-cpython-3*/cmake-out ``` And check that the headers and shared library (libexecutorch.so.1.3.0) were installed. cc @kimishpatel @YifanShenSZ @cymbalrush @metascroy --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Gregory Comer <gjcomer@meta.com>
1 parent 01a4554 commit 80072c7

6 files changed

Lines changed: 108 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
#
4747

4848
cmake_minimum_required(VERSION 3.24)
49-
project(executorch)
5049

5150
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
5251

@@ -87,6 +86,10 @@ if("${ET_VERSION_MAJOR}" STREQUAL ""
8786
)
8887
endif()
8988

89+
project(executorch
90+
VERSION "${ET_VERSION_MAJOR}.${ET_VERSION_MINOR}.${ET_VERSION_PATCH}"
91+
)
92+
9093
message(
9194
STATUS
9295
"ExecuTorch version: ${ET_VERSION_MAJOR}.${ET_VERSION_MINOR}.${ET_VERSION_PATCH}"
@@ -177,6 +180,10 @@ if(DEFINED EXECUTORCH_BAREMETAL_SKIP_INSTALL
177180
)
178181
endif()
179182

183+
if(EXECUTORCH_BUILD_SHARED)
184+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
185+
endif()
186+
180187
# Enable ccache if available
181188
find_program(CCACHE_PROGRAM ccache)
182189
if(CCACHE_PROGRAM)
@@ -1165,6 +1172,49 @@ if(EXECUTORCH_BUILD_KERNELS_LLM)
11651172
list(APPEND _executorch_kernels custom_ops_aot_lib)
11661173
endif()
11671174

1175+
# Consolidated shared library: bundles executorch_core plus commonly used
1176+
# extensions into a single libexecutorch.so.
1177+
if(EXECUTORCH_BUILD_SHARED)
1178+
executorch_add_shared_library(executorch_shared)
1179+
set_target_properties(
1180+
executorch_shared
1181+
PROPERTIES OUTPUT_NAME executorch
1182+
ARCHIVE_OUTPUT_NAME executorch_shared
1183+
EXPORT_NAME executorch-shared
1184+
)
1185+
target_include_directories(
1186+
executorch_shared PUBLIC ${_common_include_directories}
1187+
)
1188+
target_compile_definitions(
1189+
executorch_shared PUBLIC C10_USING_CUSTOM_GENERATED_MACROS
1190+
)
1191+
# Link executorch without WHOLE_ARCHIVE because its INTERFACE link options
1192+
# (from executorch_target_link_options_shared_lib) already force
1193+
# whole-archive. Link executorch_core explicitly since executorch only has a
1194+
# PRIVATE dep on it (symbols wouldn't propagate otherwise).
1195+
target_link_libraries(
1196+
executorch_shared PRIVATE executorch
1197+
$<LINK_LIBRARY:WHOLE_ARCHIVE,executorch_core>
1198+
)
1199+
foreach(_ext_target
1200+
extension_data_loader extension_flat_tensor extension_named_data_map
1201+
extension_module_static extension_tensor
1202+
)
1203+
if(TARGET ${_ext_target})
1204+
target_link_libraries(
1205+
executorch_shared PRIVATE $<LINK_LIBRARY:WHOLE_ARCHIVE,${_ext_target}>
1206+
)
1207+
endif()
1208+
endforeach()
1209+
configure_file(
1210+
tools/cmake/executorch.pc.in ${CMAKE_CURRENT_BINARY_DIR}/executorch.pc
1211+
@ONLY
1212+
)
1213+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/executorch.pc
1214+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
1215+
)
1216+
endif()
1217+
11681218
if(EXECUTORCH_BUILD_KERNELS_QUANTIZED)
11691219
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/quantized)
11701220
executorch_target_link_options_shared_lib(quantized_ops_lib)

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

kernels/quantized/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,10 @@ install(
157157
PUBLIC_HEADER
158158
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/kernels/quantized/
159159
)
160+
161+
if(EXECUTORCH_BUILD_SHARED)
162+
executorch_add_shared_library(
163+
executorch_quantized_ops quantized_ops_lib quantized_kernels
164+
executorch_shared
165+
)
166+
endif()

tools/cmake/Utils.cmake

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,33 @@ 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(
220+
GENERATE
221+
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${empty_source_name}"
222+
CONTENT "// intentionally empty\n"
223+
)
224+
add_library(
225+
${target_name} SHARED "${CMAKE_CURRENT_BINARY_DIR}/${empty_source_name}"
226+
)
227+
if(ARGN)
228+
target_link_libraries(${target_name} PRIVATE ${ARGN})
229+
endif()
230+
set_target_properties(
231+
${target_name}
232+
PROPERTIES VERSION "${PROJECT_VERSION}"
233+
SOVERSION "${PROJECT_VERSION_MAJOR}"
234+
LINKER_LANGUAGE CXX
235+
)
236+
install(
237+
TARGETS ${target_name}
238+
EXPORT ExecuTorchTargets
239+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
240+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
241+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
242+
)
243+
endfunction()

tools/cmake/executorch.pc.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prefix=@CMAKE_INSTALL_PREFIX@
2+
exec_prefix=${prefix}
3+
libdir=@CMAKE_INSTALL_FULL_LIBDIR@
4+
includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
5+
6+
Name: ExecuTorch
7+
Description: On-device AI framework for PyTorch models
8+
Version: @PROJECT_VERSION@
9+
Cflags: -I${includedir} -I${includedir}/executorch/runtime/core/portable_type -I${includedir}/executorch/runtime/core/portable_type/c10 -DC10_USING_CUSTOM_GENERATED_MACROS
10+
Libs: -L${libdir} -lexecutorch

tools/cmake/preset/default.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ define_overridable_option(
221221
EXECUTORCH_BUILD_CPUINFO "Build cpuinfo library." BOOL
222222
${_default_executorch_build_cpuinfo}
223223
)
224+
define_overridable_option(
225+
EXECUTORCH_BUILD_SHARED "Build a consolidated ExecuTorch shared library" BOOL
226+
OFF
227+
)
224228

225229
# Threadpool size options. At most one can be specified. Note that the default
226230
# is managed in threadpool.cpp to allow the user to specify an alternate mode

0 commit comments

Comments
 (0)