Skip to content

Commit

Permalink
Does not rely on CMake's FindBoost module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Keita Iwabuchi committed Oct 4, 2024
1 parent ace8bc2 commit 9ab6778
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 27 deletions.
16 changes: 10 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13")
cmake_policy(SET CMP0077 NEW)
endif ()

# When using the URL download method with the ExternalProject_Add() or
# FetchContent_Declare() commands, sets the timestamps of extracted contents
# to the time of extraction.
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24")
cmake_policy(SET CMP0135 NEW)
endif ()

# Accept <PACKAGENAME>_ROOT variables for find_package() command.
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.27")
cmake_policy(SET CMP0144 NEW)
endif ()

# find_package(Boost) to search for the upstream BoostConfig.cmake.
# Disable CMake's FindBoost module.
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.30")
cmake_policy(SET CMP0167 NEW)
endif ()
Expand Down Expand Up @@ -196,15 +202,13 @@ if (PRIVATEER_ROOT)
endif ()

# ---------- Boost ---------- #
find_package(Boost 1.80)
include(find_boost_headers)
find_boost_headers(1.80 FALSE)
if (NOT Boost_FOUND)
message(STATUS "Boost is not found. Fetching Boost.")
message(STATUS "Fetching Boost")
FetchContent_Declare(Boost
URL https://boostorg.jfrog.io/artifactory/main/release/1.83.0/source/boost_1_83_0.tar.bz2)
FetchContent_GetProperties(Boost)
if (NOT Boost_POPULATED)
FetchContent_Populate(Boost)
endif ()
FetchContent_MakeAvailable(Boost)
set(BOOST_ROOT ${boost_SOURCE_DIR})
find_package(Boost 1.80 REQUIRED)
endif ()
Expand Down
60 changes: 60 additions & 0 deletions cmake/find_boost_headers.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Find Boost header files
# Input:
# min_version - minimum required Boost version
# required - if TRUE, the function will stop the build if Boost headers are not found
#
# This function first tries to find Boost headers using the find_package() command.
# If Boost headers are not found, the function tries to find Boost headers manually.
# In the case, the following variables as hints to find Boost headers, in the following order:
# BOOST_ROOT CMake variable, Boost_ROOT CMake variable, BOOST_ROOT environment variable, Boost_ROOT environment variable
#
# Output:
# Boost_FOUND - TRUE if Boost headers are found
# Boost_INCLUDE_DIRS - Boost header files directory
# Boost_VERSION - Boost version
function(find_boost_headers min_version required)
find_package(Boost ${min_version})
if (Boost_FOUND)
set(Boost_INCLUDE_DIRS ${Boost_INCLUDE_DIRS} PARENT_SCOPE)
set(Boost_FOUND ${Boost_FOUND} PARENT_SCOPE)
return()
endif ()

message(STATUS "Could not find Boost headers using the find_package() command. Trying to find Boost headers manually.")

# Try to find Boost headers manually using the BOOST_ROOT as a hint
find_path(Boost_INCLUDE_DIRS boost/version.hpp PATHS ${BOOST_ROOT} ${Boost_ROOT} $ENV{BOOST_ROOT} $ENV{Boost_ROOT} NO_DEFAULT_PATH)
if (Boost_INCLUDE_DIRS)

# Extract Boost version value from the boost/version.hpp file
file(STRINGS ${Boost_INCLUDE_DIRS}/boost/version.hpp _boost_version REGEX "#define BOOST_VERSION[ \t]+[0-9]+")
string(REGEX REPLACE "#define BOOST_VERSION[ \t]+([0-9]+)" "\\1" Boost_VERSION ${_boost_version})

# Convert Boost version to the format 'MAJOR.MINOR.PATCH'
# Major version
math(EXPR Boost_VERSION_MAJOR "${Boost_VERSION} / 100000")
# Minor version
math(EXPR Boost_VERSION_MINOR "(${Boost_VERSION} / 100) % 1000")
# Patch version
math(EXPR Boost_VERSION_PATCH "${Boost_VERSION} % 100")
set(Boost_VERSION "${Boost_VERSION_MAJOR}.${Boost_VERSION_MINOR}.${Boost_VERSION_PATCH}")

if (${Boost_VERSION} VERSION_GREATER_EQUAL ${min_version})
message(STATUS "Found Boost headers at ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost version: ${Boost_VERSION}")
set(Boost_FOUND TRUE PARENT_SCOPE)
set(Boost_INCLUDE_DIRS ${Boost_INCLUDE_DIRS} PARENT_SCOPE)
set(Boost_VERSION ${Boost_VERSION} PARENT_SCOPE)
return()
else ()
message(WARNING "Found an old Boost version '${Boost_VERSION}'. Required version is '${min_version}'.")
endif ()
endif ()

# Not found
set(Boost_FOUND FALSE PARENT_SCOPE)
if (required)
message(FATAL_ERROR "Could not find Boost headers")
endif ()

endfunction()
21 changes: 0 additions & 21 deletions cmake/get_macos_version.cmake

This file was deleted.

0 comments on commit 9ab6778

Please sign in to comment.