1 # Copyright 2019-2021 Peter Dimov
2 # Distributed under the Boost Software License, Version 1.0.
3 # See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
5 if(CMAKE_SOURCE_DIR STREQUAL Boost_SOURCE_DIR AND WIN32 AND CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
7 set(CMAKE_INSTALL_PREFIX "C:/Boost" CACHE PATH "Installation path prefix, prepended to installation directories" FORCE)
16 set(__boost_incompatible_libraries
20 # Define cache variables if root project
22 if(CMAKE_SOURCE_DIR STREQUAL Boost_SOURCE_DIR)
25 set(BOOST_INCLUDE_LIBRARIES "" CACHE STRING
26 "List of libraries to build (default: all but excluded and incompatible)")
29 set(BOOST_EXCLUDE_LIBRARIES "" CACHE STRING
30 "List of libraries to exclude from build")
32 set(BOOST_INCOMPATIBLE_LIBRARIES
33 "${__boost_incompatible_libraries}"
35 "List of libraries with incompatible CMakeLists.txt files")
37 option(BOOST_ENABLE_MPI
38 "Build and enable installation of Boost.MPI and its dependents (requires MPI, CMake 3.9)")
40 option(BOOST_ENABLE_PYTHON
41 "Build and enable installation of Boost.Python and its dependents (requires Python, CMake 3.14)")
43 # --layout, --libdir, --cmakedir, --includedir in BoostInstall
45 # runtime-link=static|shared
47 set(BOOST_RUNTIME_LINK shared CACHE STRING "Runtime library selection for the MS ABI (shared or static)")
48 set_property(CACHE BOOST_RUNTIME_LINK PROPERTY STRINGS shared static)
50 option(BUILD_TESTING "Build the tests." OFF)
52 add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -C $<CONFIG>)
55 option(BUILD_SHARED_LIBS "Build shared libraries")
58 set(BOOST_STAGEDIR "${CMAKE_CURRENT_BINARY_DIR}/stage" CACHE STRING "Build output directory")
60 if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
61 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${BOOST_STAGEDIR}/bin")
64 if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
65 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${BOOST_STAGEDIR}/lib")
68 if(NOT CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
69 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${BOOST_STAGEDIR}/lib")
74 # add_subdirectory use
76 if(NOT DEFINED BOOST_INCOMPATIBLE_LIBRARIES)
77 set(BOOST_INCOMPATIBLE_LIBRARIES ${__boost_incompatible_libraries})
80 if(NOT DEFINED BOOST_ENABLE_MPI)
81 set(BOOST_ENABLE_MPI OFF)
84 if(NOT DEFINED BOOST_ENABLE_PYTHON)
85 set(BOOST_ENABLE_PYTHON OFF)
88 set(BUILD_TESTING OFF)
89 set(CMAKE_SKIP_INSTALL_RULES ON)
93 if(NOT CMAKE_MSVC_RUNTIME_LIBRARY)
95 set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
97 if(NOT BOOST_RUNTIME_LINK STREQUAL "static")
98 string(APPEND CMAKE_MSVC_RUNTIME_LIBRARY "DLL")
103 # Output configuration status lines
107 if(NOT CMAKE_CONFIGURATION_TYPES AND CMAKE_BUILD_TYPE)
108 string(APPEND _msg "${CMAKE_BUILD_TYPE} build, ")
111 if(BUILD_SHARED_LIBS)
112 string(APPEND _msg "shared libraries, ")
114 string(APPEND _msg "static libraries, ")
118 if(CMAKE_MSVC_RUNTIME_LIBRARY STREQUAL "MultiThreaded$<$<CONFIG:Debug>:Debug>")
119 string(APPEND _msg "static runtime, ")
120 elseif(CMAKE_MSVC_RUNTIME_LIBRARY STREQUAL "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
121 string(APPEND _msg "shared runtime, ")
125 string(APPEND _msg "MPI ${BOOST_ENABLE_MPI}, Python ${BOOST_ENABLE_PYTHON}, testing ${BUILD_TESTING}")
127 message(STATUS "Boost: ${_msg}")
131 if(BOOST_INCLUDE_LIBRARIES)
132 message(STATUS "Boost: libraries included: ${BOOST_INCLUDE_LIBRARIES}")
135 if(BOOST_EXCLUDE_LIBRARIES)
136 message(STATUS "Boost: libraries excluded: ${BOOST_EXCLUDE_LIBRARIES}")
141 function(__boost_auto_install __boost_lib)
142 if(NOT CMAKE_VERSION VERSION_LESS 3.13)
144 string(MAKE_C_IDENTIFIER "${__boost_lib}" __boost_lib_target)
146 if(TARGET "Boost::${__boost_lib_target}" AND TARGET "boost_${__boost_lib_target}")
148 get_target_property(__boost_lib_incdir "boost_${__boost_lib_target}" INTERFACE_INCLUDE_DIRECTORIES)
150 if(__boost_lib_incdir STREQUAL "${BOOST_SUPERPROJECT_SOURCE_DIR}/libs/${__boost_lib}/include")
152 boost_message(DEBUG "Enabling installation for '${__boost_lib}'")
153 boost_install(TARGETS "boost_${__boost_lib_target}" VERSION "${BOOST_SUPERPROJECT_VERSION}" HEADER_DIRECTORY "${BOOST_SUPERPROJECT_SOURCE_DIR}/libs/${__boost_lib}/include")
156 boost_message(DEBUG "Not enabling installation for '${__boost_lib}'; interface include directory '${__boost_lib_incdir}' does not equal '${BOOST_SUPERPROJECT_SOURCE_DIR}/libs/${__boost_lib}/include'")
160 boost_message(DEBUG "Not enabling installation for '${__boost_lib}'; targets 'Boost::${__boost_lib_target}' and 'boost_${__boost_lib_target}' weren't found")
166 function(__boost_scan_dependencies lib var)
170 if(EXISTS "${BOOST_SUPERPROJECT_SOURCE_DIR}/libs/${lib}/CMakeLists.txt")
172 file(STRINGS "${BOOST_SUPERPROJECT_SOURCE_DIR}/libs/${lib}/CMakeLists.txt" data)
174 foreach(line IN LISTS data)
176 if(line MATCHES "^[ ]*Boost::([A-Za-z0-9_]+)[ ]*$")
178 string(REGEX REPLACE "^numeric_" "numeric/" dep ${CMAKE_MATCH_1})
179 list(APPEND result ${dep})
187 set(${var} ${result} PARENT_SCOPE)
193 file(GLOB __boost_libraries RELATIVE "${BOOST_SUPERPROJECT_SOURCE_DIR}/libs" "${BOOST_SUPERPROJECT_SOURCE_DIR}/libs/*/CMakeLists.txt" "${BOOST_SUPERPROJECT_SOURCE_DIR}/libs/numeric/*/CMakeLists.txt")
195 # Check for mistakes in BOOST_INCLUDE_LIBRARIES
197 foreach(__boost_included_lib IN LISTS BOOST_INCLUDE_LIBRARIES)
199 if(NOT "${__boost_included_lib}/CMakeLists.txt" IN_LIST __boost_libraries)
201 message(WARNING "Library '${__boost_included_lib}' given in BOOST_INCLUDE_LIBRARIES has not been found.")
207 # Scan for dependencies
209 set(__boost_include_libraries ${BOOST_INCLUDE_LIBRARIES})
211 if(__boost_include_libraries)
212 list(REMOVE_DUPLICATES __boost_include_libraries)
215 set(__boost_libs_to_scan ${__boost_include_libraries})
217 while(__boost_libs_to_scan)
219 boost_message(DEBUG "Scanning dependencies: ${__boost_libs_to_scan}")
221 set(__boost_dependencies "")
223 foreach(__boost_lib IN LISTS __boost_libs_to_scan)
225 __boost_scan_dependencies(${__boost_lib} __boost_deps)
226 list(APPEND __boost_dependencies ${__boost_deps})
230 list(REMOVE_DUPLICATES __boost_dependencies)
232 set(__boost_libs_to_scan ${__boost_dependencies})
234 if(__boost_libs_to_scan)
235 list(REMOVE_ITEM __boost_libs_to_scan ${__boost_include_libraries})
238 list(APPEND __boost_include_libraries ${__boost_libs_to_scan})
242 # Installing targets created in other directories requires CMake 3.13
243 if(CMAKE_VERSION VERSION_LESS 3.13)
245 boost_message(STATUS "Boost installation support requires CMake 3.13 (have ${CMAKE_VERSION})")
249 set(__boost_mpi_libs mpi graph_parallel property_map_parallel)
250 set(__boost_python_libs python parameter_python)
252 foreach(__boost_lib_cml IN LISTS __boost_libraries)
254 get_filename_component(__boost_lib "${__boost_lib_cml}" DIRECTORY)
256 if(__boost_lib IN_LIST BOOST_INCOMPATIBLE_LIBRARIES)
258 boost_message(DEBUG "Skipping incompatible Boost library ${__boost_lib}")
260 elseif(__boost_lib IN_LIST BOOST_EXCLUDE_LIBRARIES)
262 boost_message(DEBUG "Skipping excluded Boost library ${__boost_lib}")
264 elseif(NOT BOOST_ENABLE_MPI AND __boost_lib IN_LIST __boost_mpi_libs)
266 boost_message(DEBUG "Skipping Boost library ${__boost_lib}, BOOST_ENABLE_MPI is OFF")
268 elseif(NOT BOOST_ENABLE_PYTHON AND __boost_lib IN_LIST __boost_python_libs)
270 boost_message(DEBUG "Skipping Boost library ${__boost_lib}, BOOST_ENABLE_PYTHON is OFF")
272 elseif(NOT BOOST_INCLUDE_LIBRARIES OR __boost_lib IN_LIST BOOST_INCLUDE_LIBRARIES)
274 boost_message(VERBOSE "Adding Boost library ${__boost_lib}")
275 add_subdirectory(libs/${__boost_lib})
277 __boost_auto_install(${__boost_lib})
279 elseif(__boost_lib IN_LIST __boost_include_libraries)
281 # Disable tests for dependencies
283 set(__boost_build_testing ${BUILD_TESTING})
284 set(BUILD_TESTING OFF) # hide cache variable
286 boost_message(VERBOSE "Adding Boost dependency ${__boost_lib}")
287 add_subdirectory(libs/${__boost_lib})
289 __boost_auto_install(${__boost_lib})
291 set(BUILD_TESTING ${__boost_build_testing})
293 elseif(BUILD_TESTING)
295 # Disable tests and installation for libraries neither included nor dependencies
297 set(__boost_build_testing ${BUILD_TESTING})
298 set(BUILD_TESTING OFF) # hide cache variable
300 set(__boost_skip_install ${CMAKE_SKIP_INSTALL_RULES})
301 set(CMAKE_SKIP_INSTALL_RULES ON)
303 boost_message(DEBUG "Adding Boost library ${__boost_lib} with EXCLUDE_FROM_ALL")
304 add_subdirectory(libs/${__boost_lib} EXCLUDE_FROM_ALL)
306 set(BUILD_TESTING ${__boost_build_testing})
307 set(CMAKE_SKIP_INSTALL_RULES ${__boost_skip_install})