CMakeLists.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. cmake_minimum_required(VERSION 3.5)
  2. list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
  3. ##################################################
  4. # C++ standard version selection
  5. ##################################################
  6. function(constexpr_if_std std_flag var)
  7. try_compile(
  8. worked
  9. ${CMAKE_BINARY_DIR}
  10. ${CMAKE_CURRENT_SOURCE_DIR}/cmake/constexpr_if.cpp
  11. COMPILE_DEFINITIONS ${std_flag} -DCHECK_CONSTEXPR_IF=1
  12. )
  13. set(${var} ${worked} PARENT_SCOPE)
  14. endfunction ()
  15. function(try_std_flag std_flag)
  16. try_compile(
  17. std_supported
  18. ${CMAKE_BINARY_DIR}
  19. ${CMAKE_CURRENT_SOURCE_DIR}/cmake/constexpr_if.cpp
  20. COMPILE_DEFINITIONS ${std_flag} -DCHECK_CONSTEXPR_IF=0
  21. )
  22. if (std_supported)
  23. message("-- Checking compiler flag ${std_flag} -- success")
  24. set(std_flag ${std_flag} PARENT_SCOPE)
  25. constexpr_if_std(${std_flag} have_constexpr_if)
  26. if (have_constexpr_if)
  27. set(constexpr_if_define -DBOOST_NO_CONSTEXPR_IF=0 PARENT_SCOPE)
  28. message("-- Checking constexpr if support -- success")
  29. else ()
  30. set(constexpr_if_define -DBOOST_NO_CONSTEXPR_IF=1 PARENT_SCOPE)
  31. message("-- Checking constexpr if support -- failed to compile")
  32. endif ()
  33. else ()
  34. message("-- Checking compiler flag ${std_flag} -- failed to compile")
  35. endif ()
  36. endfunction ()
  37. try_std_flag(-std=c++17)
  38. if (NOT std_flag)
  39. try_std_flag(-std=c++1z)
  40. elseif (NOT std_flag)
  41. try_std_flag(-std=c++14)
  42. elseif (NOT std_flag)
  43. try_std_flag(/std:c++14)
  44. elseif (NOT std_flag)
  45. message(FATAL_ERROR "Only c++14 or later will work")
  46. endif ()
  47. ##################################################
  48. # Sanitizers
  49. ##################################################
  50. set(USE_ASAN false CACHE BOOL "Set to true to enable -fsanitize=address when building tests.")
  51. set(USE_UBSAN false CACHE BOOL "Set to true to enable -fsanitize=undefined when building tests.")
  52. if (USE_ASAN AND USE_UBSAN)
  53. message(FATAL_ERROR "USE_ASAN and USE_UBSAN must not be enabled at the same time")
  54. elseif (USE_ASAN)
  55. set(compile_flags -fsanitize=address)
  56. set(link_flags -fsanitize=address)
  57. message("-- Using -fsanitize=address")
  58. elseif (USE_UBSAN)
  59. set(compile_flags -fsanitize=undefined)
  60. set(link_flags -fsanitize=undefined)
  61. message("-- Using -fsanitize=undefined")
  62. endif()
  63. ##################################################
  64. # Code coverage
  65. ##################################################
  66. if (UNIX)
  67. set(BUILD_COVERAGE false CACHE BOOL "Set to true to enable code coverage when building tests. Only Linux and Mac are supported.")
  68. if (BUILD_COVERAGE)
  69. message("-- Building for code coverage; disabling any sanitizers")
  70. if (APPLE)
  71. set(compile_flags -fprofile-arcs -ftest-coverage)
  72. set(CMAKE_BUILD_TYPE RelWithDebInfo)
  73. set(link_flags --coverage)
  74. else ()
  75. set(compile_flags --coverage)
  76. set(CMAKE_BUILD_TYPE RelWithDebInfo)
  77. set(link_flags --coverage)
  78. endif ()
  79. endif ()
  80. endif ()
  81. ##################################################
  82. # Clang+Linux support
  83. ##################################################
  84. set(clang_on_linux false)
  85. if (CMAKE_CXX_COMPILER_ID STREQUAL Clang)
  86. add_definitions(${std_flag} -stdlib=libc++ -g -Wall)
  87. if (CMAKE_SYSTEM_NAME STREQUAL Linux)
  88. set(clang_on_linux true)
  89. endif ()
  90. elseif (CMAKE_CXX_COMPILER_ID STREQUAL GNU)
  91. add_definitions(${std_flag} -g -Wall)
  92. endif ()
  93. ##################################################
  94. # Dependencies
  95. ##################################################
  96. include(dependencies)
  97. ##################################################
  98. # yap library
  99. ##################################################
  100. add_library(yap INTERFACE)
  101. target_include_directories(yap INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
  102. target_link_libraries(yap INTERFACE boost)
  103. target_compile_definitions(yap INTERFACE ${constexpr_if_define} BOOST_ALL_NO_LIB=1)
  104. if (link_flags)
  105. target_link_libraries(yap INTERFACE ${link_flags})
  106. target_compile_options(yap INTERFACE ${compile_flags})
  107. endif ()
  108. add_subdirectory(test)
  109. add_subdirectory(example)
  110. add_subdirectory(perf)
  111. add_subdirectory(doc) # Doesn't build docs, just the snippets files.