Release 18.02
[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_UNIT_TESTS "Build unit tests" ON)
4 option(BUILD_TESTS "Build test applications" OFF)
5 option(BUILD_FOR_COVERAGE "Use no optimization and output .gcno and .gcda files" OFF)
6 option(ARMCOMPUTENEON "Build with ARM Compute NEON support" OFF)
7 option(ARMCOMPUTECL "Build with ARM Compute OpenCL support" OFF)
8 option(PROFILING "Build with ArmNN built-in profiling support" OFF)
9 option(PROFILING_BACKEND_STREAMLINE "Forward the armNN profiling events to DS-5/Streamline as annotations" OFF)
10
11 include(SelectLibraryConfigurations)
12
13 set(COMPILER_IS_GNU_LIKE 0)
14 if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU OR ${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
15     set(COMPILER_IS_GNU_LIKE 1)
16 endif()
17
18 # Enable CCache if available and not disabled
19 option(USE_CCACHE "USE_CCACHE" ON)
20 find_program(CCACHE_FOUND ccache)
21 if(CCACHE_FOUND AND USE_CCACHE)
22     get_property(rule_launch_compile DIRECTORY PROPERTY RULE_LAUNCH_COMPILE)
23     set_property(DIRECTORY PROPERTY RULE_LAUNCH_COMPILE "CCACHE_CPP2=yes ${rule_launch_compile} ccache")
24 endif()
25
26 # Enable distcc if available and not disabled
27 option(USE_DISTCC "USE_DISTCC" OFF)
28 find_program(DISTCC_FOUND distcc)
29 if(DISTCC_FOUND AND USE_DISTCC)
30     get_property(rule_launch_compile DIRECTORY PROPERTY RULE_LAUNCH_COMPILE)
31     set_property(DIRECTORY PROPERTY RULE_LAUNCH_COMPILE "${rule_launch_compile} distcc")
32 endif()
33
34 # Set to release configuration by default
35 if(NOT CMAKE_BUILD_TYPE)
36     set(CMAKE_BUILD_TYPE "Release")
37 endif()
38
39 # Compiler flags that are always set
40 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
41 if(COMPILER_IS_GNU_LIKE)
42     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Werror -Wold-style-cast -Wno-missing-braces -Wconversion -Wsign-conversion")
43 elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
44     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP")
45     add_definitions(-DNOMINMAX=1 -DNO_STRICT=1)
46 endif()
47 if("${CMAKE_SYSTEM_NAME}" STREQUAL Android)
48     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -llog")
49     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -llog")
50 endif()
51
52 # Compiler flags for Release builds
53 set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG")
54 if(COMPILER_IS_GNU_LIKE)
55     set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
56 elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
57     set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD /O2")
58 endif()
59
60 # Compiler flags for Debug builds
61 if(COMPILER_IS_GNU_LIKE)
62     set(CMAKE_CXX_FLAGS_DEBUG "-g")
63 elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
64     set(CMAKE_CXX_FLAGS_DEBUG "/MDd /ZI /Od")
65     # Disable SAFESEH which is necessary for Edit and Continue to work
66     set(CMAKE_EXE_LINKER_FLAGS_DEBUG  "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /SAFESEH:NO")
67     set(CMAKE_SHARED_LINKER_FLAGS_DEBUG  "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /SAFESEH:NO")
68 endif()
69
70 # Modify RelWithDebInfo so that NDEBUG isn't defined.
71 # This enables asserts.
72 if (COMPILER_IS_GNU_LIKE)
73     string(REPLACE "-DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
74 elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
75     string(REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
76 endif()
77
78 # Compiler flags for code coverage measurements
79 if(BUILD_FOR_COVERAGE)
80     if(NOT CMAKE_BUILD_TYPE EQUAL "Debug")
81         message(WARNING "BUILD_FOR_COVERAGE set so forcing to Debug build")
82         set(CMAKE_BUILD_TYPE "Debug")
83     endif()
84
85     set(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} --coverage")
86     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
87 endif()
88
89 if(BUILD_FOR_COVERAGE AND NOT BUILD_UNIT_TESTS)
90     message(WARNING "BUILD_FOR_COVERAGE set but not BUILD_UNIT_TESTS, so code coverage will not be able to run")
91 endif()
92
93 set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
94
95 # Boost
96 add_definitions("-DBOOST_ALL_NO_LIB") # Turn off auto-linking as we specify the libs manually
97 set(Boost_USE_STATIC_LIBS ON)
98 find_package(Boost 1.59 REQUIRED COMPONENTS unit_test_framework system filesystem log program_options)
99 include_directories(SYSTEM "${Boost_INCLUDE_DIR}")
100 link_directories(${Boost_LIBRARY_DIR})
101
102 # pthread
103 find_package (Threads)
104
105 # Favour the protobuf passed on command line
106 if(BUILD_TF_PARSER OR BUILD_CAFFE_PARSER)
107     find_library(PROTOBUF_LIBRARY_DEBUG NAMES "protobufd"
108         PATHS ${PROTOBUF_ROOT}/lib
109         NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
110     find_library(PROTOBUF_LIBRARY_DEBUG NAMES "protobufd")
111
112     find_library(PROTOBUF_LIBRARY_RELEASE NAMES "protobuf"
113         PATHS ${PROTOBUF_ROOT}/lib
114         NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
115     find_library(PROTOBUF_LIBRARY_RELEASE NAMES "protobuf")
116
117     select_library_configurations(PROTOBUF)
118
119     find_path(PROTOBUF_INCLUDE_DIRS "google/protobuf/message.h"
120               PATHS ${PROTOBUF_ROOT}/include
121               NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
122     find_path(PROTOBUF_INCLUDE_DIRS "google/protobuf/message.h")
123
124     include_directories(SYSTEM "${PROTOBUF_INCLUDE_DIRS}")
125     add_definitions(-DPROTOBUF_USE_DLLS)
126 endif()
127
128 # Caffe and its dependencies
129 if(BUILD_CAFFE_PARSER)
130     add_definitions(-DARMNN_CAFFE_PARSER)
131
132     find_path(CAFFE_GENERATED_SOURCES "caffe/proto/caffe.pb.h"
133         HINTS ${CAFFE_BUILD_ROOT}/include)
134     include_directories(SYSTEM "${CAFFE_GENERATED_SOURCES}")
135 endif()
136
137 if(BUILD_TF_PARSER)
138     add_definitions(-DARMNN_TF_PARSER)
139
140     find_path(TF_GENERATED_SOURCES "tensorflow/core/protobuf/saved_model.pb.cc")
141
142     # C++ sources generated for tf protobufs
143     file(GLOB_RECURSE TF_PROTOBUFS "${TF_GENERATED_SOURCES}/*.pb.cc")
144
145     # C++ headers generated for tf protobufs
146     include_directories(SYSTEM "${TF_GENERATED_SOURCES}")
147 endif()
148
149
150 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
151
152 # ARM Compute
153 # Note that ARM Compute has a different folder layout depending on the branch but also on
154 # whether it comes from a prepackaged archive (this is why we add several hints below)
155 if(ARMCOMPUTENEON OR ARMCOMPUTECL)
156     find_path(ARMCOMPUTE_INCLUDE arm_compute/core/CL/ICLKernel.h
157               PATHS ${ARMCOMPUTE_ROOT}/include
158               PATHS ${ARMCOMPUTE_ROOT}/applications/arm_compute
159               PATHS ${ARMCOMPUTE_ROOT}
160               NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
161     find_path(ARMCOMPUTE_INCLUDE arm_compute/core/CL/ICLKernel.h)
162     include_directories(SYSTEM "${ARMCOMPUTE_INCLUDE}")
163
164     # Find the Arm Compute libraries if not already specified (the user may have already defined this in advance,
165     # e.g. if building clframework as a dependent cmake project)
166     if (NOT DEFINED ARMCOMPUTE_LIBRARIES)
167         # We link to the static variant so that customers don't need to find and build a compatible version of clframework.
168         # First try the folders specified ARMCOMPUTE_BUILD_DIR (with PATH_SUFFIXES for
169         # Windows builds)
170         find_library(ARMCOMPUTE_LIBRARY_DEBUG NAMES arm_compute-static
171                      PATHS ${ARMCOMPUTE_BUILD_DIR}
172                      PATH_SUFFIXES "Debug"
173                      NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
174         find_library(ARMCOMPUTE_LIBRARY_RELEASE NAMES arm_compute-static
175                      PATHS ${ARMCOMPUTE_BUILD_DIR}
176                      PATH_SUFFIXES "Release"
177                      NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
178         find_library(ARMCOMPUTE_CORE_LIBRARY_DEBUG NAMES arm_compute_core-static
179                      PATHS ${ARMCOMPUTE_BUILD_DIR}
180                      PATH_SUFFIXES "Debug"
181                      NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
182         find_library(ARMCOMPUTE_CORE_LIBRARY_RELEASE NAMES arm_compute_core-static
183                      PATHS ${ARMCOMPUTE_BUILD_DIR}
184                      PATH_SUFFIXES "Release"
185                      NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
186
187         # In case it wasn't there, try a default search (will work in cases where
188         # the library has been installed into a standard location)
189         find_library(ARMCOMPUTE_LIBRARY_DEBUG NAMES arm_compute-static)
190         find_library(ARMCOMPUTE_LIBRARY_RELEASE NAMES arm_compute-static)
191         find_library(ARMCOMPUTE_CORE_LIBRARY_DEBUG NAMES arm_compute_core-static)
192         find_library(ARMCOMPUTE_CORE_LIBRARY_RELEASE NAMES arm_compute_core-static)
193
194         set(ARMCOMPUTE_LIBRARIES
195             debug ${ARMCOMPUTE_LIBRARY_DEBUG} ${ARMCOMPUTE_CORE_LIBRARY_DEBUG}
196             optimized ${ARMCOMPUTE_LIBRARY_RELEASE} ${ARMCOMPUTE_CORE_LIBRARY_RELEASE} )
197     endif()
198 endif()
199
200 # ARM Compute NEON backend
201 if(ARMCOMPUTENEON)
202     # Add preprocessor definition for ARM Compute NEON
203     add_definitions(-DARMCOMPUTENEON_ENABLED)
204     # The ARM Compute headers contain some NEON intrinsics, so we need to build armnn with NEON support on armv7
205     if(${CMAKE_SYSTEM_PROCESSOR} MATCHES armv7 AND COMPILER_IS_GNU_LIKE)
206         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon")
207     endif()
208 endif()
209
210 # ARM Compute OpenCL backend
211 if(ARMCOMPUTECL)
212     # Always use Arm compute library OpenCL headers
213     find_path(OPENCL_INCLUDE CL/cl2.hpp
214               PATHS ${ARMCOMPUTE_ROOT}/include
215               NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
216
217     find_library(OPENCL_LIBRARIES OpenCL)
218     if (NOT OPENCL_LIBRARIES)
219         # Link against libOpenCL in opencl-1.2-stubs, but don't search there at runtime
220         link_libraries(-L${ARMCOMPUTE_BUILD_DIR}/opencl-1.2-stubs)
221         set(OPENCL_LIBRARIES OpenCL)
222     endif()
223
224     include_directories(${OPENCL_INCLUDE})
225
226     # Add preprocessor definition for ARM Compute OpenCL
227     add_definitions(-DARMCOMPUTECL_ENABLED)
228
229     set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DARM_COMPUTE_DEBUG_ENABLED")
230 endif()
231
232 # Used by both Arm Compute backends, but should be added
233 # to the search path after the system directories if necessary
234 if(ARMCOMPUTENEON OR ARMCOMPUTECL)
235     find_path(HALF_INCLUDE half/half.hpp)
236     find_path(HALF_INCLUDE half/half.hpp
237               PATHS ${ARMCOMPUTE_ROOT}/include
238               NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
239     include_directories(${HALF_INCLUDE})
240 endif()
241
242 # Built-in profiler
243 if(PROFILING)
244     add_definitions(-DARMNN_PROFILING_ENABLED)
245 endif()
246
247 # Streamline annotate
248 if(PROFILING_BACKEND_STREAMLINE)
249     include_directories("${GATOR_ROOT}/annotate")
250     add_definitions(-DARMNN_STREAMLINE_ENABLED)
251 endif()
252