blob: 6b6a424ca795a3ed345d212cc5421ce3f177e327 [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
Kevin May43a799c2019-02-08 16:31:42 +0000224if(BUILD_ARMNN_SERIALIZER)
225 include_directories(SYSTEM "${FLATBUFFERS_INCLUDE_PATH}")
226 add_definitions(-DARMNN_SERIALIZER)
227 add_definitions(-DARMNN_SERIALIZER_SCHEMA_PATH="${CMAKE_CURRENT_SOURCE_DIR}/src/armnnSerializer/Schema.fbs")
228endif()
229
telsoa014fcda012018-03-09 14:13:49 +0000230include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
231
232# ARM Compute
233# Note that ARM Compute has a different folder layout depending on the branch but also on
234# whether it comes from a prepackaged archive (this is why we add several hints below)
235if(ARMCOMPUTENEON OR ARMCOMPUTECL)
236 find_path(ARMCOMPUTE_INCLUDE arm_compute/core/CL/ICLKernel.h
237 PATHS ${ARMCOMPUTE_ROOT}/include
238 PATHS ${ARMCOMPUTE_ROOT}/applications/arm_compute
239 PATHS ${ARMCOMPUTE_ROOT}
240 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
241 find_path(ARMCOMPUTE_INCLUDE arm_compute/core/CL/ICLKernel.h)
242 include_directories(SYSTEM "${ARMCOMPUTE_INCLUDE}")
243
244 # Find the Arm Compute libraries if not already specified (the user may have already defined this in advance,
245 # e.g. if building clframework as a dependent cmake project)
246 if (NOT DEFINED ARMCOMPUTE_LIBRARIES)
247 # We link to the static variant so that customers don't need to find and build a compatible version of clframework.
248 # First try the folders specified ARMCOMPUTE_BUILD_DIR (with PATH_SUFFIXES for
249 # Windows builds)
250 find_library(ARMCOMPUTE_LIBRARY_DEBUG NAMES arm_compute-static
251 PATHS ${ARMCOMPUTE_BUILD_DIR}
252 PATH_SUFFIXES "Debug"
253 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
254 find_library(ARMCOMPUTE_LIBRARY_RELEASE NAMES arm_compute-static
255 PATHS ${ARMCOMPUTE_BUILD_DIR}
256 PATH_SUFFIXES "Release"
257 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
258 find_library(ARMCOMPUTE_CORE_LIBRARY_DEBUG NAMES arm_compute_core-static
259 PATHS ${ARMCOMPUTE_BUILD_DIR}
260 PATH_SUFFIXES "Debug"
261 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
262 find_library(ARMCOMPUTE_CORE_LIBRARY_RELEASE NAMES arm_compute_core-static
263 PATHS ${ARMCOMPUTE_BUILD_DIR}
264 PATH_SUFFIXES "Release"
265 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
266
267 # In case it wasn't there, try a default search (will work in cases where
268 # the library has been installed into a standard location)
269 find_library(ARMCOMPUTE_LIBRARY_DEBUG NAMES arm_compute-static)
270 find_library(ARMCOMPUTE_LIBRARY_RELEASE NAMES arm_compute-static)
271 find_library(ARMCOMPUTE_CORE_LIBRARY_DEBUG NAMES arm_compute_core-static)
272 find_library(ARMCOMPUTE_CORE_LIBRARY_RELEASE NAMES arm_compute_core-static)
273
274 set(ARMCOMPUTE_LIBRARIES
275 debug ${ARMCOMPUTE_LIBRARY_DEBUG} ${ARMCOMPUTE_CORE_LIBRARY_DEBUG}
276 optimized ${ARMCOMPUTE_LIBRARY_RELEASE} ${ARMCOMPUTE_CORE_LIBRARY_RELEASE} )
277 endif()
278endif()
279
280# ARM Compute NEON backend
281if(ARMCOMPUTENEON)
282 # Add preprocessor definition for ARM Compute NEON
283 add_definitions(-DARMCOMPUTENEON_ENABLED)
284 # The ARM Compute headers contain some NEON intrinsics, so we need to build armnn with NEON support on armv7
285 if(${CMAKE_SYSTEM_PROCESSOR} MATCHES armv7 AND COMPILER_IS_GNU_LIKE)
286 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon")
287 endif()
288endif()
289
290# ARM Compute OpenCL backend
291if(ARMCOMPUTECL)
292 # Always use Arm compute library OpenCL headers
293 find_path(OPENCL_INCLUDE CL/cl2.hpp
294 PATHS ${ARMCOMPUTE_ROOT}/include
295 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
296
Matthew Bentham3b72db02018-10-11 09:47:01 +0100297 # Link against libOpenCL in opencl-1.2-stubs, but don't search there at runtime
298 link_libraries(-L${ARMCOMPUTE_BUILD_DIR}/opencl-1.2-stubs)
299 set(OPENCL_LIBRARIES OpenCL)
telsoa014fcda012018-03-09 14:13:49 +0000300
301 include_directories(${OPENCL_INCLUDE})
302
303 # Add preprocessor definition for ARM Compute OpenCL
304 add_definitions(-DARMCOMPUTECL_ENABLED)
305
306 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DARM_COMPUTE_DEBUG_ENABLED")
307endif()
308
309# Used by both Arm Compute backends, but should be added
310# to the search path after the system directories if necessary
311if(ARMCOMPUTENEON OR ARMCOMPUTECL)
312 find_path(HALF_INCLUDE half/half.hpp)
313 find_path(HALF_INCLUDE half/half.hpp
314 PATHS ${ARMCOMPUTE_ROOT}/include
315 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
telsoa01c577f2c2018-08-31 09:22:23 +0100316 include_directories(SYSTEM ${HALF_INCLUDE})
telsoa014fcda012018-03-09 14:13:49 +0000317endif()
318
319# Streamline annotate
320if(PROFILING_BACKEND_STREAMLINE)
321 include_directories("${GATOR_ROOT}/annotate")
322 add_definitions(-DARMNN_STREAMLINE_ENABLED)
323endif()
324
telsoa01c577f2c2018-08-31 09:22:23 +0100325if(HEAP_PROFILING OR LEAK_CHECKING)
surmeh013537c2c2018-05-18 16:31:43 +0100326 # enable heap profiling for everything except for referencetests
327 if(NOT ${PROJECT_NAME} STREQUAL "referencetests")
328 find_path(HEAP_PROFILER_INCLUDE gperftools/heap-profiler.h
329 PATHS ${GPERFTOOLS_ROOT}/include
330 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
331 include_directories(SYSTEM "${HEAP_PROFILER_INCLUDE}")
332 find_library(GPERF_TOOLS_LIBRARY
333 NAMES tcmalloc_debug
334 HINTS ${GPERFTOOLS_ROOT}/lib)
335 link_directories(${GPERFTOOLS_ROOT}/lib)
336
337 link_libraries(${GPERF_TOOLS_LIBRARY})
telsoa01c577f2c2018-08-31 09:22:23 +0100338 if (HEAP_PROFILING)
339 add_definitions("-DARMNN_HEAP_PROFILING_ENABLED=1")
340 endif()
341 if (LEAK_CHECKING)
342 add_definitions("-DARMNN_LEAK_CHECKING_ENABLED=1")
343 endif()
surmeh013537c2c2018-05-18 16:31:43 +0100344 else()
telsoa01c577f2c2018-08-31 09:22:23 +0100345 message("Heap profiling and leak checking are disabled for referencetests")
surmeh013537c2c2018-05-18 16:31:43 +0100346 endif()
347else()
348 # Valgrind only works with gperftools version number <= 2.4
349 CHECK_INCLUDE_FILE(valgrind/memcheck.h VALGRIND_FOUND)
350endif()
351
352
353if(NOT BUILD_CAFFE_PARSER)
354 message(STATUS "Caffe parser support is disabled")
355endif()
356
357if(NOT BUILD_TF_PARSER)
358 message(STATUS "Tensorflow parser support is disabled")
359endif()
360
telsoa01c577f2c2018-08-31 09:22:23 +0100361if(NOT BUILD_TF_LITE_PARSER)
362 message(STATUS "Tensorflow Lite parser support is disabled")
363endif()
David Beck10b4dfd2018-09-19 12:03:20 +0100364
Nattapat Chaimanowong949f1252019-01-31 15:36:39 +0000365if(NOT BUILD_ARMNN_SERIALIZER)
366 message(STATUS "Armnn Serializer support is disabled")
367endif()
368
David Beck10b4dfd2018-09-19 12:03:20 +0100369# ArmNN source files required for all build options
370include_directories(SYSTEM third-party)