CMakeLists.txt 6.7 KB

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