Release 18.08
[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(TF_LITE_GENERATED_PATH "Tensorflow lite generated C++ schema location" OFF)
17 option(FLATBUFFERS_ROOT "Location where the flatbuffers 'include' and 'lib' folders to be found" Off)
18
19 include(SelectLibraryConfigurations)
20
21 set(COMPILER_IS_GNU_LIKE 0)
22 if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU OR ${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
23     set(COMPILER_IS_GNU_LIKE 1)
24 endif()
25
26 # Enable CCache if available and not disabled
27 option(USE_CCACHE "USE_CCACHE" ON)
28 find_program(CCACHE_FOUND ccache)
29 if(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")
32 endif()
33
34 # Enable distcc if available and not disabled
35 option(USE_DISTCC "USE_DISTCC" OFF)
36 find_program(DISTCC_FOUND distcc)
37 if(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")
40 endif()
41
42 # Set to release configuration by default
43 if(NOT CMAKE_BUILD_TYPE)
44     set(CMAKE_BUILD_TYPE "Release")
45 endif()
46
47 # Compiler flags that are always set
48 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
49 if(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")
51 elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
52     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP")
53     add_definitions(-DNOMINMAX=1 -DNO_STRICT=1)
54 endif()
55 if("${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")
58 endif()
59
60 # Compiler flags for Release builds
61 set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG")
62 if(COMPILER_IS_GNU_LIKE)
63     set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
64 elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
65     set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD /O2")
66 endif()
67
68 # Compiler flags for Debug builds
69 if(COMPILER_IS_GNU_LIKE)
70     set(CMAKE_CXX_FLAGS_DEBUG "-g")
71 elseif(${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")
76 endif()
77
78 # Modify RelWithDebInfo so that NDEBUG isn't defined.
79 # This enables asserts.
80 if (COMPILER_IS_GNU_LIKE)
81     string(REPLACE "-DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
82 elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
83     string(REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
84 endif()
85
86 # Compiler flags for code coverage measurements
87 if(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")
95 endif()
96
97 if(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")
99 endif()
100
101 set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
102
103 # Boost
104 add_definitions("-DBOOST_ALL_NO_LIB") # Turn off auto-linking as we specify the libs manually
105 set(Boost_USE_STATIC_LIBS ON)
106 find_package(Boost 1.59 REQUIRED COMPONENTS unit_test_framework system filesystem log program_options)
107 include_directories(SYSTEM "${Boost_INCLUDE_DIR}")
108 link_directories(${Boost_LIBRARY_DIR})
109
110 # pthread
111 find_package (Threads)
112
113 # Favour the protobuf passed on command line
114 if(BUILD_TF_PARSER OR BUILD_CAFFE_PARSER OR BUILD_ONNX_PARSER)
115     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)
134 endif()
135
136 # Caffe and its dependencies
137 if(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}")
143 endif()
144
145 if(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}")
155 endif()
156
157 if(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}")
164 endif()
165
166
167 # Flatbuffers support for TF Lite
168 if(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()
212 endif()
213
214 include_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)
219 if(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()
262 endif()
263
264 # ARM Compute NEON backend
265 if(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()
272 endif()
273
274 # ARM Compute OpenCL backend
275 if(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
281     find_library(OPENCL_LIBRARIES OpenCL)
282     if (NOT OPENCL_LIBRARIES)
283         # Link against libOpenCL in opencl-1.2-stubs, but don't search there at runtime
284         link_libraries(-L${ARMCOMPUTE_BUILD_DIR}/opencl-1.2-stubs)
285         set(OPENCL_LIBRARIES OpenCL)
286     endif()
287
288     include_directories(${OPENCL_INCLUDE})
289
290     # Add preprocessor definition for ARM Compute OpenCL
291     add_definitions(-DARMCOMPUTECL_ENABLED)
292
293     set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DARM_COMPUTE_DEBUG_ENABLED")
294 endif()
295
296 # Used by both Arm Compute backends, but should be added
297 # to the search path after the system directories if necessary
298 if(ARMCOMPUTENEON OR ARMCOMPUTECL)
299     find_path(HALF_INCLUDE half/half.hpp)
300     find_path(HALF_INCLUDE half/half.hpp
301               PATHS ${ARMCOMPUTE_ROOT}/include
302               NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
303     include_directories(SYSTEM ${HALF_INCLUDE})
304 endif()
305
306 # Streamline annotate
307 if(PROFILING_BACKEND_STREAMLINE)
308     include_directories("${GATOR_ROOT}/annotate")
309     add_definitions(-DARMNN_STREAMLINE_ENABLED)
310 endif()
311
312 if(HEAP_PROFILING OR LEAK_CHECKING)
313     # enable heap profiling for everything except for referencetests
314     if(NOT ${PROJECT_NAME} STREQUAL "referencetests")
315         find_path(HEAP_PROFILER_INCLUDE gperftools/heap-profiler.h
316                 PATHS ${GPERFTOOLS_ROOT}/include
317                 NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
318         include_directories(SYSTEM "${HEAP_PROFILER_INCLUDE}")
319         find_library(GPERF_TOOLS_LIBRARY
320                     NAMES tcmalloc_debug
321                     HINTS ${GPERFTOOLS_ROOT}/lib)
322         link_directories(${GPERFTOOLS_ROOT}/lib)
323
324         link_libraries(${GPERF_TOOLS_LIBRARY})
325         if (HEAP_PROFILING)
326             add_definitions("-DARMNN_HEAP_PROFILING_ENABLED=1")
327         endif()
328         if (LEAK_CHECKING)
329             add_definitions("-DARMNN_LEAK_CHECKING_ENABLED=1")
330         endif()
331     else()
332         message("Heap profiling and leak checking are disabled for referencetests")
333     endif()
334 else()
335     # Valgrind only works with gperftools version number <= 2.4
336     CHECK_INCLUDE_FILE(valgrind/memcheck.h VALGRIND_FOUND)
337 endif()
338
339
340 if(NOT BUILD_CAFFE_PARSER)
341     message(STATUS "Caffe parser support is disabled")
342 endif()
343
344 if(NOT BUILD_TF_PARSER)
345     message(STATUS "Tensorflow parser support is disabled")
346 endif()
347
348 if(NOT BUILD_TF_LITE_PARSER)
349     message(STATUS "Tensorflow Lite parser support is disabled")
350 endif()