• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
7cmake_minimum_required(VERSION 3.19)
8
9set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
10if(NOT CMAKE_CXX_STANDARD)
11  set(CMAKE_CXX_STANDARD 17)
12endif()
13
14if(NOT PYTHON_EXECUTABLE)
15  set(PYTHON_EXECUTABLE python3)
16endif()
17
18# Source root directory for executorch.
19if(NOT EXECUTORCH_ROOT)
20  set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
21endif()
22
23set(_common_compile_options -Wno-deprecated-declarations -fPIC)
24
25include(${EXECUTORCH_ROOT}/build/Utils.cmake)
26include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
27
28#
29# The `_<target>_srcs` lists are defined by including ${EXECUTORCH_SRCS_FILE}.
30#
31set(EXECUTORCH_SRCS_FILE
32    "${CMAKE_CURRENT_BINARY_DIR}/../../../executorch_srcs.cmake"
33)
34
35extract_sources(${EXECUTORCH_SRCS_FILE})
36
37include(${EXECUTORCH_SRCS_FILE})
38
39# Let files say "include <executorch/path/to/header.h>".
40set(_common_include_directories ${EXECUTORCH_ROOT}/..)
41
42# Custom op libraries
43set(custom_ops_libs pthreadpool)
44list(APPEND custom_ops_libs cpuinfo)
45list(APPEND custom_ops_libs cpublas)
46list(APPEND custom_ops_libs eigen_blas)
47
48list(TRANSFORM _custom_ops__srcs PREPEND "${EXECUTORCH_ROOT}/")
49
50if(NOT EXECUTORCH_BUILD_XNNPACK)
51  list(APPEND custom_ops_libs extension_threadpool)
52else()
53  list(APPEND custom_ops_libs extension_threadpool xnnpack_backend)
54endif()
55
56add_library(custom_ops ${_custom_ops__srcs})
57
58target_include_directories(custom_ops PUBLIC "${_common_include_directories}")
59target_include_directories(
60  custom_ops PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../../../include"
61)
62target_link_libraries(custom_ops PUBLIC ${custom_ops_libs} executorch_core)
63
64target_compile_options(
65  custom_ops PUBLIC ${_common_compile_options} -DET_USE_THREADPOOL
66)
67
68install(TARGETS custom_ops DESTINATION lib)
69
70if(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT)
71  # Add a AOT library
72  find_package(Torch CONFIG REQUIRED)
73  add_library(
74    custom_ops_aot_lib SHARED
75    ${_custom_ops__srcs}
76    ${CMAKE_CURRENT_SOURCE_DIR}/op_sdpa_aot.cpp
77    ${CMAKE_CURRENT_SOURCE_DIR}/op_fast_hadamard_transform_aten.cpp
78    ${CMAKE_CURRENT_SOURCE_DIR}/op_tile_crop.cpp
79    ${CMAKE_CURRENT_SOURCE_DIR}/op_tile_crop_aot.cpp
80  )
81  target_include_directories(
82    custom_ops_aot_lib PUBLIC "${_common_include_directories}"
83  )
84  target_include_directories(
85    custom_ops_aot_lib PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../../../include"
86  )
87  if(TARGET portable_lib)
88    # If we have portable_lib built, custom_ops_aot_lib gives the ability to use
89    # the ops in PyTorch and ExecuTorch through pybind
90    target_link_libraries(custom_ops_aot_lib PUBLIC portable_lib)
91  else()
92    # If no portable_lib, custom_ops_aot_lib still gives the ability to use the
93    # ops in PyTorch
94    target_link_libraries(custom_ops_aot_lib PUBLIC executorch_core)
95  endif()
96
97  target_link_libraries(
98    custom_ops_aot_lib PUBLIC cpublas torch extension_tensor
99                              extension_threadpool
100  )
101  if(WIN32)
102    # There is no direct replacement for libpthread.so on Windows. For the
103    # Windows build, link directly against pthreadpool and cpuinfo.
104    target_link_libraries(custom_ops_aot_lib PUBLIC pthreadpool cpuinfo)
105  endif()
106  target_compile_options(
107    custom_ops_aot_lib
108    PUBLIC -Wno-deprecated-declarations -fPIC -frtti -fexceptions
109           ${_common_compile_options} -DET_USE_THREADPOOL
110  )
111
112  # pip wheels will need to be able to find the dependent libraries. On Linux,
113  # the .so has non-absolute dependencies on libs like "_portable_lib.so"
114  # without paths; as long as we `import torch` first, those dependencies will
115  # work. But Apple dylibs do not support non-absolute dependencies, so we need
116  # to tell the loader where to look for its libraries. The LC_LOAD_DYLIB
117  # entries for the portable_lib libraries will look like
118  # "@rpath/_portable_lib.cpython-310-darwin.so", so we can add an LC_RPATH
119  # entry to look in a directory relative to the installed location of our
120  # _portable_lib.so file. To see these LC_* values, run `otool -l
121  # libcustom_ops_aot_lib.dylib`.
122  if(APPLE)
123    set_target_properties(
124      custom_ops_aot_lib
125      PROPERTIES # Assume this library will be installed in
126                 # <site-packages>/executorch/extension/llm/custom_ops/, and the
127                 # _portable_lib.so is installed in
128                 # <site-packages>/executorch/extension/pybindings/
129                 BUILD_RPATH "@loader_path/../../pybindings"
130                 INSTALL_RPATH "@loader_path/../../pybindings"
131    )
132  endif()
133  install(TARGETS custom_ops_aot_lib DESTINATION lib)
134endif()
135