1 cmake_minimum_required(VERSION 3.5)
2 project(YAP LANGUAGES CXX)
4 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
6 ##################################################
7 # C++ standard version selection
8 ##################################################
9 function(constexpr_if_std std_flag var)
13 ${CMAKE_CURRENT_SOURCE_DIR}/cmake/constexpr_if.cpp
14 COMPILE_DEFINITIONS ${std_flag} -DCHECK_CONSTEXPR_IF=1
16 set(${var} ${worked} PARENT_SCOPE)
19 function(try_std_flag std_flag)
23 ${CMAKE_CURRENT_SOURCE_DIR}/cmake/constexpr_if.cpp
24 COMPILE_DEFINITIONS ${std_flag} -DCHECK_CONSTEXPR_IF=0
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")
34 set(constexpr_if_define -DBOOST_NO_CONSTEXPR_IF=1 PARENT_SCOPE)
35 message("-- Checking constexpr if support -- failed to compile")
38 message("-- Checking compiler flag ${std_flag} -- failed to compile")
42 try_std_flag(-std=c++17)
44 try_std_flag(-std=c++1z)
46 try_std_flag(-std=c++14)
48 try_std_flag(/std:c++14)
50 message(FATAL_ERROR "Only c++14 or later will work")
54 ##################################################
56 ##################################################
57 set(USE_ASAN false CACHE BOOL "Set to true to enable -fsanitize=address when building tests.")
58 set(USE_UBSAN false CACHE BOOL "Set to true to enable -fsanitize=undefined when building tests.")
59 if (USE_ASAN AND USE_UBSAN)
60 message(FATAL_ERROR "USE_ASAN and USE_UBSAN must not be enabled at the same time")
62 set(compile_flags -fsanitize=address)
63 set(link_flags -fsanitize=address)
64 message("-- Using -fsanitize=address")
66 set(compile_flags -fsanitize=undefined)
67 set(link_flags -fsanitize=undefined)
68 message("-- Using -fsanitize=undefined")
71 ##################################################
73 ##################################################
75 set(BUILD_COVERAGE false CACHE BOOL "Set to true to enable code coverage when building tests. Only Linux and Mac are supported.")
77 message("-- Building for code coverage; disabling any sanitizers")
79 set(compile_flags -fprofile-arcs -ftest-coverage)
80 set(CMAKE_BUILD_TYPE RelWithDebInfo)
81 set(link_flags --coverage)
83 set(compile_flags --coverage)
84 set(CMAKE_BUILD_TYPE RelWithDebInfo)
85 set(link_flags --coverage)
90 ##################################################
92 ##################################################
93 set(clang_on_linux false)
94 if (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)
99 elseif (CMAKE_CXX_COMPILER_ID STREQUAL GNU)
100 add_definitions(${std_flag} -g -Wall)
103 ##################################################
105 ##################################################
106 add_library(yap INTERFACE)
107 target_include_directories(yap INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
108 target_link_libraries(yap INTERFACE boost)
109 target_compile_features(yap INTERFACE cxx_variadic_templates cxx_constexpr)
110 target_compile_definitions(yap INTERFACE ${constexpr_if_define} BOOST_ALL_NO_LIB=1)
112 target_link_libraries(yap INTERFACE ${link_flags})
113 target_compile_options(yap INTERFACE ${compile_flags})
116 macro(cond_build subdir)
118 string(TOUPPER ${subdir} SUBDIRU)
119 option(YAP_BUILD_${SUBDIRU} "Build ${subdir}" ON)
120 if(YAP_BUILD_${SUBDIRU})
121 add_subdirectory(${subdir})
129 if (YAP_BUILD_EXAMPLE)
133 cond_build(doc) # Doesn't build docs, just the snippets files.
135 ##################################################
137 ##################################################
138 include(dependencies)