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