-
Notifications
You must be signed in to change notification settings - Fork 894
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
1305 lines (1148 loc) · 40.1 KB
/
CMakeLists.txt
File metadata and controls
1305 lines (1148 loc) · 40.1 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# Copyright 2024-2026 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
#
# Simple CMake build system for runtime components.
#
# ### One-time setup ###
#
# Configure the CMake build system. It's good practice to do this whenever
# cloning or pulling the upstream repo. Once this is done, you don't need to do
# it again until you pull from the upstream repo again.
#
# NOTE: Build options can be configured by passing arguments to cmake. For
# example, to enable the EXECUTORCH_BUILD_XNNPACK option, change the cmake
# command to 'cmake -DEXECUTORCH_BUILD_XNNPACK=ON ..'.
#[[
(rm -rf cmake-out \
&& mkdir cmake-out \
&& cd cmake-out \
&& cmake ..)
]]
#
# ### Build ###
#
# NOTE: The `-j` argument specifies how many jobs/processes to use when
# building, and tends to speed up the build significantly. It's typical to use
# "core count + 1" as the `-j` value.
# ~~~
# cmake --build cmake-out -j9
# ~~~
#
# ### Editing this file ###
#
# This file should be formatted with
# ~~~
# cmake-format -i CMakeLists.txt
# ~~~
# It should also be checked with a linter via
# ~~~
# cmake-lint CMakeLists.txt
# ~~~
#
cmake_minimum_required(VERSION 3.24)
project(executorch)
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
# --- ExecuTorch Version ---
# Parse version from version.txt (single source of truth)
file(READ "${EXECUTORCH_ROOT}/version.txt" ET_VERSION_STRING)
string(STRIP "${ET_VERSION_STRING}" ET_VERSION_STRING)
# Extract major.minor.patch (handles formats like "1.2.0a0")
string(REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.([0-9]+)" ET_VERSION_MATCH
"${ET_VERSION_STRING}"
)
set(ET_VERSION_MAJOR "${CMAKE_MATCH_1}")
set(ET_VERSION_MINOR "${CMAKE_MATCH_2}")
set(ET_VERSION_PATCH "${CMAKE_MATCH_3}")
# Validate that version components were successfully extracted
if("${ET_VERSION_MAJOR}" STREQUAL ""
OR "${ET_VERSION_MINOR}" STREQUAL ""
OR "${ET_VERSION_PATCH}" STREQUAL ""
)
message(
FATAL_ERROR
"Failed to parse version from ${EXECUTORCH_ROOT}/version.txt. "
"Expected format: MAJOR.MINOR.PATCH (e.g., '1.2.0' or '1.2.0a0'), "
"but got: '${ET_VERSION_STRING}'"
)
endif()
message(
STATUS
"ExecuTorch version: ${ET_VERSION_MAJOR}.${ET_VERSION_MINOR}.${ET_VERSION_PATCH}"
)
# Generate version.h from template
configure_file(
"${EXECUTORCH_ROOT}/runtime/core/version.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/include/executorch/runtime/core/version.h" @ONLY
)
include(${PROJECT_SOURCE_DIR}/tools/cmake/common/preset.cmake)
include(${PROJECT_SOURCE_DIR}/tools/cmake/Codegen.cmake)
include(${PROJECT_SOURCE_DIR}/tools/cmake/Utils.cmake)
include(CMakeDependentOption)
include(ExternalProject)
include(GNUInstallDirs)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
announce_configured_options(CMAKE_CXX_STANDARD)
if(NOT CMAKE_SYSTEM_PROCESSOR)
set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
endif()
announce_configured_options(CMAKE_SYSTEM_PROCESSOR)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()
announce_configured_options(CMAKE_BUILD_TYPE)
# Sanitizer support (ASAN + UBSAN)
if(EXECUTORCH_USE_SANITIZER)
if(MSVC)
message(WARNING "Sanitizers are not fully supported on MSVC, skipping")
else()
set(EXECUTORCH_SANITIZER_FLAGS
"-fsanitize=address,undefined -fno-omit-frame-pointer"
)
# Suppress deprecation warnings on macOS (third-party code like flatcc uses
# deprecated sprintf)
if(APPLE)
set(EXECUTORCH_SANITIZER_FLAGS
"${EXECUTORCH_SANITIZER_FLAGS} -Wno-deprecated-declarations"
)
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXECUTORCH_SANITIZER_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXECUTORCH_SANITIZER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS
"${CMAKE_EXE_LINKER_FLAGS} ${EXECUTORCH_SANITIZER_FLAGS}"
)
set(CMAKE_SHARED_LINKER_FLAGS
"${CMAKE_SHARED_LINKER_FLAGS} ${EXECUTORCH_SANITIZER_FLAGS}"
)
message(STATUS "Sanitizers enabled: address, undefined")
endif()
endif()
announce_configured_options(EXECUTORCH_USE_SANITIZER)
if(NOT PYTHON_EXECUTABLE)
resolve_python_executable()
endif()
announce_configured_options(PYTHON_EXECUTABLE)
announce_configured_options(CMAKE_CXX_COMPILER_ID)
announce_configured_options(CMAKE_TOOLCHAIN_FILE)
announce_configured_options(BUILD_TESTING)
load_build_preset()
include(${PROJECT_SOURCE_DIR}/tools/cmake/preset/default.cmake)
# Enable ccache if available
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
message(STATUS "ccache found and enabled for faster builds")
else()
message(STATUS "ccache not found, builds will not be cached")
endif()
announce_configured_options(CCACHE_PROGRAM)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(NOT EXECUTORCH_ENABLE_LOGGING)
# Avoid pulling in the logging strings, which can be large. Note that this
# will set the compiler flag for all targets in this directory, and for all
# subdirectories included after this point.
add_definitions(-DET_LOG_ENABLED=0)
endif()
add_definitions(-DET_MIN_LOG_LEVEL=${ET_MIN_LOG_LEVEL})
if(NOT EXECUTORCH_ENABLE_PROGRAM_VERIFICATION)
# Avoid pulling in the flatbuffer data verification logic, which can add about
# 20kB. Note that this will set the compiler flag for all targets in this
# directory, and for all subdirectories included after this point.
add_definitions(-DET_ENABLE_PROGRAM_VERIFICATION=0)
endif()
if(EXECUTORCH_ENABLE_EVENT_TRACER)
add_definitions(-DET_EVENT_TRACER_ENABLED)
endif()
if(EXECUTORCH_ENABLE_BUNDLE_IO)
add_definitions(-DET_BUNDLE_IO_ENABLED)
endif()
if(EXECUTORCH_BUILD_CUDA)
add_definitions(-DCUDA_AVAILABLE=1)
endif()
# -ffunction-sections -fdata-sections: breaks function and data into sections so
# they can be properly gc'd. -s: strip symbol.
if(WIN32)
set(CMAKE_CXX_FLAGS_RELEASE "/Gy /Gw ${CMAKE_CXX_FLAGS_RELEASE}")
else()
set(CMAKE_CXX_FLAGS_RELEASE
"-ffunction-sections -fdata-sections ${CMAKE_CXX_FLAGS_RELEASE}"
)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s")
endif()
if(EXECUTORCH_OPTIMIZE_SIZE)
# -Os: Optimize for size.
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Os")
else()
# -O2: Moderate opt.
set(CMAKE_CXX_FLAGS_RELEASE "-O2 ${CMAKE_CXX_FLAGS_RELEASE}")
endif()
if(EXECUTORCH_BUILD_TESTS)
include(CTest)
else()
# It looks like some of our third-party deps will try to turn this on if it's
# not explicitly set, leading to confusing behavior.
set(BUILD_TESTING OFF)
endif()
add_subdirectory(third-party)
# Size-optimized builds disable exceptions, RTTI, and unwind tables.
# ExecuTorch's runtime uses Result<T>/Error instead of exceptions, so
# -fno-exceptions is safe. Suppressing .eh_frame via -fno-unwind-tables is safe
# because exceptions are disabled. Placed after add_subdirectory(third-party) to
# avoid leaking into third-party configure checks (e.g. gflags).
if(EXECUTORCH_OPTIMIZE_SIZE
AND NOT WIN32
AND NOT MSVC
)
set(CMAKE_CXX_FLAGS_RELEASE
"${CMAKE_CXX_FLAGS_RELEASE} -fno-exceptions -fno-rtti -fno-asynchronous-unwind-tables -fno-unwind-tables"
)
set(CMAKE_C_FLAGS_RELEASE
"${CMAKE_C_FLAGS_RELEASE} -fno-asynchronous-unwind-tables -fno-unwind-tables"
)
endif()
if(NOT DEFINED FXDIV_SOURCE_DIR)
set(ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG
${CMAKE_POSITION_INDEPENDENT_CODE}
)
set(FXDIV_SOURCE_DIR "backends/xnnpack/third-party/FXdiv")
add_subdirectory("${FXDIV_SOURCE_DIR}")
executorch_move_interface_include_directories_to_build_time_only(fxdiv)
set(CMAKE_POSITION_INDEPENDENT_CODE
${ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG}
)
endif()
if(EXECUTORCH_BUILD_CPUINFO)
# --- cpuinfo
set(ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG
${CMAKE_POSITION_INDEPENDENT_CODE}
)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CPUINFO_SOURCE_DIR
"${CMAKE_CURRENT_LIST_DIR}/backends/xnnpack/third-party/cpuinfo"
)
set(CPUINFO_BUILD_TOOLS
OFF
CACHE BOOL ""
)
set(CPUINFO_BUILD_UNIT_TESTS
OFF
CACHE BOOL ""
)
set(CPUINFO_BUILD_MOCK_TESTS
OFF
CACHE BOOL ""
)
set(CPUINFO_BUILD_BENCHMARKS
OFF
CACHE BOOL ""
)
set(CPUINFO_LIBRARY_TYPE
"static"
CACHE STRING ""
)
set(CPUINFO_LOG_LEVEL
"error"
CACHE STRING ""
)
set(CLOG_SOURCE_DIR "${CPUINFO_SOURCE_DIR}/deps/clog")
add_subdirectory("${CPUINFO_SOURCE_DIR}")
set(CMAKE_POSITION_INDEPENDENT_CODE
${ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG}
)
executorch_add_prefix_to_public_headers(cpuinfo "${CPUINFO_SOURCE_DIR}/")
install(
TARGETS cpuinfo
EXPORT ExecuTorchTargets
DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES
DESTINATION ${_common_include_directories}
)
endif()
if(EXECUTORCH_BUILD_PTHREADPOOL)
# --- pthreadpool
set(ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG
${CMAKE_POSITION_INDEPENDENT_CODE}
)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(PTHREADPOOL_SOURCE_DIR "backends/xnnpack/third-party/pthreadpool")
set(PTHREADPOOL_BUILD_TESTS
OFF
CACHE BOOL ""
)
set(PTHREADPOOL_BUILD_BENCHMARKS
OFF
CACHE BOOL ""
)
set(PTHREADPOOL_LIBRARY_TYPE
"static"
CACHE STRING ""
)
set(PTHREADPOOL_ALLOW_DEPRECATED_API
ON
CACHE BOOL ""
)
if(APPLE)
set(PTHREADPOOL_SYNC_PRIMITIVE
"condvar"
CACHE STRING ""
)
endif()
add_subdirectory("${PTHREADPOOL_SOURCE_DIR}")
executorch_move_interface_include_directories_to_build_time_only(pthreadpool)
executorch_move_interface_include_directories_to_build_time_only(
pthreadpool_interface
)
if(APPLE)
# Use hidden visibility for pthreadpool on Apple platforms to avoid issues
# with pthreadpool symbols from libtorch_cpu taking precedence over the ones
# from the pthreadpool library statically linked in _portable_lib. The
# pthreadpool public APIs are marked as weak by default on some Apple
# platforms, so setting to hidden visibility works around this by not
# putting the symbol in the indirection table. See
# https://github.com/pytorch/executorch/issues/14321 for more details.
target_compile_options(pthreadpool PRIVATE -fvisibility=hidden)
endif()
install(
TARGETS pthreadpool pthreadpool_interface fxdiv
EXPORT ExecuTorchTargets
DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES
DESTINATION ${_common_include_directories}
)
set(CMAKE_POSITION_INDEPENDENT_CODE
${ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG}
)
endif()
if(EXECUTORCH_BUILD_TESTS)
set(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR ON)
include(CTest)
endif()
# TODO(dbort): Fix these warnings and remove this flag.
list(APPEND _common_compile_options $<$<CXX_COMPILER_ID:MSVC>:/wd4996>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated-declarations>
)
# Set default CMAKE_POSITION_INDEPENDENT_CODE behavior if ON or not set
# (default) (and not for MSVC compiler)
if(NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE
OR CMAKE_POSITION_INDEPENDENT_CODE
)
list(APPEND _common_compile_options $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-fPIC>)
endif()
# Let files say "include <executorch/path/to/header.h>".
# TODO(#6475): This requires/assumes that the repo lives in a directory named
# exactly `executorch`. Check the assumption first. Remove this check once we
# stop relying on the assumption.
cmake_path(GET CMAKE_CURRENT_SOURCE_DIR FILENAME _repo_dir_name)
if(NOT "${_repo_dir_name}" STREQUAL "executorch")
message(
FATAL_ERROR
"The ExecuTorch repo must be cloned into a directory named exactly "
"`executorch`; found `${_repo_dir_name}`. See "
"https://github.com/pytorch/executorch/issues/6475 for progress on a "
"fix for this restriction."
)
endif()
set(_common_include_directories
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/runtime/core/portable_type/c10>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/executorch/runtime/core/portable_type/c10>
)
#
# The `_<target>_srcs` lists are defined by executorch_load_build_variables.
#
if(EXECUTORCH_SRCS_FILE)
message(
WARNING
"EXECUTORCH_SRCS_FILE is no longer necessary and will not affect the build."
)
endif()
executorch_load_build_variables()
# Detect if an iOS toolchain is set.
if(CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$")
set(CMAKE_TOOLCHAIN_IOS ON)
else()
set(CMAKE_TOOLCHAIN_IOS OFF)
endif()
# Detect if an Android toolchain is set.
if(CMAKE_TOOLCHAIN_FILE MATCHES ".*android\.toolchain\.cmake$")
set(CMAKE_TOOLCHAIN_ANDROID ON)
if(NOT ANDROID_PLATFORM)
set(ANDROID_PLATFORM android-30)
endif()
else()
set(CMAKE_TOOLCHAIN_ANDROID OFF)
endif()
# Add code coverage flags to supported compilers
if(EXECUTORCH_USE_CPP_CODE_COVERAGE)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
string(APPEND CMAKE_C_FLAGS " --coverage -fprofile-abs-path")
string(APPEND CMAKE_CXX_FLAGS " --coverage -fprofile-abs-path")
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
string(APPEND CMAKE_C_FLAGS " -fprofile-instr-generate -fcoverage-mapping")
string(APPEND CMAKE_CXX_FLAGS
" -fprofile-instr-generate -fcoverage-mapping"
)
else()
message(
FATAL_ERROR
"Code coverage for compiler ${CMAKE_CXX_COMPILER_ID} is unsupported"
)
endif()
endif()
#
# program_schema: Generated .h files from schema/*.fbs inputs
#
add_subdirectory(schema)
#
# executorch_core: Minimal runtime library
#
# The bare-minimum runtime library, supporting the Program and Method
# interfaces. Does not contain any operators, including primitive ops. Does not
# contain any backends.
#
# Remove any PAL-definition files from the sources.
list(FILTER _executorch_core__srcs EXCLUDE REGEX
"runtime/platform/default/[^/]*.cpp$"
)
# Add the source file that maps to the requested default PAL implementation.
list(APPEND _executorch_core__srcs ${EXECUTORCH_PAL_DEFAULT_FILE_PATH})
add_library(executorch_core ${_executorch_core__srcs})
# Legacy name alias.
add_library(executorch_no_prim_ops ALIAS executorch_core)
# A list of all configured backends.
set(_executorch_backends "")
# A list of all configured extensions.
set(_executorch_extensions "")
# A list of all configured kernel libraries.
set(_executorch_kernels "")
target_link_libraries(executorch_core PRIVATE program_schema)
if(ANDROID)
target_link_libraries(executorch_core PUBLIC log)
endif()
if(EXECUTORCH_USE_DL)
# Check if dl exists for this toolchain and only then link it.
find_library(DL_LIBRARY_EXISTS NAMES dl)
# Check if the library was found
if(DL_LIBRARY_EXISTS)
target_link_libraries(executorch_core PRIVATE dl) # For dladdr()
endif()
endif()
target_include_directories(
executorch_core PUBLIC ${_common_include_directories}
)
target_compile_definitions(
executorch_core PUBLIC C10_USING_CUSTOM_GENERATED_MACROS
)
if(NOT EXECUTORCH_ENABLE_LOGGING)
target_compile_definitions(executorch_core PUBLIC ET_LOG_ENABLED=0)
endif()
target_compile_options(executorch_core PUBLIC ${_common_compile_options})
if(MAX_KERNEL_NUM)
target_compile_definitions(
executorch_core PRIVATE MAX_KERNEL_NUM=${MAX_KERNEL_NUM}
)
endif()
# Build devtools first if needed - some backends depend on protobuf from
# devtools
if(EXECUTORCH_BUILD_DEVTOOLS)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/devtools)
endif()
if(EXECUTORCH_BUILD_PYBIND AND APPLE)
# shared version
add_library(executorch_core_shared SHARED ${_executorch_core__srcs})
target_link_libraries(executorch_core_shared PRIVATE program_schema)
if(DL_LIBRARY_EXISTS)
# For dladdr()
target_link_libraries(executorch_core_shared PRIVATE dl)
endif()
target_include_directories(
executorch_core_shared PUBLIC ${_common_include_directories}
)
target_compile_definitions(
executorch_core_shared PUBLIC C10_USING_CUSTOM_GENERATED_MACROS
)
target_compile_options(
executorch_core_shared PUBLIC ${_common_compile_options}
)
if(MAX_KERNEL_NUM)
target_compile_definitions(
executorch_core_shared PRIVATE MAX_KERNEL_NUM=${MAX_KERNEL_NUM}
)
endif()
endif()
#
# executorch: Primary runtime library with primitive operators.
#
# Provides the Program and Method interfaces, along with primitive operators.
# Does not contain portable kernels or other full operators. Does not contain
# any backends.
#
add_library(executorch ${_executorch__srcs})
target_link_libraries(executorch PRIVATE executorch_core)
target_include_directories(executorch PUBLIC ${_common_include_directories})
target_compile_definitions(executorch PUBLIC C10_USING_CUSTOM_GENERATED_MACROS)
if(NOT EXECUTORCH_ENABLE_LOGGING)
target_compile_definitions(executorch PUBLIC ET_LOG_ENABLED=0)
endif()
target_compile_options(executorch PUBLIC ${_common_compile_options})
executorch_target_link_options_shared_lib(executorch)
#
# portable_ops_lib: A library to register core ATen ops using portable kernels,
# see kernels/portable/CMakeLists.txt.
#
# Real integrations should supply their own YAML file that only lists the
# operators necessary for the models that will run.
#
if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
# find pytorch lib here to make it available to all sub-directories. Find it
# before including portable so that optimized_portable_kernels can use it.
find_package_torch_headers()
endif()
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/portable/cpu/util)
# Custom ops AOT needs Torch. Find it at root scope before processing the
# portable subdirectory so that imported targets (e.g. MKL::MKL) are created at
# root scope rather than in a child directory scope.
if(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT AND EXECUTORCH_BUILD_PORTABLE_OPS)
find_package_torch()
endif()
if(EXECUTORCH_BUILD_PORTABLE_OPS)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/portable)
endif()
if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/optimized)
endif()
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/configurations)
# Install `executorch` library as well as `executorch-config.cmake` under
# ${CMAKE_INSTALL_PREFIX}/
install(
DIRECTORY runtime/core/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/runtime/core
FILES_MATCHING
PATTERN "*.h"
)
# Install generated version.h
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/include/executorch/runtime/core/version.h"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/runtime/core
)
install(
DIRECTORY runtime/executor/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/runtime/executor
FILES_MATCHING
PATTERN "*.h"
)
install(
DIRECTORY runtime/kernel/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/runtime/kernel
FILES_MATCHING
PATTERN "*.h"
)
install(
DIRECTORY runtime/backend/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/runtime/backend
FILES_MATCHING
PATTERN "*.h"
)
install(
DIRECTORY runtime/platform/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/runtime/platform
FILES_MATCHING
PATTERN "*.h"
)
install(
DIRECTORY extension/kernel_util/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/extension/kernel_util
FILES_MATCHING
PATTERN "*.h"
)
install(
DIRECTORY extension/tensor/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/extension/tensor
FILES_MATCHING
PATTERN "*.h"
)
install(
DIRECTORY extension/threadpool/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/extension/threadpool
FILES_MATCHING
PATTERN "*.h"
)
install(
TARGETS executorch executorch_core
EXPORT ExecuTorchTargets
INCLUDES
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(FILES tools/cmake/executorch-config.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ExecuTorch
)
if(EXECUTORCH_BUILD_ARM_BAREMETAL
OR EXECUTORCH_BUILD_ARM_ETHOSU_LINUX
OR EXECUTORCH_BUILD_VGF
)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/arm)
endif()
if(EXECUTORCH_BUILD_ARM_BAREMETAL OR EXECUTORCH_BUILD_ARM_ETHOSU_LINUX)
list(APPEND _executorch_backends executorch_delegate_ethos_u)
endif()
if(EXECUTORCH_BUILD_CADENCE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/cadence)
endif()
if(EXECUTORCH_BUILD_NXP_NEUTRON)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/nxp)
list(APPEND _executorch_backends executorch_delegate_neutron)
endif()
if(EXECUTORCH_BUILD_NXP_NEUTRON_RUNNER)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples/nxp/executor_runner)
endif()
if(EXECUTORCH_BUILD_COREML)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/apple/coreml)
list(APPEND _executorch_backends coremldelegate)
endif()
if(EXECUTORCH_BUILD_MPS)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/apple/mps)
list(APPEND _executorch_backends mpsdelegate)
endif()
if(EXECUTORCH_BUILD_NEURON)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/mediatek)
list(APPEND _executorch_backends neuron_backend)
endif()
if(EXECUTORCH_BUILD_OPENVINO)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/openvino)
list(APPEND _executorch_backends openvino_backend)
endif()
if(EXECUTORCH_BUILD_QNN)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/qualcomm)
list(APPEND _executorch_backends qnn_executorch_backend)
endif()
if(EXECUTORCH_BUILD_ENN)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/samsung)
list(APPEND _executorch_backends enn_backend)
endif()
if(EXECUTORCH_BUILD_XNNPACK)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/xnnpack)
list(APPEND _executorch_backends xnnpack_backend)
endif()
if(EXECUTORCH_BUILD_CORTEX_M)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/cortex_m)
endif()
# If CUDA, Metal, or pybind will need Torch, find it at root scope first so that
# imported targets (e.g. MKL::MKL) aren't created in child directory scopes and
# then duplicated when found again at root scope.
if(EXECUTORCH_BUILD_CUDA
OR EXECUTORCH_BUILD_METAL
OR EXECUTORCH_BUILD_PYBIND
)
find_package_torch()
endif()
# Build common AOTI functionality if needed by CUDA or Metal backends
if(EXECUTORCH_BUILD_CUDA OR EXECUTORCH_BUILD_METAL)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/aoti)
endif()
if(EXECUTORCH_BUILD_CUDA)
# Build CUDA-specific AOTI functionality
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/cuda)
# Add aoti_cuda_backend to backends - it transitively includes aoti_cuda_shims
# and cuda_platform
list(APPEND _executorch_backends aoti_cuda_backend)
endif()
if(EXECUTORCH_BUILD_METAL)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/apple/metal)
list(APPEND _executorch_backends metal_backend)
endif()
if(EXECUTORCH_BUILD_EXTENSION_APPLE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/apple)
endif()
if(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/data_loader)
install(
DIRECTORY extension/data_loader/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/extension/data_loader
FILES_MATCHING
PATTERN "*.h"
)
list(APPEND _executorch_extensions extension_data_loader)
endif()
if(EXECUTORCH_BUILD_EXTENSION_EVALUE_UTIL)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/evalue_util)
install(
DIRECTORY extension/evalue_util/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/extension/evalue_util
FILES_MATCHING
PATTERN "*.h"
)
endif()
if(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/flat_tensor)
list(APPEND _executorch_extensions extension_flat_tensor)
endif()
if(EXECUTORCH_BUILD_EXTENSION_MODULE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/module)
install(
DIRECTORY extension/module/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/extension/module
FILES_MATCHING
PATTERN "*.h"
)
list(APPEND _executorch_extensions extension_module_static)
endif()
if(EXECUTORCH_BUILD_EXTENSION_NAMED_DATA_MAP)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/named_data_map)
list(APPEND _executorch_extensions extension_named_data_map)
endif()
if(EXECUTORCH_BUILD_EXTENSION_LLM)
if(EXECUTORCH_BUILD_EXTENSION_LLM_RUNNER)
set(SUPPORT_REGEX_LOOKAHEAD ON)
# llama/runner/CMakeLists.txt builds a shared library libllama_runner.so
# that transitively depends on tokenizers. Need to build tokenizers with
# -fPIC.
set(ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG
${CMAKE_POSITION_INDEPENDENT_CODE}
)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/llm/tokenizers)
if(EXECUTORCH_BUILD_EXTENSION_LLM_RUNNER)
set(CMAKE_POSITION_INDEPENDENT_CODE
${ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG}
)
endif()
list(APPEND _executorch_extensions tokenizers)
endif()
if(EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/runner_util)
install(
DIRECTORY extension/runner_util/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/executorch/extension/runner_util
FILES_MATCHING
PATTERN "*.h"
)
list(APPEND _executorch_extensions extension_runner_util)
endif()
if(EXECUTORCH_BUILD_EXTENSION_TENSOR)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/tensor)
list(APPEND _executorch_extensions extension_tensor)
endif()
if(EXECUTORCH_BUILD_PTHREADPOOL AND EXECUTORCH_BUILD_CPUINFO)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/threadpool)
endif()
if(EXECUTORCH_BUILD_KERNELS_TORCHAO)
if(NOT TARGET cpuinfo)
message(
FATAL_ERROR
"EXECUTORCH_BUILD_KERNELS_TORCHAO requires EXECUTORCH_BUILD_CPUINFO be set ON"
)
endif()
if(NOT TARGET pthreadpool)
message(
FATAL_ERROR
"EXECUTORCH_BUILD_KERNELS_TORCHAO requires EXECUTORCH_BUILD_PTHREADPOOL be set ON"
)
endif()
# Configure TorchAO kernels
set(TORCHAO_BUILD_ATEN_OPS OFF)
set(TORCHAO_BUILD_EXECUTORCH_OPS ON)
set(TORCHAO_BUILD_CPU_AARCH64 ON)
set(TORCHAO_ENABLE_ARM_NEON_DOT ON)
set(TORCHAO_BUILD_KLEIDIAI ON)
# TorchAO kernels look for EXECUTORCH_INCLUDE_DIRS
if(DEFINED EXECUTORCH_INCLUDE_DIRS)
message(FATAL_ERROR "EXECUTORCH_INCLUDE_DIRS is already defined")
endif()
set(EXECUTORCH_INCLUDE_DIRS
${EXECUTORCH_ROOT}/backends/xnnpack/third-party/pthreadpool/include
${EXECUTORCH_ROOT}/backends/xnnpack/third-party/cpuinfo/include
)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/ao/torchao/csrc/cpu)
unset(EXECUTORCH_INCLUDE_DIRS)
executorch_target_link_options_shared_lib(torchao_ops_executorch)
list(APPEND _executorch_kernels torchao_ops_executorch)
install(
TARGETS torchao_ops_executorch torchao_kernels_aarch64
EXPORT ExecuTorchTargets
DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES
DESTINATION ${_common_include_directories}
)
# If using KleidiAI and XNNPACK has not installed it already, install it
if(TORCHAO_BUILD_KLEIDIAI AND NOT (EXECUTORCH_BUILD_XNNPACK
AND EXECUTORCH_XNNPACK_ENABLE_KLEIDI)
)
install(
TARGETS kleidiai
EXPORT ExecuTorchTargets
DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES
DESTINATION ${_common_include_directories}
)
endif()
endif()
if(EXECUTORCH_BUILD_PYBIND)
if(NOT EXECUTORCH_BUILD_EXTENSION_DATA_LOADER)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/data_loader)
endif()
if(NOT EXECUTORCH_BUILD_DEVTOOLS)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/devtools)
endif()
# Add codegen tools subdirectory for selective_build pybind module
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/codegen/tools)
# Create bundled_module target only for pybindings when bundled_program exists
# This target has hard dependencies on devtools generated headers
if(TARGET bundled_program)
add_library(
bundled_module STATIC
${CMAKE_CURRENT_SOURCE_DIR}/extension/module/bundled_module.cpp
)
# Ensure bundled_module waits for bundled_program's generated headers
add_dependencies(bundled_module bundled_program)
target_link_libraries(bundled_module PRIVATE extension_data_loader)
target_link_libraries(
bundled_module PUBLIC extension_module_static bundled_program
)
target_include_directories(
bundled_module PUBLIC ${_common_include_directories}
)
target_compile_options(
bundled_module
PUBLIC $<$<CXX_COMPILER_ID:MSVC>:/wd4996>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated-declarations
-fPIC>
)
endif()
# find pytorch lib, to allow pybind to take at::Tensor as input/output
find_package_torch()
find_library(
TORCH_PYTHON_LIBRARY torch_python PATHS "${TORCH_INSTALL_PREFIX}/lib"
)
set(_dep_libs
${TORCH_PYTHON_LIBRARY}
bundled_program
etdump
flatccrt
executorch
extension_data_loader
util
torch
)
# Build common AOTI functionality if needed by CUDA or Metal backends
if(EXECUTORCH_BUILD_CUDA)
# CUDA uses SlimTensor-based shims
list(APPEND _dep_libs aoti_cuda_backend)
elseif(EXECUTORCH_BUILD_METAL)
# Metal still uses ETensor-based shims (for now)
list(APPEND _dep_libs aoti_common)
endif()
# RPATH for _portable_lib.so
set(_portable_lib_rpath "$ORIGIN/../../../torch/lib")
if(EXECUTORCH_BUILD_EXTENSION_MODULE)
# Always use static linking for pybindings to avoid runtime symbol
# resolution issues
list(APPEND _dep_libs extension_module_static)
# Add bundled_module if available
if(TARGET bundled_module)
list(APPEND _dep_libs bundled_module)
endif()
endif()
if(EXECUTORCH_BUILD_TESTS)
list(APPEND _dep_libs test_backend_compiler_lib)
endif()
if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
list(APPEND _dep_libs optimized_native_cpu_ops_lib)
else()
list(APPEND _dep_libs portable_ops_lib)
endif()
if(EXECUTORCH_BUILD_COREML AND APPLE)
list(APPEND _dep_libs coremldelegate)
endif()
if(EXECUTORCH_BUILD_MPS)
list(APPEND _dep_libs mpsdelegate)
endif()
if(EXECUTORCH_BUILD_OPENVINO)
list(APPEND _dep_libs openvino_backend)
endif()
if(EXECUTORCH_BUILD_CUDA)
string(APPEND _portable_lib_rpath ":$ORIGIN/../../backends/cuda")
endif()
if(EXECUTORCH_BUILD_QNN)
list(APPEND _dep_libs qnn_executorch_backend)
string(APPEND _portable_lib_rpath ":$ORIGIN/../../backends/qualcomm")
endif()
if(EXECUTORCH_BUILD_ENN)
list(APPEND _dep_libs enn_backend)
endif()
if(EXECUTORCH_BUILD_XNNPACK)
# need to explicitly specify XNNPACK and xnnpack-microkernels-prod here