CMakeLists.txt 6.9 KB

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