blob: 0ce95a717ab44f788f85f17509f5747db8180d8a [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)
10
11include(SelectLibraryConfigurations)
12
13set(COMPILER_IS_GNU_LIKE 0)
14if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU OR ${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
15 set(COMPILER_IS_GNU_LIKE 1)
16endif()
17
18# Enable CCache if available and not disabled
19option(USE_CCACHE "USE_CCACHE" ON)
20find_program(CCACHE_FOUND ccache)
21if(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")
24endif()
25
26# Enable distcc if available and not disabled
27option(USE_DISTCC "USE_DISTCC" OFF)
28find_program(DISTCC_FOUND distcc)
29if(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")
32endif()
33
34# Set to release configuration by default
35if(NOT CMAKE_BUILD_TYPE)
36 set(CMAKE_BUILD_TYPE "Release")
37endif()
38
39# Compiler flags that are always set
40set(CMAKE_POSITION_INDEPENDENT_CODE ON)
41if(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")
43elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
44 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /MP")
45 add_definitions(-DNOMINMAX=1 -DNO_STRICT=1)
46endif()
47if("${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")
50endif()
51
52# Compiler flags for Release builds
53set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG")
54if(COMPILER_IS_GNU_LIKE)
55 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
56elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
57 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD /O2")
58endif()
59
60# Compiler flags for Debug builds
61if(COMPILER_IS_GNU_LIKE)
62 set(CMAKE_CXX_FLAGS_DEBUG "-g")
63elseif(${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")
68endif()
69
70# Modify RelWithDebInfo so that NDEBUG isn't defined.
71# This enables asserts.
72if (COMPILER_IS_GNU_LIKE)
73 string(REPLACE "-DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
74elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
75 string(REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
76endif()
77
78# Compiler flags for code coverage measurements
79if(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")
87endif()
88
89if(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")
91endif()
92
93set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
94
95# Boost
96add_definitions("-DBOOST_ALL_NO_LIB") # Turn off auto-linking as we specify the libs manually
97set(Boost_USE_STATIC_LIBS ON)
98find_package(Boost 1.59 REQUIRED COMPONENTS unit_test_framework system filesystem log program_options)
99include_directories(SYSTEM "${Boost_INCLUDE_DIR}")
100link_directories(${Boost_LIBRARY_DIR})
101
102# pthread
103find_package (Threads)
104
105# Favour the protobuf passed on command line
106if(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)
126endif()
127
128# Caffe and its dependencies
129if(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}")
135endif()
136
137if(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}")
147endif()
148
149
150include_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)
155if(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()
198endif()
199
200# ARM Compute NEON backend
201if(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()
208endif()
209
210# ARM Compute OpenCL backend
211if(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")
230endif()
231
232# Used by both Arm Compute backends, but should be added
233# to the search path after the system directories if necessary
234if(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})
240endif()
241
242# Built-in profiler
243if(PROFILING)
244 add_definitions(-DARMNN_PROFILING_ENABLED)
245endif()
246
247# Streamline annotate
248if(PROFILING_BACKEND_STREAMLINE)
249 include_directories("${GATOR_ROOT}/annotate")
250 add_definitions(-DARMNN_STREAMLINE_ENABLED)
251endif()
252