From 9ab6778e585082d131424f009c012274cc28ffe6 Mon Sep 17 00:00:00 2001 From: Keita Iwabuchi Date: Thu, 3 Oct 2024 20:07:32 -0700 Subject: [PATCH] Does not rely on CMake's FindBoost module. --- CMakeLists.txt | 16 +++++---- cmake/find_boost_headers.cmake | 60 ++++++++++++++++++++++++++++++++++ cmake/get_macos_version.cmake | 21 ------------ 3 files changed, 70 insertions(+), 27 deletions(-) create mode 100644 cmake/find_boost_headers.cmake delete mode 100644 cmake/get_macos_version.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f4b7c42..48e998a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 _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 () @@ -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 () diff --git a/cmake/find_boost_headers.cmake b/cmake/find_boost_headers.cmake new file mode 100644 index 00000000..eec5ed79 --- /dev/null +++ b/cmake/find_boost_headers.cmake @@ -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() \ No newline at end of file diff --git a/cmake/get_macos_version.cmake b/cmake/get_macos_version.cmake deleted file mode 100644 index 38053d7d..00000000 --- a/cmake/get_macos_version.cmake +++ /dev/null @@ -1,21 +0,0 @@ -function(get_macos_version) - if (NOT APPLE) - message(WARNING "The system is not macOS.") - set(MACOS_VERSION 0 PARENT_SCOPE) - return() - endif () - - execute_process(COMMAND sw_vers -productVersion - RESULT_VARIABLE RET - OUTPUT_VARIABLE STDOUT - ERROR_VARIABLE STDERR) - if (RET) # the command failed - message(WARNING "Cannot get macOS version.") - set(MACOS_VERSION 0 PARENT_SCOPE) - return() - endif () - - set(MACOS_VERSION ${STDOUT} PARENT_SCOPE) - message(VERBOSE "macOS version ${MACOS_VERSION}") - -endfunction() \ No newline at end of file