blob: 8dad57a297833b70ce930721575b17320af72a20 [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)
telsoa014fcda012018-03-09 14:13:49 +00009option(PROFILING_BACKEND_STREAMLINE "Forward the armNN profiling events to DS-5/Streamline as annotations" OFF)
telsoa01c577f2c2018-08-31 09:22:23 +010010# options used for heap profiling and leak checking
surmeh013537c2c2018-05-18 16:31:43 +010011option(HEAP_PROFILING "Build with heap profiling enabled" OFF)
telsoa01c577f2c2018-08-31 09:22:23 +010012option(LEAK_CHECKING "Build with leak checking enabled" OFF)
surmeh013537c2c2018-05-18 16:31:43 +010013option(GPERFTOOLS_ROOT "Location where the gperftools 'include' and 'lib' folders to be found" Off)
telsoa01c577f2c2018-08-31 09:22:23 +010014# options used for tensorflow lite support
15option(BUILD_TF_LITE_PARSER "Build Tensorflow Lite parser" OFF)
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +000016option(BUILD_ARMNN_SERIALIZER "Build Armnn Serializer" OFF)
Jim Flynn3091b062019-02-15 14:45:04 +000017option(BUILD_ARMNN_QUANTIZER "Build ArmNN quantizer" OFF)
Éanna Ó Catháina563b922019-05-09 11:34:06 +010018option(BUILD_ACCURACY_TOOL "Build Accuracy Tool" OFF)
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +000019option(FLATC_DIR "Path to Flatbuffers compiler" OFF)
telsoa01c577f2c2018-08-31 09:22:23 +010020option(TF_LITE_GENERATED_PATH "Tensorflow lite generated C++ schema location" OFF)
21option(FLATBUFFERS_ROOT "Location where the flatbuffers 'include' and 'lib' folders to be found" Off)
Matteo Martincighe7d44982019-08-05 12:16:47 +010022option(DYNAMIC_BACKEND_PATHS "Colon seperated list of paths where to load the dynamic backends from" "")
telsoa014fcda012018-03-09 14:13:49 +000023
24include(SelectLibraryConfigurations)
25
26set(COMPILER_IS_GNU_LIKE 0)
27if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU OR ${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
28 set(COMPILER_IS_GNU_LIKE 1)
29endif()
30
31# Enable CCache if available and not disabled
32option(USE_CCACHE "USE_CCACHE" ON)
33find_program(CCACHE_FOUND ccache)
34if(CCACHE_FOUND AND USE_CCACHE)
35 get_property(rule_launch_compile DIRECTORY PROPERTY RULE_LAUNCH_COMPILE)
36 set_property(DIRECTORY PROPERTY RULE_LAUNCH_COMPILE "CCACHE_CPP2=yes ${rule_launch_compile} ccache")
37endif()
38
39# Enable distcc if available and not disabled
40option(USE_DISTCC "USE_DISTCC" OFF)
41find_program(DISTCC_FOUND distcc)
42if(DISTCC_FOUND AND USE_DISTCC)
43 get_property(rule_launch_compile DIRECTORY PROPERTY RULE_LAUNCH_COMPILE)
44 set_property(DIRECTORY PROPERTY RULE_LAUNCH_COMPILE "${rule_launch_compile} distcc")
45endif()
46
47# Set to release configuration by default
48if(NOT CMAKE_BUILD_TYPE)
49 set(CMAKE_BUILD_TYPE "Release")
50endif()
51
52# Compiler flags that are always set
53set(CMAKE_POSITION_INDEPENDENT_CODE ON)
54if(COMPILER_IS_GNU_LIKE)
55 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Werror -Wold-style-cast -Wno-missing-braces -Wconversion -Wsign-conversion")
56elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
57 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP")
58 add_definitions(-DNOMINMAX=1 -DNO_STRICT=1)
59endif()
60if("${CMAKE_SYSTEM_NAME}" STREQUAL Android)
61 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -llog")
62 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -llog")
63endif()
64
65# Compiler flags for Release builds
Matthew Benthame30054f2019-06-24 13:10:54 +010066set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG")
telsoa014fcda012018-03-09 14:13:49 +000067if(COMPILER_IS_GNU_LIKE)
68 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
69elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
70 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD /O2")
71endif()
72
73# Compiler flags for Debug builds
74if(COMPILER_IS_GNU_LIKE)
Matthew Benthame30054f2019-06-24 13:10:54 +010075 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0")
telsoa014fcda012018-03-09 14:13:49 +000076elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
Matthew Benthame30054f2019-06-24 13:10:54 +010077 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd /ZI /Od")
telsoa014fcda012018-03-09 14:13:49 +000078 # Disable SAFESEH which is necessary for Edit and Continue to work
79 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /SAFESEH:NO")
80 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /SAFESEH:NO")
81endif()
82
83# Modify RelWithDebInfo so that NDEBUG isn't defined.
84# This enables asserts.
85if (COMPILER_IS_GNU_LIKE)
86 string(REPLACE "-DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
87elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
88 string(REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
89endif()
90
91# Compiler flags for code coverage measurements
92if(BUILD_FOR_COVERAGE)
93 if(NOT CMAKE_BUILD_TYPE EQUAL "Debug")
94 message(WARNING "BUILD_FOR_COVERAGE set so forcing to Debug build")
95 set(CMAKE_BUILD_TYPE "Debug")
96 endif()
97
98 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
99 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
100endif()
101
102if(BUILD_FOR_COVERAGE AND NOT BUILD_UNIT_TESTS)
103 message(WARNING "BUILD_FOR_COVERAGE set but not BUILD_UNIT_TESTS, so code coverage will not be able to run")
104endif()
105
106set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
107
108# Boost
109add_definitions("-DBOOST_ALL_NO_LIB") # Turn off auto-linking as we specify the libs manually
110set(Boost_USE_STATIC_LIBS ON)
111find_package(Boost 1.59 REQUIRED COMPONENTS unit_test_framework system filesystem log program_options)
Matthew Bentham97fb2de2019-07-12 17:26:57 +0100112include_directories(SYSTEM "${Boost_INCLUDE_DIRS}")
113link_directories(${Boost_LIBRARY_DIRS})
telsoa014fcda012018-03-09 14:13:49 +0000114
115# pthread
116find_package (Threads)
117
118# Favour the protobuf passed on command line
telsoa01c577f2c2018-08-31 09:22:23 +0100119if(BUILD_TF_PARSER OR BUILD_CAFFE_PARSER OR BUILD_ONNX_PARSER)
telsoa014fcda012018-03-09 14:13:49 +0000120 find_library(PROTOBUF_LIBRARY_DEBUG NAMES "protobufd"
121 PATHS ${PROTOBUF_ROOT}/lib
122 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
123 find_library(PROTOBUF_LIBRARY_DEBUG NAMES "protobufd")
124
125 find_library(PROTOBUF_LIBRARY_RELEASE NAMES "protobuf"
126 PATHS ${PROTOBUF_ROOT}/lib
127 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
128 find_library(PROTOBUF_LIBRARY_RELEASE NAMES "protobuf")
129
130 select_library_configurations(PROTOBUF)
131
132 find_path(PROTOBUF_INCLUDE_DIRS "google/protobuf/message.h"
133 PATHS ${PROTOBUF_ROOT}/include
134 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
135 find_path(PROTOBUF_INCLUDE_DIRS "google/protobuf/message.h")
136
137 include_directories(SYSTEM "${PROTOBUF_INCLUDE_DIRS}")
138 add_definitions(-DPROTOBUF_USE_DLLS)
139endif()
140
141# Caffe and its dependencies
142if(BUILD_CAFFE_PARSER)
143 add_definitions(-DARMNN_CAFFE_PARSER)
144
145 find_path(CAFFE_GENERATED_SOURCES "caffe/proto/caffe.pb.h"
146 HINTS ${CAFFE_BUILD_ROOT}/include)
147 include_directories(SYSTEM "${CAFFE_GENERATED_SOURCES}")
148endif()
149
150if(BUILD_TF_PARSER)
151 add_definitions(-DARMNN_TF_PARSER)
152
153 find_path(TF_GENERATED_SOURCES "tensorflow/core/protobuf/saved_model.pb.cc")
154
155 # C++ sources generated for tf protobufs
156 file(GLOB_RECURSE TF_PROTOBUFS "${TF_GENERATED_SOURCES}/*.pb.cc")
157
158 # C++ headers generated for tf protobufs
159 include_directories(SYSTEM "${TF_GENERATED_SOURCES}")
160endif()
161
telsoa01c577f2c2018-08-31 09:22:23 +0100162if(BUILD_ONNX_PARSER)
163 add_definitions(-DARMNN_ONNX_PARSER)
164
165 find_path(ONNX_GENERATED_SOURCES "onnx/onnx.pb.cc")
166
167 # C++ headers generated for onnx protobufs
168 include_directories(SYSTEM "${ONNX_GENERATED_SOURCES}")
169endif()
170
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000171# Flatbuffers support for TF Lite and Armnn Serializer
172if(BUILD_TF_LITE_PARSER OR BUILD_ARMNN_SERIALIZER)
telsoa01c577f2c2018-08-31 09:22:23 +0100173 # verify we have a valid flatbuffers include path
174 find_path(FLATBUFFERS_INCLUDE_PATH flatbuffers/flatbuffers.h
175 HINTS ${FLATBUFFERS_ROOT}/include /usr/local/include /usr/include)
176
Matthew Benthamd1ae3a62019-02-22 17:30:32 +0000177 message(STATUS "Flatbuffers headers are located at: ${FLATBUFFERS_INCLUDE_PATH}")
telsoa01c577f2c2018-08-31 09:22:23 +0100178
179 find_library(FLATBUFFERS_LIBRARY
180 NAMES libflatbuffers.a flatbuffers
181 HINTS ${FLATBUFFERS_ROOT}/lib /usr/local/lib /usr/lib)
182
Matthew Benthamd1ae3a62019-02-22 17:30:32 +0000183 message(STATUS "Flatbuffers library located at: ${FLATBUFFERS_LIBRARY}")
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000184endif()
185
186# Flatbuffers schema support for TF Lite
187if(BUILD_TF_LITE_PARSER)
188 find_path(TF_LITE_SCHEMA_INCLUDE_PATH
189 schema_generated.h
190 HINTS ${TF_LITE_GENERATED_PATH})
191
Matthew Benthamd1ae3a62019-02-22 17:30:32 +0000192 message(STATUS "Tf Lite generated header found at: ${TF_LITE_SCHEMA_INCLUDE_PATH}")
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000193
Matthew Benthamd1ae3a62019-02-22 17:30:32 +0000194 add_definitions(-DARMNN_TF_LITE_PARSER)
195 add_definitions(-DARMNN_TF_LITE_SCHEMA_PATH="${TF_LITE_SCHEMA_INCLUDE_PATH}/schema.fbs")
telsoa01c577f2c2018-08-31 09:22:23 +0100196endif()
197
Kevin May43a799c2019-02-08 16:31:42 +0000198if(BUILD_ARMNN_SERIALIZER)
Kevin May43a799c2019-02-08 16:31:42 +0000199 add_definitions(-DARMNN_SERIALIZER)
Matthew Bentham268509a2019-02-25 13:58:24 +0000200 add_definitions(-DARMNN_SERIALIZER_SCHEMA_PATH="${CMAKE_CURRENT_SOURCE_DIR}/src/armnnSerializer/ArmnnSchema.fbs")
Kevin May43a799c2019-02-08 16:31:42 +0000201endif()
202
telsoa014fcda012018-03-09 14:13:49 +0000203include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
204
205# ARM Compute
206# Note that ARM Compute has a different folder layout depending on the branch but also on
207# whether it comes from a prepackaged archive (this is why we add several hints below)
208if(ARMCOMPUTENEON OR ARMCOMPUTECL)
209 find_path(ARMCOMPUTE_INCLUDE arm_compute/core/CL/ICLKernel.h
210 PATHS ${ARMCOMPUTE_ROOT}/include
211 PATHS ${ARMCOMPUTE_ROOT}/applications/arm_compute
212 PATHS ${ARMCOMPUTE_ROOT}
213 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
214 find_path(ARMCOMPUTE_INCLUDE arm_compute/core/CL/ICLKernel.h)
215 include_directories(SYSTEM "${ARMCOMPUTE_INCLUDE}")
216
217 # Find the Arm Compute libraries if not already specified (the user may have already defined this in advance,
218 # e.g. if building clframework as a dependent cmake project)
219 if (NOT DEFINED ARMCOMPUTE_LIBRARIES)
220 # We link to the static variant so that customers don't need to find and build a compatible version of clframework.
221 # First try the folders specified ARMCOMPUTE_BUILD_DIR (with PATH_SUFFIXES for
222 # Windows builds)
223 find_library(ARMCOMPUTE_LIBRARY_DEBUG NAMES arm_compute-static
224 PATHS ${ARMCOMPUTE_BUILD_DIR}
225 PATH_SUFFIXES "Debug"
226 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
227 find_library(ARMCOMPUTE_LIBRARY_RELEASE NAMES arm_compute-static
228 PATHS ${ARMCOMPUTE_BUILD_DIR}
229 PATH_SUFFIXES "Release"
230 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
231 find_library(ARMCOMPUTE_CORE_LIBRARY_DEBUG NAMES arm_compute_core-static
232 PATHS ${ARMCOMPUTE_BUILD_DIR}
233 PATH_SUFFIXES "Debug"
234 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
235 find_library(ARMCOMPUTE_CORE_LIBRARY_RELEASE NAMES arm_compute_core-static
236 PATHS ${ARMCOMPUTE_BUILD_DIR}
237 PATH_SUFFIXES "Release"
238 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
239
240 # In case it wasn't there, try a default search (will work in cases where
241 # the library has been installed into a standard location)
242 find_library(ARMCOMPUTE_LIBRARY_DEBUG NAMES arm_compute-static)
243 find_library(ARMCOMPUTE_LIBRARY_RELEASE NAMES arm_compute-static)
244 find_library(ARMCOMPUTE_CORE_LIBRARY_DEBUG NAMES arm_compute_core-static)
245 find_library(ARMCOMPUTE_CORE_LIBRARY_RELEASE NAMES arm_compute_core-static)
246
247 set(ARMCOMPUTE_LIBRARIES
248 debug ${ARMCOMPUTE_LIBRARY_DEBUG} ${ARMCOMPUTE_CORE_LIBRARY_DEBUG}
249 optimized ${ARMCOMPUTE_LIBRARY_RELEASE} ${ARMCOMPUTE_CORE_LIBRARY_RELEASE} )
250 endif()
251endif()
252
253# ARM Compute NEON backend
254if(ARMCOMPUTENEON)
255 # Add preprocessor definition for ARM Compute NEON
256 add_definitions(-DARMCOMPUTENEON_ENABLED)
257 # The ARM Compute headers contain some NEON intrinsics, so we need to build armnn with NEON support on armv7
258 if(${CMAKE_SYSTEM_PROCESSOR} MATCHES armv7 AND COMPILER_IS_GNU_LIKE)
259 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon")
260 endif()
261endif()
262
263# ARM Compute OpenCL backend
264if(ARMCOMPUTECL)
265 # Always use Arm compute library OpenCL headers
266 find_path(OPENCL_INCLUDE CL/cl2.hpp
267 PATHS ${ARMCOMPUTE_ROOT}/include
268 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
269
Matthew Bentham3b72db02018-10-11 09:47:01 +0100270 # Link against libOpenCL in opencl-1.2-stubs, but don't search there at runtime
271 link_libraries(-L${ARMCOMPUTE_BUILD_DIR}/opencl-1.2-stubs)
272 set(OPENCL_LIBRARIES OpenCL)
telsoa014fcda012018-03-09 14:13:49 +0000273
274 include_directories(${OPENCL_INCLUDE})
275
276 # Add preprocessor definition for ARM Compute OpenCL
277 add_definitions(-DARMCOMPUTECL_ENABLED)
278
279 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DARM_COMPUTE_DEBUG_ENABLED")
280endif()
281
282# Used by both Arm Compute backends, but should be added
283# to the search path after the system directories if necessary
284if(ARMCOMPUTENEON OR ARMCOMPUTECL)
285 find_path(HALF_INCLUDE half/half.hpp)
286 find_path(HALF_INCLUDE half/half.hpp
287 PATHS ${ARMCOMPUTE_ROOT}/include
288 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
telsoa01c577f2c2018-08-31 09:22:23 +0100289 include_directories(SYSTEM ${HALF_INCLUDE})
telsoa014fcda012018-03-09 14:13:49 +0000290endif()
291
292# Streamline annotate
293if(PROFILING_BACKEND_STREAMLINE)
294 include_directories("${GATOR_ROOT}/annotate")
295 add_definitions(-DARMNN_STREAMLINE_ENABLED)
296endif()
297
telsoa01c577f2c2018-08-31 09:22:23 +0100298if(HEAP_PROFILING OR LEAK_CHECKING)
surmeh013537c2c2018-05-18 16:31:43 +0100299 # enable heap profiling for everything except for referencetests
300 if(NOT ${PROJECT_NAME} STREQUAL "referencetests")
301 find_path(HEAP_PROFILER_INCLUDE gperftools/heap-profiler.h
302 PATHS ${GPERFTOOLS_ROOT}/include
303 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
304 include_directories(SYSTEM "${HEAP_PROFILER_INCLUDE}")
305 find_library(GPERF_TOOLS_LIBRARY
306 NAMES tcmalloc_debug
307 HINTS ${GPERFTOOLS_ROOT}/lib)
308 link_directories(${GPERFTOOLS_ROOT}/lib)
309
310 link_libraries(${GPERF_TOOLS_LIBRARY})
telsoa01c577f2c2018-08-31 09:22:23 +0100311 if (HEAP_PROFILING)
312 add_definitions("-DARMNN_HEAP_PROFILING_ENABLED=1")
313 endif()
314 if (LEAK_CHECKING)
315 add_definitions("-DARMNN_LEAK_CHECKING_ENABLED=1")
316 endif()
surmeh013537c2c2018-05-18 16:31:43 +0100317 else()
telsoa01c577f2c2018-08-31 09:22:23 +0100318 message("Heap profiling and leak checking are disabled for referencetests")
surmeh013537c2c2018-05-18 16:31:43 +0100319 endif()
320else()
321 # Valgrind only works with gperftools version number <= 2.4
322 CHECK_INCLUDE_FILE(valgrind/memcheck.h VALGRIND_FOUND)
323endif()
324
325
326if(NOT BUILD_CAFFE_PARSER)
327 message(STATUS "Caffe parser support is disabled")
328endif()
329
330if(NOT BUILD_TF_PARSER)
331 message(STATUS "Tensorflow parser support is disabled")
332endif()
333
telsoa01c577f2c2018-08-31 09:22:23 +0100334if(NOT BUILD_TF_LITE_PARSER)
335 message(STATUS "Tensorflow Lite parser support is disabled")
336endif()
David Beck10b4dfd2018-09-19 12:03:20 +0100337
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000338if(NOT BUILD_ARMNN_SERIALIZER)
339 message(STATUS "Armnn Serializer support is disabled")
340endif()
341
Jim Flynn3091b062019-02-15 14:45:04 +0000342if(NOT BUILD_ARMNN_QUANTIZER)
343 message(STATUS "ArmNN Quantizer support is disabled")
344endif()
345
David Beck10b4dfd2018-09-19 12:03:20 +0100346# ArmNN source files required for all build options
347include_directories(SYSTEM third-party)