CMakeLists.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. cmake_minimum_required (VERSION 3.3)
  2. project(ggml VERSION 0.1.0)
  3. set(CMAKE_EXPORT_COMPILE_COMMANDS "on")
  4. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  5. set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
  6. if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  7. set(GGML_STANDALONE ON)
  8. include(cmake/GitVars.cmake)
  9. include(cmake/BuildTypes.cmake)
  10. else()
  11. set(GGML_STANDALONE OFF)
  12. endif()
  13. # options
  14. option(GGML_ALL_WARNINGS "ggml: enable all compiler warnings" ON)
  15. option(GGML_ALL_WARNINGS_3RD_PARTY "ggml: enable all compiler warnings in 3rd party libs" OFF)
  16. option(GGML_SANITIZE_THREAD "ggml: enable thread sanitizer" OFF)
  17. option(GGML_SANITIZE_ADDRESS "ggml: enable address sanitizer" OFF)
  18. option(GGML_SANITIZE_UNDEFINED "ggml: enable undefined sanitizer" OFF)
  19. option(GGML_BUILD_TESTS "ggml: build tests" ${GGML_STANDALONE})
  20. option(GGML_BUILD_EXAMPLES "ggml: build examples" ${GGML_STANDALONE})
  21. option(GGML_TEST_COVERAGE "ggml: enable test coverage" OFF)
  22. option(GGML_PERF "ggml: enable perf timings" OFF)
  23. option(GGML_NO_ACCELERATE "ggml: disable Accelerate framework" OFF)
  24. option(GGML_OPENBLAS "ggml: use OpenBLAS" OFF)
  25. option(GGML_CLBLAST "ggml: use clBLAST" OFF)
  26. option(GGML_CUBLAS "ggml: use cuBLAS" OFF)
  27. option(GGML_METAL "ggml: use Metal" OFF)
  28. # sanitizers
  29. if (GGML_SANITIZE_THREAD)
  30. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
  31. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
  32. endif()
  33. if (GGML_SANITIZE_ADDRESS)
  34. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
  35. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
  36. endif()
  37. if (GGML_SANITIZE_UNDEFINED)
  38. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
  39. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
  40. endif()
  41. # instruction set specific
  42. option(GGML_AVX "ggml: enable AVX" ON)
  43. option(GGML_AVX2 "ggml: enable AVX2" ON)
  44. option(GGML_AVX512 "ggml: enable AVX512" OFF)
  45. option(GGML_AVX512_VBMI "ggml: enable AVX512-VBMI" OFF)
  46. option(GGML_AVX512_VNNI "ggml: enable AVX512-VNNI" OFF)
  47. option(GGML_FMA "ggml: enable FMA" ON)
  48. # in MSVC F16C is implied with AVX2/AVX512
  49. if (NOT MSVC)
  50. option(GGML_F16C "ggml: enable F16C" ON)
  51. endif()
  52. #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffast-math")
  53. #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native")
  54. #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mcpu=native")
  55. # warning flags
  56. if (GGML_ALL_WARNINGS)
  57. if (NOT MSVC)
  58. set(c_flags -Wall -Wpedantic -Wformat=2 -Wno-unused -Wstrict-prototypes)
  59. set(cxx_flags -Wall -Wpedantic -Wformat=2)
  60. else()
  61. # todo : windows
  62. endif()
  63. add_compile_options(
  64. "$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
  65. "$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
  66. )
  67. endif()
  68. if (NOT MSVC)
  69. add_compile_options(
  70. "$<$<COMPILE_LANGUAGE:C>:-Werror=vla>"
  71. "$<$<COMPILE_LANGUAGE:CXX>:-Werror=vla>"
  72. "$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler;-Werror=vla>"
  73. )
  74. endif()
  75. #
  76. # POSIX conformance
  77. #
  78. # clock_gettime came in POSIX.1b (1993)
  79. # CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
  80. # posix_memalign came in POSIX.1-2001 / SUSv3
  81. # M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
  82. add_compile_definitions(_XOPEN_SOURCE=600)
  83. # Somehow in OpenBSD whenever POSIX conformance is specified
  84. # some string functions rely on locale_t availability,
  85. # which was introduced in POSIX.1-2008, forcing us to go higher
  86. if (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
  87. remove_definitions(-D_XOPEN_SOURCE=600)
  88. add_compile_definitions(_XOPEN_SOURCE=700)
  89. endif()
  90. # Data types, macros and functions related to controlling CPU affinity
  91. # are available on Linux through GNU extensions in libc
  92. if (CMAKE_SYSTEM_NAME MATCHES "Linux")
  93. add_compile_definitions(_GNU_SOURCE)
  94. endif()
  95. # RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
  96. # and on macOS its availability depends on enabling Darwin extensions
  97. # similarly on DragonFly, enabling BSD extensions is necessary
  98. if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
  99. add_compile_definitions(_DARWIN_C_SOURCE)
  100. endif()
  101. if (CMAKE_SYSTEM_NAME MATCHES "DragonFly")
  102. add_compile_definitions(_DARWIN_C_SOURCE)
  103. endif()
  104. # alloca is a non-standard interface that is not visible on BSDs when
  105. # POSIX conformance is specified, but not all of them provide a clean way
  106. # to enable it in such cases
  107. if (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
  108. add_compile_definitions(__BSD_VISIBLE)
  109. endif()
  110. if (CMAKE_SYSTEM_NAME MATCHES "NetBSD")
  111. add_compile_definitions(_NETBSD_SOURCE)
  112. endif()
  113. if (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
  114. add_compile_definitions(_BSD_SOURCE)
  115. endif()
  116. if (WHISPER_PERF)
  117. set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_PERF)
  118. endif()
  119. # dependencies
  120. set(CMAKE_C_STANDARD 11)
  121. set(CMAKE_CXX_STANDARD 11)
  122. find_package(Threads REQUIRED)
  123. # main
  124. if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  125. set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
  126. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "RelWithDebInfo")
  127. endif ()
  128. if (GGML_BUILD_TESTS)
  129. if (GGML_TEST_COVERAGE)
  130. if (CMAKE_C_COMPILER_ID MATCHES "Clang")
  131. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
  132. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
  133. else()
  134. message(WARNING "Test coverage is only supported for Clang")
  135. endif()
  136. endif()
  137. endif()
  138. add_subdirectory(src)
  139. if (GGML_BUILD_TESTS)
  140. enable_testing()
  141. add_subdirectory(tests)
  142. endif ()
  143. if (GGML_BUILD_EXAMPLES)
  144. add_subdirectory(examples)
  145. endif ()
  146. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/ggml.pc.in
  147. ${CMAKE_CURRENT_BINARY_DIR}/ggml.pc
  148. @ONLY)
  149. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ggml.pc
  150. DESTINATION share/pkgconfig)