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