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