blob: 7c56ca9f1c99661d1158de70c51a125f2cc88ab0 [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)
17option(FLATC_DIR "Path to Flatbuffers compiler" OFF)
telsoa01c577f2c2018-08-31 09:22:23 +010018option(TF_LITE_GENERATED_PATH "Tensorflow lite generated C++ schema location" OFF)
19option(FLATBUFFERS_ROOT "Location where the flatbuffers 'include' and 'lib' folders to be found" Off)
telsoa014fcda012018-03-09 14:13:49 +000020
21include(SelectLibraryConfigurations)
22
23set(COMPILER_IS_GNU_LIKE 0)
24if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU OR ${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
25 set(COMPILER_IS_GNU_LIKE 1)
26endif()
27
28# Enable CCache if available and not disabled
29option(USE_CCACHE "USE_CCACHE" ON)
30find_program(CCACHE_FOUND ccache)
31if(CCACHE_FOUND AND USE_CCACHE)
32 get_property(rule_launch_compile DIRECTORY PROPERTY RULE_LAUNCH_COMPILE)
33 set_property(DIRECTORY PROPERTY RULE_LAUNCH_COMPILE "CCACHE_CPP2=yes ${rule_launch_compile} ccache")
34endif()
35
36# Enable distcc if available and not disabled
37option(USE_DISTCC "USE_DISTCC" OFF)
38find_program(DISTCC_FOUND distcc)
39if(DISTCC_FOUND AND USE_DISTCC)
40 get_property(rule_launch_compile DIRECTORY PROPERTY RULE_LAUNCH_COMPILE)
41 set_property(DIRECTORY PROPERTY RULE_LAUNCH_COMPILE "${rule_launch_compile} distcc")
42endif()
43
44# Set to release configuration by default
45if(NOT CMAKE_BUILD_TYPE)
46 set(CMAKE_BUILD_TYPE "Release")
47endif()
48
49# Compiler flags that are always set
50set(CMAKE_POSITION_INDEPENDENT_CODE ON)
51if(COMPILER_IS_GNU_LIKE)
52 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Werror -Wold-style-cast -Wno-missing-braces -Wconversion -Wsign-conversion")
53elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
54 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP")
55 add_definitions(-DNOMINMAX=1 -DNO_STRICT=1)
56endif()
57if("${CMAKE_SYSTEM_NAME}" STREQUAL Android)
58 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -llog")
59 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -llog")
60endif()
61
62# Compiler flags for Release builds
63set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG")
64if(COMPILER_IS_GNU_LIKE)
65 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
66elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
67 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD /O2")
68endif()
69
70# Compiler flags for Debug builds
71if(COMPILER_IS_GNU_LIKE)
Matteo Martincigh3d6898c2019-01-15 16:11:44 +000072 set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
telsoa014fcda012018-03-09 14:13:49 +000073elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
74 set(CMAKE_CXX_FLAGS_DEBUG "/MDd /ZI /Od")
75 # Disable SAFESEH which is necessary for Edit and Continue to work
76 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /SAFESEH:NO")
77 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /SAFESEH:NO")
78endif()
79
80# Modify RelWithDebInfo so that NDEBUG isn't defined.
81# This enables asserts.
82if (COMPILER_IS_GNU_LIKE)
83 string(REPLACE "-DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
84elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
85 string(REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
86endif()
87
88# Compiler flags for code coverage measurements
89if(BUILD_FOR_COVERAGE)
90 if(NOT CMAKE_BUILD_TYPE EQUAL "Debug")
91 message(WARNING "BUILD_FOR_COVERAGE set so forcing to Debug build")
92 set(CMAKE_BUILD_TYPE "Debug")
93 endif()
94
95 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
96 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
97endif()
98
99if(BUILD_FOR_COVERAGE AND NOT BUILD_UNIT_TESTS)
100 message(WARNING "BUILD_FOR_COVERAGE set but not BUILD_UNIT_TESTS, so code coverage will not be able to run")
101endif()
102
103set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
104
105# Boost
106add_definitions("-DBOOST_ALL_NO_LIB") # Turn off auto-linking as we specify the libs manually
107set(Boost_USE_STATIC_LIBS ON)
108find_package(Boost 1.59 REQUIRED COMPONENTS unit_test_framework system filesystem log program_options)
109include_directories(SYSTEM "${Boost_INCLUDE_DIR}")
110link_directories(${Boost_LIBRARY_DIR})
111
112# pthread
113find_package (Threads)
114
115# Favour the protobuf passed on command line
telsoa01c577f2c2018-08-31 09:22:23 +0100116if(BUILD_TF_PARSER OR BUILD_CAFFE_PARSER OR BUILD_ONNX_PARSER)
telsoa014fcda012018-03-09 14:13:49 +0000117 find_library(PROTOBUF_LIBRARY_DEBUG NAMES "protobufd"
118 PATHS ${PROTOBUF_ROOT}/lib
119 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
120 find_library(PROTOBUF_LIBRARY_DEBUG NAMES "protobufd")
121
122 find_library(PROTOBUF_LIBRARY_RELEASE NAMES "protobuf"
123 PATHS ${PROTOBUF_ROOT}/lib
124 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
125 find_library(PROTOBUF_LIBRARY_RELEASE NAMES "protobuf")
126
127 select_library_configurations(PROTOBUF)
128
129 find_path(PROTOBUF_INCLUDE_DIRS "google/protobuf/message.h"
130 PATHS ${PROTOBUF_ROOT}/include
131 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
132 find_path(PROTOBUF_INCLUDE_DIRS "google/protobuf/message.h")
133
134 include_directories(SYSTEM "${PROTOBUF_INCLUDE_DIRS}")
135 add_definitions(-DPROTOBUF_USE_DLLS)
136endif()
137
138# Caffe and its dependencies
139if(BUILD_CAFFE_PARSER)
140 add_definitions(-DARMNN_CAFFE_PARSER)
141
142 find_path(CAFFE_GENERATED_SOURCES "caffe/proto/caffe.pb.h"
143 HINTS ${CAFFE_BUILD_ROOT}/include)
144 include_directories(SYSTEM "${CAFFE_GENERATED_SOURCES}")
145endif()
146
147if(BUILD_TF_PARSER)
148 add_definitions(-DARMNN_TF_PARSER)
149
150 find_path(TF_GENERATED_SOURCES "tensorflow/core/protobuf/saved_model.pb.cc")
151
152 # C++ sources generated for tf protobufs
153 file(GLOB_RECURSE TF_PROTOBUFS "${TF_GENERATED_SOURCES}/*.pb.cc")
154
155 # C++ headers generated for tf protobufs
156 include_directories(SYSTEM "${TF_GENERATED_SOURCES}")
157endif()
158
telsoa01c577f2c2018-08-31 09:22:23 +0100159if(BUILD_ONNX_PARSER)
160 add_definitions(-DARMNN_ONNX_PARSER)
161
162 find_path(ONNX_GENERATED_SOURCES "onnx/onnx.pb.cc")
163
164 # C++ headers generated for onnx protobufs
165 include_directories(SYSTEM "${ONNX_GENERATED_SOURCES}")
166endif()
167
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000168# Flatbuffers support for TF Lite and Armnn Serializer
169if(BUILD_TF_LITE_PARSER OR BUILD_ARMNN_SERIALIZER)
telsoa01c577f2c2018-08-31 09:22:23 +0100170 # verify we have a valid flatbuffers include path
171 find_path(FLATBUFFERS_INCLUDE_PATH flatbuffers/flatbuffers.h
172 HINTS ${FLATBUFFERS_ROOT}/include /usr/local/include /usr/include)
173
174 if(NOT FLATBUFFERS_INCLUDE_PATH)
175 message(WARNING
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000176 "Couldn't find 'flatbuffers/flatbuffers.h' at ${FLATBUFFERS_ROOT}/include. \
177 Disabling Tf Lite and Armnn Serializer support")
telsoa01c577f2c2018-08-31 09:22:23 +0100178 set(BUILD_TF_LITE_PARSER Off)
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000179 set(BUILD_ARMNN_SERIALIZER Off)
telsoa01c577f2c2018-08-31 09:22:23 +0100180 else()
181 message(STATUS "Flatbuffers headers are located at: ${FLATBUFFERS_INCLUDE_PATH}")
182 endif()
183
184 find_library(FLATBUFFERS_LIBRARY
185 NAMES libflatbuffers.a flatbuffers
186 HINTS ${FLATBUFFERS_ROOT}/lib /usr/local/lib /usr/lib)
187
188 if(NOT FLATBUFFERS_LIBRARY)
189 message(WARNING
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000190 "Couldn't find flatbuffers library. Disabling Tf Lite and Armnn Serializer support")
telsoa01c577f2c2018-08-31 09:22:23 +0100191 set(BUILD_TF_LITE_PARSER Off)
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000192 set(BUILD_ARMNN_SERIALIZER Off)
telsoa01c577f2c2018-08-31 09:22:23 +0100193 else()
194 message(STATUS "Flatbuffers library located at: ${FLATBUFFERS_LIBRARY}")
195 endif()
196
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000197 # Setup includes and libs only if we still want Tf Lite or Armnn Serializer
198 if(BUILD_TF_LITE_PARSER OR BUILD_ARMNN_SERIALIZER)
199 include_directories(SYSTEM "${FLATBUFFERS_INCLUDE_PATH}")
200 endif()
201endif()
202
203# Flatbuffers schema support for TF Lite
204if(BUILD_TF_LITE_PARSER)
205 find_path(TF_LITE_SCHEMA_INCLUDE_PATH
206 schema_generated.h
207 HINTS ${TF_LITE_GENERATED_PATH})
208
209 if(NOT TF_LITE_SCHEMA_INCLUDE_PATH)
210 message(WARNING
211 "Couldn't find 'schema_generated.h' at ${TF_LITE_GENERATED_PATH}. Disabling Tf Lite support")
212 set(BUILD_TF_LITE_PARSER Off)
213 else()
214 message(STATUS "Tf Lite generated header found at: ${TF_LITE_SCHEMA_INCLUDE_PATH}")
215 endif()
216
telsoa01c577f2c2018-08-31 09:22:23 +0100217 # Setup includes and libs only if we still want Tf Lite
218 if(BUILD_TF_LITE_PARSER)
telsoa01c577f2c2018-08-31 09:22:23 +0100219 add_definitions(-DARMNN_TF_LITE_PARSER)
220 add_definitions(-DARMNN_TF_LITE_SCHEMA_PATH="${TF_LITE_SCHEMA_INCLUDE_PATH}/schema.fbs")
221 endif()
222endif()
223
telsoa014fcda012018-03-09 14:13:49 +0000224include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
225
226# ARM Compute
227# Note that ARM Compute has a different folder layout depending on the branch but also on
228# whether it comes from a prepackaged archive (this is why we add several hints below)
229if(ARMCOMPUTENEON OR ARMCOMPUTECL)
230 find_path(ARMCOMPUTE_INCLUDE arm_compute/core/CL/ICLKernel.h
231 PATHS ${ARMCOMPUTE_ROOT}/include
232 PATHS ${ARMCOMPUTE_ROOT}/applications/arm_compute
233 PATHS ${ARMCOMPUTE_ROOT}
234 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
235 find_path(ARMCOMPUTE_INCLUDE arm_compute/core/CL/ICLKernel.h)
236 include_directories(SYSTEM "${ARMCOMPUTE_INCLUDE}")
237
238 # Find the Arm Compute libraries if not already specified (the user may have already defined this in advance,
239 # e.g. if building clframework as a dependent cmake project)
240 if (NOT DEFINED ARMCOMPUTE_LIBRARIES)
241 # We link to the static variant so that customers don't need to find and build a compatible version of clframework.
242 # First try the folders specified ARMCOMPUTE_BUILD_DIR (with PATH_SUFFIXES for
243 # Windows builds)
244 find_library(ARMCOMPUTE_LIBRARY_DEBUG NAMES arm_compute-static
245 PATHS ${ARMCOMPUTE_BUILD_DIR}
246 PATH_SUFFIXES "Debug"
247 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
248 find_library(ARMCOMPUTE_LIBRARY_RELEASE NAMES arm_compute-static
249 PATHS ${ARMCOMPUTE_BUILD_DIR}
250 PATH_SUFFIXES "Release"
251 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
252 find_library(ARMCOMPUTE_CORE_LIBRARY_DEBUG NAMES arm_compute_core-static
253 PATHS ${ARMCOMPUTE_BUILD_DIR}
254 PATH_SUFFIXES "Debug"
255 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
256 find_library(ARMCOMPUTE_CORE_LIBRARY_RELEASE NAMES arm_compute_core-static
257 PATHS ${ARMCOMPUTE_BUILD_DIR}
258 PATH_SUFFIXES "Release"
259 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
260
261 # In case it wasn't there, try a default search (will work in cases where
262 # the library has been installed into a standard location)
263 find_library(ARMCOMPUTE_LIBRARY_DEBUG NAMES arm_compute-static)
264 find_library(ARMCOMPUTE_LIBRARY_RELEASE NAMES arm_compute-static)
265 find_library(ARMCOMPUTE_CORE_LIBRARY_DEBUG NAMES arm_compute_core-static)
266 find_library(ARMCOMPUTE_CORE_LIBRARY_RELEASE NAMES arm_compute_core-static)
267
268 set(ARMCOMPUTE_LIBRARIES
269 debug ${ARMCOMPUTE_LIBRARY_DEBUG} ${ARMCOMPUTE_CORE_LIBRARY_DEBUG}
270 optimized ${ARMCOMPUTE_LIBRARY_RELEASE} ${ARMCOMPUTE_CORE_LIBRARY_RELEASE} )
271 endif()
272endif()
273
274# ARM Compute NEON backend
275if(ARMCOMPUTENEON)
276 # Add preprocessor definition for ARM Compute NEON
277 add_definitions(-DARMCOMPUTENEON_ENABLED)
278 # The ARM Compute headers contain some NEON intrinsics, so we need to build armnn with NEON support on armv7
279 if(${CMAKE_SYSTEM_PROCESSOR} MATCHES armv7 AND COMPILER_IS_GNU_LIKE)
280 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon")
281 endif()
282endif()
283
284# ARM Compute OpenCL backend
285if(ARMCOMPUTECL)
286 # Always use Arm compute library OpenCL headers
287 find_path(OPENCL_INCLUDE CL/cl2.hpp
288 PATHS ${ARMCOMPUTE_ROOT}/include
289 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
290
Matthew Bentham3b72db02018-10-11 09:47:01 +0100291 # Link against libOpenCL in opencl-1.2-stubs, but don't search there at runtime
292 link_libraries(-L${ARMCOMPUTE_BUILD_DIR}/opencl-1.2-stubs)
293 set(OPENCL_LIBRARIES OpenCL)
telsoa014fcda012018-03-09 14:13:49 +0000294
295 include_directories(${OPENCL_INCLUDE})
296
297 # Add preprocessor definition for ARM Compute OpenCL
298 add_definitions(-DARMCOMPUTECL_ENABLED)
299
300 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DARM_COMPUTE_DEBUG_ENABLED")
301endif()
302
303# Used by both Arm Compute backends, but should be added
304# to the search path after the system directories if necessary
305if(ARMCOMPUTENEON OR ARMCOMPUTECL)
306 find_path(HALF_INCLUDE half/half.hpp)
307 find_path(HALF_INCLUDE half/half.hpp
308 PATHS ${ARMCOMPUTE_ROOT}/include
309 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
telsoa01c577f2c2018-08-31 09:22:23 +0100310 include_directories(SYSTEM ${HALF_INCLUDE})
telsoa014fcda012018-03-09 14:13:49 +0000311endif()
312
313# Streamline annotate
314if(PROFILING_BACKEND_STREAMLINE)
315 include_directories("${GATOR_ROOT}/annotate")
316 add_definitions(-DARMNN_STREAMLINE_ENABLED)
317endif()
318
telsoa01c577f2c2018-08-31 09:22:23 +0100319if(HEAP_PROFILING OR LEAK_CHECKING)
surmeh013537c2c2018-05-18 16:31:43 +0100320 # enable heap profiling for everything except for referencetests
321 if(NOT ${PROJECT_NAME} STREQUAL "referencetests")
322 find_path(HEAP_PROFILER_INCLUDE gperftools/heap-profiler.h
323 PATHS ${GPERFTOOLS_ROOT}/include
324 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
325 include_directories(SYSTEM "${HEAP_PROFILER_INCLUDE}")
326 find_library(GPERF_TOOLS_LIBRARY
327 NAMES tcmalloc_debug
328 HINTS ${GPERFTOOLS_ROOT}/lib)
329 link_directories(${GPERFTOOLS_ROOT}/lib)
330
331 link_libraries(${GPERF_TOOLS_LIBRARY})
telsoa01c577f2c2018-08-31 09:22:23 +0100332 if (HEAP_PROFILING)
333 add_definitions("-DARMNN_HEAP_PROFILING_ENABLED=1")
334 endif()
335 if (LEAK_CHECKING)
336 add_definitions("-DARMNN_LEAK_CHECKING_ENABLED=1")
337 endif()
surmeh013537c2c2018-05-18 16:31:43 +0100338 else()
telsoa01c577f2c2018-08-31 09:22:23 +0100339 message("Heap profiling and leak checking are disabled for referencetests")
surmeh013537c2c2018-05-18 16:31:43 +0100340 endif()
341else()
342 # Valgrind only works with gperftools version number <= 2.4
343 CHECK_INCLUDE_FILE(valgrind/memcheck.h VALGRIND_FOUND)
344endif()
345
346
347if(NOT BUILD_CAFFE_PARSER)
348 message(STATUS "Caffe parser support is disabled")
349endif()
350
351if(NOT BUILD_TF_PARSER)
352 message(STATUS "Tensorflow parser support is disabled")
353endif()
354
telsoa01c577f2c2018-08-31 09:22:23 +0100355if(NOT BUILD_TF_LITE_PARSER)
356 message(STATUS "Tensorflow Lite parser support is disabled")
357endif()
David Beck10b4dfd2018-09-19 12:03:20 +0100358
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000359if(NOT BUILD_ARMNN_SERIALIZER)
360 message(STATUS "Armnn Serializer support is disabled")
361endif()
362
David Beck10b4dfd2018-09-19 12:03:20 +0100363# ArmNN source files required for all build options
364include_directories(SYSTEM third-party)