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