Skip to content

Commit 0f42b0f

Browse files
committed
ver 2.0: full refactor
changes: made it macro, not function, because CPM set some variables as PARENT_SCOPE, though they were not going upper than the function. Now the logic is more in CPM, not in the project.
1 parent 22e454e commit 0f42b0f

File tree

2 files changed

+82
-119
lines changed

2 files changed

+82
-119
lines changed

CMakeLists.txt

+80-117
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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 1.1
8+
VERSION 2.0
99
LANGUAGES CXX
1010
)
1111

@@ -27,64 +27,53 @@ function(SUBDIRLIST result_var curdir)
2727
)
2828
endfunction()
2929

30-
function(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
31-
BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED target_to_link_to
32-
return_var_name_boost_install_targets
30+
macro(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
31+
BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED target_to_link_to
3332
)
34-
set(IS_BOOST_LOCAL OFF)
35-
if(${CPM_LOCAL_PACKAGES_ONLY})
36-
message(STATUS "Trying to find Boost with version at least ${TRY_BOOST_VERSION}")
37-
find_package(
38-
Boost ${TRY_BOOST_VERSION} REQUIRED
39-
COMPONENTS ${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}
40-
)
41-
set(IS_BOOST_LOCAL ON)
42-
elseif(${CPM_USE_LOCAL_PACKAGES} OR NOT ${CPM_DOWNLOAD_ALL})
43-
message(STATUS "Trying to use local Boost with version at least ${TRY_BOOST_VERSION}")
44-
find_package(
45-
Boost ${TRY_BOOST_VERSION} COMPONENTS ${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}
46-
)
47-
if(${BOOST_FOUND})
48-
set(IS_BOOST_LOCAL ON)
49-
message(DEBUG "boost include dir: ${Boost_INCLUDE_DIRS}")
50-
endif()
51-
endif()
52-
53-
if(NOT (${BOOST_FOUND}) OR (NOT DEFINED BOOST_FOUND))
54-
message(STATUS "Trying to download Boost ${TRY_BOOST_VERSION}")
55-
56-
set(BOOST_INCLUDE_LIBRARIES
57-
"${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED};${BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
58-
)
33+
set(BOOST_INCLUDE_LIBRARIES
34+
"${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED};${BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
35+
)
5936

37+
set(BOOST_URL
38+
"https://github.com/boostorg/boost/releases/download/boost-${TRY_BOOST_VERSION}/boost-${TRY_BOOST_VERSION}-cmake.tar.xz"
39+
)
40+
set(boost_is_old FALSE)
41+
if(${TRY_BOOST_VERSION} STRLESS "1.85.0")
6042
set(BOOST_URL
61-
"https://github.com/boostorg/boost/releases/download/boost-${TRY_BOOST_VERSION}/boost-${TRY_BOOST_VERSION}-cmake.tar.xz"
43+
"https://github.com/boostorg/boost/releases/download/boohttps://annas-archive.org/search?index=&page=1&q=c%2B%2B+concurrency+in+action&sort=st-${TRY_BOOST_VERSION}/boost-${TRY_BOOST_VERSION}.tar.xz"
6244
)
63-
set(boost_is_old FALSE)
64-
if(${TRY_BOOST_VERSION} STRLESS "1.85.0")
65-
set(BOOST_URL
66-
"https://github.com/boostorg/boost/releases/download/boohttps://annas-archive.org/search?index=&page=1&q=c%2B%2B+concurrency+in+action&sort=st-${TRY_BOOST_VERSION}/boost-${TRY_BOOST_VERSION}.tar.xz"
67-
)
68-
endif()
69-
if(${TRY_BOOST_VERSION} STRLESS "1.81.0.beta1")
70-
set(boost_is_old TRUE)
71-
endif()
45+
endif()
46+
if(${TRY_BOOST_VERSION} STRLESS "1.81.0.beta1")
47+
set(boost_is_old TRUE)
48+
endif()
7249

73-
set(patches_for_boost "")
50+
set(patches_for_boost "")
7451

52+
file(GLOB global_patches_for_boost CONFIGURE_DEPENDS
53+
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/*.patch"
54+
)
55+
list(APPEND patches_for_boost ${global_patches_for_boost})
56+
if(DEFINED BOOST_ADD_MY_PATCHES)
7557
file(GLOB global_patches_for_boost CONFIGURE_DEPENDS
76-
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/*.patch"
58+
"${BOOST_ADD_MY_PATCHES}/patches/boost/*.patch"
7759
)
7860
list(APPEND patches_for_boost ${global_patches_for_boost})
79-
if(DEFINED BOOST_ADD_MY_PATCHES)
80-
file(GLOB global_patches_for_boost CONFIGURE_DEPENDS
81-
"${BOOST_ADD_MY_PATCHES}/patches/boost/*.patch"
82-
)
83-
list(APPEND patches_for_boost ${global_patches_for_boost})
8461

85-
endif()
62+
endif()
8663

87-
subdirlist(subdirs_boost_patches "${CMAKE_CURRENT_LIST_DIR}/patches/boost")
64+
subdirlist(subdirs_boost_patches "${CMAKE_CURRENT_LIST_DIR}/patches/boost")
65+
if(subdirs_boost_patches)
66+
foreach(subdir ${subdirs_boost_patches})
67+
if(${subdir} STREQUAL ${TRY_BOOST_VERSION})
68+
file(GLOB_RECURSE patches_with_max_applicable_version_up_to CONFIGURE_DEPENDS
69+
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/${subdir}/*.patch"
70+
)
71+
list(APPEND patches_for_boost ${patches_with_max_applicable_version_up_to})
72+
endif()
73+
endforeach()
74+
endif()
75+
if(DEFINED BOOST_ADD_MY_PATCHES)
76+
subdirlist(subdirs_boost_patches "${BOOST_ADD_MY_PATCHES}/patches/boost")
8877
if(subdirs_boost_patches)
8978
foreach(subdir ${subdirs_boost_patches})
9079
if(${subdir} STREQUAL ${TRY_BOOST_VERSION})
@@ -95,64 +84,42 @@ function(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
9584
endif()
9685
endforeach()
9786
endif()
98-
if(DEFINED BOOST_ADD_MY_PATCHES)
99-
subdirlist(subdirs_boost_patches "${BOOST_ADD_MY_PATCHES}/patches/boost")
100-
if(subdirs_boost_patches)
101-
foreach(subdir ${subdirs_boost_patches})
102-
if(${subdir} STREQUAL ${TRY_BOOST_VERSION})
103-
file(GLOB_RECURSE patches_with_max_applicable_version_up_to CONFIGURE_DEPENDS
104-
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/${subdir}/*.patch"
105-
)
106-
list(APPEND patches_for_boost ${patches_with_max_applicable_version_up_to})
107-
endif()
108-
endforeach()
109-
endif()
110-
endif()
111-
112-
if(patches_for_boost AND NOT boost_is_old)
113-
message(
114-
STATUS
115-
"Downloading boost release from github in form of a .tar.xz tarball... If there's nothing for too long, set at CMake's configuring -DFETCHCONTENT_QUIET=OFF to see download progress"
116-
)
117-
CPMAddPackage(
118-
NAME Boost
119-
VERSION ${TRY_BOOST_VERSION}
120-
URL ${BOOST_URL} PATCHES ${patches_for_boost}
121-
OPTIONS "BOOST_ENABLE_CMAKE ON" "BOOST_SKIP_INSTALL_RULES OFF"
122-
)
123-
elseif(NOT boost_is_old)
124-
message(
125-
STATUS
126-
"Downloading boost release from github in form of a .tar.xz tarball... If there's nothing for too long, set at CMake's configuring -DFETCHCONTENT_QUIET=OFF to see download progress"
127-
)
128-
CPMAddPackage(
129-
NAME Boost
130-
VERSION ${TRY_BOOST_VERSION}
131-
URL ${BOOST_URL}
132-
OPTIONS "BOOST_SKIP_INSTALL_RULES OFF"
133-
)
134-
else()
135-
message(
136-
STATUS
137-
"Since there's no release at github for such old version, cloning boost from github with GIT_TAG \"boost_${TRY_BOOST_VERSION}\"... If there's nothing for too long, set at CMake's configuring -DFETCHCONTENT_QUIET=OFF to see download progress"
138-
)
139-
CPMAddPackage(
140-
NAME Boost
141-
VERSION ${TRY_BOOST_VERSION}
142-
GIT_REPOSITORY "https://github.com/boostorg/boost"
143-
GIT_TAG "boost-${TRY_BOOST_VERSION}" PATCHES ${patches_for_boost}
144-
OPTIONS "BOOST_ENABLE_CMAKE ON" "BOOST_SKIP_INSTALL_RULES OFF"
145-
)
146-
147-
endif()
87+
endif()
14888

149-
set(IS_BOOST_LOCAL OFF)
89+
if(patches_for_boost AND NOT boost_is_old)
90+
CPMAddPackage(
91+
NAME Boost
92+
VERSION ${TRY_BOOST_VERSION}
93+
URL ${BOOST_URL} PATCHES ${patches_for_boost}
94+
FIND_PACKAGE_ARGUMENTS "COMPONENTS ${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
95+
OPTIONS "BOOST_ENABLE_CMAKE ON" "BOOST_SKIP_INSTALL_RULES OFF"
96+
)
97+
elseif(NOT boost_is_old)
98+
CPMAddPackage(
99+
NAME Boost
100+
VERSION ${TRY_BOOST_VERSION}
101+
URL ${BOOST_URL}
102+
FIND_PACKAGE_ARGUMENTS "COMPONENTS ${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
103+
OPTIONS "BOOST_SKIP_INSTALL_RULES OFF"
104+
)
105+
else()
106+
CPMAddPackage(
107+
NAME Boost
108+
VERSION ${TRY_BOOST_VERSION}
109+
GIT_REPOSITORY "https://github.com/boostorg/boost"
110+
GIT_TAG "boost-${TRY_BOOST_VERSION}" PATCHES ${patches_for_boost}
111+
FIND_PACKAGE_ARGUMENTS "COMPONENTS ${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
112+
OPTIONS "BOOST_ENABLE_CMAKE ON" "BOOST_SKIP_INSTALL_RULES OFF"
113+
)
150114
endif()
151115

116+
# set(IS_BOOST_LOCAL OFF) endif()
117+
152118
# ====================== link and install part =======================
153119

154120
# Link dependencies
155-
if(${IS_BOOST_LOCAL})
121+
message(DEBUG "Boost_ADDED is defined : ${Boost_ADDED}")
122+
if(Boost_ADDED STREQUAL "")
156123
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::boost)
157124
else()
158125
foreach(a_lib ${BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED})
@@ -164,40 +131,36 @@ function(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
164131
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::${a_lib})
165132
endforeach()
166133

167-
if(${IS_BOOST_LOCAL})
168-
set(OUR_BOOST_INSTALL_TARGETS
134+
if(Boost_ADDED STREQUAL "")
135+
set(ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS
169136
"Boost ${TRY_BOOST_VERSION} REQUIRED ${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
170137
)
171138
else()
172-
set(BOOST_DOWNLOADED_INSTALL_TARGETS
139+
set(ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS
173140
""
174-
CACHE STRING "hello_doc..."
175141
)
176142
set(BOOST_ALL_DEPENDENCIES
177143
"${BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED};${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
178144
)
179145
foreach(dep ${BOOST_ALL_DEPENDENCIES})
180-
string(APPEND BOOST_DOWNLOADED_INSTALL_TARGETS "boost_${dep} ${TRY_BOOST_VERSION};")
146+
string(APPEND ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS "boost_${dep} ${TRY_BOOST_VERSION};")
181147
endforeach()
182-
string(LENGTH "${BOOST_DOWNLOADED_INSTALL_TARGETS}" BOOST_DOWNLOADED_INSTALL_TARGETS_LENGTH)
183-
math(EXPR BOOST_DOWNLOADED_INSTALL_TARGETS_LENGTH "${BOOST_DOWNLOADED_INSTALL_TARGETS_LENGTH}-1"
148+
string(LENGTH "${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS}" ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH)
149+
math(EXPR ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH "${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH}-1"
184150
OUTPUT_FORMAT DECIMAL
185151
)
186-
string(SUBSTRING "${BOOST_DOWNLOADED_INSTALL_TARGETS}" 0
187-
${BOOST_DOWNLOADED_INSTALL_TARGETS_LENGTH} BOOST_DOWNLOADED_INSTALL_TARGETS
152+
string(SUBSTRING "${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS}" 0
153+
${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH} ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS
188154
)
189155
message(
190156
DEBUG
191-
"BOOST_DOWNLOADED_INSTALL_TARGETS ${BOOST_DOWNLOADED_INSTALL_TARGETS_LENGTH}: ${BOOST_DOWNLOADED_INSTALL_TARGETS}"
157+
"ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS ${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH}: ${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS}"
192158
)
193-
set(OUR_BOOST_INSTALL_TARGETS ${BOOST_DOWNLOADED_INSTALL_TARGETS})
194159
endif()
195160

196-
message(DEBUG "OUR_BOOST_INSTALL_TARGETS: ${OUR_BOOST_INSTALL_TARGETS}")
197-
198-
set(${return_var_name_boost_install_targets}
199-
"${OUR_BOOST_INSTALL_TARGETS}"
200-
PARENT_SCOPE
161+
message(
162+
DEBUG
163+
"ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS: ${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS}"
201164
)
202165

203-
endfunction()
166+
endmacro()

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ set(BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED "asio")
2828
2929
add_boost(
3030
${TRY_BOOST_VERSION} ${BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}
31-
${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED} your_target_name boost_install_targets
31+
${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED} your_target_name
3232
)
3333
```
3434
4. Add Boost's install target (notice `${boost_install_targets}`):
@@ -56,7 +56,7 @@ packageProject(
5656
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
5757
EXPORT_HEADER "${EXPORT_HEADER_LOCATION}"
5858
COMPATIBILITY "AnyNewerVersion" DISABLE_VERSION_SUFFIX ON
59-
DEPENDENCIES "${boost_install_targets}"
59+
DEPENDENCIES "${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS}"
6060
)
6161
6262
```

0 commit comments

Comments
 (0)