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