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