Skip to content

Commit bfdd849

Browse files
committed
ver 3.1 ADDBOOSTCMAKE_LINK_TYPE, fixed one if clause, more message(DEBUG...)
ver 3.1 ADDBOOSTCMAKE_LINK_TYPE ; fixed if clause ; if `CPM_LOCAL_PACKAGES_ONLY=1` emit more logs Added ADDBOOSTCMAKE_LINK_TYPE, which allows specifying link type that the `add_boost` will use to a target. By default it will use PUBLIC, unless your package is `INTERFACE`, in which case it will link with `INTERFACE` Fixed `if` clause: there was `endif` after `if(DEFINED BOOST_USE_MY_BOOST_DIRECTORY) ...` , not `elseif` More message(DEBUG ...) . If you configure your project with `--log-level=DEBUG` , it will generate more DEBUG info. Use my version of CPM until PR is accepted: now if used CPM_LOCAL_PACKAGES_ONLY, it will make CMake's `find_package` to emit error with description. Which is useful if correct boost version is found but failed to find a COMPONENT part. Previously CPM tried to just say "it failed. live with it` without logs.
1 parent 91e5190 commit bfdd849

File tree

2 files changed

+104
-66
lines changed

2 files changed

+104
-66
lines changed

CMakeLists.txt

+102-65
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ cmake_minimum_required(VERSION 3.14...3.22)
55
# Note: update this to your new project's name and version
66
project(
77
AddBoost.cmake
8-
VERSION 3.0
8+
VERSION 3.1
99
LANGUAGES CXX
1010
)
1111

1212
function(SUBDIRLIST result_var curdir)
1313
file(
1414
GLOB children
1515
RELATIVE ${curdir}
16-
${curdir}/*
17-
)
16+
${curdir}/*)
1817
set(result "")
1918
foreach(child ${children})
2019
if(IS_DIRECTORY ${curdir}/${child})
@@ -23,13 +22,18 @@ function(SUBDIRLIST result_var curdir)
2322
endforeach()
2423
set(${result_var}
2524
${result}
26-
PARENT_SCOPE
27-
)
25+
PARENT_SCOPE)
2826
endfunction()
2927

3028
macro(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
31-
BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
32-
)
29+
BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED)
30+
message(
31+
DEBUG
32+
"add_boost called with next arguments:
33+
\$\{\$\{TRY_BOOST_VERSION\}\} : ${${TRY_BOOST_VERSION}}
34+
\$\{\$\{BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED\}\} : ${${BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}
35+
\$\{\$\{BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED\}\} : ${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}
36+
")
3337
set(targets_to_link_to "${ARGN}")
3438
message(DEBUG "targets_to_link_to: \"${targets_to_link_to}\"")
3539
set(BOOST_INCLUDE_LIBRARIES
@@ -52,13 +56,11 @@ macro(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
5256
set(patches_for_boost "")
5357

5458
file(GLOB global_patches_for_boost CONFIGURE_DEPENDS
55-
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/*.patch"
56-
)
59+
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/*.patch")
5760
list(APPEND patches_for_boost ${global_patches_for_boost})
5861
if(DEFINED BOOST_ADD_MY_PATCHES)
5962
file(GLOB global_patches_for_boost CONFIGURE_DEPENDS
60-
"${BOOST_ADD_MY_PATCHES}/patches/boost/*.patch"
61-
)
63+
"${BOOST_ADD_MY_PATCHES}/patches/boost/*.patch")
6264
list(APPEND patches_for_boost ${global_patches_for_boost})
6365

6466
endif()
@@ -67,10 +69,11 @@ macro(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
6769
if(subdirs_boost_patches)
6870
foreach(subdir ${subdirs_boost_patches})
6971
if(${subdir} STREQUAL ${${TRY_BOOST_VERSION}})
70-
file(GLOB_RECURSE patches_with_max_applicable_version_up_to CONFIGURE_DEPENDS
71-
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/${subdir}/*.patch"
72-
)
73-
list(APPEND patches_for_boost ${patches_with_max_applicable_version_up_to})
72+
file(GLOB_RECURSE patches_with_max_applicable_version_up_to
73+
CONFIGURE_DEPENDS
74+
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/${subdir}/*.patch")
75+
list(APPEND patches_for_boost
76+
${patches_with_max_applicable_version_up_to})
7477
endif()
7578
endforeach()
7679
endif()
@@ -79,49 +82,80 @@ macro(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
7982
if(subdirs_boost_patches)
8083
foreach(subdir ${subdirs_boost_patches})
8184
if(${subdir} STREQUAL ${${TRY_BOOST_VERSION}})
82-
file(GLOB_RECURSE patches_with_max_applicable_version_up_to CONFIGURE_DEPENDS
83-
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/${subdir}/*.patch"
84-
)
85-
list(APPEND patches_for_boost ${patches_with_max_applicable_version_up_to})
85+
file(GLOB_RECURSE patches_with_max_applicable_version_up_to
86+
CONFIGURE_DEPENDS
87+
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/${subdir}/*.patch")
88+
list(APPEND patches_for_boost
89+
${patches_with_max_applicable_version_up_to})
8690
endif()
8791
endforeach()
8892
endif()
8993
endif()
9094

95+
string(REPLACE ";" "\;"
96+
ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
97+
"${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}")
98+
message(
99+
DEBUG
100+
"\$\{ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED\} : ${ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
101+
)
102+
91103
if(DEFINED BOOST_USE_MY_BOOST_DIRECTORY)
92-
CPMAddPackage(
93-
NAME Boost
94-
VERSION ${${TRY_BOOST_VERSION}}
95-
SOURCE_DIR ${BOOST_USE_MY_BOOST_DIRECTORY} PATCHES ${patches_for_boost}
96-
FIND_PACKAGE_ARGUMENTS "COMPONENTS ${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}"
97-
OPTIONS "BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}"
98-
)
99-
endif()
100-
if(patches_for_boost AND NOT boost_is_old) # is it 1.80.0 < x < 1.84.0
101-
CPMAddPackage(
102-
NAME Boost
103-
VERSION ${${TRY_BOOST_VERSION}}
104-
URL ${BOOST_URL} PATCHES ${patches_for_boost}
105-
FIND_PACKAGE_ARGUMENTS "COMPONENTS ${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}"
106-
OPTIONS "BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}"
107-
)
104+
cpmaddpackage(
105+
NAME
106+
Boost
107+
VERSION
108+
${${TRY_BOOST_VERSION}}
109+
SOURCE_DIR
110+
${BOOST_USE_MY_BOOST_DIRECTORY}
111+
PATCHES
112+
${patches_for_boost}
113+
FIND_PACKAGE_ARGUMENTS
114+
"COMPONENTS ${ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
115+
OPTIONS
116+
"BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}")
117+
elseif(patches_for_boost AND NOT boost_is_old) # is it 1.80.0 < x < 1.84.0
118+
cpmaddpackage(
119+
NAME
120+
Boost
121+
VERSION
122+
${${TRY_BOOST_VERSION}}
123+
URL
124+
${BOOST_URL}
125+
PATCHES
126+
${patches_for_boost}
127+
FIND_PACKAGE_ARGUMENTS
128+
"COMPONENTS ${ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
129+
OPTIONS
130+
"BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}")
108131
elseif(NOT boost_is_old) # is it 1.85.0+ ?
109-
CPMAddPackage(
110-
NAME Boost
111-
VERSION ${${TRY_BOOST_VERSION}}
112-
URL ${BOOST_URL}
113-
FIND_PACKAGE_ARGUMENTS "COMPONENTS ${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}"
114-
OPTIONS "BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}"
115-
)
132+
cpmaddpackage(
133+
NAME
134+
Boost
135+
VERSION
136+
${${TRY_BOOST_VERSION}}
137+
URL
138+
${BOOST_URL}
139+
FIND_PACKAGE_ARGUMENTS
140+
"COMPONENTS ${ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
141+
OPTIONS
142+
"BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}")
116143
else() # is it <1.80.0 ?
117-
CPMAddPackage(
118-
NAME Boost
119-
VERSION ${${TRY_BOOST_VERSION}}
120-
GIT_REPOSITORY "https://github.com/boostorg/boost"
121-
GIT_TAG "boost-${${TRY_BOOST_VERSION}}" PATCHES ${patches_for_boost}
122-
FIND_PACKAGE_ARGUMENTS "COMPONENTS ${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}"
123-
OPTIONS "BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}"
124-
)
144+
cpmaddpackage(
145+
NAME
146+
Boost
147+
VERSION
148+
${${TRY_BOOST_VERSION}}
149+
GIT_REPOSITORY
150+
"https://github.com/boostorg/boost"
151+
GIT_TAG
152+
"boost-${${TRY_BOOST_VERSION}}"
153+
PATCHES
154+
${patches_for_boost}
155+
FIND_PACKAGE_ARGUMENTS
156+
"COMPONENTS ${ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
157+
OPTIONS
158+
"BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}")
125159
endif()
126160

127161
# set(IS_BOOST_LOCAL OFF) endif()
@@ -131,16 +165,26 @@ macro(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
131165
# Link dependencies
132166
foreach(target IN LISTS targets_to_link_to)
133167
message(DEBUG "Boost_ADDED is defined : ${Boost_ADDED}")
168+
if(NOT DEFINED ADDBOOSTCMAKE_LINK_TYPE)
169+
get_target_property(type ${target} TYPE)
170+
if("${type}" STREQUAL "INTERFACE_LIBRARY")
171+
set(ADDBOOSTCMAKE_LINK_TYPE "INTERFACE")
172+
else()
173+
set(ADDBOOSTCMAKE_LINK_TYPE "PUBLIC")
174+
endif()
175+
endif()
134176
if(Boost_ADDED STREQUAL "")
135-
target_link_libraries(${target} PUBLIC Boost::boost)
177+
target_link_libraries(${target} ${ADDBOOSTCMAKE_LINK_TYPE} Boost::boost)
136178
else()
137179
foreach(a_lib ${${BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}})
138-
target_link_libraries(${target} PUBLIC Boost::${a_lib})
180+
target_link_libraries(${target} ${ADDBOOSTCMAKE_LINK_TYPE}
181+
Boost::${a_lib})
139182
endforeach()
140183
endif()
141184

142185
foreach(a_lib ${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}})
143-
target_link_libraries(${target} PUBLIC Boost::${a_lib})
186+
target_link_libraries(${target} ${ADDBOOSTCMAKE_LINK_TYPE}
187+
Boost::${a_lib})
144188
endforeach()
145189
endforeach()
146190
if(Boost_ADDED STREQUAL "")
@@ -154,23 +198,16 @@ macro(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
154198
)
155199
foreach(dep ${BOOST_ALL_DEPENDENCIES})
156200
string(APPEND ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS
157-
"boost_${dep} ${${TRY_BOOST_VERSION}};"
158-
)
201+
"boost_${dep} ${${TRY_BOOST_VERSION}};")
159202
endforeach()
160203
string(LENGTH "${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS}"
161-
ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH
162-
)
204+
ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH)
163205
math(EXPR ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH
164-
"${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH}-1" OUTPUT_FORMAT DECIMAL
165-
)
206+
"${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH}-1"
207+
OUTPUT_FORMAT DECIMAL)
166208
string(SUBSTRING "${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS}" 0
167209
${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH}
168-
ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS
169-
)
170-
message(
171-
DEBUG
172-
"ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS ${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH}: ${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS}"
173-
)
210+
ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS)
174211
endif()
175212

176213
message(

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Versions tested: from 1.79.0 upto 1.85.0 .
1616
```cmake
1717
CPMAddPackage(
1818
NAME AddBoost.CMake
19-
GIT_TAG 3.0
19+
GIT_TAG 3.1
2020
GITHUB_REPOSITORY Arniiiii/AddBoost.cmake
2121
)
2222
```
@@ -76,6 +76,7 @@ packageProject(
7676
- [x] If you have your own Boost directory, set `BOOST_USE_MY_BOOST_DIRECTORY` to be the path with your Boost.
7777
- [x] If you want, you can link Boost libs yourself, since the code is macro, not a function.
7878
- [x] You can link Boost libs automagically to multiple targets just by adding them to the end of the `addboost(...)` macro.
79+
- [x] You can use `ADDBOOSTCMAKE_LINK_TYPE` to override default behaviour of linking: if target is INTERFACE, use INTERFACE, if else: PUBLIC
7980
- [x] You can apply your patches to Boost. Define variable `BOOST_ADD_MY_PATCHES` to be a path to folder in which there's `*.patch` in such layout:
8081
```
8182
patches/

0 commit comments

Comments
 (0)