xref: /aosp_15_r20/external/executorch/backends/apple/coreml/CMakeLists.txt (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1# Copyright © 2023 Apple Inc. All rights reserved.
2
3cmake_minimum_required(VERSION 3.19)
4
5project(executorch_coreml_backend)
6
7if(NOT CMAKE_CXX_STANDARD)
8  set(CMAKE_CXX_STANDARD 17)
9endif()
10
11# Source root directory for executorch.
12if(NOT EXECUTORCH_ROOT)
13  set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
14endif()
15
16if(EXECUTORCH_BUILD_DEVTOOLS)
17  # protobuf requires frtti
18  set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -frtti")
19endif()
20
21option(COREML_BUILD_EXECUTOR_RUNNER "Build CoreML executor runner." OFF)
22
23# inmemoryfs sources
24set(INMEMORYFS_SOURCES
25    runtime/inmemoryfs/inmemory_filesystem.cpp
26    runtime/inmemoryfs/inmemory_filesystem_utils.mm
27    runtime/inmemoryfs/memory_buffer.cpp
28    runtime/inmemoryfs/memory_stream.cpp
29    runtime/inmemoryfs/reversed_memory_stream.cpp
30)
31
32# kvstore sources
33set(KVSTORE_SOURCES
34    runtime/kvstore/database.cpp runtime/kvstore/json_key_value_store.cpp
35    runtime/kvstore/sqlite_error.cpp runtime/kvstore/key_value_store.cpp
36    runtime/kvstore/statement.cpp
37)
38
39# delegate sources
40set(DELEGATE_SOURCES
41    runtime/delegate/asset.mm
42    runtime/delegate/backend_delegate.mm
43    runtime/delegate/coreml_backend_delegate.mm
44    runtime/delegate/ETCoreMLAsset.mm
45    runtime/delegate/ETCoreMLAssetManager.mm
46    runtime/delegate/ETCoreMLDefaultModelExecutor.mm
47    runtime/delegate/ETCoreMLModelLoader.mm
48    runtime/delegate/ETCoreMLModelCompiler.mm
49    runtime/delegate/ETCoreMLLogging.mm
50    runtime/delegate/ETCoreMLModel.mm
51    runtime/delegate/ETCoreMLModelManager.mm
52    runtime/delegate/ETCoreMLStrings.mm
53    runtime/delegate/MLModel_Prewarm.mm
54    runtime/delegate/MLMultiArray_Copy.mm
55    runtime/delegate/multiarray.mm
56    runtime/delegate/serde_json.mm
57)
58
59# util sources
60set(UTIL_SOURCES runtime/util/json_util.cpp runtime/util/objc_json_serde.mm)
61
62# sdk sources
63set(SDK_SOURCES
64    runtime/sdk/ETCoreMLModelAnalyzer.mm
65    runtime/sdk/ETCoreMLModelStructurePath.mm
66    runtime/sdk/ETCoreMLOperationProfilingInfo.mm
67    runtime/sdk/ETCoreMLModelDebugInfo.mm
68    runtime/sdk/ETCoreMLModelDebugger.mm
69    runtime/sdk/ETCoreMLModelProfiler.mm
70    runtime/sdk/ETCoreMLPair.mm
71    runtime/sdk/model_package_info.mm
72    runtime/sdk/model_event_logger_impl.mm
73    runtime/sdk/program_path.mm
74)
75
76# protobuf sources
77set(PROTOBUF_SOURCES
78    runtime/sdk/format/ArrayFeatureExtractor.pb.cc
79    runtime/sdk/format/AudioFeaturePrint.pb.cc
80    runtime/sdk/format/BayesianProbitRegressor.pb.cc
81    runtime/sdk/format/CategoricalMapping.pb.cc
82    runtime/sdk/format/ClassConfidenceThresholding.pb.cc
83    runtime/sdk/format/CustomModel.pb.cc
84    runtime/sdk/format/DataStructures.pb.cc
85    runtime/sdk/format/DictVectorizer.pb.cc
86    runtime/sdk/format/FeatureTypes.pb.cc
87    runtime/sdk/format/FeatureVectorizer.pb.cc
88    runtime/sdk/format/Gazetteer.pb.cc
89    runtime/sdk/format/GLMClassifier.pb.cc
90    runtime/sdk/format/GLMRegressor.pb.cc
91    runtime/sdk/format/Identity.pb.cc
92    runtime/sdk/format/Imputer.pb.cc
93    runtime/sdk/format/ItemSimilarityRecommender.pb.cc
94    runtime/sdk/format/LinkedModel.pb.cc
95    runtime/sdk/format/MIL.pb.cc
96    runtime/sdk/format/Model.pb.cc
97    runtime/sdk/format/NearestNeighbors.pb.cc
98    runtime/sdk/format/NeuralNetwork.pb.cc
99    runtime/sdk/format/NonMaximumSuppression.pb.cc
100    runtime/sdk/format/Normalizer.pb.cc
101    runtime/sdk/format/NonMaximumSuppression.pb.cc
102    runtime/sdk/format/OneHotEncoder.pb.cc
103    runtime/sdk/format/Parameters.pb.cc
104    runtime/sdk/format/Scaler.pb.cc
105    runtime/sdk/format/SoundAnalysisPreprocessing.pb.cc
106    runtime/sdk/format/SVM.pb.cc
107    runtime/sdk/format/TextClassifier.pb.cc
108    runtime/sdk/format/TreeEnsemble.pb.cc
109    runtime/sdk/format/VisionFeaturePrint.pb.cc
110    runtime/sdk/format/WordEmbedding.pb.cc
111    runtime/sdk/format/WordTagger.pb.cc
112)
113
114# Define the delegate library
115add_library(coremldelegate)
116target_sources(
117  coremldelegate PRIVATE ${INMEMORYFS_SOURCES} ${KVSTORE_SOURCES}
118                         ${DELEGATE_SOURCES} ${UTIL_SOURCES}
119)
120
121target_include_directories(
122  coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/include
123)
124target_include_directories(
125  coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/kvstore
126)
127target_include_directories(
128  coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/inmemoryfs
129)
130target_include_directories(
131  coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/delegate
132)
133target_include_directories(
134  coremldelegate PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/runtime/util
135)
136target_include_directories(coremldelegate PRIVATE ${EXECUTORCH_ROOT}/..)
137target_link_libraries(coremldelegate PRIVATE executorch_core)
138
139if(EXECUTORCH_BUILD_DEVTOOLS)
140  target_sources(coremldelegate PRIVATE ${SDK_SOURCES} ${PROTOBUF_SOURCES})
141  target_include_directories(
142    coremldelegate
143    PRIVATE
144      ${CMAKE_CURRENT_SOURCE_DIR}/runtime/sdk
145      ${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/src
146  )
147  add_subdirectory(
148    ${CMAKE_CURRENT_SOURCE_DIR}/third-party/coremltools/deps/protobuf/cmake
149  )
150
151  target_link_options_shared_lib(libprotobuf-lite)
152  target_link_libraries(coremldelegate PRIVATE libprotobuf-lite)
153endif()
154
155find_library(ACCELERATE_FRAMEWORK Accelerate)
156find_library(COREML_FRAMEWORK CoreML)
157find_library(FOUNDATION_FRAMEWORK Foundation)
158find_library(SQLITE_LIBRARY sqlite3)
159
160target_link_libraries(
161  coremldelegate
162  PRIVATE executorch_core ${ACCELERATE_FRAMEWORK} ${COREML_FRAMEWORK}
163          ${FOUNDATION_FRAMEWORK} ${SQLITE_LIBRARY}
164)
165
166target_link_options_shared_lib(coremldelegate)
167
168if(COREML_BUILD_EXECUTOR_RUNNER)
169  target_link_libraries(
170    coremldelegate PRIVATE portable_ops_lib portable_kernels
171  )
172endif()
173
174target_compile_options(coremldelegate PRIVATE "-fobjc-arc")
175target_compile_options(coremldelegate PRIVATE "-fno-exceptions")
176
177if(EXECUTORCH_BUILD_DEVTOOLS)
178  target_compile_options(
179    executorch_core PUBLIC -DET_EVENT_TRACER_ENABLED
180  )
181  target_compile_options(coremldelegate PRIVATE "-frtti")
182  target_compile_options(libprotobuf-lite PRIVATE "-frtti")
183else()
184  target_compile_options(coremldelegate PRIVATE "-fno-rtti")
185endif()
186
187set(TARGET coremldelegate APPEND_STRING PROPERTY COMPILE_FLAGS
188           "-x objective-c++"
189)
190
191set(TARGET coremldelegate APPEND_STRING PROPERTY COMPILE_FLAGS
192           "-Wno-null-character"
193)
194
195set(TARGET coremldelegate APPEND_STRING PROPERTY COMPILE_FLAGS
196           "-Wno-receiver-expr"
197)
198
199install(
200  TARGETS coremldelegate
201  DESTINATION lib
202  INCLUDES
203  DESTINATION ${_common_include_directories}
204)
205