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