1 # Copyright Louis Dionne 2013-2017
2 # Distributed under the Boost Software License, Version 1.0.
3 # (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
5 cmake_minimum_required(VERSION 3.7)
6 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
9 ##############################################################################
12 # We parse the canonical version number located in <boost/hana/version.hpp>.
13 # This is done to allow the library to be used without requiring a proper
14 # installation during which the version would be written to this header.
15 ##############################################################################
16 foreach(level MAJOR MINOR PATCH)
17 file(STRINGS include/boost/hana/version.hpp
19 REGEX "#define BOOST_HANA_${level}_VERSION")
20 string(REGEX MATCH "([0-9]+)" _version_${level} "${_define_${level}}")
23 set(Boost.Hana_VERSION_STRING "${_version_MAJOR}.${_version_MINOR}.${_version_PATCH}")
24 project(Boost.Hana VERSION ${Boost.Hana_VERSION_STRING} LANGUAGES CXX)
26 # Perform checks to make sure we support the current compiler
27 include(CheckCxxCompilerSupport)
30 ##############################################################################
31 # Setup the 'hana' header-only library target.
32 ##############################################################################
33 add_library(hana INTERFACE)
34 target_include_directories(hana INTERFACE include)
36 include(CheckCXXCompilerFlag)
37 # On Clang for Windows, -std=c++14 is not known, but -std=c++1y appears to work.
38 if (MSVC AND ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
39 check_cxx_compiler_flag(-std=c++1y BOOST_HANA_HAS_STD_CPP1Y)
40 if (BOOST_HANA_HAS_STD_CPP1Y)
41 target_add_compile_options(hana INTERFACE -std=c++1y)
44 # TODO: Set these as interface properties when supported
45 set(CMAKE_CXX_STANDARD 14)
46 set(CMAKE_CXX_STANDARD_REQUIRED YES)
50 ##############################################################################
52 ##############################################################################
53 option(BOOST_HANA_ENABLE_CONCEPT_CHECKS "Enable concept checking in the interface methods." ON)
54 option(BOOST_HANA_ENABLE_DEBUG_MODE "Enable Hana's debug mode." OFF)
56 option(BOOST_HANA_ENABLE_STRING_UDL
57 "Enable the GNU extension allowing the special string literal operator\
58 template, which enables the _s suffix for creating compile-time strings." ON)
60 option(BOOST_HANA_ENABLE_EXCEPTIONS
61 "Build with exceptions enabled. Note that Hana does not make use of exceptions,\
62 but this switch can be disabled when building the tests to assess that it is\
66 ##############################################################################
67 # Function to setup common compiler flags on tests and examples
68 ##############################################################################
69 function(boost_hana_set_test_properties target)
70 target_link_libraries(${target} PRIVATE hana)
71 set_target_properties(${target} PROPERTIES CXX_EXTENSIONS NO)
73 macro(setflag testname flag)
74 check_cxx_compiler_flag(${flag} ${testname})
76 target_compile_options(${target} PRIVATE ${flag})
80 setflag(BOOST_HANA_HAS_FDIAGNOSTICS_COLOR -fdiagnostics-color)
81 setflag(BOOST_HANA_HAS_FTEMPLATE_BACKTRACE_LIMIT -ftemplate-backtrace-limit=0)
82 setflag(BOOST_HANA_HAS_PEDANTIC -pedantic)
83 setflag(BOOST_HANA_HAS_WALL -Wall)
84 setflag(BOOST_HANA_HAS_WERROR -Werror)
85 setflag(BOOST_HANA_HAS_WEXTRA -Wextra)
86 setflag(BOOST_HANA_HAS_WNO_UNUSED_LOCAL_TYPEDEFS -Wno-unused-local-typedefs)
87 setflag(BOOST_HANA_HAS_WWRITE_STRINGS -Wwrite-strings)
89 if (NOT BOOST_HANA_ENABLE_EXCEPTIONS)
90 setflag(BOOST_HANA_HAS_FNO_EXCEPTIONS -fno-exceptions)
93 if (NOT BOOST_HANA_ENABLE_CONCEPT_CHECKS)
94 target_compile_definitions(${target} PRIVATE -DBOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS)
97 if (BOOST_HANA_ENABLE_DEBUG_MODE)
98 target_compile_definitions(${target} PRIVATE -DBOOST_HANA_CONFIG_ENABLE_DEBUG_MODE)
101 if (BOOST_HANA_ENABLE_STRING_UDL)
102 target_compile_definitions(${target} PRIVATE -DBOOST_HANA_CONFIG_ENABLE_STRING_UDL)
103 # GCC pretends to have the flag, but produces a "unrecognized command line option"
104 # warning when we use it.
105 if (NOT ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
106 setflag(BOOST_HANA_HAS_WNO_GNU_STRING_UDL -Wno-gnu-string-literal-operator-template)
112 ##############################################################################
113 # Setup include paths. More include paths can be added in subdirectories.
114 ##############################################################################
115 include_directories(${Boost.Hana_SOURCE_DIR}/include)
117 find_package(Boost 1.59)
119 include_directories(${Boost_INCLUDE_DIRS})
122 "The Boost library headers were not found; targets depending "
123 "on Boost won't be available.")
127 ##############################################################################
128 # Setup custom functions to ease the creation of targets
129 ##############################################################################
130 # boost_hana_target_name_for(<output variable> <source file> [ext])
132 # Return the target name associated to a source file. If the path of the
133 # source file relative from the root of Hana is `path/to/source/file.ext`,
134 # the target name associated to it will be `path.to.source.file`.
136 # The extension of the file should be specified as a last argument. If no
137 # extension is specified, the `.cpp` extension is assumed.
138 function(boost_hana_target_name_for out file)
140 set(_extension ".cpp")
142 set(_extension "${ARGV2}")
145 file(RELATIVE_PATH _relative ${Boost.Hana_SOURCE_DIR} ${file})
146 string(REPLACE "${_extension}" "" _name ${_relative})
147 string(REGEX REPLACE "/" "." _name ${_name})
148 set(${out} "${_name}" PARENT_SCOPE)
152 ##############################################################################
153 # Setup the `check` target to build and then run all the tests and examples.
154 ##############################################################################
155 add_custom_target(check
156 COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
157 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
158 COMMENT "Build and then run all the tests and examples.")
161 ##############################################################################
162 # Setup subdirectories and testing
163 ##############################################################################
165 find_program(MEMORYCHECK_COMMAND valgrind)
166 if (MEMORYCHECK_COMMAND)
167 message(STATUS "Found Valgrind: ${MEMORYCHECK_COMMAND}")
168 set(MEMORYCHECK_COMMAND_OPTIONS "--leak-check=full --error-exitcode=1")
170 message("Valgrind not found")
174 add_subdirectory(benchmark)
175 add_subdirectory(doc)
176 add_subdirectory(example)
177 add_subdirectory(test)
180 ##############################################################################
181 # Setup the 'install' target.
182 # This copies the whole content of include/ to ${CMAKE_INSTALL_PREFIX}.
183 ##############################################################################
184 install(DIRECTORY include/boost DESTINATION include
185 PATTERN ".DS_Store" EXCLUDE)
187 # We also install an optional pkg-config file.
188 configure_file(cmake/hana.pc.in hana.pc @ONLY)
189 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/hana.pc DESTINATION lib/pkgconfig)