blob: 0063cd19d5f1eef03f0f5ae7f610c265b81613cc [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001option(BUILD_CAFFE_PARSER "Build Caffe parser" OFF)
2option(BUILD_TF_PARSER "Build Tensorflow parser" OFF)
telsoa01c577f2c2018-08-31 09:22:23 +01003option(BUILD_ONNX_PARSER "Build Onnx parser" OFF)
telsoa014fcda012018-03-09 14:13:49 +00004option(BUILD_UNIT_TESTS "Build unit tests" ON)
5option(BUILD_TESTS "Build test applications" OFF)
6option(BUILD_FOR_COVERAGE "Use no optimization and output .gcno and .gcda files" OFF)
7option(ARMCOMPUTENEON "Build with ARM Compute NEON support" OFF)
8option(ARMCOMPUTECL "Build with ARM Compute OpenCL support" OFF)
Matteo Martincighe67edb22019-08-14 14:05:46 +01009option(ARMCOMPUTEREF "Build with ARM Compute reference support" OFF)
telsoa014fcda012018-03-09 14:13:49 +000010option(PROFILING_BACKEND_STREAMLINE "Forward the armNN profiling events to DS-5/Streamline as annotations" OFF)
telsoa01c577f2c2018-08-31 09:22:23 +010011# options used for heap profiling and leak checking
surmeh013537c2c2018-05-18 16:31:43 +010012option(HEAP_PROFILING "Build with heap profiling enabled" OFF)
telsoa01c577f2c2018-08-31 09:22:23 +010013option(LEAK_CHECKING "Build with leak checking enabled" OFF)
surmeh013537c2c2018-05-18 16:31:43 +010014option(GPERFTOOLS_ROOT "Location where the gperftools 'include' and 'lib' folders to be found" Off)
telsoa01c577f2c2018-08-31 09:22:23 +010015# options used for tensorflow lite support
16option(BUILD_TF_LITE_PARSER "Build Tensorflow Lite parser" OFF)
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +000017option(BUILD_ARMNN_SERIALIZER "Build Armnn Serializer" OFF)
Jim Flynn3091b062019-02-15 14:45:04 +000018option(BUILD_ARMNN_QUANTIZER "Build ArmNN quantizer" OFF)
Éanna Ó Catháina563b922019-05-09 11:34:06 +010019option(BUILD_ACCURACY_TOOL "Build Accuracy Tool" OFF)
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +000020option(FLATC_DIR "Path to Flatbuffers compiler" OFF)
telsoa01c577f2c2018-08-31 09:22:23 +010021option(TF_LITE_GENERATED_PATH "Tensorflow lite generated C++ schema location" OFF)
22option(FLATBUFFERS_ROOT "Location where the flatbuffers 'include' and 'lib' folders to be found" Off)
Matteo Martincighe7d44982019-08-05 12:16:47 +010023option(DYNAMIC_BACKEND_PATHS "Colon seperated list of paths where to load the dynamic backends from" "")
telsoa014fcda012018-03-09 14:13:49 +000024
25include(SelectLibraryConfigurations)
26
27set(COMPILER_IS_GNU_LIKE 0)
28if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU OR ${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
29 set(COMPILER_IS_GNU_LIKE 1)
30endif()
31
32# Enable CCache if available and not disabled
33option(USE_CCACHE "USE_CCACHE" ON)
34find_program(CCACHE_FOUND ccache)
35if(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")
38endif()
39
40# Enable distcc if available and not disabled
41option(USE_DISTCC "USE_DISTCC" OFF)
42find_program(DISTCC_FOUND distcc)
43if(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")
46endif()
47
48# Set to release configuration by default
49if(NOT CMAKE_BUILD_TYPE)
50 set(CMAKE_BUILD_TYPE "Release")
51endif()
52
53# Compiler flags that are always set
54set(CMAKE_POSITION_INDEPENDENT_CODE ON)
55if(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")
57elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
58 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP")
59 add_definitions(-DNOMINMAX=1 -DNO_STRICT=1)
60endif()
61if("${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")
64endif()
65
66# Compiler flags for Release builds
Matthew Benthame30054f2019-06-24 13:10:54 +010067set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG")
telsoa014fcda012018-03-09 14:13:49 +000068if(COMPILER_IS_GNU_LIKE)
69 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
70elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
71 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD /O2")
72endif()
73
74# Compiler flags for Debug builds
75if(COMPILER_IS_GNU_LIKE)
Matthew Benthame30054f2019-06-24 13:10:54 +010076 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0")
telsoa014fcda012018-03-09 14:13:49 +000077elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
Matthew Benthame30054f2019-06-24 13:10:54 +010078 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd /ZI /Od")
telsoa014fcda012018-03-09 14:13:49 +000079 # 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")
82endif()
83
84# Modify RelWithDebInfo so that NDEBUG isn't defined.
85# This enables asserts.
86if (COMPILER_IS_GNU_LIKE)
87 string(REPLACE "-DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
88elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
89 string(REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
90endif()
91
92# Compiler flags for code coverage measurements
93if(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")
97 endif()
98
99 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
100 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
101endif()
102
103if(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")
105endif()
106
107set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
108
109# Boost
110add_definitions("-DBOOST_ALL_NO_LIB") # Turn off auto-linking as we specify the libs manually
111set(Boost_USE_STATIC_LIBS ON)
112find_package(Boost 1.59 REQUIRED COMPONENTS unit_test_framework system filesystem log program_options)
Matthew Bentham97fb2de2019-07-12 17:26:57 +0100113include_directories(SYSTEM "${Boost_INCLUDE_DIRS}")
114link_directories(${Boost_LIBRARY_DIRS})
telsoa014fcda012018-03-09 14:13:49 +0000115
116# pthread
117find_package (Threads)
118
119# Favour the protobuf passed on command line
telsoa01c577f2c2018-08-31 09:22:23 +0100120if(BUILD_TF_PARSER OR BUILD_CAFFE_PARSER OR BUILD_ONNX_PARSER)
telsoa014fcda012018-03-09 14:13:49 +0000121 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")
125
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")
130
131 select_library_configurations(PROTOBUF)
132
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")
137
138 include_directories(SYSTEM "${PROTOBUF_INCLUDE_DIRS}")
139 add_definitions(-DPROTOBUF_USE_DLLS)
140endif()
141
142# Caffe and its dependencies
143if(BUILD_CAFFE_PARSER)
144 add_definitions(-DARMNN_CAFFE_PARSER)
145
146 find_path(CAFFE_GENERATED_SOURCES "caffe/proto/caffe.pb.h"
147 HINTS ${CAFFE_BUILD_ROOT}/include)
148 include_directories(SYSTEM "${CAFFE_GENERATED_SOURCES}")
149endif()
150
151if(BUILD_TF_PARSER)
152 add_definitions(-DARMNN_TF_PARSER)
153
154 find_path(TF_GENERATED_SOURCES "tensorflow/core/protobuf/saved_model.pb.cc")
155
156 # C++ sources generated for tf protobufs
157 file(GLOB_RECURSE TF_PROTOBUFS "${TF_GENERATED_SOURCES}/*.pb.cc")
158
159 # C++ headers generated for tf protobufs
160 include_directories(SYSTEM "${TF_GENERATED_SOURCES}")
161endif()
162
telsoa01c577f2c2018-08-31 09:22:23 +0100163if(BUILD_ONNX_PARSER)
164 add_definitions(-DARMNN_ONNX_PARSER)
165
166 find_path(ONNX_GENERATED_SOURCES "onnx/onnx.pb.cc")
167
168 # C++ headers generated for onnx protobufs
169 include_directories(SYSTEM "${ONNX_GENERATED_SOURCES}")
170endif()
171
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000172# Flatbuffers support for TF Lite and Armnn Serializer
173if(BUILD_TF_LITE_PARSER OR BUILD_ARMNN_SERIALIZER)
telsoa01c577f2c2018-08-31 09:22:23 +0100174 # 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)
177
Matthew Benthamd1ae3a62019-02-22 17:30:32 +0000178 message(STATUS "Flatbuffers headers are located at: ${FLATBUFFERS_INCLUDE_PATH}")
telsoa01c577f2c2018-08-31 09:22:23 +0100179
180 find_library(FLATBUFFERS_LIBRARY
181 NAMES libflatbuffers.a flatbuffers
182 HINTS ${FLATBUFFERS_ROOT}/lib /usr/local/lib /usr/lib)
183
Matthew Benthamd1ae3a62019-02-22 17:30:32 +0000184 message(STATUS "Flatbuffers library located at: ${FLATBUFFERS_LIBRARY}")
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000185endif()
186
187# Flatbuffers schema support for TF Lite
188if(BUILD_TF_LITE_PARSER)
189 find_path(TF_LITE_SCHEMA_INCLUDE_PATH
190 schema_generated.h
191 HINTS ${TF_LITE_GENERATED_PATH})
192
Matthew Benthamd1ae3a62019-02-22 17:30:32 +0000193 message(STATUS "Tf Lite generated header found at: ${TF_LITE_SCHEMA_INCLUDE_PATH}")
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000194
Matthew Benthamd1ae3a62019-02-22 17:30:32 +0000195 add_definitions(-DARMNN_TF_LITE_PARSER)
196 add_definitions(-DARMNN_TF_LITE_SCHEMA_PATH="${TF_LITE_SCHEMA_INCLUDE_PATH}/schema.fbs")
telsoa01c577f2c2018-08-31 09:22:23 +0100197endif()
198
Kevin May43a799c2019-02-08 16:31:42 +0000199if(BUILD_ARMNN_SERIALIZER)
Kevin May43a799c2019-02-08 16:31:42 +0000200 add_definitions(-DARMNN_SERIALIZER)
Matthew Bentham268509a2019-02-25 13:58:24 +0000201 add_definitions(-DARMNN_SERIALIZER_SCHEMA_PATH="${CMAKE_CURRENT_SOURCE_DIR}/src/armnnSerializer/ArmnnSchema.fbs")
Kevin May43a799c2019-02-08 16:31:42 +0000202endif()
203
telsoa014fcda012018-03-09 14:13:49 +0000204include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
205
206# ARM Compute
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)
209if(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}")
217
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
223 # Windows builds)
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)
240
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)
247
248 set(ARMCOMPUTE_LIBRARIES
249 debug ${ARMCOMPUTE_LIBRARY_DEBUG} ${ARMCOMPUTE_CORE_LIBRARY_DEBUG}
250 optimized ${ARMCOMPUTE_LIBRARY_RELEASE} ${ARMCOMPUTE_CORE_LIBRARY_RELEASE} )
251 endif()
252endif()
253
254# ARM Compute NEON backend
255if(ARMCOMPUTENEON)
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")
261 endif()
262endif()
263
264# ARM Compute OpenCL backend
265if(ARMCOMPUTECL)
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)
270
Matthew Bentham3b72db02018-10-11 09:47:01 +0100271 # 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)
telsoa014fcda012018-03-09 14:13:49 +0000274
275 include_directories(${OPENCL_INCLUDE})
276
277 # Add preprocessor definition for ARM Compute OpenCL
278 add_definitions(-DARMCOMPUTECL_ENABLED)
279
280 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DARM_COMPUTE_DEBUG_ENABLED")
281endif()
282
283# Used by both Arm Compute backends, but should be added
284# to the search path after the system directories if necessary
285if(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)
telsoa01c577f2c2018-08-31 09:22:23 +0100290 include_directories(SYSTEM ${HALF_INCLUDE})
telsoa014fcda012018-03-09 14:13:49 +0000291endif()
292
Matteo Martincighe67edb22019-08-14 14:05:46 +0100293# ARM Compute reference backend
294if(ARMCOMPUTEREF)
295 add_definitions(-DARMCOMPUTEREF_ENABLED)
296endif()
297
telsoa014fcda012018-03-09 14:13:49 +0000298# Streamline annotate
299if(PROFILING_BACKEND_STREAMLINE)
300 include_directories("${GATOR_ROOT}/annotate")
301 add_definitions(-DARMNN_STREAMLINE_ENABLED)
302endif()
303
telsoa01c577f2c2018-08-31 09:22:23 +0100304if(HEAP_PROFILING OR LEAK_CHECKING)
surmeh013537c2c2018-05-18 16:31:43 +0100305 # 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
312 NAMES tcmalloc_debug
313 HINTS ${GPERFTOOLS_ROOT}/lib)
314 link_directories(${GPERFTOOLS_ROOT}/lib)
315
316 link_libraries(${GPERF_TOOLS_LIBRARY})
telsoa01c577f2c2018-08-31 09:22:23 +0100317 if (HEAP_PROFILING)
318 add_definitions("-DARMNN_HEAP_PROFILING_ENABLED=1")
319 endif()
320 if (LEAK_CHECKING)
321 add_definitions("-DARMNN_LEAK_CHECKING_ENABLED=1")
322 endif()
surmeh013537c2c2018-05-18 16:31:43 +0100323 else()
telsoa01c577f2c2018-08-31 09:22:23 +0100324 message("Heap profiling and leak checking are disabled for referencetests")
surmeh013537c2c2018-05-18 16:31:43 +0100325 endif()
326else()
327 # Valgrind only works with gperftools version number <= 2.4
328 CHECK_INCLUDE_FILE(valgrind/memcheck.h VALGRIND_FOUND)
329endif()
330
331
332if(NOT BUILD_CAFFE_PARSER)
333 message(STATUS "Caffe parser support is disabled")
334endif()
335
336if(NOT BUILD_TF_PARSER)
337 message(STATUS "Tensorflow parser support is disabled")
338endif()
339
telsoa01c577f2c2018-08-31 09:22:23 +0100340if(NOT BUILD_TF_LITE_PARSER)
341 message(STATUS "Tensorflow Lite parser support is disabled")
342endif()
David Beck10b4dfd2018-09-19 12:03:20 +0100343
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000344if(NOT BUILD_ARMNN_SERIALIZER)
345 message(STATUS "Armnn Serializer support is disabled")
346endif()
347
Jim Flynn3091b062019-02-15 14:45:04 +0000348if(NOT BUILD_ARMNN_QUANTIZER)
349 message(STATUS "ArmNN Quantizer support is disabled")
350endif()
351
David Beck10b4dfd2018-09-19 12:03:20 +0100352# ArmNN source files required for all build options
353include_directories(SYSTEM third-party)