CMakeLists.txt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. cmake_minimum_required(VERSION 3.9)
  5. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
  6. ##############################################################################
  7. # Setup CMake options
  8. ##############################################################################
  9. option(BOOST_HANA_ENABLE_CONCEPT_CHECKS "Enable concept checking in the interface methods." ON)
  10. option(BOOST_HANA_ENABLE_DEBUG_MODE "Enable Hana's debug mode." OFF)
  11. option(BOOST_HANA_ENABLE_STRING_UDL
  12. "Enable the GNU extension allowing the special string literal operator\
  13. template, which enables the _s suffix for creating compile-time strings." ON)
  14. option(BOOST_HANA_ENABLE_EXCEPTIONS
  15. "Build with exceptions enabled. Note that Hana does not make use of exceptions,\
  16. but this switch can be disabled when building the tests to assess that it is\
  17. really the case." ON)
  18. ##############################################################################
  19. # Setup project
  20. #
  21. # We parse the canonical version number located in <boost/hana/version.hpp>.
  22. # This is done to allow the library to be used without requiring a proper
  23. # installation during which the version would be written to this header.
  24. ##############################################################################
  25. foreach(level MAJOR MINOR PATCH)
  26. file(STRINGS include/boost/hana/version.hpp
  27. _define_${level}
  28. REGEX "#define BOOST_HANA_${level}_VERSION")
  29. string(REGEX MATCH "([0-9]+)" _version_${level} "${_define_${level}}")
  30. endforeach()
  31. set(Boost.Hana_VERSION_STRING "${_version_MAJOR}.${_version_MINOR}.${_version_PATCH}")
  32. project(Boost.Hana VERSION ${Boost.Hana_VERSION_STRING} LANGUAGES CXX)
  33. # Perform checks to make sure we support the current compiler
  34. include(CheckCxxCompilerSupport)
  35. ##############################################################################
  36. # Setup the 'hana' header-only library target, along with its install target
  37. # and exports.
  38. ##############################################################################
  39. include(CheckCXXCompilerFlag)
  40. add_library(hana INTERFACE)
  41. target_include_directories(hana INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>")
  42. target_compile_features(hana INTERFACE cxx_std_14)
  43. # Export the `hana` library into a HanaConfig.cmake file
  44. install(TARGETS hana
  45. EXPORT HanaConfig
  46. INCLUDES DESTINATION include)
  47. install(EXPORT HanaConfig
  48. DESTINATION lib/cmake/hana)
  49. install(DIRECTORY include/boost
  50. DESTINATION include
  51. FILES_MATCHING PATTERN "*.hpp")
  52. # Also install an optional pkg-config file
  53. configure_file(cmake/hana.pc.in hana.pc @ONLY)
  54. install(FILES "${CMAKE_CURRENT_BINARY_DIR}/hana.pc" DESTINATION lib/pkgconfig)
  55. ##############################################################################
  56. # Function to setup common compiler flags on tests and examples
  57. ##############################################################################
  58. function(boost_hana_set_test_properties target)
  59. target_link_libraries(${target} PRIVATE hana)
  60. set_target_properties(${target} PROPERTIES CXX_EXTENSIONS NO)
  61. macro(setflag testname flag)
  62. check_cxx_compiler_flag(${flag} ${testname})
  63. if (${testname})
  64. target_compile_options(${target} PRIVATE ${flag})
  65. endif()
  66. endmacro()
  67. if (NOT MSVC)
  68. setflag(BOOST_HANA_HAS_FDIAGNOSTICS_COLOR -fdiagnostics-color)
  69. setflag(BOOST_HANA_HAS_FTEMPLATE_BACKTRACE_LIMIT -ftemplate-backtrace-limit=0)
  70. setflag(BOOST_HANA_HAS_PEDANTIC -pedantic)
  71. setflag(BOOST_HANA_HAS_WALL -Wall)
  72. setflag(BOOST_HANA_HAS_WERROR -Werror)
  73. setflag(BOOST_HANA_HAS_WEXTRA -Wextra)
  74. setflag(BOOST_HANA_HAS_WNO_SELF_ASSIGN_OVERLOADED -Wno-self-assign-overloaded)
  75. setflag(BOOST_HANA_HAS_WNO_UNUSED_LOCAL_TYPEDEFS -Wno-unused-local-typedefs)
  76. setflag(BOOST_HANA_HAS_WWRITE_STRINGS -Wwrite-strings)
  77. else()
  78. setflag(BOOST_HANA_HAS_MSVC_EHSC -EHsc)
  79. setflag(BOOST_HANA_HAS_MSVC_BIGOBJ -bigobj)
  80. setflag(BOOST_HANA_HAS_MSVC_TERNARY -Zc:ternary)
  81. endif()
  82. if (NOT BOOST_HANA_ENABLE_EXCEPTIONS)
  83. setflag(BOOST_HANA_HAS_FNO_EXCEPTIONS -fno-exceptions)
  84. endif()
  85. if (NOT BOOST_HANA_ENABLE_CONCEPT_CHECKS)
  86. target_compile_definitions(${target} PRIVATE -DBOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS)
  87. endif()
  88. if (BOOST_HANA_ENABLE_DEBUG_MODE)
  89. target_compile_definitions(${target} PRIVATE -DBOOST_HANA_CONFIG_ENABLE_DEBUG_MODE)
  90. endif()
  91. if (BOOST_HANA_ENABLE_STRING_UDL)
  92. if (NOT MSVC)
  93. target_compile_definitions(${target} PRIVATE -DBOOST_HANA_CONFIG_ENABLE_STRING_UDL)
  94. # GCC pretends to have the flag, but produces a "unrecognized command line option"
  95. # warning when we use it.
  96. if (NOT ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
  97. setflag(BOOST_HANA_HAS_WNO_GNU_STRING_UDL -Wno-gnu-string-literal-operator-template)
  98. endif()
  99. endif()
  100. endif()
  101. endfunction()
  102. ##############################################################################
  103. # Look for the rest of Boost, which is an optional dependency of some tests.
  104. ##############################################################################
  105. find_package(Boost 1.64)
  106. if (NOT Boost_FOUND)
  107. message(WARNING "The rest of Boost was not found; some tests and examples will be disabled.")
  108. endif()
  109. ##############################################################################
  110. # Setup custom functions to ease the creation of targets
  111. ##############################################################################
  112. # boost_hana_target_name_for(<output variable> <source file> [ext])
  113. #
  114. # Return the target name associated to a source file. If the path of the
  115. # source file relative from the root of Hana is `path/to/source/file.ext`,
  116. # the target name associated to it will be `path.to.source.file`.
  117. #
  118. # The extension of the file should be specified as a last argument. If no
  119. # extension is specified, the `.cpp` extension is assumed.
  120. function(boost_hana_target_name_for out file)
  121. if (NOT ARGV2)
  122. set(_extension ".cpp")
  123. else()
  124. set(_extension "${ARGV2}")
  125. endif()
  126. file(RELATIVE_PATH _relative "${Boost.Hana_SOURCE_DIR}" "${file}")
  127. string(REPLACE "${_extension}" "" _name "${_relative}")
  128. string(REGEX REPLACE "/" "." _name "${_name}")
  129. set(${out} "${_name}" PARENT_SCOPE)
  130. endfunction()
  131. ##############################################################################
  132. # Setup the `check` target to build and then run all the tests and examples.
  133. ##############################################################################
  134. add_custom_target(hana_check
  135. COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
  136. WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
  137. COMMENT "Build and then run all the tests and examples."
  138. USES_TERMINAL)
  139. if (NOT TARGET check)
  140. add_custom_target(check DEPENDS hana_check)
  141. else()
  142. add_dependencies(check hana_check)
  143. endif()
  144. ##############################################################################
  145. # Setup subdirectories and testing
  146. ##############################################################################
  147. enable_testing()
  148. find_program(MEMORYCHECK_COMMAND valgrind)
  149. if (MEMORYCHECK_COMMAND)
  150. message(STATUS "Found Valgrind: ${MEMORYCHECK_COMMAND}")
  151. set(MEMORYCHECK_COMMAND_OPTIONS "--leak-check=full --error-exitcode=1")
  152. else()
  153. message("Valgrind not found")
  154. endif()
  155. include(CTest)
  156. add_subdirectory(benchmark)
  157. add_subdirectory(doc)
  158. add_subdirectory(example)
  159. add_subdirectory(test)