CMakeLists.txt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. cmake_minimum_required(VERSION 3.5)
  2. project(BEE2 C)
  3. set(BEE2_VERSION_MAJOR "2")
  4. set(BEE2_VERSION_MINOR "1")
  5. set(BEE2_VERSION_PATCH "5")
  6. set(BEE2_VERSION
  7. "${BEE2_VERSION_MAJOR}.${BEE2_VERSION_MINOR}.${BEE2_VERSION_PATCH}")
  8. # \remark Set BUILD_PIC even for static libraries if they are linked
  9. # into shared libraries
  10. option(BUILD_SHARED_LIBS "Build shared libraries." ON)
  11. option(BUILD_PIC "Build position independent code." ON)
  12. option(BUILD_FAST "Build with the SAFE_FAST directive." OFF)
  13. option(BUILD_CMD "Build bee2cmd." ON)
  14. option(BUILD_TESTS "Build tests." ON)
  15. option(BUILD_DOC "Build documentation (doxygen required)." OFF)
  16. option(INSTALL_HEADERS "Install headers." ON)
  17. if(CMAKE_BUILD_TYPE)
  18. set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}
  19. CACHE STRING "Choose the type of build: \
  20. Release Debug Coverage ASan ASanDbg MemSan MemSanDbg Check"
  21. FORCE)
  22. else()
  23. set(CMAKE_BUILD_TYPE Release
  24. CACHE STRING "Choose the type of build: \
  25. Release Debug Coverage ASan ASanDbg MemSan MemSanDbg Check"
  26. FORCE)
  27. endif()
  28. message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
  29. message(STATUS "Compiler: ${CMAKE_C_COMPILER_ID}")
  30. string(COMPARE EQUAL "GNU" "${CMAKE_C_COMPILER_ID}" CMAKE_COMPILER_IS_GNUCC)
  31. string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
  32. string(COMPARE EQUAL "MSVC" "${CMAKE_C_COMPILER_ID}" CMAKE_COMPILER_IS_MSVC)
  33. if (BASH_PLATFORM)
  34. if(BASH_PLATFORM STREQUAL "BASH_32")
  35. set(BASH_32 ON BOOL)
  36. add_definitions(-DBASH_32)
  37. elseif(BASH_PLATFORM STREQUAL "BASH_SSE2")
  38. set(BASH_SSE2 ON BOOL)
  39. add_definitions(-DBASH_SSE2)
  40. elseif(BASH_PLATFORM STREQUAL "BASH_AVX2")
  41. set(BASH_AVX2 ON BOOL)
  42. add_definitions(-DBASH_AVX2)
  43. elseif(BASH_PLATFORM STREQUAL "BASH_AVX512")
  44. set(BASH_AVX512 ON BOOL)
  45. add_definitions(-DBASH_AVX512)
  46. elseif(BASH_PLATFORM STREQUAL "BASH_NEON")
  47. set(BASH_NEON ON BOOL)
  48. add_definitions(-DBASH_NEON)
  49. elseif(NOT BASH_PLATFORM STREQUAL "BASH_64")
  50. message(WARNING "Unknown BASH_PLATFORM (${BASH_PLATFORM}). \
  51. This option will be ignored")
  52. unset(BASH_PLATFORM CACHE)
  53. endif()
  54. endif()
  55. if (BASH_PLATFORM)
  56. message(STATUS "Requested BASH_PLATFORM: ${BASH_PLATFORM}")
  57. endif()
  58. # Lists of warnings and command-line flags:
  59. # * https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  60. # * https://clang.llvm.org/docs/ClangCommandLineReference.html
  61. # * https://clang.llvm.org/docs/DiagnosticsReference.html
  62. set(CMAKE_C_WARNINGS "-Wall -Wextra -W -Wdeclaration-after-statement \
  63. -Wwrite-strings -Wno-parentheses -Wno-unused-parameter -Wno-strict-aliasing \
  64. -Wno-xor-used-as-pow")
  65. if(CMAKE_COMPILER_IS_GNUCC)
  66. set(CMAKE_C_WARNINGS "${CMAKE_C_WARNINGS} -Wno-maybe-uninitialized")
  67. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_WARNINGS}")
  68. if(BASH_SSE2)
  69. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse2")
  70. elseif(BASH_AVX2)
  71. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx2")
  72. elseif(BASH_AVX512)
  73. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx512f \
  74. -fno-asynchronous-unwind-tables")
  75. elseif(BASH_NEON)
  76. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  77. endif()
  78. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 \
  79. -fno-strict-aliasing")
  80. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g3")
  81. set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_COVERAGE} -O0 -g3 -coverage")
  82. set(CMAKE_C_FLAGS_ASAN "${CMAKE_C_FLAGS_ASAN} -fsanitize=address \
  83. -fno-common -O3 -fno-strict-aliasing")
  84. set(CMAKE_C_FLAGS_ASANDBG "${CMAKE_C_FLAGS_ASANDBG} -fsanitize=address \
  85. -fno-common -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls")
  86. set(CMAKE_C_FLAGS_CHECK "${CMAKE_C_FLAGS_CHECK} -Werror -O1 \
  87. -Wcast-qual -fanalyzer")
  88. endif(CMAKE_COMPILER_IS_GNUCC)
  89. if(CMAKE_COMPILER_IS_CLANG)
  90. set(CMAKE_C_WARNINGS "${CMAKE_C_WARNINGS} -Wno-parentheses-equality \
  91. -Wno-logical-op-parentheses -Wno-bitwise-op-parentheses")
  92. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_WARNINGS}")
  93. if(BASH_SSE2)
  94. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse2")
  95. elseif(BASH_AVX2)
  96. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx2")
  97. elseif(BASH_AVX512)
  98. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} \
  99. -mavx512f -fno-asynchronous-unwind-tables")
  100. elseif(BASH_NEON)
  101. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  102. endif()
  103. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 \
  104. -fno-strict-aliasing")
  105. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g3")
  106. set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_COVERAGE} -O0 -g3 -coverage")
  107. set(CMAKE_C_FLAGS_ASAN "${CMAKE_C_FLAGS_ASAN} -fsanitize=address \
  108. -fno-common -fsanitize=undefined -fno-sanitize-recover -O3 \
  109. -fno-strict-aliasing")
  110. set(CMAKE_C_FLAGS_ASANDBG "${CMAKE_C_FLAGS_ASANDBG} -fsanitize=address \
  111. -fno-common -fsanitize=undefined -fno-sanitize-recover -O1 -g3 \
  112. -fno-omit-frame-pointer -fno-optimize-sibling-calls")
  113. set(CMAKE_C_FLAGS_MEMSAN "${CMAKE_C_FLAGS_MEMSAN} -fsanitize=memory -O3 \
  114. -fno-strict-aliasing")
  115. set(CMAKE_C_FLAGS_MEMSANDBG "${CMAKE_C_FLAGS_MEMSANDBG} -fsanitize=memory \
  116. -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls \
  117. -fsanitize-memory-track-origins=2")
  118. set(CMAKE_C_FLAGS_CHECK "${CMAKE_C_FLAGS_CHECK} -Werror -O1 -Wcast-qual")
  119. endif(CMAKE_COMPILER_IS_CLANG)
  120. if(CMAKE_COMPILER_IS_MSVC)
  121. # disable security warnings for sprintf()
  122. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_NO_WARNINGS")
  123. if(BASH_SSE2)
  124. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:SSE2")
  125. elseif(BASH_AVX2)
  126. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX2")
  127. elseif(BASH_AVX512)
  128. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX512")
  129. endif()
  130. endif(CMAKE_COMPILER_IS_MSVC)
  131. if(BUILD_FAST)
  132. add_definitions(-DSAFE_FAST)
  133. endif()
  134. if(NOT LIB_INSTALL_DIR)
  135. set(LIB_INSTALL_DIR lib)
  136. endif()
  137. if(NOT BIN_INSTALL_DIR)
  138. set(BIN_INSTALL_DIR bin)
  139. endif()
  140. include_directories(include)
  141. include_directories(src)
  142. add_subdirectory(include)
  143. add_subdirectory(src)
  144. if(BUILD_CMD)
  145. add_subdirectory(cmd)
  146. endif()
  147. if(BUILD_TESTS)
  148. enable_testing()
  149. add_subdirectory(test)
  150. endif()
  151. if(BUILD_DOC)
  152. add_subdirectory(doc)
  153. endif()