You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CMakeLists.txt 11 kB

4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. cmake_minimum_required(VERSION 3.14)
  2. project(Lite)
  3. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.3.0)
  4. message(FATAL_ERROR "GCC version ${CMAKE_CXX_COMPILER_VERSION} must not be less than 7.3.0")
  5. endif()
  6. option(MS_VERSION_MAJOR "major version" 0)
  7. option(MS_VERSION_MINOR "minor version" 7)
  8. option(MS_VERSION_REVISION "revision version" 0)
  9. option(CMAKE_BUILD_TYPE "build type" Release)
  10. option(COMPILE_LITE "compile for lite" on)
  11. option(SUPPORT_TRAIN "if build for on-device train" off)
  12. option(PLATFORM_ARM64 "if build device for arm64" off)
  13. option(PLATFORM_ARM32 "if build device for arm32" off)
  14. option(ENABLE_CONVERTER "if build converter" on)
  15. option(ENABLE_FP16 "if build fp16 ops" off)
  16. option(ENABLE_TOOLS "if build tools" on)
  17. option(BUILD_TESTCASES "if build testcase" on)
  18. option(SUPPORT_GPU "if support gpu" off)
  19. option(GPU_OPENCL "if support gpu opencl" off)
  20. option(GPU_VULKAN "if support gpu vulkan" off)
  21. option(GPU_CUDA "if support gpu cuda" off)
  22. option(SUPPORT_NPU "if support npu" off)
  23. option(OFFLINE_COMPILE "if offline compile OpenCL kernel" off)
  24. option(BUILD_MINDDATA_EXAMPLE "" on)
  25. option(ENABLE_VERBOSE "" off)
  26. option(ENABLE_SSE "if x86_64 support SSE instruction set" off)
  27. option(ENABLE_AVX "if x86_64 support SSE instruction set" off)
  28. option(ENABLE_MINDRT "if support mindrt" on)
  29. set(DIR_PREFIX mindspore-lite)
  30. set(MS_VERSION ${MS_VERSION_MAJOR}.${MS_VERSION_MINOR}.${MS_VERSION_REVISION})
  31. set(MAIN_DIR ${DIR_PREFIX}-${MS_VERSION})
  32. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
  33. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMS_VERSION_MAJOR=${MS_VERSION_MAJOR} -DMS_VERSION_MINOR=${MS_VERSION_MINOR} \
  34. -DMS_VERSION_REVISION=${MS_VERSION_REVISION}")
  35. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMS_VERSION_MAJOR=${MS_VERSION_MAJOR} -DMS_VERSION_MINOR=${MS_VERSION_MINOR} \
  36. -DMS_VERSION_REVISION=${MS_VERSION_REVISION}")
  37. set(BUILD_MINDDATA "lite_cv" CACHE STRING "off, lite, lite_cv, wrapper or full")
  38. set(BUILD_LITE "on")
  39. set(PLATFORM_ARM "off")
  40. if(PLATFORM_ARM64 OR PLATFORM_ARM32)
  41. set(PLATFORM_ARM "on")
  42. #set for cross-compiling toolchain
  43. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
  44. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
  45. set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
  46. endif()
  47. #if(BUILD_MINDDATA STREQUAL "lite" OR BUILD_MINDDATA STREQUAL "full" OR BUILD_MINDDATA STREQUAL "wrapper")
  48. if(SUPPORT_GPU)
  49. set(PROCESS_UNIT gpu)
  50. elseif(SUPPORT_NPU)
  51. set(PROCESS_UNIT npu)
  52. else()
  53. set(PROCESS_UNIT cpu)
  54. endif()
  55. if(SUPPORT_NPU)
  56. set(DDK_PATH "$ENV{HWHIAI_DDK}/ddk/ai_ddk_lib")
  57. if(PLATFORM_ARM64)
  58. set(DDK_LIB_PATH ${DDK_PATH}/lib64)
  59. elseif(PLATFORM_ARM32)
  60. set(DDK_LIB_PATH ${DDK_PATH}/lib)
  61. endif()
  62. add_compile_definitions(SUPPORT_NPU)
  63. endif()
  64. add_compile_definitions(NO_DLIB)
  65. add_compile_options(-fPIC)
  66. if(SUPPORT_TRAIN)
  67. set(BUILD_MINDDATA "full")
  68. if(PLATFORM_ARM64)
  69. set(RUNTIME_COMPONENT_NAME train-android-aarch64)
  70. elseif(PLATFORM_ARM32)
  71. set(RUNTIME_COMPONENT_NAME train-android-aarch32)
  72. elseif(WIN32)
  73. set(RUNTIME_COMPONENT_NAME train-win-x64)
  74. else()
  75. set(RUNTIME_COMPONENT_NAME train-linux-x64)
  76. endif()
  77. else()
  78. if(PLATFORM_ARM64)
  79. set(RUNTIME_COMPONENT_NAME inference-android-aarch64)
  80. elseif(PLATFORM_ARM32)
  81. set(RUNTIME_COMPONENT_NAME inference-android-aarch32)
  82. elseif(WIN32)
  83. if("${X86_64_SIMD}" STREQUAL "off")
  84. set(RUNTIME_COMPONENT_NAME inference-win-x64)
  85. else()
  86. set(RUNTIME_COMPONENT_NAME inference-win-x64-${X86_64_SIMD})
  87. endif()
  88. else()
  89. if("${X86_64_SIMD}" STREQUAL "off")
  90. set(RUNTIME_COMPONENT_NAME inference-linux-x64)
  91. else()
  92. set(RUNTIME_COMPONENT_NAME inference-linux-x64-${X86_64_SIMD})
  93. endif()
  94. endif()
  95. endif()
  96. string(REPLACE "/mindspore/lite" "" TOP_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  97. set(CORE_DIR ${TOP_DIR}/mindspore/core)
  98. set(CCSRC_DIR ${TOP_DIR}/mindspore/ccsrc)
  99. include_directories(${TOP_DIR})
  100. include_directories(${CORE_DIR})
  101. include_directories(${CORE_DIR}/ir)
  102. include_directories(${CCSRC_DIR})
  103. include_directories(${CMAKE_CURRENT_SOURCE_DIR})
  104. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/runtime/kernel/arm)
  105. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/)
  106. include_directories(${TOP_DIR}/third_party)
  107. include_directories(${CMAKE_BINARY_DIR})
  108. include_directories(${CCSRC_DIR}/minddata/dataset/liteapi)
  109. include(${TOP_DIR}/cmake/utils.cmake)
  110. include(${TOP_DIR}/cmake/dependency_utils.cmake)
  111. include(${TOP_DIR}/cmake/dependency_securec.cmake)
  112. include(${TOP_DIR}/cmake/external_libs/flatbuffers.cmake)
  113. if(SUPPORT_GPU STREQUAL opencl)
  114. include(${TOP_DIR}/cmake/external_libs/opencl.cmake)
  115. endif()
  116. if(SUPPORT_GPU STREQUAL vulkan)
  117. include(${TOP_DIR}/cmake/external_libs/vulkan.cmake)
  118. endif()
  119. if(ENABLE_CONVERTER OR BUILD_MINDDATA STREQUAL "full" OR BUILD_MINDDATA STREQUAL "wrapper")
  120. include(${TOP_DIR}/cmake/external_libs/json.cmake)
  121. endif()
  122. if(SUPPORT_TRAIN OR WIN32)
  123. set(ENABLE_MINDRT "off")
  124. endif()
  125. if(DEFINED ARCHS)
  126. add_definitions(-DMS_COMPILE_IOS)
  127. endif()
  128. file(GLOB FBS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/schema/*.fbs)
  129. ms_build_flatbuffers_lite(FBS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/schema/ fbs_src ${CMAKE_BINARY_DIR}/schema "")
  130. ms_build_flatbuffers_lite(FBS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/schema/ fbs_inner_src ${CMAKE_BINARY_DIR}/schema/inner
  131. "inner")
  132. string(REPLACE "-g" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  133. string(REPLACE "-g" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  134. if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
  135. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDebug -g")
  136. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDebug -g")
  137. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=default")
  138. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=default")
  139. else()
  140. ## enable for binscope for release
  141. set(CMAKE_C_FLAGS "-fPIC -fPIE -D_FORTIFY_SOURCE=2 -O2 -Wall -Werror -fstack-protector-strong -Wno-attributes \
  142. -Wno-deprecated-declarations -Wno-missing-braces ${CMAKE_C_FLAGS}")
  143. set(CMAKE_CXX_FLAGS "-fPIC -fPIE -D_FORTIFY_SOURCE=2 -O2 -Wall -Werror -fstack-protector-strong -Wno-attributes \
  144. -Wno-deprecated-declarations -Wno-missing-braces -Wno-overloaded-virtual ${CMAKE_CXX_FLAGS}")
  145. if(NOT WIN32)
  146. set(CMAKE_SHARED_LINKER_FLAGS "-Wl,-z,relro,-z,now -Wl,-z,noexecstack ${CMAKE_SHARED_LINKER_FLAGS}")
  147. set(CMAKE_EXE_LINKER_FLAGS "-Wl,-z,relro,-z,now -Wl,-z,noexecstack ${CMAKE_EXE_LINKER_FLAGS}")
  148. endif()
  149. endif()
  150. if(ENABLE_VERBOSE)
  151. set(CMAKE_VERBOSE_MAKEFILE on)
  152. endif()
  153. if(SUPPORT_TRAIN)
  154. add_compile_definitions(SUPPORT_TRAIN)
  155. endif()
  156. if(ENABLE_NEON)
  157. add_compile_definitions(ENABLE_NEON)
  158. endif()
  159. if(ENABLE_FP16)
  160. add_compile_definitions(ENABLE_FP16)
  161. endif()
  162. if(SUPPORT_GPU STREQUAL opencl)
  163. add_definitions(-DGPU_OPENCL)
  164. gene_opencl(${CMAKE_CURRENT_SOURCE_DIR})
  165. add_definitions(-DUSE_OPENCL_WRAPPER)
  166. add_definitions(-DMS_OPENCL_PROFILE=false)
  167. add_definitions(-DCL_TARGET_OPENCL_VERSION=200)
  168. add_definitions(-DCL_HPP_TARGET_OPENCL_VERSION=120)
  169. add_definitions(-DCL_HPP_MINIMUM_OPENCL_VERSION=120)
  170. add_compile_definitions(SUPPORT_GPU)
  171. if(OFFLINE_COMPILE)
  172. add_compile_definitions(PROGRAM_WITH_IL)
  173. endif()
  174. include_directories(${CMAKE_BINARY_DIR}/_deps/opencl-headers-src/)
  175. include_directories(${CMAKE_BINARY_DIR}/_deps/opencl-clhpp-src/include)
  176. endif()
  177. if(SUPPORT_GPU STREQUAL vulkan)
  178. add_definitions(-DGPU_VULKAN)
  179. add_definitions(-DVK_NO_PROTOTYPES)
  180. add_compile_definitions(SUPPORT_GPU)
  181. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/build/_deps/vulkan-headers-src/include)
  182. endif()
  183. if(SUPPORT_GPU STREQUAL cuda)
  184. add_definitions(-DGPU_CUDA)
  185. add_compile_definitions(SUPPORT_GPU)
  186. endif()
  187. if(WIN32)
  188. add_compile_definitions(LITE_EXPORTS)
  189. add_compile_definitions(BUILDING_DLL)
  190. endif()
  191. if(ENABLE_MINDRT)
  192. include_directories(${CORE_DIR}/mindrt/include)
  193. endif()
  194. if(ENABLE_CONVERTER)
  195. if(PLATFORM_ARM)
  196. MESSAGE(FATAL_ERROR "Cannot build converter in arm platform")
  197. endif()
  198. include_directories(${PYTHON_INCLUDE_DIRS})
  199. include(${TOP_DIR}/cmake/external_libs/eigen.cmake)
  200. include(${TOP_DIR}/cmake/external_libs/protobuf.cmake)
  201. add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/converter)
  202. endif()
  203. if(ENABLE_MINDRT)
  204. add_compile_definitions(ENABLE_MINDRT)
  205. endif()
  206. if(PLATFORM_ARM32 OR PLATFORM_ARM64)
  207. if(NOT APPLE AND NOT DEFINED ENV{ANDROID_NDK})
  208. message(FATAL_ERROR "env ANDROID_NDK should be set for ARM compile")
  209. endif()
  210. add_compile_definitions(ENABLE_ARM)
  211. endif()
  212. if(PLATFORM_ARM32)
  213. add_definitions(-mfloat-abi=softfp -mfpu=neon)
  214. add_compile_definitions(ENABLE_ARM32)
  215. endif()
  216. if(PLATFORM_ARM64)
  217. add_compile_definitions(ENABLE_ARM64)
  218. endif()
  219. if(PLATFORM_ARM32 OR PLATFORM_ARM64)
  220. if(ENABLE_CONVERTER)
  221. set(BUILD_MINDDATA "off")
  222. endif()
  223. endif()
  224. if(NOT PLATFORM_ARM32 AND NOT PLATFORM_ARM64)
  225. if("${X86_64_SIMD}" STREQUAL "sse")
  226. add_compile_definitions(ENABLE_SSE)
  227. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.1")
  228. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.1")
  229. endif()
  230. if("${X86_64_SIMD}" STREQUAL "avx")
  231. add_compile_definitions(ENABLE_SSE)
  232. add_compile_definitions(ENABLE_AVX)
  233. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx -mfma")
  234. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx -mfma")
  235. endif()
  236. endif()
  237. if(BUILD_MINDDATA STREQUAL "lite" OR BUILD_MINDDATA STREQUAL "full" OR BUILD_MINDDATA STREQUAL "wrapper")
  238. add_compile_definitions(ENABLE_ANDROID)
  239. if(NOT PLATFORM_ARM32 AND NOT PLATFORM_ARM64)
  240. add_compile_definitions(ENABLE_MD_LITE_X86_64)
  241. endif()
  242. add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/minddata)
  243. endif()
  244. if(BUILD_MINDDATA STREQUAL "lite_cv")
  245. add_compile_definitions(ENABLE_ANDROID)
  246. add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/minddata)
  247. endif()
  248. add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/src)
  249. add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/nnacl)
  250. add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/micro/coder)
  251. if(NOT APPLE AND ENABLE_TOOLS)
  252. if(SUPPORT_TRAIN)
  253. add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/benchmark_train)
  254. else()
  255. add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/benchmark)
  256. endif()
  257. endif()
  258. if(NOT WIN32)
  259. if(ENABLE_TOOLS)
  260. if(NOT PLATFORM_ARM32 AND NOT PLATFORM_ARM64)
  261. add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/cropper)
  262. add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools/schema_gen)
  263. add_dependencies(fbs_src gen_ops)
  264. add_dependencies(fbs_inner_src gen_ops)
  265. endif()
  266. endif()
  267. if(BUILD_TESTCASES)
  268. add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/test)
  269. endif()
  270. endif()
  271. if(NOT APPLE)
  272. include(${TOP_DIR}/cmake/package_lite.cmake)
  273. endif()