CMakeLists.txt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. cmake_minimum_required(VERSION 3.12) # Don't bump this version for no reason
  2. project("llama.cpp" C CXX)
  3. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  4. if (NOT XCODE AND NOT MSVC AND NOT CMAKE_BUILD_TYPE)
  5. set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
  6. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
  7. endif()
  8. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  9. if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  10. set(LLAMA_STANDALONE ON)
  11. # configure project version
  12. # TODO
  13. else()
  14. set(LLAMA_STANDALONE OFF)
  15. endif()
  16. if (EMSCRIPTEN)
  17. set(BUILD_SHARED_LIBS_DEFAULT OFF)
  18. option(LLAMA_WASM_SINGLE_FILE "llama: embed WASM inside the generated llama.js" ON)
  19. else()
  20. if (MINGW)
  21. set(BUILD_SHARED_LIBS_DEFAULT OFF)
  22. else()
  23. set(BUILD_SHARED_LIBS_DEFAULT ON)
  24. endif()
  25. endif()
  26. #
  27. # Option list
  28. #
  29. # general
  30. option(LLAMA_STATIC "llama: static link libraries" OFF)
  31. option(LLAMA_NATIVE "llama: enable -march=native flag" OFF)
  32. option(LLAMA_LTO "llama: enable link time optimization" OFF)
  33. # debug
  34. option(LLAMA_ALL_WARNINGS "llama: enable all compiler warnings" ON)
  35. option(LLAMA_ALL_WARNINGS_3RD_PARTY "llama: enable all compiler warnings in 3rd party libs" OFF)
  36. option(LLAMA_GPROF "llama: enable gprof" OFF)
  37. # sanitizers
  38. option(LLAMA_SANITIZE_THREAD "llama: enable thread sanitizer" OFF)
  39. option(LLAMA_SANITIZE_ADDRESS "llama: enable address sanitizer" OFF)
  40. option(LLAMA_SANITIZE_UNDEFINED "llama: enable undefined sanitizer" OFF)
  41. # instruction set specific
  42. option(LLAMA_AVX "llama: enable AVX" ON)
  43. option(LLAMA_AVX2 "llama: enable AVX2" ON)
  44. option(LLAMA_AVX512 "llama: enable AVX512" OFF)
  45. option(LLAMA_FMA "llama: enable FMA" ON)
  46. # in MSVC F16C is implied with AVX2/AVX512
  47. if (NOT MSVC)
  48. option(LLAMA_F16C "llama: enable F16C" ON)
  49. endif()
  50. # 3rd party libs
  51. option(LLAMA_ACCELERATE "llama: enable Accelerate framework" ON)
  52. option(LLAMA_OPENBLAS "llama: use OpenBLAS" OFF)
  53. option(LLAMA_BUILD_TESTS "llama: build tests" ${LLAMA_STANDALONE})
  54. option(LLAMA_BUILD_EXAMPLES "llama: build examples" ${LLAMA_STANDALONE})
  55. #
  56. # Compile flags
  57. #
  58. set(CMAKE_CXX_STANDARD 11)
  59. set(CMAKE_CXX_STANDARD_REQUIRED true)
  60. set(CMAKE_C_STANDARD 11)
  61. set(CMAKE_C_STANDARD_REQUIRED true)
  62. set(THREADS_PREFER_PTHREAD_FLAG ON)
  63. find_package(Threads REQUIRED)
  64. if (NOT MSVC)
  65. if (LLAMA_SANITIZE_THREAD)
  66. add_compile_options(-fsanitize=thread)
  67. link_libraries(-fsanitize=thread)
  68. endif()
  69. if (LLAMA_SANITIZE_ADDRESS)
  70. add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
  71. link_libraries(-fsanitize=address)
  72. endif()
  73. if (LLAMA_SANITIZE_UNDEFINED)
  74. add_compile_options(-fsanitize=undefined)
  75. link_libraries(-fsanitize=undefined)
  76. endif()
  77. endif()
  78. if (APPLE AND LLAMA_ACCELERATE)
  79. find_library(ACCELERATE_FRAMEWORK Accelerate)
  80. if (ACCELERATE_FRAMEWORK)
  81. message(STATUS "Accelerate framework found")
  82. add_compile_definitions(GGML_USE_ACCELERATE)
  83. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ${ACCELERATE_FRAMEWORK})
  84. else()
  85. message(WARNING "Accelerate framework not found")
  86. endif()
  87. endif()
  88. if (LLAMA_OPENBLAS)
  89. if (LLAMA_STATIC)
  90. set(BLA_STATIC ON)
  91. endif()
  92. set(BLA_VENDOR OpenBLAS)
  93. find_package(BLAS)
  94. if (BLAS_FOUND)
  95. message(STATUS "OpenBLAS found")
  96. add_compile_definitions(GGML_USE_OPENBLAS)
  97. add_link_options(${BLAS_LIBRARIES})
  98. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} openblas)
  99. # find header file
  100. set(OPENBLAS_INCLUDE_SEARCH_PATHS
  101. /usr/include
  102. /usr/include/openblas
  103. /usr/include/openblas-base
  104. /usr/local/include
  105. /usr/local/include/openblas
  106. /usr/local/include/openblas-base
  107. /opt/OpenBLAS/include
  108. $ENV{OpenBLAS_HOME}
  109. $ENV{OpenBLAS_HOME}/include
  110. )
  111. find_path(OPENBLAS_INC NAMES cblas.h PATHS ${OPENBLAS_INCLUDE_SEARCH_PATHS})
  112. add_compile_options(-I${OPENBLAS_INC})
  113. else()
  114. message(WARNING "OpenBLAS not found")
  115. endif()
  116. endif()
  117. if (LLAMA_ALL_WARNINGS)
  118. if (NOT MSVC)
  119. set(c_flags
  120. -Wall
  121. -Wextra
  122. -Wpedantic
  123. -Wcast-qual
  124. -Wdouble-promotion
  125. -Wshadow
  126. -Wstrict-prototypes
  127. -Wpointer-arith
  128. -Wno-unused-function
  129. )
  130. set(cxx_flags
  131. -Wall
  132. -Wextra
  133. -Wpedantic
  134. -Wcast-qual
  135. -Wno-unused-function
  136. -Wno-multichar
  137. )
  138. else()
  139. # todo : msvc
  140. endif()
  141. add_compile_options(
  142. "$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
  143. "$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
  144. )
  145. endif()
  146. if (MSVC)
  147. add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
  148. endif()
  149. if (LLAMA_LTO)
  150. include(CheckIPOSupported)
  151. check_ipo_supported(RESULT result OUTPUT output)
  152. if (result)
  153. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
  154. else()
  155. message(WARNING "IPO is not supported: ${output}")
  156. endif()
  157. endif()
  158. # Architecture specific
  159. # TODO: probably these flags need to be tweaked on some architectures
  160. # feel free to update the Makefile for your architecture and send a pull request or issue
  161. message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
  162. if (NOT MSVC)
  163. if (LLAMA_STATIC)
  164. add_link_options(-static)
  165. if (MINGW)
  166. add_link_options(-static-libgcc -static-libstdc++)
  167. endif()
  168. endif()
  169. if (LLAMA_GPROF)
  170. add_compile_options(-pg)
  171. endif()
  172. if (LLAMA_NATIVE)
  173. add_compile_options(-march=native)
  174. endif()
  175. endif()
  176. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  177. message(STATUS "ARM detected")
  178. if (MSVC)
  179. # TODO: arm msvc?
  180. else()
  181. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  182. add_compile_options(-mcpu=native)
  183. endif()
  184. # TODO: armv6,7,8 version specific flags
  185. endif()
  186. elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
  187. message(STATUS "x86 detected")
  188. if (MSVC)
  189. if (LLAMA_AVX512)
  190. add_compile_options(/arch:AVX512)
  191. elseif (LLAMA_AVX2)
  192. add_compile_options(/arch:AVX2)
  193. elseif (LLAMA_AVX)
  194. add_compile_options(/arch:AVX)
  195. endif()
  196. else()
  197. if (LLAMA_F16C)
  198. add_compile_options(-mf16c)
  199. endif()
  200. if (LLAMA_FMA)
  201. add_compile_options(-mfma)
  202. endif()
  203. if (LLAMA_AVX)
  204. add_compile_options(-mavx)
  205. endif()
  206. if (LLAMA_AVX2)
  207. add_compile_options(-mavx2)
  208. endif()
  209. if (LLAMA_AVX512)
  210. add_compile_options(-mavx512f)
  211. # add_compile_options(-mavx512cd)
  212. # add_compile_options(-mavx512dq)
  213. # add_compile_options(-mavx512bw)
  214. endif()
  215. endif()
  216. else()
  217. # TODO: support PowerPC
  218. message(STATUS "Unknown architecture")
  219. endif()
  220. #
  221. # Build libraries
  222. #
  223. add_library(ggml OBJECT
  224. ggml.c
  225. ggml.h)
  226. target_include_directories(ggml PUBLIC .)
  227. target_compile_features(ggml PUBLIC c_std_11) # don't bump
  228. target_link_libraries(ggml PRIVATE Threads::Threads ${LLAMA_EXTRA_LIBS})
  229. if (BUILD_SHARED_LIBS)
  230. set_target_properties(ggml PROPERTIES POSITION_INDEPENDENT_CODE ON)
  231. endif()
  232. add_library(llama
  233. llama.cpp
  234. llama.h
  235. llama_util.h)
  236. target_include_directories(llama PUBLIC .)
  237. target_compile_features(llama PUBLIC cxx_std_11) # don't bump
  238. target_link_libraries(llama PRIVATE ggml ${LLAMA_EXTRA_LIBS})
  239. if (BUILD_SHARED_LIBS)
  240. set_target_properties(llama PROPERTIES POSITION_INDEPENDENT_CODE ON)
  241. target_compile_definitions(llama PRIVATE LLAMA_SHARED LLAMA_BUILD)
  242. endif()
  243. #
  244. # programs, examples and tests
  245. #
  246. if (LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION)
  247. include(CTest)
  248. add_subdirectory(tests)
  249. endif ()
  250. if (LLAMA_BUILD_EXAMPLES)
  251. add_subdirectory(examples)
  252. endif()