Skip to content

Commit 7fc2bb6

Browse files
committed
if Boost is missing will use https://github.com/Orphis/boost-cmake to build it. WARNING: results in unusable BTAS in install tree, to be fixed by Orphis/boost-cmake#45
1 parent 049804d commit 7fc2bb6

File tree

2 files changed

+72
-26
lines changed

2 files changed

+72
-26
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
if (NOT TARGET Boost::boost)
2+
include (FetchContent)
3+
4+
FetchContent_Declare(
5+
CMAKEBOOST
6+
GIT_REPOSITORY https://github.com/Orphis/boost-cmake
7+
)
8+
FetchContent_MakeAvailable(CMAKEBOOST)
9+
FetchContent_GetProperties(CMAKEBOOST
10+
SOURCE_DIR CMAKEBOOST_SOURCE_DIR
11+
BINARY_DIR CMAKEBOOST_BINARY_DIR
12+
)
13+
14+
# current boost-cmake/master does not install boost correctly, so warn that installed BTAS will not be usable
15+
# boost-cmake/install_rules https://github.com/Orphis/boost-cmake/pull/45 is supposed to fix it but is inactive
16+
message(WARNING "Building Boost from source makes BTAS unusable from the install location! Install Boost using package manage or manually and reconfigure/reinstall BTAS to fix this")
17+
install(TARGETS Boost_serialization EXPORT btas COMPONENT boost-libs)
18+
export(EXPORT btas
19+
FILE "${PROJECT_BINARY_DIR}/boost-targets.cmake")
20+
install(EXPORT btas
21+
FILE "boost-targets.cmake"
22+
DESTINATION "${BTAS_INSTALL_CMAKEDIR}"
23+
COMPONENT boost-libs)
24+
25+
endif(NOT TARGET Boost::boost)
26+
27+
# postcond check
28+
if (NOT TARGET Boost::boost)
29+
message(FATAL_ERROR "FindOrFetchBOOST could not make Boost::boost target available")
30+
endif(NOT TARGET Boost::boost)

external/boost.cmake

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,40 @@ if (NOT TARGET Boost::boost OR NOT TARGET Boost::serialization)
1313
cmake_minimum_required(VERSION 3.11.0)
1414
find_package(Boost CONFIG OPTIONAL_COMPONENTS ${Boost_BTAS_DEPS_LIBRARIES})
1515
if (NOT TARGET Boost::boost)
16-
find_package(Boost REQUIRED OPTIONAL_COMPONENTS ${Boost_BTAS_DEPS_LIBRARIES})
17-
set(Boost_USE_CONFIG FALSE)
16+
find_package(Boost OPTIONAL_COMPONENTS ${Boost_BTAS_DEPS_LIBRARIES})
17+
if (TARGET Boost::boost)
18+
set(Boost_USE_CONFIG FALSE)
19+
endif(TARGET Boost::boost)
1820
else()
1921
set(Boost_USE_CONFIG TRUE)
2022
endif()
2123
endif (NOT TARGET Boost::boost OR NOT TARGET Boost::serialization)
2224

23-
# Perform a compile check with Boost
24-
list(APPEND CMAKE_REQUIRED_INCLUDES ${Boost_INCLUDE_DIRS})
25-
list(APPEND CMAKE_REQUIRED_LIBRARIES ${Boost_LIBRARIES})
26-
target_compile_definitions(BTAS INTERFACE -DBTAS_HAS_BOOST_ITERATOR=1)
27-
if (Boost_CONTAINER_FOUND)
28-
target_compile_definitions(BTAS INTERFACE -DBTAS_HAS_BOOST_CONTAINER=1 -DBTAS_TARGET_MAX_INDEX_RANK=${TARGET_MAX_INDEX_RANK})
29-
endif()
25+
# if Boost not found, and BTAS_BUILD_DEPS_FROM_SOURCE=ON, use FetchContent to build it
26+
if (NOT TARGET Boost::boost)
27+
if (BTAS_BUILD_DEPS_FROM_SOURCE)
28+
include(FindOrFetchBoost)
29+
set(BTAS_BUILT_BOOST_FROM_SOURCE 1)
30+
else(BTAS_BUILD_DEPS_FROM_SOURCE)
31+
message(FATAL_ERROR "Boost is a required prerequisite of BTAS, but not found; install Boost or set BTAS_BUILD_DEPS_FROM_SOURCE=ON to obtain from source")
32+
endif(BTAS_BUILD_DEPS_FROM_SOURCE)
33+
endif (NOT TARGET Boost::boost)
34+
35+
# make BTAS depend on Boost
36+
set(Boost_LIBRARIES Boost::boost)
37+
if (TARGET Boost::serialization)
38+
list(APPEND Boost_LIBRARIES Boost::serialization)
39+
endif (TARGET Boost::serialization)
40+
target_link_libraries(BTAS INTERFACE ${Boost_LIBRARIES})
3041

31-
set(_btas_boostcheck_source "
42+
# If building unit tests, perform a compile check with Boost
43+
# this is only possible, though, if did not build Boost from source,
44+
# since only imported targets can be used in CMAKE_REQUIRED_LIBRARIES
45+
if (BTAS_BUILD_UNITTEST AND NOT BTAS_BUILT_BOOST_FROM_SOURCE)
46+
list(APPEND CMAKE_REQUIRED_LIBRARIES ${Boost_LIBRARIES})
47+
target_compile_definitions(BTAS INTERFACE -DBTAS_HAS_BOOST_ITERATOR=1 -DBTAS_HAS_BOOST_CONTAINER=1 -DBTAS_TARGET_MAX_INDEX_RANK=${TARGET_MAX_INDEX_RANK})
48+
49+
set(_btas_boostcheck_source "
3250
#define BOOST_TEST_MAIN main_tester
3351
#include <boost/test/included/unit_test.hpp>
3452
@@ -90,21 +108,19 @@ set(_btas_boostcheck_source "
90108
}
91109
#endif // BTAS_HAS_BOOST_CONTAINER
92110
")
93-
if (CMAKE_CROSSCOMPILING)
94-
include(CheckCXXSourceCompiles)
95-
check_cxx_source_compiles("${_btas_boostcheck_source}" BOOST_COMPILES_AND_RUNS)
96-
else(CMAKE_CROSSCOMPILING)
97-
include(CheckCXXSourceRuns)
98-
check_cxx_source_runs("${_btas_boostcheck_source}" BOOST_COMPILES_AND_RUNS)
99-
endif(CMAKE_CROSSCOMPILING)
111+
if (CMAKE_CROSSCOMPILING)
112+
include(CheckCXXSourceCompiles)
113+
check_cxx_source_compiles("${_btas_boostcheck_source}" BOOST_COMPILES_AND_RUNS)
114+
else(CMAKE_CROSSCOMPILING)
115+
include(CheckCXXSourceRuns)
116+
check_cxx_source_runs("${_btas_boostcheck_source}" BOOST_COMPILES_AND_RUNS)
117+
endif(CMAKE_CROSSCOMPILING)
100118

101-
if (BOOST_COMPILES_AND_RUNS)
119+
if (BOOST_COMPILES_AND_RUNS)
102120
target_compile_definitions(BTAS INTERFACE -DBTAS_HAS_BOOST_SERIALIZATION=1)
103-
else ()
104-
message(STATUS "Boost found at ${BOOST_ROOT}, but could not compile and/or run test program")
105-
message(WARNING "To obtain usable Boost, use your system package manager (HomeBrew, apt, etc.) OR download at www.boost.org and compile (unpacking alone is not enough)")
106-
message(WARNING "** !! due to missing Boost.Serialization the corresponding unit tests will be disabled !!")
107-
endif(BOOST_COMPILES_AND_RUNS)
108-
109-
target_include_directories(BTAS INTERFACE ${Boost_INCLUDE_DIRS})
110-
target_link_libraries(BTAS INTERFACE ${Boost_LIBRARIES})
121+
else ()
122+
message(STATUS "Boost found at ${BOOST_ROOT}, but could not compile and/or run test program")
123+
message(WARNING "To obtain usable Boost, use your system package manager (HomeBrew, apt, etc.) OR download at www.boost.org and compile (unpacking alone is not enough)")
124+
message(WARNING "** !! due to missing Boost.Serialization the corresponding unit tests will be disabled !!")
125+
endif(BOOST_COMPILES_AND_RUNS)
126+
endif(BTAS_BUILD_UNITTEST AND NOT BTAS_BUILT_BOOST_FROM_SOURCE)

0 commit comments

Comments
 (0)