|
| 1 | +# Error if building out of a build directory |
| 2 | +file(TO_CMAKE_PATH "${PROJECT_BINARY_DIR}/CMakeLists.txt" LOC_PATH) |
| 3 | +if(EXISTS "${LOC_PATH}") |
| 4 | + message(FATAL_ERROR "You cannot build in a source directory (or any directory with " |
| 5 | + "CMakeLists.txt file). Please make a build subdirectory. Feel free to " |
| 6 | + "remove CMakeCache.txt and CMakeFiles.") |
| 7 | +endif() |
| 8 | + |
| 9 | +set(BOOST_VERSION "1.83.0") |
| 10 | +set(BOOST_VERSION_UNDERSCORE "1_83_0") |
| 11 | +set(BOOST_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/boost_${BOOST_VERSION_UNDERSCORE}") |
| 12 | +# b2 stage (default) puts libs in <source>/stage/lib or stage/lib/<variant>/, headers stay in <source> |
| 13 | +set(BOOST_STAGE_DIR "${BOOST_SOURCE_DIR}/stage") |
| 14 | +set(BOOST_STAGE_LIB_DIR "${BOOST_SOURCE_DIR}/stage/lib") |
| 15 | +set(ENV{BOOSTROOT} ${BOOST_SOURCE_DIR}) |
| 16 | + |
| 17 | +# If we already built Boost here (stage), use it directly and skip find_package/build |
| 18 | +if(EXISTS "${BOOST_SOURCE_DIR}/boost/version.hpp") |
| 19 | + if(EXISTS "${BOOST_STAGE_DIR}") |
| 20 | + set(Boost_ROOT ${BOOST_SOURCE_DIR}) |
| 21 | + endif() |
| 22 | +endif() |
| 23 | + |
| 24 | +find_package(Boost 1.83 QUIET) |
| 25 | + |
| 26 | +if(NOT Boost_FOUND) |
| 27 | + message(STATUS "Boost not found. Downloading and building Boost...") |
| 28 | + |
| 29 | + set(BOOST_ARCHIVE_URL "https://archives.boost.io/release/${BOOST_VERSION}/source/boost_${BOOST_VERSION_UNDERSCORE}.tar.gz") |
| 30 | + set(BOOST_ARCHIVE_FILE "${CMAKE_CURRENT_BINARY_DIR}/boost_${BOOST_VERSION_UNDERSCORE}.tar.gz") |
| 31 | + |
| 32 | + message(STATUS "Downloading Boost from archives.boost.io...") |
| 33 | + file(DOWNLOAD ${BOOST_ARCHIVE_URL} ${BOOST_ARCHIVE_FILE} |
| 34 | + SHOW_PROGRESS) |
| 35 | + |
| 36 | + message(STATUS "Unpacking the archive...") |
| 37 | + execute_process(COMMAND ${CMAKE_COMMAND} -E tar xvf ${BOOST_ARCHIVE_FILE} |
| 38 | + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
| 39 | + OUTPUT_QUIET |
| 40 | + ) |
| 41 | + |
| 42 | + message(STATUS "Running bootstrap...") |
| 43 | + if(WIN32) |
| 44 | + execute_process(COMMAND "${BOOST_SOURCE_DIR}/bootstrap.bat" gcc |
| 45 | + WORKING_DIRECTORY ${BOOST_SOURCE_DIR}) |
| 46 | + set(B2_EXECUTABLE "${BOOST_SOURCE_DIR}/b2.exe") |
| 47 | + else() |
| 48 | + execute_process(COMMAND "${BOOST_SOURCE_DIR}/bootstrap.sh" --with-toolset=gcc |
| 49 | + WORKING_DIRECTORY ${BOOST_SOURCE_DIR}) |
| 50 | + set(B2_EXECUTABLE "${BOOST_SOURCE_DIR}/b2") |
| 51 | + endif() |
| 52 | + |
| 53 | + message(STATUS "Building Boost with b2 stage (this may take a long time)...") |
| 54 | + execute_process( |
| 55 | + COMMAND ${B2_EXECUTABLE} stage |
| 56 | + toolset=gcc |
| 57 | + address-model=64 |
| 58 | + link=static |
| 59 | + runtime-link=static |
| 60 | + threading=multi |
| 61 | + --layout=versioned |
| 62 | + --build-type=complete |
| 63 | + WORKING_DIRECTORY ${BOOST_SOURCE_DIR} |
| 64 | + ) |
| 65 | + |
| 66 | + set(Boost_ROOT "${BOOST_SOURCE_DIR}") |
| 67 | + find_package(Boost 1.83 QUIET) |
| 68 | +else() |
| 69 | + message(STATUS "Boost found in the system.") |
| 70 | +endif() |
| 71 | + |
| 72 | +# When using our staged Boost, FindBoost does not set Boost_LIBRARIES; collect built libs from stage. |
| 73 | +# b2 --build-type=complete produces both release (-mt-s-) and debug (-mt-sd-) variants; link only one |
| 74 | +# to avoid multiple definition errors. |
| 75 | +if(Boost_FOUND AND EXISTS "${BOOST_STAGE_LIB_DIR}") |
| 76 | + file(GLOB_RECURSE _stage_libs "${BOOST_STAGE_LIB_DIR}/*.a" "${BOOST_STAGE_LIB_DIR}/*.lib") |
| 77 | + if(_stage_libs) |
| 78 | + if(CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 79 | + list(FILTER _stage_libs INCLUDE REGEX "[-]mt[-]sd[-]|[-]sgd[-]") |
| 80 | + else() |
| 81 | + list(FILTER _stage_libs EXCLUDE REGEX "[-]mt[-]sd[-]|[-]sgd[-]") |
| 82 | + endif() |
| 83 | + set(Boost_LIBRARIES ${_stage_libs}) |
| 84 | + endif() |
| 85 | +elseif(Boost_FOUND) |
| 86 | + set(Boost_LIBRARIES boost_filesystem boost_system boost_chrono boost_date_time boost_regex boost_program_options boost_serialization boost_thread boost_wave boost_iostreams boost_locale boost_unit_test_framework boost_wave boost_iostreams boost_locale boost_unit_test_framework) |
| 87 | + if(LINUX) |
| 88 | + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/lib") |
| 89 | + endif() |
| 90 | +endif() |
| 91 | +# Do not pass NOTFOUND to the linker |
| 92 | +if(Boost_LIBRARIES) |
| 93 | + list(FILTER Boost_LIBRARIES EXCLUDE REGEX "NOTFOUND$") |
| 94 | +endif() |
| 95 | + |
| 96 | +add_library(boost INTERFACE) |
| 97 | +target_include_directories(boost INTERFACE ${Boost_INCLUDE_DIRS}) |
| 98 | +if(Boost_LIBRARIES) |
| 99 | + target_link_libraries(boost INTERFACE ${Boost_LIBRARIES}) |
| 100 | +endif() |
0 commit comments