1# Copyright (c) Meta Platforms, Inc. and affiliates. 2# All rights reserved. 3# 4# This source code is licensed under the BSD-style license found in the 5# LICENSE file in the root directory of this source tree. 6 7# 8# Simple CMake build system for runtime components. 9# 10# ### One-time setup ### 11# 12# Configure the CMake build system. It's good practice to do this whenever 13# cloning or pulling the upstream repo. Once this is done, you don't need to do 14# it again until you pull from the upstream repo again. 15# 16# NOTE: Build options can be configured by passing arguments to cmake. For 17# example, to enable the EXECUTORCH_BUILD_XNNPACK option, change the cmake 18# command to 'cmake -DEXECUTORCH_BUILD_XNNPACK=ON ..'. 19#[[ 20 (rm -rf cmake-out \ 21 && mkdir cmake-out \ 22 && cd cmake-out \ 23 && cmake ..) 24]] 25# 26# ### Build ### 27# 28# NOTE: The `-j` argument specifies how many jobs/processes to use when 29# building, and tends to speed up the build significantly. It's typical to use 30# "core count + 1" as the `-j` value. 31# ~~~ 32# cmake --build cmake-out -j9 33# ~~~ 34# 35# ### Editing this file ### 36# 37# This file should be formatted with 38# ~~~ 39# cmake-format -i CMakeLists.txt 40# ~~~ 41# It should also be cmake-lint clean. 42# 43 44cmake_minimum_required(VERSION 3.19) 45project(executorch) 46include(build/Utils.cmake) 47include(CMakeDependentOption) 48 49set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 50 51if(NOT CMAKE_CXX_STANDARD) 52 set(CMAKE_CXX_STANDARD 17) 53endif() 54 55if(NOT CMAKE_BUILD_TYPE) 56 set(CMAKE_BUILD_TYPE Debug) 57endif() 58 59# ------------------------------ OPTIONS ------------------------------------- 60# WARNING: Please don't add example specific options in this CMakeLists.txt. 61# Instead please use `find_package(executorch REQUIRED)` in the example 62# directory and add a new executable in the example `CMakeLists.txt`. 63 64# _default_release_disabled_options: default value for options that should be 65# disabled in Release mode by default. Users can still manually enable them, 66# though. 67if(CMAKE_BUILD_TYPE STREQUAL "Release") 68 set(_default_release_disabled_options OFF) 69else() 70 set(_default_release_disabled_options ON) 71endif() 72 73# Let users override which PAL defaults to use. 74# 75# TODO(dbort): Add another option that lets users point to a specific source 76# file; if set, would override the default option. 77set(EXECUTORCH_PAL_DEFAULT 78 "posix" 79 CACHE STRING 80 "Which PAL default implementation to use: one of {posix, minimal}" 81) 82 83option(EXECUTORCH_ENABLE_LOGGING "Build with ET_LOG_ENABLED" 84 ${_default_release_disabled_options} 85) 86if(NOT EXECUTORCH_ENABLE_LOGGING) 87 # Avoid pulling in the logging strings, which can be large. Note that this 88 # will set the compiler flag for all targets in this directory, and for all 89 # subdirectories included after this point. 90 add_definitions(-DET_LOG_ENABLED=0) 91endif() 92 93# Configure log level. Must be one of debug, info, error, fatal. 94set(EXECUTORCH_LOG_LEVEL 95 "Info" 96 CACHE STRING "Build with the given ET_MIN_LOG_LEVEL value" 97) 98string(TOLOWER "${EXECUTORCH_LOG_LEVEL}" LOG_LEVEL_LOWER) 99if(LOG_LEVEL_LOWER STREQUAL "debug") 100 add_definitions(-DET_MIN_LOG_LEVEL=Debug) 101elseif(LOG_LEVEL_LOWER STREQUAL "info") 102 add_definitions(-DET_MIN_LOG_LEVEL=Info) 103elseif(LOG_LEVEL_LOWER STREQUAL "error") 104 add_definitions(-DET_MIN_LOG_LEVEL=Error) 105elseif(LOG_LEVEL_LOWER STREQUAL "fatal") 106 add_definitions(-DET_MIN_LOG_LEVEL=Fatal) 107else() 108 message( 109 SEND_ERROR 110 "Unknown log level \"${EXECUTORCH_LOG_LEVEL}\". Expected one of Debug, " 111 + "Info, Error, or Fatal." 112 ) 113endif() 114 115option(EXECUTORCH_ENABLE_PROGRAM_VERIFICATION 116 "Build with ET_ENABLE_PROGRAM_VERIFICATION" 117 ${_default_release_disabled_options} 118) 119if(NOT EXECUTORCH_ENABLE_PROGRAM_VERIFICATION) 120 # Avoid pulling in the flatbuffer data verification logic, which can add about 121 # 20kB. Note that this will set the compiler flag for all targets in this 122 # directory, and for all subdirectories included after this point. 123 add_definitions(-DET_ENABLE_PROGRAM_VERIFICATION=0) 124endif() 125 126option(EXECUTORCH_ENABLE_EVENT_TRACER "Build with ET_EVENT_TRACER_ENABLED=ON" 127 OFF 128) 129if(EXECUTORCH_ENABLE_EVENT_TRACER) 130 add_definitions(-DET_EVENT_TRACER_ENABLED) 131endif() 132 133option(EXECUTORCH_DO_NOT_USE_CXX11_ABI "Define _GLIBCXX_USE_CXX11_ABI=0 if ON" 134 OFF 135) 136if(EXECUTORCH_DO_NOT_USE_CXX11_ABI) 137 add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) 138endif() 139# -ffunction-sections -fdata-sections: breaks function and data into sections so 140# they can be properly gc'd. -s: strip symbol. -fno-exceptions -fno-rtti: 141# disables exceptions and runtime type. 142set(CMAKE_CXX_FLAGS_RELEASE 143 "-ffunction-sections -fdata-sections -fno-exceptions -fno-rtti" 144) 145if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 146 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s") 147endif() 148 149option(OPTIMIZE_SIZE "Build executorch runtime optimizing for binary size" OFF) 150if(OPTIMIZE_SIZE) 151 # -Os: Optimize for size 152 set(CMAKE_CXX_FLAGS_RELEASE "-Os ${CMAKE_CXX_FLAGS_RELEASE}") 153else() 154 # -O2: Moderate opt. 155 set(CMAKE_CXX_FLAGS_RELEASE "-O2 ${CMAKE_CXX_FLAGS_RELEASE}") 156endif() 157 158set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") 159 160option(EXECUTORCH_BUILD_ANDROID_JNI "Build Android JNI" OFF) 161 162option(EXECUTORCH_BUILD_ARM_BAREMETAL 163 "Build the Arm Baremetal flow for Cortex-M and Ethos-U" OFF 164) 165 166option(EXECUTORCH_BUILD_COREML "Build the Core ML backend" OFF) 167 168option(EXECUTORCH_BUILD_KERNELS_CUSTOM "Build the custom kernels" OFF) 169 170option(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT "Build the custom ops lib for AOT" 171 OFF 172) 173 174option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER "Build the Data Loader extension" 175 OFF 176) 177 178option(EXECUTORCH_BUILD_EXTENSION_MODULE "Build the Module extension" OFF) 179 180option(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL "Build the Runner Util extension" 181 OFF 182) 183 184option(EXECUTORCH_BUILD_EXTENSION_TENSOR "Build the Tensor extension" OFF) 185 186option(EXECUTORCH_BUILD_EXTENSION_TRAINING "Build the training extension" OFF) 187 188option(EXECUTORCH_BUILD_GTESTS "Build googletest based test binaries" OFF) 189 190option(EXECUTORCH_BUILD_MPS "Build the MPS backend" OFF) 191 192option(EXECUTORCH_BUILD_NEURON "Build the backends/mediatek directory" OFF) 193 194option(EXECUTORCH_BUILD_PYBIND "Build the Python Bindings" OFF) 195 196option(EXECUTORCH_BUILD_QNN "Build the Qualcomm backend" OFF) 197 198option(EXECUTORCH_BUILD_KERNELS_OPTIMIZED "Build the optimized kernels" OFF) 199 200option(EXECUTORCH_BUILD_KERNELS_QUANTIZED "Build the quantized kernels" OFF) 201 202option(EXECUTORCH_BUILD_DEVTOOLS "Build the ExecuTorch Developer Tools") 203 204option(EXECUTORCH_NNLIB_OPT "Build Cadence backend Hifi nnlib kernel" OFF) 205 206option(EXECUTORCH_CADENCE_CPU_RUNNER "Build Cadence backend CPU runner" OFF) 207 208option(EXECUTORCH_BUILD_SIZE_TEST "Build the size test" OFF) 209 210option(EXECUTORCH_BUILD_XNNPACK "Build the XNNPACK backend" OFF) 211 212option(EXECUTORCH_BUILD_VULKAN "Build the Vulkan backend" OFF) 213 214option(BUILD_EXECUTORCH_PORTABLE_OPS "Build portable_ops library" ON) 215 216option(EXECUTORCH_USE_DL "Use libdl library" ON) 217 218option(EXECUTORCH_BUILD_CADENCE "Build the Cadence DSP backend" OFF) 219 220# 221# pthreadpool: build pthreadpool library. Disable on unsupported platforms 222# 223cmake_dependent_option( 224 EXECUTORCH_BUILD_PTHREADPOOL "Build pthreadpool library." ON 225 "NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF 226) 227 228# 229# cpuinfo: build cpuinfo library. Disable on unsupported platforms 230# 231cmake_dependent_option( 232 EXECUTORCH_BUILD_CPUINFO "Build cpuinfo library." ON 233 "NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF 234) 235 236if(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT) 237 set(EXECUTORCH_BUILD_EXTENSION_TENSOR ON) 238 set(EXECUTORCH_BUILD_KERNELS_CUSTOM ON) 239endif() 240 241if(EXECUTORCH_BUILD_KERNELS_CUSTOM) 242 set(EXECUTORCH_BUILD_KERNELS_OPTIMIZED ON) 243endif() 244 245if(EXECUTORCH_BUILD_CPUINFO) 246 # --- cpuinfo 247 set(ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG 248 ${CMAKE_POSITION_INDEPENDENT_CODE} 249 ) 250 set(CMAKE_POSITION_INDEPENDENT_CODE ON) 251 set(CPUINFO_SOURCE_DIR "backends/xnnpack/third-party/cpuinfo") 252 set(CPUINFO_BUILD_TOOLS 253 OFF 254 CACHE BOOL "" 255 ) 256 set(CPUINFO_BUILD_UNIT_TESTS 257 OFF 258 CACHE BOOL "" 259 ) 260 set(CPUINFO_BUILD_MOCK_TESTS 261 OFF 262 CACHE BOOL "" 263 ) 264 set(CPUINFO_BUILD_BENCHMARKS 265 OFF 266 CACHE BOOL "" 267 ) 268 set(CPUINFO_LIBRARY_TYPE 269 "static" 270 CACHE STRING "" 271 ) 272 set(CPUINFO_LOG_LEVEL 273 "error" 274 CACHE STRING "" 275 ) 276 set(CLOG_SOURCE_DIR "${CPUINFO_SOURCE_DIR}/deps/clog") 277 add_subdirectory("${CPUINFO_SOURCE_DIR}") 278 set(CMAKE_POSITION_INDEPENDENT_CODE 279 ${ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG} 280 ) 281endif() 282 283if(EXECUTORCH_BUILD_PTHREADPOOL) 284 # --- pthreadpool 285 set(ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG 286 ${CMAKE_POSITION_INDEPENDENT_CODE} 287 ) 288 set(CMAKE_POSITION_INDEPENDENT_CODE ON) 289 set(PTHREADPOOL_SOURCE_DIR "backends/xnnpack/third-party/pthreadpool") 290 set(PTHREADPOOL_BUILD_TESTS 291 OFF 292 CACHE BOOL "" 293 ) 294 set(PTHREADPOOL_BUILD_BENCHMARKS 295 OFF 296 CACHE BOOL "" 297 ) 298 set(PTHREADPOOL_LIBRARY_TYPE 299 "static" 300 CACHE STRING "" 301 ) 302 set(PTHREADPOOL_ALLOW_DEPRECATED_API 303 ON 304 CACHE BOOL "" 305 ) 306 if(APPLE) 307 set(PTHREADPOOL_SYNC_PRIMITIVE 308 "condvar" 309 CACHE STRING "" 310 ) 311 endif() 312 add_subdirectory("${PTHREADPOOL_SOURCE_DIR}") 313 set(CMAKE_POSITION_INDEPENDENT_CODE 314 ${ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG} 315 ) 316endif() 317 318if(NOT PYTHON_EXECUTABLE) 319 resolve_python_executable() 320endif() 321message(STATUS "Using python executable '${PYTHON_EXECUTABLE}'") 322 323# TODO(dbort): Fix these warnings and remove this flag. 324set(_common_compile_options -Wno-deprecated-declarations -fPIC) 325 326# Let files say "include <executorch/path/to/header.h>". 327# TODO(#6475): This requires/assumes that the repo lives in a directory named 328# exactly `executorch`. Check the assumption first. Remove this check once we 329# stop relying on the assumption. 330cmake_path(GET CMAKE_CURRENT_SOURCE_DIR FILENAME _repo_dir_name) 331if(NOT "${_repo_dir_name}" STREQUAL "executorch") 332 message( 333 FATAL_ERROR 334 "The ExecuTorch repo must be cloned into a directory named exactly " 335 "`executorch`; found `${_repo_dir_name}`. See " 336 "https://github.com/pytorch/executorch/issues/6475 for progress on a " 337 "fix for this restriction." 338 ) 339endif() 340set(_common_include_directories ${CMAKE_CURRENT_SOURCE_DIR}/..) 341 342# 343# The `_<target>_srcs` lists are defined by including ${EXECUTORCH_SRCS_FILE}. 344# 345 346if(NOT EXECUTORCH_SRCS_FILE) 347 # Find or download buck2 binary. 348 resolve_buck2() 349 350 # A file wasn't provided. Run a script to extract the source lists from the 351 # buck2 build system and write them to a file we can include. 352 # 353 # NOTE: This will only happen once during cmake setup, so it will not re-run 354 # if the buck2 targets change. 355 message(STATUS "executorch: Generating source lists") 356 set(EXECUTORCH_SRCS_FILE "${CMAKE_CURRENT_BINARY_DIR}/executorch_srcs.cmake") 357 extract_sources(${EXECUTORCH_SRCS_FILE}) 358endif() 359 360# This file defines the `_<target>__srcs` variables used below. 361message(STATUS "executorch: Using sources file ${EXECUTORCH_SRCS_FILE}") 362include(${EXECUTORCH_SRCS_FILE}) 363 364# 365# Modify default options when cross-compiling. 366# 367# The intent is for the EXECUTORCH_BUILD_HOST_TARGETS option to affect the 368# default ON/OFF values of host targets around the tree. This way, a user can 369# disable EXECUTORCH_BUILD_HOST_TARGETS to disable all host targets, and then 370# optionally re-enable some of those targets. Or they could leave 371# EXECUTORCH_BUILD_HOST_TARGETS enabled and then optionally disable any given 372# host target. 373# 374# We can then use various cross-compilation hints to set the default value of 375# EXECUTORCH_BUILD_HOST_TARGETS, which can still be overridden if desired. 376# 377 378# Detect if an iOS toolchain is set. 379if(CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$") 380 set(CMAKE_TOOLCHAIN_IOS ON) 381else() 382 set(CMAKE_TOOLCHAIN_IOS OFF) 383endif() 384 385# Detect if an Android toolchain is set. 386if(CMAKE_TOOLCHAIN_FILE MATCHES ".*android\.toolchain\.cmake$") 387 set(CMAKE_TOOLCHAIN_ANDROID ON) 388if(NOT ANDROID_PLATFORM) 389 set(ANDROID_PLATFORM android-30) 390endif() 391else() 392 set(CMAKE_TOOLCHAIN_ANDROID OFF) 393endif() 394 395# Add code coverage flags to supported compilers 396if(EXECUTORCH_USE_CPP_CODE_COVERAGE) 397 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") 398 string(APPEND CMAKE_C_FLAGS " --coverage -fprofile-abs-path") 399 string(APPEND CMAKE_CXX_FLAGS " --coverage -fprofile-abs-path") 400 elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 401 string(APPEND CMAKE_C_FLAGS " -fprofile-instr-generate -fcoverage-mapping") 402 string(APPEND CMAKE_CXX_FLAGS 403 " -fprofile-instr-generate -fcoverage-mapping" 404 ) 405 else() 406 message(ERROR 407 "Code coverage for compiler ${CMAKE_CXX_COMPILER_ID} is unsupported" 408 ) 409 endif() 410endif() 411 412# EXECUTORCH_BUILD_HOST_TARGETS: Option to control the building of host-only 413# tools like `flatc`, along with example executables like `executor_runner` and 414# libraries that it uses, like `gflags`. Disabling this can be helpful when 415# cross-compiling, but some required tools that would have been built need to be 416# provided directly (via, for example, FLATC_EXECUTABLE). 417cmake_dependent_option( 418 EXECUTORCH_BUILD_HOST_TARGETS "Build host-only targets." ON 419 "NOT CMAKE_TOOLCHAIN_IOS" OFF 420) 421 422# 423# flatc: Flatbuffer commandline tool to generate .h files from .fbs files 424# 425cmake_dependent_option( 426 EXECUTORCH_BUILD_FLATC "Build the flatc executable." ON 427 "NOT FLATC_EXECUTABLE;EXECUTORCH_BUILD_HOST_TARGETS" OFF 428) 429 430if(EXECUTORCH_BUILD_FLATC) 431 if(FLATC_EXECUTABLE) 432 # We could ignore this, but it could lead to confusion about which `flatc` 433 # is actually being used. 434 message( 435 FATAL_ERROR "May not set both EXECUTORCH_BUILD_FLATC and FLATC_EXECUTABLE" 436 ) 437 endif() 438 set(FLATC_EXECUTABLE flatc) 439 set(FLATBUFFERS_BUILD_FLATC 440 ON 441 CACHE BOOL "" 442 ) 443 set(FLATBUFFERS_BUILD_FLATHASH 444 OFF 445 CACHE BOOL "" 446 ) 447 set(FLATBUFFERS_BUILD_FLATLIB 448 OFF 449 CACHE BOOL "" 450 ) 451 set(FLATBUFFERS_BUILD_TESTS 452 OFF 453 CACHE BOOL "" 454 ) 455 set(FLATBUFFERS_INSTALL 456 OFF 457 CACHE BOOL "" 458 ) 459 add_subdirectory(third-party/flatbuffers) 460 461 # exir lets users set the alignment of tensor data embedded in the flatbuffer, 462 # and some users need an alignment larger than the default, which is typically 463 # 32. 464 target_compile_definitions(flatc PRIVATE FLATBUFFERS_MAX_ALIGNMENT=1024) 465endif() 466if(NOT FLATC_EXECUTABLE) 467 message( 468 FATAL_ERROR 469 "FLATC_EXECUTABLE must be set when EXECUTORCH_BUILD_FLATC is disabled. " 470 "Note that EXECUTORCH_BUILD_FLATC may be disabled implicitly when " 471 "cross-compiling or when EXECUTORCH_BUILD_HOST_TARGETS is disabled." 472 ) 473endif() 474 475# 476# program_schema: Generated .h files from schema/*.fbs inputs 477# 478add_subdirectory(schema) 479 480# 481# executorch_core: Minimal runtime library 482# 483# The bare-minimum runtime library, supporting the Program and Method 484# interfaces. Does not contain any operators, including primitive ops. Does not 485# contain any backends. 486# 487 488# Remove any PAL-definition files from the sources. 489list(FILTER _executorch_core__srcs EXCLUDE REGEX 490 "runtime/platform/default/[^/]*.cpp$" 491) 492 493# Add the source file that maps to the requested default PAL implementation. 494if(EXECUTORCH_PAL_DEFAULT MATCHES "^(posix|minimal)$") 495 message(STATUS "executorch: Using PAL default '${EXECUTORCH_PAL_DEFAULT}'") 496 list(APPEND _executorch_core__srcs 497 "runtime/platform/default/${EXECUTORCH_PAL_DEFAULT}.cpp" 498 ) 499else() 500 message( 501 FATAL_ERROR "Unknown EXECUTORCH_PAL_DEFAULT \"${EXECUTORCH_PAL_DEFAULT}\". " 502 "Expected one of {posix, minimal}." 503 ) 504endif() 505 506add_library(executorch_core ${_executorch_core__srcs}) 507 508# Legacy name alias. 509add_library(executorch_no_prim_ops ALIAS executorch_core) 510 511target_link_libraries(executorch_core PRIVATE program_schema) 512if(EXECUTORCH_USE_DL) 513 # Check if dl exists for this toolchain and only then link it. 514 find_library(DL_LIBRARY_EXISTS NAMES dl) 515 # Check if the library was found 516 if(DL_LIBRARY_EXISTS) 517 target_link_libraries(executorch_core PRIVATE dl) # For dladdr() 518 endif() 519endif() 520target_include_directories( 521 executorch_core PUBLIC ${_common_include_directories} 522) 523target_compile_options(executorch_core PUBLIC ${_common_compile_options}) 524if(MAX_KERNEL_NUM) 525 target_compile_definitions( 526 executorch_core PRIVATE MAX_KERNEL_NUM=${MAX_KERNEL_NUM} 527 ) 528endif() 529 530if(EXECUTORCH_BUILD_PYBIND AND APPLE) 531 # shared version 532 add_library( 533 executorch_core_shared SHARED ${_executorch_core__srcs} 534 ) 535 target_link_libraries(executorch_core_shared PRIVATE program_schema) 536 if(DL_LIBRARY_EXISTS) 537 # For dladdr() 538 target_link_libraries(executorch_core_shared PRIVATE dl) 539 endif() 540 target_include_directories( 541 executorch_core_shared PUBLIC ${_common_include_directories} 542 ) 543 target_compile_options( 544 executorch_core_shared PUBLIC ${_common_compile_options} 545 ) 546 if(MAX_KERNEL_NUM) 547 target_compile_definitions( 548 executorch_core_shared PRIVATE MAX_KERNEL_NUM=${MAX_KERNEL_NUM} 549 ) 550 endif() 551endif() 552 553# 554# executorch: Primary runtime library with primitive operators. 555# 556# Provides the Program and Method interfaces, along with primitive operators. 557# Does not contain portable kernels or other full operators. Does not contain 558# any backends. 559# 560add_library(executorch ${_executorch__srcs}) 561target_link_libraries(executorch PRIVATE executorch_core) 562target_include_directories(executorch PUBLIC ${_common_include_directories}) 563target_compile_options(executorch PUBLIC ${_common_compile_options}) 564target_link_options_shared_lib(executorch) 565 566# 567# portable_ops_lib: A library to register core ATen ops using portable kernels, 568# see kernels/portable/CMakeLists.txt. 569# 570# Real integrations should supply their own YAML file that only lists the 571# operators necessary for the models that will run. 572# 573if(BUILD_EXECUTORCH_PORTABLE_OPS) 574 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/portable) 575endif() 576 577if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED) 578 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/optimized) 579endif() 580 581add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/configurations) 582 583# 584# gflags: Commandline flag host library. 585# 586cmake_dependent_option( 587 EXECUTORCH_BUILD_GFLAGS "Build the gflags library." ON 588 EXECUTORCH_BUILD_HOST_TARGETS OFF 589) 590if(EXECUTORCH_BUILD_GFLAGS) 591 add_subdirectory(third-party/gflags) 592endif() 593 594# Install `executorch` library as well as `executorch-config.cmake` under 595# ${CMAKE_INSTALL_PREFIX}/ 596install( 597 TARGETS executorch executorch_core 598 DESTINATION lib 599 INCLUDES 600 DESTINATION ${_common_include_directories} 601) 602install(FILES build/executorch-config.cmake DESTINATION lib/cmake/ExecuTorch) 603 604# 605# executor_runner: Host tool that demonstrates program execution. 606# 607cmake_dependent_option( 608 EXECUTORCH_BUILD_EXECUTOR_RUNNER "Build the executor_runner executable" ON 609 EXECUTORCH_BUILD_HOST_TARGETS OFF 610) 611 612# Add googletest if any test targets should be built 613if(EXECUTORCH_BUILD_GTESTS) 614 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/googletest) 615endif() 616 617if(EXECUTORCH_BUILD_ARM_BAREMETAL) 618 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/arm) 619endif() 620 621if(EXECUTORCH_BUILD_CADENCE) 622 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/cadence) 623endif() 624 625if(EXECUTORCH_BUILD_COREML) 626 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/apple/coreml) 627endif() 628 629if(EXECUTORCH_BUILD_MPS) 630 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/apple/mps) 631endif() 632 633if(EXECUTORCH_BUILD_NEURON) 634 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/mediatek) 635endif() 636 637if(EXECUTORCH_BUILD_QNN) 638 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/qualcomm) 639endif() 640 641if(EXECUTORCH_BUILD_XNNPACK) 642 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/xnnpack) 643endif() 644 645if(EXECUTORCH_BUILD_DEVTOOLS) 646 set(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER 647 ON 648 CACHE BOOL "EXECUTORCH_BUILD_EXTENSION_DATA_LOADER" FORCE 649 ) 650 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/devtools) 651endif() 652 653if(EXECUTORCH_BUILD_EXTENSION_APPLE) 654 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/apple) 655endif() 656 657if(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER) 658 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/data_loader) 659endif() 660 661if(EXECUTORCH_BUILD_EXTENSION_MODULE) 662 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/module) 663endif() 664 665if(EXECUTORCH_BUILD_EXTENSION_TRAINING) 666 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/training) 667endif() 668 669if(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL) 670 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/runner_util) 671endif() 672 673if(EXECUTORCH_BUILD_EXTENSION_TENSOR) 674 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/tensor) 675endif() 676 677if(EXECUTORCH_BUILD_PTHREADPOOL 678 AND EXECUTORCH_BUILD_CPUINFO 679 AND CMAKE_CXX_STANDARD GREATER_EQUAL 14 680) 681 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/threadpool) 682endif() 683 684if(EXECUTORCH_BUILD_PYBIND) 685 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/pybind11) 686 687 if(NOT EXECUTORCH_BUILD_EXTENSION_DATA_LOADER) 688 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/data_loader) 689 endif() 690 691 if(NOT EXECUTORCH_BUILD_DEVTOOLS) 692 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/devtools) 693 endif() 694 695 # find pytorch lib, to allow pybind to take at::Tensor as input/output 696 find_package(Torch CONFIG REQUIRED) 697 find_library( 698 TORCH_PYTHON_LIBRARY torch_python PATHS "${TORCH_INSTALL_PREFIX}/lib" 699 ) 700 701 set(_dep_libs 702 ${TORCH_PYTHON_LIBRARY} 703 bundled_program 704 etdump 705 executorch 706 extension_data_loader 707 util 708 torch 709 ) 710 711 if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED) 712 list(APPEND _dep_libs optimized_native_cpu_ops_lib) 713 else() 714 list(APPEND _dep_libs portable_ops_lib) 715 endif() 716 717 if(EXECUTORCH_BUILD_COREML) 718 list(APPEND _dep_libs coremldelegate) 719 endif() 720 721 if(EXECUTORCH_BUILD_MPS) 722 list(APPEND _dep_libs mpsdelegate) 723 endif() 724 725 if(EXECUTORCH_BUILD_XNNPACK) 726 # need to explicitly specify XNNPACK here otherwise uses XNNPACK symbols 727 # from libtorch_cpu 728 list(APPEND _dep_libs xnnpack_backend XNNPACK) 729 endif() 730 731 # compile options for pybind 732 set(_pybind_compile_options 733 -Wno-deprecated-declarations 734 -fPIC 735 -frtti 736 -fexceptions 737 ) 738 if(EXECUTORCH_DO_NOT_USE_CXX11_ABI) 739 # libtorch is built with the old ABI, so we need to do the same for any 740 # .cpp files that include torch, c10, or ATen targets. Note that PyTorch 741 # nightly binary is built with _GLIBCXX_USE_CXX11_ABI set to 0 while its 742 # CI build sets this to 1 (default) 743 list(APPEND _pybind_compile_options -D_GLIBCXX_USE_CXX11_ABI=0) 744 endif() 745 746 # util lib 747 add_library( 748 util ${CMAKE_CURRENT_SOURCE_DIR}/extension/evalue_util/print_evalue.cpp 749 ${CMAKE_CURRENT_SOURCE_DIR}/extension/aten_util/aten_bridge.cpp 750 ) 751 target_include_directories( 752 util PUBLIC ${_common_include_directories} ${TORCH_INCLUDE_DIRS} 753 ) 754 target_compile_options(util PUBLIC ${_pybind_compile_options}) 755 target_link_libraries(util PRIVATE torch c10 executorch extension_tensor) 756 757 # pybind portable_lib 758 pybind11_add_module(portable_lib SHARED extension/pybindings/pybindings.cpp) 759 # The actual output file needs a leading underscore so it can coexist with 760 # portable_lib.py in the same python package. 761 set_target_properties(portable_lib PROPERTIES OUTPUT_NAME "_portable_lib") 762 target_compile_definitions( 763 portable_lib PUBLIC EXECUTORCH_PYTHON_MODULE_NAME=_portable_lib 764 ) 765 target_include_directories(portable_lib PRIVATE ${TORCH_INCLUDE_DIRS}) 766 target_compile_options(portable_lib PUBLIC ${_pybind_compile_options}) 767 target_link_libraries(portable_lib PRIVATE ${_dep_libs}) 768 if(APPLE) 769 # pip wheels will need to be able to find the torch libraries. On Linux, the 770 # .so has non-absolute dependencies on libs like "libtorch.so" without 771 # paths; as long as we `import torch` first, those dependencies will work. 772 # But Apple dylibs do not support non-absolute dependencies, so we need to 773 # tell the loader where to look for its libraries. The LC_LOAD_DYLIB entries 774 # for the torch libraries will look like "@rpath/libtorch.dylib", so we can 775 # add an LC_RPATH entry to look in a directory relative to the installed 776 # location of our _portable_lib.so file. To see these LC_* values, run 777 # `otool -l _portable_lib*.so`. 778 set_target_properties( 779 portable_lib 780 PROPERTIES # Assume that this library will be installed in 781 # `site-packages/executorch/extension/pybindings`, and that 782 # the torch libs are in `site-packages/torch/lib`. 783 BUILD_RPATH "@loader_path/../../../torch/lib" 784 INSTALL_RPATH "@loader_path/../../../torch/lib" 785 # Assume <executorch> is the root `site-packages/executorch` 786 # Need to add <executorch>/extension/llm/custom_ops for 787 # libcustom_ops_aot_lib.dylib 788 BUILD_RPATH "@loader_path/../../extension/llm/custom_ops" 789 INSTALL_RPATH "@loader_path/../../extension/llm/custom_ops" 790 # Need to add <executorch>/kernels/quantized for 791 # libquantized_ops_aot_lib.dylib 792 BUILD_RPATH "@loader_path/../../kernels/quantized" 793 INSTALL_RPATH "@loader_path/../../kernels/quantized" 794 ) 795 else() 796 set_target_properties( 797 portable_lib 798 PROPERTIES 799 # Assume <executorch> is the root `site-packages/executorch` 800 # Need to add <executorch>/extension/llm/custom_ops for 801 # libcustom_ops_aot_lib 802 # Need to add <executorch>/kernels/quantized for 803 # libquantized_ops_aot_lib 804 BUILD_RPATH 805 "$ORIGIN:$ORIGIN/../../extension/llm/custom_ops:$ORIGIN/../../kernels/quantized" 806 ) 807 endif() 808 809 install(TARGETS portable_lib 810 LIBRARY DESTINATION executorch/extension/pybindings 811 ) 812endif() 813 814if(EXECUTORCH_BUILD_KERNELS_CUSTOM) 815 # TODO: move all custom kernels to ${CMAKE_CURRENT_SOURCE_DIR}/kernels/custom 816 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/llm/custom_ops) 817endif() 818 819if(EXECUTORCH_BUILD_KERNELS_QUANTIZED) 820 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/quantized) 821 target_link_options_shared_lib(quantized_ops_lib) 822endif() 823 824if(EXECUTORCH_BUILD_EXECUTOR_RUNNER) 825 # Baseline libraries that executor_runner will link against. 826 set(_executor_runner_libs executorch gflags) 827 828 if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED) 829 list(APPEND _executor_runner_libs optimized_native_cpu_ops_lib) 830 elseif(EXECUTORCH_BUILD_CADENCE) 831 list(APPEND _executor_runner_libs cadence_ops_lib) 832 else() 833 list(APPEND _executor_runner_libs portable_ops_lib) 834 endif() 835 836 # Generate lib to register quantized ops 837 if(EXECUTORCH_BUILD_KERNELS_QUANTIZED) 838 list(APPEND _executor_runner_libs quantized_ops_lib) 839 endif() 840 841 add_executable(executor_runner ${_executor_runner__srcs}) 842 if(CMAKE_BUILD_TYPE STREQUAL "Release") 843 if(APPLE) 844 target_link_options(executor_runner PRIVATE "LINKER:-dead_strip") 845 else() 846 target_link_options(executor_runner PRIVATE "LINKER:--gc-sections") 847 endif() 848 endif() 849 target_link_libraries(executor_runner ${_executor_runner_libs}) 850 target_compile_options(executor_runner PUBLIC ${_common_compile_options}) 851endif() 852 853if(EXECUTORCH_BUILD_VULKAN) 854 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/vulkan) 855endif() 856 857# Print all summary 858executorch_print_configuration_summary() 859