2021-01-28 10:13:02 +08:00
|
|
|
function(compile_boost)
|
|
|
|
set(options)
|
|
|
|
set(oneValueArgs TARGET)
|
|
|
|
set(multiValueArgs BUILD_ARGS CXXFLAGS LDFLAGS)
|
|
|
|
cmake_parse_arguments(MY "${options}" "${oneValueArgs}"
|
|
|
|
"${multiValueArgs}" ${ARGN} )
|
2021-01-27 08:06:08 +08:00
|
|
|
# Configure the boost toolset to use
|
|
|
|
set(BOOTSTRAP_COMMAND "./bootstrap.sh --with-libraries=context")
|
2021-02-02 23:58:42 +08:00
|
|
|
set(B2_COMMAND "./b2")
|
2021-01-27 08:06:08 +08:00
|
|
|
set(BOOST_COMPILER_FLAGS -fvisibility=hidden -fPIC -std=c++14 -w)
|
2021-01-28 10:13:02 +08:00
|
|
|
set(BOOST_CXX_COMPILER "${CMAKE_CXX_COMPILER}")
|
2021-01-27 08:06:08 +08:00
|
|
|
if(APPLE)
|
|
|
|
set(BOOST_TOOLSET "darwin")
|
2021-02-02 23:58:42 +08:00
|
|
|
# this is to fix a weird macOS issue -- by default
|
|
|
|
# cmake would otherwise pass a compiler that can't
|
|
|
|
# compile boost
|
2021-01-28 10:13:02 +08:00
|
|
|
set(BOOST_CXX_COMPILER "/usr/bin/clang++")
|
2021-01-27 08:06:08 +08:00
|
|
|
elseif(CLANG)
|
|
|
|
set(BOOST_TOOLSET "clang")
|
|
|
|
set(BOOTSTRAP_COMMAND "${BOOTSTRAP_COMMAND} --with-toolset=clang")
|
|
|
|
else()
|
|
|
|
set(BOOST_TOOLSET "gcc")
|
|
|
|
endif()
|
|
|
|
if(APPLE OR USE_LIBCXX)
|
|
|
|
list(APPEND BOOST_COMPILER_FLAGS -stdlib=libc++)
|
|
|
|
endif()
|
|
|
|
set(BOOST_ADDITIONAL_COMPILE_OPTIOINS "")
|
2021-01-28 10:13:02 +08:00
|
|
|
foreach(flag IN LISTS BOOST_COMPILER_FLAGS MY_CXXFLAGS)
|
2021-01-27 08:06:08 +08:00
|
|
|
string(APPEND BOOST_ADDITIONAL_COMPILE_OPTIOINS "<cxxflags>${flag} ")
|
|
|
|
endforeach()
|
2021-01-28 10:13:02 +08:00
|
|
|
foreach(flag IN LISTS MY_LDFLAGS)
|
|
|
|
string(APPEND BOOST_ADDITIONAL_COMPILE_OPTIOINS "<linkflags>${flag} ")
|
|
|
|
endforeach()
|
2021-01-27 08:06:08 +08:00
|
|
|
configure_file(${CMAKE_SOURCE_DIR}/cmake/user-config.jam.cmake ${CMAKE_BINARY_DIR}/user-config.jam)
|
|
|
|
|
2021-01-28 01:44:19 +08:00
|
|
|
set(USER_CONFIG_FLAG --user-config=${CMAKE_BINARY_DIR}/user-config.jam)
|
2021-01-28 07:37:04 +08:00
|
|
|
|
2018-05-06 02:11:17 +08:00
|
|
|
include(ExternalProject)
|
2021-01-27 08:06:08 +08:00
|
|
|
set(BOOST_INSTALL_DIR "${CMAKE_BINARY_DIR}/boost_install")
|
2021-01-28 10:13:02 +08:00
|
|
|
ExternalProject_add("${MY_TARGET}Project"
|
2020-02-15 10:10:13 +08:00
|
|
|
URL "https://dl.bintray.com/boostorg/release/1.72.0/source/boost_1_72_0.tar.bz2"
|
2020-04-01 07:52:55 +08:00
|
|
|
URL_HASH SHA256=59c9b274bc451cf91a9ba1dd2c7fdcaf5d60b1b3aa83f2c9fa143417cc660722
|
2021-01-28 07:37:04 +08:00
|
|
|
CONFIGURE_COMMAND ${BOOTSTRAP_COMMAND} --with-libraries=context
|
2021-01-28 10:13:02 +08:00
|
|
|
BUILD_COMMAND ${B2_COMMAND} link=static ${MY_BUILD_ARGS} --prefix=${BOOST_INSTALL_DIR} ${USER_CONFIG_FLAG} install
|
2018-05-06 02:11:17 +08:00
|
|
|
BUILD_IN_SOURCE ON
|
|
|
|
INSTALL_COMMAND ""
|
|
|
|
UPDATE_COMMAND ""
|
2021-01-27 08:06:08 +08:00
|
|
|
BUILD_BYPRODUCTS "${BOOST_INSTALL_DIR}/boost/config.hpp"
|
|
|
|
"${BOOST_INSTALL_DIR}/lib/libboost_context.a")
|
2018-05-03 00:55:29 +08:00
|
|
|
|
2021-01-28 10:13:02 +08:00
|
|
|
add_library(${MY_TARGET}_context STATIC IMPORTED)
|
|
|
|
add_dependencies(${MY_TARGET}_context ${MY_TARGET}Project)
|
|
|
|
set_target_properties(${MY_TARGET}_context PROPERTIES IMPORTED_LOCATION "${BOOST_INSTALL_DIR}/lib/libboost_context.a")
|
|
|
|
|
|
|
|
add_library(${MY_TARGET} INTERFACE)
|
|
|
|
target_include_directories(${MY_TARGET} SYSTEM INTERFACE ${BOOST_INSTALL_DIR}/include)
|
|
|
|
target_link_libraries(${MY_TARGET} INTERFACE ${MY_TARGET}_context)
|
|
|
|
endfunction()
|
2018-05-03 00:55:29 +08:00
|
|
|
|
2021-01-28 10:13:02 +08:00
|
|
|
if(USE_SANITIZER)
|
|
|
|
if(WIN32)
|
|
|
|
message(FATAL_ERROR "Sanitizers are not supported on Windows")
|
|
|
|
endif()
|
|
|
|
message(STATUS "A sanitizer is enabled, need to build boost from source")
|
|
|
|
compile_boost(TARGET boost_asan BUILD_ARGS context-impl=ucontext
|
|
|
|
CXXFLAGS ${SANITIZER_COMPILE_OPTIONS} LDFLAGS ${SANITIZER_LINK_OPTIONS})
|
|
|
|
return()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
list(APPEND CMAKE_PREFIX_PATH /opt/boost_1_72_0)
|
|
|
|
# since boost 1.72 boost installs cmake configs. We will enforce config mode
|
|
|
|
set(Boost_USE_STATIC_LIBS ON)
|
2021-01-29 00:25:26 +08:00
|
|
|
set(BOOST_HINT_PATHS /opt/boost_1_72_0)
|
|
|
|
if(BOOST_ROOT)
|
|
|
|
list(APPEND BOOST_HINT_PATHS ${BOOST_ROOT})
|
|
|
|
endif()
|
2021-02-02 07:26:49 +08:00
|
|
|
|
|
|
|
if(WIN32)
|
|
|
|
set(BOOST_REQUIRED REQUIRED)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
find_package(Boost 1.72.0 EXACT QUIET ${BOOST_REQUIRED} COMPONENTS context CONFIG PATHS ${BOOST_HINT_PATHS})
|
2021-01-28 10:13:02 +08:00
|
|
|
set(FORCE_BOOST_BUILD OFF CACHE BOOL "Forces cmake to build boost and ignores any installed boost")
|
|
|
|
|
|
|
|
if(Boost_FOUND AND NOT FORCE_BOOST_BUILD)
|
2018-05-06 02:11:17 +08:00
|
|
|
add_library(boost_target INTERFACE)
|
2021-01-28 10:13:02 +08:00
|
|
|
target_link_libraries(boost_target INTERFACE Boost::boost Boost::context)
|
2021-02-02 07:26:49 +08:00
|
|
|
elseif(WIN32)
|
|
|
|
message(FATAL_ERROR "Could not find Boost")
|
2021-01-28 10:13:02 +08:00
|
|
|
else()
|
|
|
|
if(FORCE_BOOST_BUILD)
|
|
|
|
message(STATUS "Compile boost because FORCE_BOOST_BUILD is set")
|
|
|
|
else()
|
|
|
|
message(STATUS "Didn't find Boost -- will compile from source")
|
|
|
|
endif()
|
|
|
|
compile_boost(TARGET boost_target)
|
2018-05-06 02:11:17 +08:00
|
|
|
endif()
|