Created
November 2, 2023 08:26
-
-
Save BWbwchen/3b4414c7869e12da57bdacd753875773 to your computer and use it in GitHub Desktop.
Auto Download Boost with official FindBoost (assume that builded boost will be placed in ${CMAKE_SOURCE_DIR}/external/boost)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# - Find Boost | |
# | |
# Based on https://gist.github.com/thiagowfx/970e3931387ed7db9a39709a8a130ee9 Copyright (c) 2016 Thiago Barroso Perrotta | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of | |
# this software and associated documentation files (the "Software"), to deal in | |
# the Software without restriction, including without limitation the rights to | |
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
# the Software, and to permit persons to whom the Software is furnished to do so, | |
# subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
if(NOT BOOST_BUILD_COMPONENTS) | |
message(FATAL_ERROR "No COMPONENTS specified for Boost") | |
endif() | |
# Create a list(string) for the build command (e.g. --with-program_options;--with-system) | |
# and assigns it to BOOST_COMPONENTS_FOR_BUILD | |
foreach(component ${BOOST_BUILD_COMPONENTS}) | |
list(APPEND BOOST_COMPONENTS_FOR_BUILD --with-${component}) | |
endforeach() | |
macro(DO_FIND_BOOST_DOWNLOAD) | |
if(NOT BOOST_REQUESTED_VERSION) | |
message(FATAL_ERROR "BOOST_REQUESTED_VERSION is not defined.") | |
endif() | |
string(REPLACE "." "_" BOOST_REQUESTED_VERSION_UNDERSCORE ${BOOST_REQUESTED_VERSION}) | |
set(BOOST_MAYBE_STATIC) | |
if(BOOST_USE_STATIC_LIBS) | |
set(BOOST_MAYBE_STATIC "link=static") | |
endif() | |
include(ExternalProject) | |
if(WIN32) | |
ExternalProject_Add( | |
Boost | |
URL https://boostorg.jfrog.io/artifactory/main/release/${BOOST_REQUESTED_VERSION}/source/boost_${BOOST_REQUESTED_VERSION_UNDERSCORE}.7z | |
# URL https://downloads.sourceforge.net/project/boost/boost/${BOOST_REQUESTED_VERSION}/boost_${BOOST_REQUESTED_VERSION_UNDERSCORE}.zip | |
UPDATE_COMMAND "" | |
CONFIGURE_COMMAND ./bootstrap.bat --prefix=${BOOST_ROOT_DIR} | |
BUILD_COMMAND ./b2 ${BOOST_MAYBE_STATIC} --prefix=${BOOST_ROOT_DIR} ${BOOST_COMPONENTS_FOR_BUILD} install | |
BUILD_IN_SOURCE true | |
INSTALL_COMMAND "" | |
INSTALL_DIR ${BOOST_ROOT_DIR} | |
) | |
else() | |
ExternalProject_Add( | |
Boost | |
URL https://boostorg.jfrog.io/artifactory/main/release/${BOOST_REQUESTED_VERSION}/source/boost_${BOOST_REQUESTED_VERSION_UNDERSCORE}.tar.gz | |
# URL https://downloads.sourceforge.net/project/boost/boost/${BOOST_REQUESTED_VERSION}/boost_${BOOST_REQUESTED_VERSION_UNDERSCORE}.zip | |
UPDATE_COMMAND "" | |
CONFIGURE_COMMAND ./bootstrap.sh --prefix=${BOOST_ROOT_DIR} | |
BUILD_COMMAND ./b2 ${BOOST_MAYBE_STATIC} --prefix=${BOOST_ROOT_DIR} ${BOOST_COMPONENTS_FOR_BUILD} install | |
BUILD_IN_SOURCE true | |
INSTALL_COMMAND "" | |
INSTALL_DIR ${BOOST_ROOT_DIR} | |
) | |
endif() | |
ExternalProject_Get_Property(Boost install_dir) | |
endmacro() | |
DO_FIND_BOOST_DOWNLOAD() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.1...3.27 FATAL_ERROR) | |
project(code | |
VERSION 1.0 | |
LANGUAGES CXX | |
) | |
set(CMAKE_CXX_STANDARD 17) | |
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}) | |
set(BOOST_ROOT_DIR ${CMAKE_SOURCE_DIR}/external/boost) | |
set(BOOST_BUILD_COMPONENTS system program_options) | |
set(Boost_hardcoded_INCLUDE_DIRS ${BOOST_ROOT_DIR}/include) | |
foreach(component ${BOOST_BUILD_COMPONENTS}) | |
list(APPEND Boost_hardcoded_libraries ${BOOST_ROOT_DIR}/lib/libboost_${component}.so) | |
endforeach() | |
set(BOOST_ROOT ${BOOST_ROOT_DIR}) | |
find_package(Boost COMPONENTS ${BOOST_BUILD_COMPONENTS}) | |
message(STATUS ${Boost_hardcoded_libraries}) | |
message(STATUS ${Boost_INCLUDE_DIRS}) | |
message(STATUS ${Boost_FOUND}) | |
add_executable(${PROJECT_NAME} main.cpp) | |
if(NOT Boost_FOUND) | |
set(BOOST_REQUESTED_VERSION 1.80.0) | |
message(WARNING "Boost library NOT found, download boost-${BOOST_REQUESTED_VERSION} and build from boost website.") | |
include(BoostDownloader.cmake) | |
add_custom_target(ExternalProject_Boost | |
DEPENDS Boost | |
) | |
add_dependencies(${PROJECT_NAME} ExternalProject_Boost) | |
endif() | |
target_include_directories(${PROJECT_NAME} | |
PUBLIC ${Boost_hardcoded_INCLUDE_DIRS} | |
) | |
target_link_libraries(${PROJECT_NAME} PUBLIC ${Boost_hardcoded_libraries}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment