Extracted Boost library include paths collection to a CMake module.

Also, save the collected paths to a global property to avoid potentially
scanning the filesystem in every library that needs these paths.
This commit is contained in:
Andrey Semashev 2022-02-04 14:41:47 +03:00
parent dbec3baaad
commit 0ab2d5d309
2 changed files with 39 additions and 17 deletions

View File

@ -1,5 +1,5 @@
# Copyright 2019 Mike Dev
# Copyright 2020, 2021 Andrey Semashev
# Copyright 2020-2022 Andrey Semashev
#
# Distributed under the Boost Software License, Version 1.0.
# See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
@ -20,28 +20,13 @@ set(BOOST_FILESYSTEM_DISABLE_GETRANDOM OFF CACHE BOOL "Disable usage of getrando
set(BOOST_FILESYSTEM_DISABLE_ARC4RANDOM OFF CACHE BOOL "Disable usage of arc4random API in Boost.Filesystem")
set(BOOST_FILESYSTEM_DISABLE_BCRYPT OFF CACHE BOOL "Disable usage of BCrypt API in Boost.Filesystem")
# Generates a list of include paths for all Boost libraries in \a result variable. Uses unified Boost include tree, if available.
function(generate_boost_include_paths result)
if (IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../../boost" AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../../boost/version.hpp")
set(${result} "${CMAKE_CURRENT_SOURCE_DIR}/../.." PARENT_SCOPE)
return()
endif()
file(GLOB path_list LIST_DIRECTORIES True "${CMAKE_CURRENT_SOURCE_DIR}/../*")
foreach(path IN LISTS path_list)
if (IS_DIRECTORY "${path}/include")
list(APPEND include_list "${path}/include")
endif()
endforeach()
set(${result} ${include_list} PARENT_SCOPE)
endfunction()
# Note: We can't use the Boost::library targets in the configure checks as they may not yet be included
# by the superproject when this CMakeLists.txt is included. We also don't want to hardcode include paths
# of the needed libraries and their dependencies, recursively, as this is too fragile and requires maintenance.
# Instead, we collect include paths of all libraries and use them in the configure checks. This works faster
# if there is a unified Boost include tree in the filesystem (i.e. if `b2 headers` was run or we're in the
# official monolithic Boost distribution tree).
generate_boost_include_paths(BOOST_LIBRARY_INCLUDES)
include(cmake/BoostLibraryIncludes.cmake)
set(CMAKE_REQUIRED_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/src" ${BOOST_LIBRARY_INCLUDES})

View File

@ -0,0 +1,37 @@
# Copyright 2022 Andrey Semashev
#
# Distributed under the Boost Software License, Version 1.0.
# See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
#
# After including this module, BOOST_LIBRARY_INCLUDES variable is set to the list of include
# directories for all Boost libraries. If the monolithic include directory is found, it is
# used instead.
if (NOT CMAKE_VERSION VERSION_LESS 3.10)
include_guard()
endif()
# Generates a list of include paths for all Boost libraries in \a result variable. Uses unified Boost include tree, if available.
function(generate_boost_include_paths result)
if (IS_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../../../boost" AND EXISTS "${CMAKE_CURRENT_LIST_DIR}/../../../boost/version.hpp")
get_filename_component(include_dir "${CMAKE_CURRENT_LIST_DIR}/../../.." ABSOLUTE)
set(${result} "${include_dir}" PARENT_SCOPE)
return()
endif()
file(GLOB path_list LIST_DIRECTORIES True "${CMAKE_CURRENT_LIST_DIR}/../../../libs/*")
foreach(path IN LISTS path_list)
if (IS_DIRECTORY "${path}/include")
get_filename_component(include_dir "${path}/include" ABSOLUTE)
list(APPEND include_list "${include_dir}")
endif()
endforeach()
set(${result} ${include_list} PARENT_SCOPE)
endfunction()
if (NOT DEFINED BOOST_LIBRARY_INCLUDES)
generate_boost_include_paths(__BOOST_LIBRARY_INCLUDES)
# Save the paths in a global property to avoid scanning the filesystem if this module is used in multiple libraries
set(BOOST_LIBRARY_INCLUDES ${__BOOST_LIBRARY_INCLUDES} CACHE INTERNAL "List of all Boost library include paths")
unset(__BOOST_LIBRARY_INCLUDES)
# message(STATUS "Boost library includes: ${BOOST_LIBRARY_INCLUDES}")
endif()