diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f44b650aa1..58956e6f550 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,7 +46,6 @@ # cmake_minimum_required(VERSION 3.24) -project(executorch) set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}) @@ -87,6 +86,10 @@ if("${ET_VERSION_MAJOR}" STREQUAL "" ) endif() +project(executorch + VERSION "${ET_VERSION_MAJOR}.${ET_VERSION_MINOR}.${ET_VERSION_PATCH}" +) + message( STATUS "ExecuTorch version: ${ET_VERSION_MAJOR}.${ET_VERSION_MINOR}.${ET_VERSION_PATCH}" @@ -160,6 +163,10 @@ announce_configured_options(BUILD_TESTING) load_build_preset() include(${PROJECT_SOURCE_DIR}/tools/cmake/preset/default.cmake) +if(EXECUTORCH_BUILD_SHARED) + set(CMAKE_POSITION_INDEPENDENT_CODE ON) +endif() + # Enable ccache if available find_program(CCACHE_PROGRAM ccache) if(CCACHE_PROGRAM) @@ -1158,6 +1165,48 @@ if(EXECUTORCH_BUILD_VGF) list(APPEND _executorch_backends vgf_backend) endif() +# Consolidated shared library: bundles executorch_core plus commonly used +# extensions into a single libexecutorch.so. +if(EXECUTORCH_BUILD_SHARED) + executorch_add_shared_library(executorch_shared) + set_target_properties( + executorch_shared + PROPERTIES OUTPUT_NAME executorch + EXPORT_NAME executorch + ) + target_include_directories( + executorch_shared PUBLIC ${_common_include_directories} + ) + target_compile_definitions( + executorch_shared PUBLIC C10_USING_CUSTOM_GENERATED_MACROS + ) + # Link executorch without WHOLE_ARCHIVE because its INTERFACE link options + # (from executorch_target_link_options_shared_lib) already force + # whole-archive. Link executorch_core explicitly since executorch only has a + # PRIVATE dep on it (symbols wouldn't propagate otherwise). + target_link_libraries( + executorch_shared PRIVATE executorch + $ + ) + foreach(_ext_target + extension_data_loader extension_flat_tensor extension_named_data_map + extension_module_static extension_tensor + ) + if(TARGET ${_ext_target}) + target_link_libraries( + executorch_shared PRIVATE $ + ) + endif() + endforeach() + configure_file( + tools/cmake/executorch.pc.in ${CMAKE_CURRENT_BINARY_DIR}/executorch.pc + @ONLY + ) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/executorch.pc + DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig + ) +endif() + # Top-level interface targets. # A target containing all configured backends. diff --git a/backends/apple/coreml/runtime/inmemoryfs/inmemory_filesystem.hpp b/backends/apple/coreml/runtime/inmemoryfs/inmemory_filesystem.hpp index d0ace1a5250..4ba4a3c588c 100644 --- a/backends/apple/coreml/runtime/inmemoryfs/inmemory_filesystem.hpp +++ b/backends/apple/coreml/runtime/inmemoryfs/inmemory_filesystem.hpp @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include diff --git a/backends/apple/coreml/runtime/inmemoryfs/memory_buffer.hpp b/backends/apple/coreml/runtime/inmemoryfs/memory_buffer.hpp index 5243401e2df..4b28e4062a2 100644 --- a/backends/apple/coreml/runtime/inmemoryfs/memory_buffer.hpp +++ b/backends/apple/coreml/runtime/inmemoryfs/memory_buffer.hpp @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include diff --git a/kernels/portable/CMakeLists.txt b/kernels/portable/CMakeLists.txt index 245df1c1735..b1beb25cb92 100644 --- a/kernels/portable/CMakeLists.txt +++ b/kernels/portable/CMakeLists.txt @@ -106,6 +106,12 @@ install( DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/kernels/portable/ ) +if(EXECUTORCH_BUILD_SHARED) + executorch_add_shared_library( + executorch_portable_ops portable_ops_lib portable_kernels executorch_shared + ) +endif() + # Build the portable custom ops AOT library for registering custom ops into # PyTorch. Requires find_package(Torch), which must be called at root scope # before this subdirectory is processed. Not targeting ARM_BAREMETAL as aot_lib diff --git a/kernels/quantized/CMakeLists.txt b/kernels/quantized/CMakeLists.txt index dafb7e99612..0a6dd63f792 100644 --- a/kernels/quantized/CMakeLists.txt +++ b/kernels/quantized/CMakeLists.txt @@ -157,3 +157,10 @@ install( PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/kernels/quantized/ ) + +if(EXECUTORCH_BUILD_SHARED) + executorch_add_shared_library( + executorch_quantized_ops quantized_ops_lib quantized_kernels + executorch_shared + ) +endif() diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt index 93ce08bdc7d..23c3278c39c 100644 --- a/third-party/CMakeLists.txt +++ b/third-party/CMakeLists.txt @@ -91,6 +91,7 @@ ExternalProject_Add( -DFLATCC_TEST=OFF -DFLATCC_REFLECTION=OFF -DFLATCC_DEBUG_CLANG_SANITIZE=OFF + -DFLATCC_ALLOW_WERROR=OFF -DFLATCC_INSTALL=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 -DCMAKE_INSTALL_PREFIX:PATH= @@ -100,7 +101,7 @@ ExternalProject_Add( -DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=${CMAKE_OSX_DEPLOYMENT_TARGET} ${_flatcc_extra_cmake_args} BUILD_BYPRODUCTS /bin/flatcc - {_executorch_external_project_additional_args} + ${_executorch_external_project_additional_args} ) file(REMOVE_RECURSE ${PROJECT_SOURCE_DIR}/third-party/flatcc/lib) ExternalProject_Get_Property(flatcc_ep INSTALL_DIR) @@ -117,6 +118,7 @@ set(FLATCC_TEST OFF CACHE BOOL "") set(FLATCC_REFLECTION OFF CACHE BOOL "") set(FLATCC_DEBUG_CLANG_SANITIZE OFF CACHE BOOL "") set(FLATCC_INSTALL OFF CACHE BOOL "") +set(FLATCC_ALLOW_WERROR OFF CACHE BOOL "" FORCE) add_subdirectory(flatcc) # Unfortunately flatcc writes libs directly in to the source tree [1]. So to # ensure the target lib is created last, force flatcc_cli to build first. diff --git a/tools/cmake/Utils.cmake b/tools/cmake/Utils.cmake index 3295036663c..379b3388a8e 100644 --- a/tools/cmake/Utils.cmake +++ b/tools/cmake/Utils.cmake @@ -211,3 +211,31 @@ function(executorch_target_copy_mlx_metallib target) endif() endif() endfunction() + +# Create and install a shared library composed from dependency libraries. The +# target links the provided dependencies and carries VERSION/SOVERSION. +function(executorch_add_shared_library target_name) + set(_empty_source_name "${target_name}_empty.cpp") + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${_empty_source_name}" + "// intentionally empty" + ) + add_library( + ${target_name} SHARED "${CMAKE_CURRENT_BINARY_DIR}/${_empty_source_name}" + ) + if(ARGN) + target_link_libraries(${target_name} PRIVATE ${ARGN}) + endif() + set_target_properties( + ${target_name} + PROPERTIES VERSION "${PROJECT_VERSION}" + SOVERSION "${PROJECT_VERSION_MAJOR}" + LINKER_LANGUAGE CXX + ) + install( + TARGETS ${target_name} + EXPORT ExecuTorchTargets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + ) +endfunction() diff --git a/tools/cmake/executorch.pc.in b/tools/cmake/executorch.pc.in new file mode 100644 index 00000000000..e1d47afb405 --- /dev/null +++ b/tools/cmake/executorch.pc.in @@ -0,0 +1,10 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +libdir=@CMAKE_INSTALL_FULL_LIBDIR@ +includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@ + +Name: ExecuTorch +Description: On-device AI framework for PyTorch models +Version: @PROJECT_VERSION@ +Cflags: -I${includedir} -I${includedir}/executorch/runtime/core/portable_type -I${includedir}/executorch/runtime/core/portable_type/c10 -DC10_USING_CUSTOM_GENERATED_MACROS +Libs: -L${libdir} -lexecutorch diff --git a/tools/cmake/preset/default.cmake b/tools/cmake/preset/default.cmake index 423194776bc..f5bbe2d7bf2 100644 --- a/tools/cmake/preset/default.cmake +++ b/tools/cmake/preset/default.cmake @@ -218,6 +218,10 @@ define_overridable_option( EXECUTORCH_BUILD_CPUINFO "Build cpuinfo library." BOOL ${_default_executorch_build_cpuinfo} ) +define_overridable_option( + EXECUTORCH_BUILD_SHARED "Build a consolidated ExecuTorch shared library" BOOL + OFF +) # Threadpool size options. At most one can be specified. Note that the default # is managed in threadpool.cpp to allow the user to specify an alternate mode