-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
215 lines (194 loc) · 8.99 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# ----------------------------- Setup ----------------------------------
cmake_minimum_required(VERSION 3.24)
include(FetchContent)
# https://github.com/cmake-js/cmake-js#node-api-and-node-addon-api
cmake_policy(SET CMP0091 NEW)
cmake_policy(SET CMP0042 NEW)
# Allow FetchContent_Populate.
cmake_policy(SET CMP0169 OLD)
# ----------------------------- Project --------------------------------
# Define project name.
project(executorch.js)
# Use C++20.
set(CMAKE_CXX_STANDARD 20)
# ----------------------------- Config ---------------------------------
set(TORCH_VERSION "0.0.4" CACHE STRING "The version of executorch-binaries")
set(TORCH_BINARY_URL "https://github.com/frost-beta/executorch-binaries/releases/download" CACHE STRING "The download URL of executorch-binaries")
option(TORCH_BACKEND_ALL "Build with all backends" OFF)
option(TORCH_BACKEND_COREML "Build with CoreML backend" OFF)
option(TORCH_BACKEND_MPS "Build with MPS backend" OFF)
option(TORCH_BACKEND_XNNPACK "Build with XNNPACK backend" OFF)
option(TORCH_KERNELS_CUSTOM "Build with custom kernels" ON)
option(TORCH_KERNELS_OPTIMIZED "Build with optimzied kernels" ON)
option(TORCH_KERNELS_QUANTIZED "Build with quantized kernels" ON)
if(TORCH_BACKEND_ALL)
set(TORCH_BACKEND_XNNPACK ON)
if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
set(TORCH_BACKEND_MPS ON)
endif()
endif()
# https://github.com/pytorch/executorch/issues/6839
if(CMAKE_SYSTEM_NAME MATCHES "Darwin" AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
set(TORCH_KERNELS_CUSTOM OFF)
set(TORCH_KERNELS_OPTIMIZED OFF)
endif()
# ----------------------------- Node-API -------------------------------
# Define NAPI_VERSION.
add_definitions(-DNAPI_VERSION=9)
# Use node headers/libs provided by cmake-js..
add_library(${PROJECT_NAME} SHARED ${CMAKE_JS_SRC})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC})
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})
set_target_properties(${PROJECT_NAME} PROPERTIES
PREFIX ""
OUTPUT_NAME "executorch"
SUFFIX ".node")
if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
execute_process(COMMAND ${CMAKE_AR}
/def:${CMAKE_JS_NODELIB_DEF}
/out:${CMAKE_JS_NODELIB_TARGET}
${CMAKE_STATIC_LINKER_FLAGS})
endif()
# -------------------------- Utilities ---------------------------------
# Behave the same with the target_link_options_shared_lib util of ExecuTorch.
function(target_force_link_libraries target_name linker_type)
foreach(target_lib ${ARGN})
if(APPLE)
target_link_options(${target_name} ${linker_type}
"SHELL:LINKER:-force_load,${target_lib}")
elseif(MSVC)
target_link_options(${target_name} ${linker_type}
"SHELL:LINKER:/WHOLEARCHIVE:${target_lib}")
else()
target_link_options(${target_name} ${linker_type}
"SHELL:LINKER:--whole-archive \
${target_lib} \
LINKER:--no-whole-archive")
endif()
endforeach()
endfunction()
# ----------------------------- Lib ------------------------------------
# Add source files.
file(GLOB SOURCE_FILES "src/*")
target_sources(${PROJECT_NAME} PRIVATE ${SOURCE_FILES})
target_include_directories(${PROJECT_NAME} PUBLIC ".")
# Use kizunapi.
FetchContent_Declare(kizunapi GIT_REPOSITORY https://github.com/yue/kizunapi.git
GIT_TAG 4c59c84e6dd1d2da260b257adf1bd8c5244fde32)
FetchContent_MakeAvailable(kizunapi)
target_include_directories(${PROJECT_NAME} PRIVATE "${kizunapi_SOURCE_DIR}")
# Use fmt when building with old compilers.
target_compile_definitions(${PROJECT_NAME} PRIVATE ETJS_USE_FMT)
FetchContent_Declare(fmt GIT_REPOSITORY https://github.com/fmtlib/fmt
GIT_TAG e69e5f977d458f2650bb346dadf2ad30c5320281)
# Do not call FetchContent_MakeAvailable as we only need headers.
FetchContent_GetProperties(fmt)
if(NOT fmt_POPULATED)
FetchContent_Populate(fmt)
endif()
target_include_directories(${PROJECT_NAME} PRIVATE "${fmt_SOURCE_DIR}/include")
# Download the executorch binaries.
if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
set(TORCH_OS "mac")
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
set(TORCH_OS "linux")
else()
message(FATAL_ERROR "Unsupported OS: ${CMAKE_SYSTEM_NAME}")
endif()
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
set(TORCH_ARCH "x64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
set(TORCH_ARCH "arm64")
else()
message(FATAL_ERROR "Unsupported arch: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(TORCH_CONFIG "debug")
target_compile_definitions(${PROJECT_NAME} PRIVATE ETJS_DEBUG_BUILD)
else()
set(TORCH_CONFIG "release")
endif()
FetchContent_Declare(torch_lib URL "${TORCH_BINARY_URL}/v${TORCH_VERSION}/executorch-full-${TORCH_CONFIG}-${TORCH_OS}-${TORCH_ARCH}.zip")
FetchContent_MakeAvailable(torch_lib)
set(TORCH_LIBS "${torch_lib_SOURCE_DIR}/libs")
# Link with correct libs.
target_include_directories(${PROJECT_NAME} PRIVATE
"${torch_lib_SOURCE_DIR}/include")
target_link_libraries(${PROJECT_NAME} PRIVATE
"${TORCH_LIBS}/libcpuinfo.a"
"${TORCH_LIBS}/libexecutorch_core.a"
"${TORCH_LIBS}/libextension_module_static.a")
target_force_link_libraries(${PROJECT_NAME} PRIVATE
"${TORCH_LIBS}/libexecutorch.a"
"${TORCH_LIBS}/libextension_data_loader.a")
if(TORCH_BACKEND_COREML)
target_compile_definitions(${PROJECT_NAME} PUBLIC ETJS_BACKEND_COREML)
find_library(ACCELERATE_FRAMEWORK Accelerate)
find_library(COREML_FRAMEWORK CoreML)
target_link_libraries(${PROJECT_NAME} PUBLIC
${ACCELERATE_FRAMEWORK}
${COREML_FRAMEWORK})
target_force_link_libraries(${PROJECT_NAME} PRIVATE
"${TORCH_LIBS}/libcoremldelegate.a")
endif()
if(TORCH_BACKEND_MPS)
target_compile_definitions(${PROJECT_NAME} PUBLIC ETJS_BACKEND_MPS)
find_library(FOUNDATION_FRAMEWORK Foundation)
find_library(METAL_FRAMEWORK Metal)
find_library(MPS_FRAMEWORK MetalPerformanceShaders)
find_library(MPS_GRAPH_FRAMEWORK MetalPerformanceShadersGraph)
target_link_libraries(${PROJECT_NAME} PUBLIC
${FOUNDATION_FRAMEWORK}
${METAL_FRAMEWORK}
${MPS_FRAMEWORK}
${MPS_GRAPH_FRAMEWORK})
target_force_link_libraries(${PROJECT_NAME} PRIVATE
"${TORCH_LIBS}/libmpsdelegate.a")
endif()
if(TORCH_BACKEND_XNNPACK)
target_compile_definitions(${PROJECT_NAME} PUBLIC ETJS_BACKEND_XNNPACK)
target_link_libraries(${PROJECT_NAME} PRIVATE
"${TORCH_LIBS}/libpthreadpool.a")
target_force_link_libraries(${PROJECT_NAME} PRIVATE
"${TORCH_LIBS}/libXNNPACK.a"
"${TORCH_LIBS}/libmicrokernels-prod.a"
"${TORCH_LIBS}/libxnnpack_backend.a")
endif()
if(TORCH_KERNELS_CUSTOM)
target_link_libraries(${PROJECT_NAME} PRIVATE
"${TORCH_LIBS}/libcpublas.a"
"${TORCH_LIBS}/libeigen_blas.a"
"${TORCH_LIBS}/libportable_kernels.a"
"${TORCH_LIBS}/libpthreadpool.a")
target_force_link_libraries(${PROJECT_NAME} PRIVATE
"${TORCH_LIBS}/libcustom_ops.a")
else()
target_force_link_libraries(${PROJECT_NAME} PRIVATE
"${TORCH_LIBS}/libportable_kernels.a"
"${TORCH_LIBS}/libportable_ops_lib.a")
endif()
if(TORCH_KERNELS_OPTIMIZED)
target_force_link_libraries(${PROJECT_NAME} PRIVATE
"${TORCH_LIBS}/liboptimized_kernels.a"
"${TORCH_LIBS}/liboptimized_native_cpu_ops_lib.a")
endif()
if(TORCH_KERNELS_QUANTIZED)
target_force_link_libraries(${PROJECT_NAME} PRIVATE
"${TORCH_LIBS}/libquantized_kernels.a"
"${TORCH_LIBS}/libquantized_ops_lib.a")
endif()
# --------------------------- Summary ----------------------------------
message(STATUS "")
message(STATUS "******** Summary ********")
message(STATUS " CMAKE_BUILD_TYPE : ${CMAKE_BUILD_TYPE}")
message(STATUS " CMAKE_CXX_STANDARD : ${CMAKE_CXX_STANDARD}")
message(STATUS " TORCH_VERSION : ${TORCH_VERSION}")
message(STATUS " TORCH_BINARY_URL : ${TORCH_BINARY_URL}")
message(STATUS " TORCH_BACKEND_ALL : ${TORCH_BACKEND_ALL}")
message(STATUS " TORCH_BACKEND_COREML : ${TORCH_BACKEND_COREML}")
message(STATUS " TORCH_BACKEND_MPS : ${TORCH_BACKEND_MPS}")
message(STATUS " TORCH_BACKEND_XNNPACK : ${TORCH_BACKEND_XNNPACK}")
message(STATUS " TORCH_KERNELS_CUSTOM : ${TORCH_KERNELS_CUSTOM}")
message(STATUS " TORCH_KERNELS_OPTIMIZED : ${TORCH_KERNELS_OPTIMIZED}")
message(STATUS " TORCH_KERNELS_QUANTIZED : ${TORCH_KERNELS_QUANTIZED}")
message(STATUS "")