Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / hana / CMakeLists.txt
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)
4
5 cmake_minimum_required(VERSION 3.7)
6 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
7
8
9 ##############################################################################
10 # Setup project
11 #
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
18                  _define_${level}
19                  REGEX "#define BOOST_HANA_${level}_VERSION")
20     string(REGEX MATCH "([0-9]+)" _version_${level} "${_define_${level}}")
21 endforeach()
22
23 set(Boost.Hana_VERSION_STRING "${_version_MAJOR}.${_version_MINOR}.${_version_PATCH}")
24 project(Boost.Hana VERSION ${Boost.Hana_VERSION_STRING} LANGUAGES CXX)
25
26 # Perform checks to make sure we support the current compiler
27 include(CheckCxxCompilerSupport)
28
29
30 ##############################################################################
31 # Setup the 'hana' header-only library target.
32 ##############################################################################
33 add_library(hana INTERFACE)
34 target_include_directories(hana INTERFACE include)
35
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)
42     endif()
43 else()
44     # TODO: Set these as interface properties when supported
45     set(CMAKE_CXX_STANDARD 14)
46     set(CMAKE_CXX_STANDARD_REQUIRED YES)
47 endif()
48
49
50 ##############################################################################
51 # Setup CMake options
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)
55
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)
59
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\
63  really the case." ON)
64
65
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)
72
73     macro(setflag testname flag)
74         check_cxx_compiler_flag(${flag} ${testname})
75         if (${testname})
76             target_compile_options(${target} PRIVATE ${flag})
77         endif()
78     endmacro()
79
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)
88
89     if (NOT BOOST_HANA_ENABLE_EXCEPTIONS)
90         setflag(BOOST_HANA_HAS_FNO_EXCEPTIONS -fno-exceptions)
91     endif()
92
93     if (NOT BOOST_HANA_ENABLE_CONCEPT_CHECKS)
94         target_compile_definitions(${target} PRIVATE -DBOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS)
95     endif()
96
97     if (BOOST_HANA_ENABLE_DEBUG_MODE)
98         target_compile_definitions(${target} PRIVATE -DBOOST_HANA_CONFIG_ENABLE_DEBUG_MODE)
99     endif()
100
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)
107         endif()
108     endif()
109 endfunction()
110
111
112 ##############################################################################
113 # Setup include paths. More include paths can be added in subdirectories.
114 ##############################################################################
115 include_directories(${Boost.Hana_SOURCE_DIR}/include)
116
117 find_package(Boost 1.59)
118 if (Boost_FOUND)
119     include_directories(${Boost_INCLUDE_DIRS})
120 else()
121     message(WARNING
122         "The Boost library headers were not found; targets depending "
123         "on Boost won't be available.")
124 endif()
125
126
127 ##############################################################################
128 # Setup custom functions to ease the creation of targets
129 ##############################################################################
130 #   boost_hana_target_name_for(<output variable> <source file> [ext])
131 #
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`.
135 #
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)
139     if (NOT ARGV2)
140         set(_extension ".cpp")
141     else()
142         set(_extension "${ARGV2}")
143     endif()
144
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)
149 endfunction()
150
151
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.")
159
160
161 ##############################################################################
162 # Setup subdirectories and testing
163 ##############################################################################
164 enable_testing()
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")
169 else()
170     message("Valgrind not found")
171 endif()
172 include(CTest)
173
174 add_subdirectory(benchmark)
175 add_subdirectory(doc)
176 add_subdirectory(example)
177 add_subdirectory(test)
178
179
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)
186
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)