forked from alibaba/MNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
382 lines (334 loc) · 13.5 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
cmake_minimum_required(VERSION 2.8)
project(MNN)
# complier options
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
enable_language(ASM)
# set(CMAKE_C_COMPILER gcc)
# set(CMAKE_CXX_COMPILER g++)
option(MNN_USE_CPP11 "Enable MNN use c++11" ON)
if(MNN_USE_CPP11)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
endif()
# build options
option(MNN_BUILD_HARD "Build -mfloat-abi=hard or not" OFF)
option(MNN_BUILD_SHARED_LIBS "MNN build shared or static lib" ON)
option(MNN_FORBID_MULTI_THREAD "Disable Multi Thread" OFF)
option(MNN_OPENMP "Enable Multiple Thread Linux|Android" ON)
option(MNN_USE_INT8_FAST "Enable Int8 Fast Optimization" OFF)
if(MNN_FORBID_MULTI_THREAD)
add_definitions(-DMNN_FORBIT_MULTI_THREADS)
endif()
if(MNN_USE_INT8_FAST)
add_definitions(-DMNN_USE_INT8_FAST)
endif()
# debug options
option(MNN_DEBUG "Enable MNN DEBUG" OFF)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(MNN_DEBUG ON)
endif()
option(MNN_DEBUG_MEMORY "MNN Debug Memory Access" OFF)
option(MNN_DEBUG_TENSOR_SIZE "Enable Tensor Size" OFF )
option(MNN_GPU_TRACE "Enable MNN Gpu Debug" OFF)
if(MNN_DEBUG_MEMORY)
add_definitions(-DMNN_DEBUG_MEMORY)
endif()
if(MNN_DEBUG_TENSOR_SIZE)
add_definitions(-DMNN_DEBUG_TENSOR_SIZE)
endif()
if(MNN_GPU_TRACE)
add_definitions(-DMNN_GPU_FORCE_FINISH)
endif()
# backend options
option(MNN_METAL "Enable Metal" OFF)
option(MNN_OPENCL "Enable OpenCL" OFF)
option(MNN_OPENGL "Enable OpenGL" OFF)
option(MNN_VULKAN "Enable Vulkan" OFF)
option(MNN_ARM82 "Enable ARM82" OFF)
# target options
option(MNN_BUILD_BENCHMARK "Build benchmark or not" ON)
option(MNN_BUILD_TEST "Build tests or not" OFF)
option(MNN_BUILD_FOR_ANDROID_COMMAND "Build from command" OFF)
option(MNN_BUILD_FOR_IOS "Build for ios" OFF)
set (MNN_HIDDEN FALSE)
if (NOT MNN_BUILD_TEST)
if (NOT MNN_DEBUG)
set (MNN_HIDDEN TRUE)
endif()
endif()
include(cmake/macros.cmake)
message(STATUS ">>>>>>>>>>>>>")
message(STATUS "MNN BUILD INFO:")
message(STATUS "\tSystem: ${CMAKE_SYSTEM_NAME}")
message(STATUS "\tProcessor: ${CMAKE_SYSTEM_PROCESSOR}")
message(STATUS "\tDEBUG: ${MNN_DEBUG}")
message(STATUS "\tMetal: ${MNN_METAL}")
message(STATUS "\tOpenCL: ${MNN_OPENCL}")
message(STATUS "\tOpenGL: ${MNN_OPENGL}")
message(STATUS "\tVulkan: ${MNN_VULKAN}")
message(STATUS "\tOpenMP: ${MNN_OPENMP}")
message(STATUS "\tHideen: ${MNN_HIDDEN}")
# flags
if(SYSTEM.Android)
add_definitions(-DMNN_BUILD_FOR_ANDROID)
if(PROCESSOR.arm)
add_definitions(-mfloat-abi=softfp -mfpu=neon)
endif()
endif()
if(SYSTEM.Linux)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__STRICT_ANSI__")
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^armv7")
add_definitions(-mfpu=neon) #please define in project/cross-compile/arm.toolchain.cmake
endif()
if(MNN_BUILD_HARD)
add_definitions(-mfloat-abi=hard) #better define in project/cross-compile/arm.toolchain.cmake
endif()
endif()
if(WIN32)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MT")
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MT")
set(CMAKE_CXX_STANDARD 14)
elseif(SYSTEM.Android OR SYSTEM.Linux)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fomit-frame-pointer -fstrict-aliasing -ffunction-sections -fdata-sections -ffast-math")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fomit-frame-pointer -fstrict-aliasing -ffunction-sections -fdata-sections -ffast-math -fno-rtti -fno-exceptions")
add_definitions(-fPIC)
elseif()
endif()
if(MNN_DEBUG)
add_definitions(-DMNN_DEBUG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG -g")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -fPIC")
if(MNN_BUILD_FOR_ANDROID_COMMAND)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s -Wl,--gc-sections")
endif()
endif()
if (MNN_HIDDEN)
add_definitions(-fvisibility=hidden)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden")
endif()
# include files
set(MNN.Path source)
set(MNN.Source_DIR
${MNN.Path}
${MNN.Path}/core
${MNN.Path}/backend/cpu
${MNN.Path}/backend/cpu/arm
${MNN.Path}/backend/cpu/compute
${MNN.Path}/cv
${MNN.Path}/math
${MNN.Path}/shape
)
include_directories(
"include/"
"schema/current"
"3rd_party/flatbuffers/include"
)
if(MNN_METAL)
set (MNN.Source_DIR ${MNN.Source_DIR} ${MNN.Path}/backend/metal)
endif()
if(${CMAKE_SYSTEM_NAME} MATCHES "^Linux")
if(PROCESSOR.arm OR PROCESSOR.aarch64)
set(aarch64_linux_include
#/usr/include/c++/4.9
#/usr/lib/gcc/x86_64-linux-gnu/4.9
#/usr/lib/gcc/x86_64-linux-gnu/4.9/include
#/usr/include/x86_64-linux-gnu/c++/4.9
)
include_directories(${aarch64_linux_include})
endif()
endif()
if(PROCESSOR.x86)
add_definitions(-DMNN_USE_SSE)
set (MNN.Source_DIR ${MNN.Source_DIR} ${MNN.Path}/backend/cpu/sse)
endif()
# *.c
foreach(file_path ${MNN.Source_DIR})
file(GLOB file_source_c ${file_path}/*.c ${file_path}/*.h)
set(MNN.Source_C ${MNN.Source_C} ${file_source_c})
endforeach()
message(STATUS "[*] searching *.c")
# *.cc
foreach(file_path ${MNN.Source_DIR})
file(GLOB file_source_cc ${file_path}/*.cc ${file_path}/*.hpp)
set(MNN.Source_CC ${MNN.Source_CC} ${file_source_cc})
endforeach()
message(STATUS "[*] searching *.cc")
# *.cpp
foreach(file_path ${MNN.Source_DIR})
file(GLOB file_source_cpp ${file_path}/*.cpp)
set(MNN.Source_CPP ${MNN.Source_CPP} ${file_source_cpp})
endforeach()
message(STATUS "[*] searching *.cpp")
if(MNN_METAL)
# *.m *.mm
foreach(file_path ${MNN.Source_DIR})
file(GLOB file_source_m ${file_path}/*.mm ${file_path}/*.m ${file_path}/*.metal)
set(MNN.Source_M ${MNN.Source_M} ${file_source_m})
endforeach()
message(STATUS "[*] searching *.m *.mm")
endif()
set(GLOBAL.SOURCE ${GLOBAL.SOURCE} ${MNN.Source_C} ${MNN.Source_CPP} ${MNN.Source_M} ${MNN.Source_CC})
set(GLOBAL.SOURCE_INCLUDE ${GLOBAL.SOURCE_INCLUDE} ${MNN.Source_DIR})
# *.s *.S assembly
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^armv7")
set(MNN.Source_Assembly_DIR ${MNN.Path}/backend/cpu/arm/arm32)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64")
set(MNN.Source_Assembly_DIR ${MNN.Path}/backend/cpu/arm/arm64)
elseif(ARCH_NAME STREQUAL "ios_fat_arm")
set(MNN.Source_Assembly_DIR ${MNN.Source_Assembly_DIR} ${MNN.Path}/backend/cpu/arm/arm64)
set(MNN.Source_Assembly_DIR ${MNN.Source_Assembly_DIR} ${MNN.Path}/backend/cpu/arm/arm32)
endif()
message(STATUS "[*] searching *.s *.S")
foreach(file_path ${MNN.Source_Assembly_DIR})
file(GLOB file_source_assembly ${file_path}/*.s ${file_path}/*.S)
set(MNN.Source_Assembly ${MNN.Source_Assembly} ${file_source_assembly})
endforeach()
# set assembly file use c compiler flags
set_property(SOURCE ${MNN.Source_Assembly} PROPERTY LANGUAGE C)
include_directories(${MNN.Source_DIR})
include_directories("./")
if(SYSTEM.Android AND NOT MNN_BUILD_FOR_ANDROID_COMMAND)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${NATIVE_LIBRARY_OUTPUT}/${ANDROID_ABI})
add_definitions(-DMNN_USE_LOGCAT)
endif()
if(MNN_BUILD_FOR_IOS)
add_library(MNN ${GLOBAL.SOURCE} ${MNN.Source_Assembly} ${Metal.Resource_Metal})
set_target_properties(MNN PROPERTIES FRAMEWORK TRUE)
set_target_properties(MNN PROPERTIES
MACOSX_FRAMEWORK_IDENTIFIER com.alibaba.MNN
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer"
)
set_target_properties(MNN PROPERTIES MACOSX_FRAMEWORK_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/project/ios/MNN/Info.plist)
# set_source_files_properties($Metal.Resource_Metal} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
# set_target_properties(MNN PROPERTIES RESOURCE ${Metal.Resource_Metal})
else()
if(MNN_BUILD_SHARED_LIBS)
add_library(MNN SHARED ${GLOBAL.SOURCE} ${MNN.Source_Assembly})
install(TARGETS MNN LIBRARY DESTINATION lib)
else()
add_library(MNN STATIC ${GLOBAL.SOURCE} ${MNN.Source_Assembly})
install(TARGETS MNN ARCHIVE DESTINATION lib)
endif()
endif()
if(${CMAKE_SYSTEM_NAME} MATCHES "Android")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE -s")
endif()
# pthread lib
if(SYSTEM.Linux)
target_link_libraries(MNN pthread)
endif()
set(MNN_DEPEND MNN)
if(NOT MNN_BUILD_SHARED_LIBS)
if (BUILD_IOS OR APPLE)
set(MNN_DEPEND -all_load, ${MNN_DEPEND}, -noall_load)
elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(MNN_DEPEND -Wl,--whole-archive ${MNN_DEPEND} -Wl,--no-whole-archive)
endif()
endif()
if(MNN_VULKAN)
set(MNN_DEPEND ${MNN_DEPEND} MNN_Vulkan)
add_subdirectory(${MNN.Path}/backend/vulkan)
message(STATUS "[*] linking MNN with Vulkan done")
endif()
if(MNN_OPENGL)
set(MNN_DEPEND ${MNN_DEPEND} MNN_GL)
add_subdirectory(${MNN.Path}/backend/opengl)
message(STATUS "[*] linking MNN with OpenGL done")
endif()
if(MNN_OPENCL)
set(MNN_DEPEND ${MNN_DEPEND} MNN_CL)
add_subdirectory(${MNN.Path}/backend/opencl)
message(STATUS "[*] linking MNN with OpenCL done")
endif()
if (MNN_ARM82)
set(MNN_DEPEND ${MNN_DEPEND} MNN_Arm82)
add_subdirectory(${MNN.Path}/backend/arm82)
message(STATUS "[*] linking MNN with ARM 82 done")
endif()
if (BUILD_IOS OR APPLE)
else()
if(MNN_OPENMP)
message(STATUS "[*] Checking OpenMP")
find_package(OpenMP)
# For CMake < 3.9, we need to make the target ourselves
if(NOT TARGET OpenMP::OpenMP_CXX)
find_package(Threads REQUIRED)
add_library(OpenMP::OpenMP_CXX IMPORTED INTERFACE)
set_property(TARGET OpenMP::OpenMP_CXX
PROPERTY INTERFACE_COMPILE_OPTIONS ${OpenMP_CXX_FLAGS})
# Only works if the same flag is passed to the linker; use CMake 3.9+ otherwise (Intel, AppleClang)
set_property(TARGET OpenMP::OpenMP_CXX
PROPERTY INTERFACE_LINK_LIBRARIES ${OpenMP_CXX_FLAGS} Threads::Threads)
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OpenMP_SHARED_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
endif()
# Android thrid party
if(CMAKE_SYSTEM_NAME MATCHES "^Android")
target_link_libraries(MNN log)
endif()
# Linux library
if(CMAKE_SYSTEM_NAME MATCHES "^Linux")
set (CMAKE_FIND_ROOT_PATH C:/MinGW)
endif()
# Darwin third party
if(MNN_BUILD_FOR_IOS OR MNN_METAL)
target_link_libraries(MNN "-framework Foundation")
target_link_libraries(MNN "-framework Metal")
endif()
if(WIN32)
target_compile_definitions(MNN PRIVATE "-D_CRT_SECURE_NO_WARNINGS")
endif()
if(SYSTEM.Android AND NOT MNN_BUILD_FOR_ANDROID_COMMAND)
set(MNN_INCLUDE_OUTPUT ${NATIVE_INCLUDE_OUTPUT}/MNN)
add_custom_command(
TARGET MNN
POST_BUILD
COMMAND mkdir -p ${MNN_INCLUDE_OUTPUT}
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/${ROOT_SRC}/include/MNNDefine.h ${MNN_INCLUDE_OUTPUT}
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/${ROOT_SRC}/include/Interpreter.hpp ${MNN_INCLUDE_OUTPUT}
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/${ROOT_SRC}/include/MNNForwardType.h ${MNN_INCLUDE_OUTPUT}
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/${ROOT_SRC}/include/HalideRuntime.h ${MNN_INCLUDE_OUTPUT}
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/${ROOT_SRC}/include/Tensor.hpp ${MNN_INCLUDE_OUTPUT}
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/${ROOT_SRC}/include/AutoTime.hpp ${MNN_INCLUDE_OUTPUT}
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/${ROOT_SRC}/include/ErrorCode.hpp ${MNN_INCLUDE_OUTPUT}
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/${ROOT_SRC}/include/ImageProcess.hpp ${MNN_INCLUDE_OUTPUT}
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/${ROOT_SRC}/include/Matrix.h ${MNN_INCLUDE_OUTPUT}
COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/${ROOT_SRC}/include/Rect.h ${MNN_INCLUDE_OUTPUT}
)
else()
add_subdirectory(demo/exec)
add_subdirectory(tools/cpp)
endif()
if(MNN_BUILD_TEST)
add_subdirectory(test)
endif()
if(MNN_BUILD_BENCHMARK)
add_subdirectory(benchmark)
endif()
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/MNNDefine.h DESTINATION include)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/Interpreter.hpp DESTINATION include)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/HalideRuntime.h DESTINATION include)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/Tensor.hpp DESTINATION include)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/ErrorCode.hpp DESTINATION include)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/ImageProcess.hpp DESTINATION include)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/Matrix.h DESTINATION include)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/Rect.h DESTINATION include)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/MNNForwardType.h DESTINATION include)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/AutoTime.hpp DESTINATION include)