CMakeLists.txt 7.8 KB

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