1 option(BUILD_CAFFE_PARSER "Build Caffe parser" OFF)
2 option(BUILD_TF_PARSER "Build Tensorflow parser" OFF)
3 option(BUILD_ONNX_PARSER "Build Onnx parser" OFF)
4 option(BUILD_UNIT_TESTS "Build unit tests" ON)
5 option(BUILD_TESTS "Build test applications" OFF)
6 option(BUILD_FOR_COVERAGE "Use no optimization and output .gcno and .gcda files" OFF)
7 option(ARMCOMPUTENEON "Build with ARM Compute NEON support" OFF)
8 option(ARMCOMPUTECL "Build with ARM Compute OpenCL support" OFF)
9 option(ARMCOMPUTEREF "Build with ARM Compute reference support" OFF)
10 option(PROFILING_BACKEND_STREAMLINE "Forward the armNN profiling events to DS-5/Streamline as annotations" OFF)
11 # options used for heap profiling and leak checking
12 option(HEAP_PROFILING "Build with heap profiling enabled" OFF)
13 option(LEAK_CHECKING "Build with leak checking enabled" OFF)
14 option(GPERFTOOLS_ROOT "Location where the gperftools 'include' and 'lib' folders to be found" Off)
15 # options used for tensorflow lite support
16 option(BUILD_TF_LITE_PARSER "Build Tensorflow Lite parser" OFF)
17 option(BUILD_ARMNN_SERIALIZER "Build Armnn Serializer" OFF)
18 option(BUILD_ARMNN_QUANTIZER "Build ArmNN quantizer" OFF)
19 option(BUILD_ACCURACY_TOOL "Build Accuracy Tool" OFF)
20 option(FLATC_DIR "Path to Flatbuffers compiler" OFF)
21 option(TF_LITE_GENERATED_PATH "Tensorflow lite generated C++ schema location" OFF)
22 option(FLATBUFFERS_ROOT "Location where the flatbuffers 'include' and 'lib' folders to be found" Off)
23 option(DYNAMIC_BACKEND_PATHS "Colon seperated list of paths where to load the dynamic backends from" "")
25 include(SelectLibraryConfigurations)
27 set(COMPILER_IS_GNU_LIKE 0)
28 if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU OR ${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
29 set(COMPILER_IS_GNU_LIKE 1)
32 # Enable CCache if available and not disabled
33 option(USE_CCACHE "USE_CCACHE" ON)
34 find_program(CCACHE_FOUND ccache)
35 if(CCACHE_FOUND AND USE_CCACHE)
36 get_property(rule_launch_compile DIRECTORY PROPERTY RULE_LAUNCH_COMPILE)
37 set_property(DIRECTORY PROPERTY RULE_LAUNCH_COMPILE "CCACHE_CPP2=yes ${rule_launch_compile} ccache")
40 # Enable distcc if available and not disabled
41 option(USE_DISTCC "USE_DISTCC" OFF)
42 find_program(DISTCC_FOUND distcc)
43 if(DISTCC_FOUND AND USE_DISTCC)
44 get_property(rule_launch_compile DIRECTORY PROPERTY RULE_LAUNCH_COMPILE)
45 set_property(DIRECTORY PROPERTY RULE_LAUNCH_COMPILE "${rule_launch_compile} distcc")
48 # Set to release configuration by default
49 if(NOT CMAKE_BUILD_TYPE)
50 set(CMAKE_BUILD_TYPE "Release")
53 # Compiler flags that are always set
54 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
55 if(COMPILER_IS_GNU_LIKE)
56 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Werror -Wold-style-cast -Wno-missing-braces -Wconversion -Wsign-conversion")
57 elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
58 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP")
59 add_definitions(-DNOMINMAX=1 -DNO_STRICT=1)
61 if("${CMAKE_SYSTEM_NAME}" STREQUAL Android)
62 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -llog")
63 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -llog")
66 # Compiler flags for Release builds
67 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG")
68 if(COMPILER_IS_GNU_LIKE)
69 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
70 elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
71 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD /O2")
74 # Compiler flags for Debug builds
75 if(COMPILER_IS_GNU_LIKE)
76 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0")
77 elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
78 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd /ZI /Od")
79 # Disable SAFESEH which is necessary for Edit and Continue to work
80 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /SAFESEH:NO")
81 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /SAFESEH:NO")
84 # Modify RelWithDebInfo so that NDEBUG isn't defined.
85 # This enables asserts.
86 if (COMPILER_IS_GNU_LIKE)
87 string(REPLACE "-DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
88 elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
89 string(REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
92 # Compiler flags for code coverage measurements
93 if(BUILD_FOR_COVERAGE)
94 if(NOT CMAKE_BUILD_TYPE EQUAL "Debug")
95 message(WARNING "BUILD_FOR_COVERAGE set so forcing to Debug build")
96 set(CMAKE_BUILD_TYPE "Debug")
99 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
100 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
103 if(BUILD_FOR_COVERAGE AND NOT BUILD_UNIT_TESTS)
104 message(WARNING "BUILD_FOR_COVERAGE set but not BUILD_UNIT_TESTS, so code coverage will not be able to run")
107 set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
110 add_definitions("-DBOOST_ALL_NO_LIB") # Turn off auto-linking as we specify the libs manually
111 set(Boost_USE_STATIC_LIBS ON)
112 find_package(Boost 1.59 REQUIRED COMPONENTS unit_test_framework system filesystem log program_options)
113 include_directories(SYSTEM "${Boost_INCLUDE_DIRS}")
114 link_directories(${Boost_LIBRARY_DIRS})
117 find_package (Threads)
119 # Favour the protobuf passed on command line
120 if(BUILD_TF_PARSER OR BUILD_CAFFE_PARSER OR BUILD_ONNX_PARSER)
121 find_library(PROTOBUF_LIBRARY_DEBUG NAMES "protobufd"
122 PATHS ${PROTOBUF_ROOT}/lib
123 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
124 find_library(PROTOBUF_LIBRARY_DEBUG NAMES "protobufd")
126 find_library(PROTOBUF_LIBRARY_RELEASE NAMES "protobuf"
127 PATHS ${PROTOBUF_ROOT}/lib
128 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
129 find_library(PROTOBUF_LIBRARY_RELEASE NAMES "protobuf")
131 select_library_configurations(PROTOBUF)
133 find_path(PROTOBUF_INCLUDE_DIRS "google/protobuf/message.h"
134 PATHS ${PROTOBUF_ROOT}/include
135 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
136 find_path(PROTOBUF_INCLUDE_DIRS "google/protobuf/message.h")
138 include_directories(SYSTEM "${PROTOBUF_INCLUDE_DIRS}")
139 add_definitions(-DPROTOBUF_USE_DLLS)
142 # Caffe and its dependencies
143 if(BUILD_CAFFE_PARSER)
144 add_definitions(-DARMNN_CAFFE_PARSER)
146 find_path(CAFFE_GENERATED_SOURCES "caffe/proto/caffe.pb.h"
147 HINTS ${CAFFE_BUILD_ROOT}/include)
148 include_directories(SYSTEM "${CAFFE_GENERATED_SOURCES}")
152 add_definitions(-DARMNN_TF_PARSER)
154 find_path(TF_GENERATED_SOURCES "tensorflow/core/protobuf/saved_model.pb.cc")
156 # C++ sources generated for tf protobufs
157 file(GLOB_RECURSE TF_PROTOBUFS "${TF_GENERATED_SOURCES}/*.pb.cc")
159 # C++ headers generated for tf protobufs
160 include_directories(SYSTEM "${TF_GENERATED_SOURCES}")
163 if(BUILD_ONNX_PARSER)
164 add_definitions(-DARMNN_ONNX_PARSER)
166 find_path(ONNX_GENERATED_SOURCES "onnx/onnx.pb.cc")
168 # C++ headers generated for onnx protobufs
169 include_directories(SYSTEM "${ONNX_GENERATED_SOURCES}")
172 # Flatbuffers support for TF Lite and Armnn Serializer
173 if(BUILD_TF_LITE_PARSER OR BUILD_ARMNN_SERIALIZER)
174 # verify we have a valid flatbuffers include path
175 find_path(FLATBUFFERS_INCLUDE_PATH flatbuffers/flatbuffers.h
176 HINTS ${FLATBUFFERS_ROOT}/include /usr/local/include /usr/include)
178 message(STATUS "Flatbuffers headers are located at: ${FLATBUFFERS_INCLUDE_PATH}")
180 find_library(FLATBUFFERS_LIBRARY
181 NAMES libflatbuffers.a flatbuffers
182 HINTS ${FLATBUFFERS_ROOT}/lib /usr/local/lib /usr/lib)
184 message(STATUS "Flatbuffers library located at: ${FLATBUFFERS_LIBRARY}")
187 # Flatbuffers schema support for TF Lite
188 if(BUILD_TF_LITE_PARSER)
189 find_path(TF_LITE_SCHEMA_INCLUDE_PATH
191 HINTS ${TF_LITE_GENERATED_PATH})
193 message(STATUS "Tf Lite generated header found at: ${TF_LITE_SCHEMA_INCLUDE_PATH}")
195 add_definitions(-DARMNN_TF_LITE_PARSER)
196 add_definitions(-DARMNN_TF_LITE_SCHEMA_PATH="${TF_LITE_SCHEMA_INCLUDE_PATH}/schema.fbs")
199 if(BUILD_ARMNN_SERIALIZER)
200 add_definitions(-DARMNN_SERIALIZER)
201 add_definitions(-DARMNN_SERIALIZER_SCHEMA_PATH="${CMAKE_CURRENT_SOURCE_DIR}/src/armnnSerializer/ArmnnSchema.fbs")
204 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
207 # Note that ARM Compute has a different folder layout depending on the branch but also on
208 # whether it comes from a prepackaged archive (this is why we add several hints below)
209 if(ARMCOMPUTENEON OR ARMCOMPUTECL)
210 find_path(ARMCOMPUTE_INCLUDE arm_compute/core/CL/ICLKernel.h
211 PATHS ${ARMCOMPUTE_ROOT}/include
212 PATHS ${ARMCOMPUTE_ROOT}/applications/arm_compute
213 PATHS ${ARMCOMPUTE_ROOT}
214 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
215 find_path(ARMCOMPUTE_INCLUDE arm_compute/core/CL/ICLKernel.h)
216 include_directories(SYSTEM "${ARMCOMPUTE_INCLUDE}")
218 # Find the Arm Compute libraries if not already specified (the user may have already defined this in advance,
219 # e.g. if building clframework as a dependent cmake project)
220 if (NOT DEFINED ARMCOMPUTE_LIBRARIES)
221 # We link to the static variant so that customers don't need to find and build a compatible version of clframework.
222 # First try the folders specified ARMCOMPUTE_BUILD_DIR (with PATH_SUFFIXES for
224 find_library(ARMCOMPUTE_LIBRARY_DEBUG NAMES arm_compute-static
225 PATHS ${ARMCOMPUTE_BUILD_DIR}
226 PATH_SUFFIXES "Debug"
227 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
228 find_library(ARMCOMPUTE_LIBRARY_RELEASE NAMES arm_compute-static
229 PATHS ${ARMCOMPUTE_BUILD_DIR}
230 PATH_SUFFIXES "Release"
231 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
232 find_library(ARMCOMPUTE_CORE_LIBRARY_DEBUG NAMES arm_compute_core-static
233 PATHS ${ARMCOMPUTE_BUILD_DIR}
234 PATH_SUFFIXES "Debug"
235 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
236 find_library(ARMCOMPUTE_CORE_LIBRARY_RELEASE NAMES arm_compute_core-static
237 PATHS ${ARMCOMPUTE_BUILD_DIR}
238 PATH_SUFFIXES "Release"
239 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
241 # In case it wasn't there, try a default search (will work in cases where
242 # the library has been installed into a standard location)
243 find_library(ARMCOMPUTE_LIBRARY_DEBUG NAMES arm_compute-static)
244 find_library(ARMCOMPUTE_LIBRARY_RELEASE NAMES arm_compute-static)
245 find_library(ARMCOMPUTE_CORE_LIBRARY_DEBUG NAMES arm_compute_core-static)
246 find_library(ARMCOMPUTE_CORE_LIBRARY_RELEASE NAMES arm_compute_core-static)
248 set(ARMCOMPUTE_LIBRARIES
249 debug ${ARMCOMPUTE_LIBRARY_DEBUG} ${ARMCOMPUTE_CORE_LIBRARY_DEBUG}
250 optimized ${ARMCOMPUTE_LIBRARY_RELEASE} ${ARMCOMPUTE_CORE_LIBRARY_RELEASE} )
254 # ARM Compute NEON backend
256 # Add preprocessor definition for ARM Compute NEON
257 add_definitions(-DARMCOMPUTENEON_ENABLED)
258 # The ARM Compute headers contain some NEON intrinsics, so we need to build armnn with NEON support on armv7
259 if(${CMAKE_SYSTEM_PROCESSOR} MATCHES armv7 AND COMPILER_IS_GNU_LIKE)
260 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon")
264 # ARM Compute OpenCL backend
266 # Always use Arm compute library OpenCL headers
267 find_path(OPENCL_INCLUDE CL/cl2.hpp
268 PATHS ${ARMCOMPUTE_ROOT}/include
269 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
271 # Link against libOpenCL in opencl-1.2-stubs, but don't search there at runtime
272 link_libraries(-L${ARMCOMPUTE_BUILD_DIR}/opencl-1.2-stubs)
273 set(OPENCL_LIBRARIES OpenCL)
275 include_directories(${OPENCL_INCLUDE})
277 # Add preprocessor definition for ARM Compute OpenCL
278 add_definitions(-DARMCOMPUTECL_ENABLED)
280 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DARM_COMPUTE_DEBUG_ENABLED")
283 # Used by both Arm Compute backends, but should be added
284 # to the search path after the system directories if necessary
285 if(ARMCOMPUTENEON OR ARMCOMPUTECL)
286 find_path(HALF_INCLUDE half/half.hpp)
287 find_path(HALF_INCLUDE half/half.hpp
288 PATHS ${ARMCOMPUTE_ROOT}/include
289 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
290 include_directories(SYSTEM ${HALF_INCLUDE})
293 # ARM Compute reference backend
295 add_definitions(-DARMCOMPUTEREF_ENABLED)
298 # Streamline annotate
299 if(PROFILING_BACKEND_STREAMLINE)
300 include_directories("${GATOR_ROOT}/annotate")
301 add_definitions(-DARMNN_STREAMLINE_ENABLED)
304 if(HEAP_PROFILING OR LEAK_CHECKING)
305 # enable heap profiling for everything except for referencetests
306 if(NOT ${PROJECT_NAME} STREQUAL "referencetests")
307 find_path(HEAP_PROFILER_INCLUDE gperftools/heap-profiler.h
308 PATHS ${GPERFTOOLS_ROOT}/include
309 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
310 include_directories(SYSTEM "${HEAP_PROFILER_INCLUDE}")
311 find_library(GPERF_TOOLS_LIBRARY
313 HINTS ${GPERFTOOLS_ROOT}/lib)
314 link_directories(${GPERFTOOLS_ROOT}/lib)
316 link_libraries(${GPERF_TOOLS_LIBRARY})
318 add_definitions("-DARMNN_HEAP_PROFILING_ENABLED=1")
321 add_definitions("-DARMNN_LEAK_CHECKING_ENABLED=1")
324 message("Heap profiling and leak checking are disabled for referencetests")
327 # Valgrind only works with gperftools version number <= 2.4
328 CHECK_INCLUDE_FILE(valgrind/memcheck.h VALGRIND_FOUND)
332 if(NOT BUILD_CAFFE_PARSER)
333 message(STATUS "Caffe parser support is disabled")
336 if(NOT BUILD_TF_PARSER)
337 message(STATUS "Tensorflow parser support is disabled")
340 if(NOT BUILD_TF_LITE_PARSER)
341 message(STATUS "Tensorflow Lite parser support is disabled")
344 if(NOT BUILD_ARMNN_SERIALIZER)
345 message(STATUS "Armnn Serializer support is disabled")
348 if(NOT BUILD_ARMNN_QUANTIZER)
349 message(STATUS "ArmNN Quantizer support is disabled")
352 # ArmNN source files required for all build options
353 include_directories(SYSTEM third-party)