1#============================================================================= 2# Copyright (c) 2017 Paul Fultz II 3# CMakeLists.txt 4# Distributed under the Boost Software License, Version 1.0. (See accompanying 5# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6#============================================================================== 7cmake_minimum_required (VERSION 2.8) 8project (boost_hof) 9list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 10 11foreach(level MAJOR MINOR PATCH) 12 file(STRINGS include/boost/hof/version.hpp 13 _define_${level} 14 REGEX "#define BOOST_HOF_${level}_VERSION") 15 string(REGEX MATCH "([0-9]+)" boost_hof_VERSION_${level} "${_define_${level}}") 16endforeach() 17 18include(CheckCXXCompilerFlag) 19enable_language(CXX) 20 21if(NOT CMAKE_CXX_FLAGS MATCHES "-std=") 22 23 if(CMAKE_HOST_APPLE) 24 check_cxx_compiler_flag("-stdlib=libc++" COMPILER_HAS_CXX_FLAG_libcxx) 25 if(COMPILER_HAS_CXX_FLAG_libcxx) 26 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") 27 endif() 28 endif() 29 30 set(ENABLE_CXXFLAGS_TO_CHECK 31 -std=gnu++17 32 -std=c++17 33 -std=gnu++1z 34 -std=c++1z 35 -std=gnu++14 36 -std=c++14 37 -std=gnu++1y 38 -std=c++1y 39 -std=gnu++11 40 -std=c++11 41 -std=gnu++0x 42 -std=c++0x) 43 44 foreach(flag ${ENABLE_CXXFLAGS_TO_CHECK}) 45 string(REPLACE "-std=" "_" flag_var ${flag}) 46 string(REPLACE "+" "x" flag_var ${flag_var}) 47 check_cxx_compiler_flag("${flag}" COMPILER_HAS_CXX_FLAG${flag_var}) 48 if(COMPILER_HAS_CXX_FLAG${flag_var}) 49 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") 50 break() 51 endif() 52 endforeach() 53 54endif() 55 56install (DIRECTORY include/boost DESTINATION include) 57configure_file(boost_hof.pc.in boost_hof.pc) 58install(FILES ${CMAKE_CURRENT_BINARY_DIR}/boost_hof.pc DESTINATION lib/pkgconfig) 59 60include(SphinxDoc) 61 62add_sphinx_doc(${CMAKE_CURRENT_SOURCE_DIR}/doc HTML_DIR ${CMAKE_CURRENT_SOURCE_DIR}/doc/html) 63 64include(BCMTest) 65 66include_directories(include) 67 68file(GLOB TESTS test/*.cpp) 69foreach(TEST ${TESTS}) 70 get_filename_component(BASE_NAME ${TEST} NAME_WE) 71 bcm_add_test(NAME ${BASE_NAME} SOURCES ${TEST}) 72 set_tests_properties(${BASE_NAME} PROPERTIES FAIL_REGULAR_EXPRESSION "FAILED") 73endforeach() 74bcm_add_test(NAME static_def SOURCES test/static_def/static_def.cpp test/static_def/static_def2.cpp) 75set_tests_properties(static_def PROPERTIES FAIL_REGULAR_EXPRESSION "FAILED") 76 77file(GLOB FAIL_TESTS test/fail/*.cpp) 78foreach(TEST ${FAIL_TESTS}) 79 get_filename_component(BASE_NAME ${TEST} NAME_WE) 80 bcm_add_test(NAME fail-${BASE_NAME} COMPILE_ONLY WILL_FAIL SOURCES ${TEST}) 81endforeach() 82 83file(GLOB HEADERS include/boost/hof/*.hpp) 84foreach(HEADER ${HEADERS}) 85 get_filename_component(BASE_NAME ${HEADER} NAME_WE) 86 bcm_test_header(NAME ${BASE_NAME} HEADER boost/hof/${BASE_NAME}.hpp STATIC) 87endforeach() 88bcm_test_header(NAME hof HEADER boost/hof.hpp STATIC) 89 90function(extract_doc SOURCE OUTPUTVAR) 91 file(READ ${SOURCE} CONTENT) 92 string(REGEX REPLACE "(\n(/[^/][^/]|//[^/]|[^/][^/][^/])([^\n])*)" "" CONTENT "\n${CONTENT}") 93 string(REPLACE "/// " "" CONTENT "${CONTENT}") 94 set(${OUTPUTVAR} "${CONTENT}" PARENT_SCOPE) 95endfunction() 96 97function(build_example SECTIONCONTENT NAME) 98 string(MD5 MD5_SECTION "${SECTIONCONTENT}") 99 set(TARGET_NAME "example-${NAME}-${MD5_SECTION}") 100 set(CONTENT "") 101 foreach(LINE ${SECTIONCONTENT}) 102 if(LINE MATCHES "^ ") 103 string(SUBSTRING ${LINE} "4" "-1" OUTPUT_LINE) 104 list(APPEND CONTENT ${OUTPUT_LINE}) 105 endif() 106 endforeach() 107 string(REPLACE ";" "\n" CONTENT "${CONTENT}") 108 string(REPLACE "$$__semicolon__$$" ";" CONTENT "${CONTENT}") 109 if(NOT CONTENT MATCHES "int main") 110 string(APPEND CONTENT "int main() {}") 111 endif() 112 message(STATUS "Adding example: ${TARGET_NAME}") 113 bcm_add_test(NAME ${TARGET_NAME} CONTENT "${CONTENT}\n") 114endfunction() 115 116function(extract_example SOURCE) 117 extract_doc(${SOURCE} CONTENT) 118 string(REPLACE ";" "$$__semicolon__$$" CONTENT "${CONTENT}") 119 string(REPLACE "\n" ";" CONTENT "${CONTENT}") 120 121 get_filename_component(BASE_NAME ${SOURCE} NAME_WE) 122 123 set(SECTION "") 124 set(PREVLINE "") 125 set(APPEND_SECTION Off) 126 foreach(LINE ${CONTENT}) 127 if(APPEND_SECTION) 128 if((LINE MATCHES "^---") OR (LINE MATCHES "^===")) 129 build_example("${SECTION}" ${BASE_NAME}) 130 set(APPEND_SECTION Off) 131 set(SECTION "") 132 elseif(NOT PREVLINE MATCHES "^---") 133 list(APPEND SECTION ${PREVLINE}) 134 endif() 135 else() 136 if((LINE MATCHES "^---") AND (PREVLINE MATCHES "^Example")) 137 set(APPEND_SECTION One) 138 endif() 139 endif() 140 set(PREVLINE "${LINE}") 141 endforeach() 142 list(APPEND SECTION ${PREVLINE}) 143 if(APPEND_SECTION) 144 build_example("${SECTION}" ${BASE_NAME}) 145 endif() 146endfunction() 147 148set(BUILD_EXAMPLES off CACHE BOOL "Set this to build the examples") 149 150if (BUILD_EXAMPLES) 151 file(GLOB EXAMPLES example/*.cpp) 152 foreach(EXAMPLE ${EXAMPLES}) 153 get_filename_component(BASE_NAME ${EXAMPLE} NAME_WE) 154 bcm_add_test(NAME example-${BASE_NAME} SOURCES ${EXAMPLE}) 155 endforeach() 156 foreach(HEADER ${HEADERS}) 157 extract_example(${HEADER}) 158 endforeach() 159endif() 160