CMakeLists.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. # 3rd party libs
  47. option(LLAMA_ACCELERATE "llama: enable Accelerate framework" ON)
  48. option(LLAMA_OPENBLAS "llama: use OpenBLAS" OFF)
  49. option(LLAMA_BUILD_TESTS "llama: build tests" ${LLAMA_STANDALONE})
  50. option(LLAMA_BUILD_EXAMPLES "llama: build examples" ${LLAMA_STANDALONE})
  51. #
  52. # Compile flags
  53. #
  54. set(CMAKE_CXX_STANDARD_REQUIRED true)
  55. set(CMAKE_C_STANDARD_REQUIRED true)
  56. set(THREADS_PREFER_PTHREAD_FLAG ON)
  57. find_package(Threads REQUIRED)
  58. if (NOT MSVC)
  59. if (LLAMA_SANITIZE_THREAD)
  60. add_compile_options(-fsanitize=thread)
  61. link_libraries(-fsanitize=thread)
  62. endif()
  63. if (LLAMA_SANITIZE_ADDRESS)
  64. add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
  65. link_libraries(-fsanitize=address)
  66. endif()
  67. if (LLAMA_SANITIZE_UNDEFINED)
  68. add_compile_options(-fsanitize=undefined)
  69. link_libraries(-fsanitize=undefined)
  70. endif()
  71. endif()
  72. if (APPLE AND LLAMA_ACCELERATE)
  73. find_library(ACCELERATE_FRAMEWORK Accelerate)
  74. if (ACCELERATE_FRAMEWORK)
  75. message(STATUS "Accelerate framework found")
  76. add_compile_definitions(GGML_USE_ACCELERATE)
  77. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ${ACCELERATE_FRAMEWORK})
  78. else()
  79. message(WARNING "Accelerate framework not found")
  80. endif()
  81. endif()
  82. if (LLAMA_OPENBLAS)
  83. if (LLAMA_STATIC)
  84. set(BLA_STATIC ON)
  85. endif()
  86. set(BLA_VENDOR OpenBLAS)
  87. find_package(BLAS)
  88. if (BLAS_FOUND)
  89. message(STATUS "OpenBLAS found")
  90. add_compile_definitions(GGML_USE_OPENBLAS)
  91. add_link_options(${BLAS_LIBRARIES})
  92. else()
  93. message(WARNING "OpenBLAS not found")
  94. endif()
  95. endif()
  96. if (LLAMA_ALL_WARNINGS)
  97. if (NOT MSVC)
  98. set(c_flags
  99. -Wall
  100. -Wextra
  101. -Wpedantic
  102. -Wcast-qual
  103. -Wdouble-promotion
  104. -Wshadow
  105. -Wstrict-prototypes
  106. -Wpointer-arith
  107. -Wno-unused-function
  108. )
  109. set(cxx_flags
  110. -Wall
  111. -Wextra
  112. -Wpedantic
  113. -Wcast-qual
  114. -Wno-unused-function
  115. )
  116. else()
  117. # todo : msvc
  118. endif()
  119. add_compile_options(
  120. "$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
  121. "$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
  122. )
  123. endif()
  124. if (LLAMA_LTO)
  125. include(CheckIPOSupported)
  126. check_ipo_supported(RESULT result OUTPUT output)
  127. if (result)
  128. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
  129. else()
  130. message(WARNING "IPO is not supported: ${output}")
  131. endif()
  132. endif()
  133. # Architecture specific
  134. # TODO: probably these flags need to be tweaked on some architectures
  135. # feel free to update the Makefile for your architecture and send a pull request or issue
  136. message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
  137. if (NOT MSVC)
  138. if (LLAMA_STATIC)
  139. add_link_options(-static)
  140. if (MINGW)
  141. add_link_options(-static-libgcc -static-libstdc++)
  142. endif()
  143. endif()
  144. if (LLAMA_GPROF)
  145. add_compile_options(-pg)
  146. endif()
  147. if (LLAMA_NATIVE)
  148. add_compile_options(-march=native)
  149. endif()
  150. endif()
  151. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  152. message(STATUS "ARM detected")
  153. if (MSVC)
  154. # TODO: arm msvc?
  155. else()
  156. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  157. add_compile_options(-mcpu=native)
  158. endif()
  159. # TODO: armv6,7,8 version specific flags
  160. endif()
  161. elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
  162. message(STATUS "x86 detected")
  163. if (MSVC)
  164. if (LLAMA_AVX512)
  165. add_compile_options(/arch:AVX512)
  166. elseif (LLAMA_AVX2)
  167. add_compile_options(/arch:AVX2)
  168. elseif (LLAMA_AVX)
  169. add_compile_options(/arch:AVX)
  170. endif()
  171. else()
  172. add_compile_options(-mf16c)
  173. if (LLAMA_FMA)
  174. add_compile_options(-mfma)
  175. endif()
  176. if (LLAMA_AVX)
  177. add_compile_options(-mavx)
  178. endif()
  179. if (LLAMA_AVX2)
  180. add_compile_options(-mavx2)
  181. endif()
  182. if (LLAMA_AVX512)
  183. add_compile_options(-mavx512f)
  184. # add_compile_options(-mavx512cd)
  185. # add_compile_options(-mavx512dq)
  186. # add_compile_options(-mavx512bw)
  187. endif()
  188. endif()
  189. else()
  190. # TODO: support PowerPC
  191. message(STATUS "Unknown architecture")
  192. endif()
  193. #
  194. # Build libraries
  195. #
  196. add_library(ggml OBJECT
  197. ggml.c
  198. ggml.h)
  199. target_include_directories(ggml PUBLIC .)
  200. target_compile_features(ggml PUBLIC c_std_11) # don't bump
  201. target_link_libraries(ggml PRIVATE Threads::Threads ${LLAMA_EXTRA_LIBS})
  202. if (BUILD_SHARED_LIBS)
  203. set_target_properties(ggml PROPERTIES POSITION_INDEPENDENT_CODE ON)
  204. endif()
  205. add_library(llama
  206. llama.cpp
  207. llama.h)
  208. target_include_directories(llama PUBLIC .)
  209. target_compile_features(llama PUBLIC cxx_std_11) # don't bump
  210. target_link_libraries(llama PRIVATE ggml ${LLAMA_EXTRA_LIBS})
  211. if (BUILD_SHARED_LIBS)
  212. set_target_properties(llama PROPERTIES POSITION_INDEPENDENT_CODE ON)
  213. target_compile_definitions(llama PRIVATE LLAMA_SHARED LLAMA_BUILD)
  214. endif()
  215. #
  216. # programs, examples and tests
  217. #
  218. if (LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION)
  219. enable_testing()
  220. add_subdirectory(tests)
  221. endif ()
  222. if (LLAMA_BUILD_EXAMPLES)
  223. add_subdirectory(examples)
  224. endif()