Skip to content

Commit 5793da4

Browse files
tomeuvclaude
andcommitted
cmake: add consolidated shared library and pkg-config support (EXECUTORCH_BUILD_SHARED=ON)
When EXECUTORCH_BUILD_SHARED=ON is passed, all static libraries are built with PIC and a new executorch_shared target bundles executorch_core plus commonly-used extensions (data_loader, flat_tensor, named_data_map, module, tensor) into a single libexecutorch.so via whole-archive linking. A pkg-config file is also generated and installed for consumer discovery. When EXECUTORCH_BUILD_SHARED is OFF (the default), behavior is unchanged. Co-authored-by: Claude <noreply@anthropic.com>
1 parent cd66fec commit 5793da4

3 files changed

Lines changed: 81 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 67 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}"
@@ -160,6 +163,10 @@ announce_configured_options(BUILD_TESTING)
160163
load_build_preset()
161164
include(${PROJECT_SOURCE_DIR}/tools/cmake/preset/default.cmake)
162165

166+
if(EXECUTORCH_BUILD_SHARED)
167+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
168+
endif()
169+
163170
# Enable ccache if available
164171
find_program(CCACHE_PROGRAM ccache)
165172
if(CCACHE_PROGRAM)
@@ -1158,6 +1165,65 @@ if(EXECUTORCH_BUILD_VGF)
11581165
list(APPEND _executorch_backends vgf_backend)
11591166
endif()
11601167

1168+
# Consolidated shared library: bundles executorch_core plus commonly used
1169+
# extensions into a single libexecutorch.so.
1170+
if(EXECUTORCH_BUILD_SHARED)
1171+
# CMake requires at least one source for shared library targets.
1172+
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/executorch_shared_empty.cpp
1173+
"// intentionally empty"
1174+
)
1175+
add_library(
1176+
executorch_shared SHARED
1177+
${CMAKE_CURRENT_BINARY_DIR}/executorch_shared_empty.cpp
1178+
)
1179+
set_target_properties(
1180+
executorch_shared
1181+
PROPERTIES OUTPUT_NAME executorch
1182+
EXPORT_NAME executorch
1183+
VERSION "${PROJECT_VERSION}"
1184+
SOVERSION "${PROJECT_VERSION_MAJOR}"
1185+
LINKER_LANGUAGE CXX
1186+
)
1187+
target_include_directories(
1188+
executorch_shared PUBLIC ${_common_include_directories}
1189+
)
1190+
target_compile_definitions(
1191+
executorch_shared PUBLIC C10_USING_CUSTOM_GENERATED_MACROS
1192+
)
1193+
# Link executorch without WHOLE_ARCHIVE because its INTERFACE link options
1194+
# (from executorch_target_link_options_shared_lib) already force
1195+
# whole-archive. Link executorch_core explicitly since executorch only has a
1196+
# PRIVATE dep on it (symbols wouldn't propagate otherwise).
1197+
target_link_libraries(
1198+
executorch_shared PRIVATE executorch
1199+
$<LINK_LIBRARY:WHOLE_ARCHIVE,executorch_core>
1200+
)
1201+
foreach(_ext_target
1202+
extension_data_loader extension_flat_tensor extension_named_data_map
1203+
extension_module_static extension_tensor
1204+
)
1205+
if(TARGET ${_ext_target})
1206+
target_link_libraries(
1207+
executorch_shared PRIVATE $<LINK_LIBRARY:WHOLE_ARCHIVE,${_ext_target}>
1208+
)
1209+
endif()
1210+
endforeach()
1211+
install(
1212+
TARGETS executorch_shared
1213+
EXPORT ExecuTorchTargets
1214+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
1215+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
1216+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
1217+
)
1218+
configure_file(
1219+
tools/cmake/executorch.pc.in ${CMAKE_CURRENT_BINARY_DIR}/executorch.pc
1220+
@ONLY
1221+
)
1222+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/executorch.pc
1223+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
1224+
)
1225+
endif()
1226+
11611227
# Top-level interface targets.
11621228

11631229
# A target containing all configured backends.

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=${prefix}/@CMAKE_INSTALL_LIBDIR@
4+
includedir=${prefix}/@CMAKE_INSTALL_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 -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
@@ -218,6 +218,10 @@ define_overridable_option(
218218
EXECUTORCH_BUILD_CPUINFO "Build cpuinfo library." BOOL
219219
${_default_executorch_build_cpuinfo}
220220
)
221+
define_overridable_option(
222+
EXECUTORCH_BUILD_SHARED
223+
"Build a consolidated ExecuTorch shared library" BOOL OFF
224+
)
221225

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

0 commit comments

Comments
 (0)