Skip to content

Commit 593751e

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 2574e02 commit 593751e

3 files changed

Lines changed: 84 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 70 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

@@ -76,6 +75,8 @@ if("${ET_VERSION_MAJOR}" STREQUAL ""
7675
)
7776
endif()
7877

78+
project(executorch VERSION "${ET_VERSION_MAJOR}.${ET_VERSION_MINOR}.${ET_VERSION_PATCH}")
79+
7980
message(
8081
STATUS
8182
"ExecuTorch version: ${ET_VERSION_MAJOR}.${ET_VERSION_MINOR}.${ET_VERSION_PATCH}"
@@ -149,6 +150,10 @@ announce_configured_options(BUILD_TESTING)
149150
load_build_preset()
150151
include(${PROJECT_SOURCE_DIR}/tools/cmake/preset/default.cmake)
151152

153+
if(EXECUTORCH_BUILD_SHARED)
154+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
155+
endif()
156+
152157
# Enable ccache if available
153158
find_program(CCACHE_PROGRAM ccache)
154159
if(CCACHE_PROGRAM)
@@ -1135,6 +1140,70 @@ if(EXECUTORCH_BUILD_VGF)
11351140
list(APPEND _executorch_backends vgf_backend)
11361141
endif()
11371142

1143+
# Consolidated shared library: bundles executorch_core plus commonly used
1144+
# extensions into a single libexecutorch.so.
1145+
if(EXECUTORCH_BUILD_SHARED)
1146+
# CMake requires at least one source for shared library targets.
1147+
file(
1148+
WRITE ${CMAKE_CURRENT_BINARY_DIR}/executorch_shared_empty.cpp
1149+
"// intentionally empty"
1150+
)
1151+
add_library(
1152+
executorch_shared SHARED
1153+
${CMAKE_CURRENT_BINARY_DIR}/executorch_shared_empty.cpp
1154+
)
1155+
set_target_properties(
1156+
executorch_shared
1157+
PROPERTIES OUTPUT_NAME executorch
1158+
VERSION "${executorch_VERSION}"
1159+
SOVERSION "${executorch_VERSION_MAJOR}"
1160+
LINKER_LANGUAGE CXX
1161+
)
1162+
target_include_directories(
1163+
executorch_shared PUBLIC ${_common_include_directories}
1164+
)
1165+
target_compile_definitions(
1166+
executorch_shared PUBLIC C10_USING_CUSTOM_GENERATED_MACROS
1167+
)
1168+
# Link executorch without WHOLE_ARCHIVE because its INTERFACE link options
1169+
# (from executorch_target_link_options_shared_lib) already force whole-archive.
1170+
# Link executorch_core explicitly since executorch only has a PRIVATE dep on
1171+
# it (symbols wouldn't propagate otherwise).
1172+
target_link_libraries(
1173+
executorch_shared
1174+
PRIVATE executorch $<LINK_LIBRARY:WHOLE_ARCHIVE,executorch_core>
1175+
)
1176+
foreach(
1177+
_ext_target
1178+
extension_data_loader
1179+
extension_flat_tensor
1180+
extension_named_data_map
1181+
extension_module_static
1182+
extension_tensor
1183+
)
1184+
if(TARGET ${_ext_target})
1185+
target_link_libraries(
1186+
executorch_shared
1187+
PRIVATE $<LINK_LIBRARY:WHOLE_ARCHIVE,${_ext_target}>
1188+
)
1189+
endif()
1190+
endforeach()
1191+
install(
1192+
TARGETS executorch_shared
1193+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
1194+
)
1195+
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/version.txt" EXECUTORCH_VERSION)
1196+
string(STRIP "${EXECUTORCH_VERSION}" EXECUTORCH_VERSION)
1197+
configure_file(
1198+
tools/cmake/executorch.pc.in
1199+
${CMAKE_CURRENT_BINARY_DIR}/executorch.pc
1200+
@ONLY
1201+
)
1202+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/executorch.pc
1203+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
1204+
)
1205+
endif()
1206+
11381207
# Top-level interface targets.
11391208

11401209
# 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: @EXECUTORCH_VERSION@
9+
Cflags: -I${includedir} -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
@@ -217,6 +217,10 @@ define_overridable_option(
217217
EXECUTORCH_BUILD_CPUINFO "Build cpuinfo library." BOOL
218218
${_default_executorch_build_cpuinfo}
219219
)
220+
define_overridable_option(
221+
EXECUTORCH_BUILD_SHARED
222+
"Build a consolidated libexecutorch.so shared library" BOOL OFF
223+
)
220224

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

0 commit comments

Comments
 (0)