Skip to content

Commit

Permalink
ver 3.1 ADDBOOSTCMAKE_LINK_TYPE, fixed one if clause, more `message…
Browse files Browse the repository at this point in the history
…(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.
  • Loading branch information
Arniiiii committed Aug 20, 2024
1 parent 91e5190 commit bfdd849
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 66 deletions.
167 changes: 102 additions & 65 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ cmake_minimum_required(VERSION 3.14...3.22)
# Note: update this to your new project's name and version
project(
AddBoost.cmake
VERSION 3.0
VERSION 3.1
LANGUAGES CXX
)

function(SUBDIRLIST result_var curdir)
file(
GLOB children
RELATIVE ${curdir}
${curdir}/*
)
${curdir}/*)
set(result "")
foreach(child ${children})
if(IS_DIRECTORY ${curdir}/${child})
Expand All @@ -23,13 +22,18 @@ function(SUBDIRLIST result_var curdir)
endforeach()
set(${result_var}
${result}
PARENT_SCOPE
)
PARENT_SCOPE)
endfunction()

macro(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
)
BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED)
message(
DEBUG
"add_boost called with next arguments:
\$\{\$\{TRY_BOOST_VERSION\}\} : ${${TRY_BOOST_VERSION}}
\$\{\$\{BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED\}\} : ${${BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}
\$\{\$\{BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED\}\} : ${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}
")
set(targets_to_link_to "${ARGN}")
message(DEBUG "targets_to_link_to: \"${targets_to_link_to}\"")
set(BOOST_INCLUDE_LIBRARIES
Expand All @@ -52,13 +56,11 @@ macro(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
set(patches_for_boost "")

file(GLOB global_patches_for_boost CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/*.patch"
)
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/*.patch")
list(APPEND patches_for_boost ${global_patches_for_boost})
if(DEFINED BOOST_ADD_MY_PATCHES)
file(GLOB global_patches_for_boost CONFIGURE_DEPENDS
"${BOOST_ADD_MY_PATCHES}/patches/boost/*.patch"
)
"${BOOST_ADD_MY_PATCHES}/patches/boost/*.patch")
list(APPEND patches_for_boost ${global_patches_for_boost})

endif()
Expand All @@ -67,10 +69,11 @@ macro(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
if(subdirs_boost_patches)
foreach(subdir ${subdirs_boost_patches})
if(${subdir} STREQUAL ${${TRY_BOOST_VERSION}})
file(GLOB_RECURSE patches_with_max_applicable_version_up_to CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/${subdir}/*.patch"
)
list(APPEND patches_for_boost ${patches_with_max_applicable_version_up_to})
file(GLOB_RECURSE patches_with_max_applicable_version_up_to
CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/${subdir}/*.patch")
list(APPEND patches_for_boost
${patches_with_max_applicable_version_up_to})
endif()
endforeach()
endif()
Expand All @@ -79,49 +82,80 @@ macro(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
if(subdirs_boost_patches)
foreach(subdir ${subdirs_boost_patches})
if(${subdir} STREQUAL ${${TRY_BOOST_VERSION}})
file(GLOB_RECURSE patches_with_max_applicable_version_up_to CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/${subdir}/*.patch"
)
list(APPEND patches_for_boost ${patches_with_max_applicable_version_up_to})
file(GLOB_RECURSE patches_with_max_applicable_version_up_to
CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/patches/boost/${subdir}/*.patch")
list(APPEND patches_for_boost
${patches_with_max_applicable_version_up_to})
endif()
endforeach()
endif()
endif()

string(REPLACE ";" "\;"
ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
"${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}")
message(
DEBUG
"\$\{ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED\} : ${ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
)

if(DEFINED BOOST_USE_MY_BOOST_DIRECTORY)
CPMAddPackage(
NAME Boost
VERSION ${${TRY_BOOST_VERSION}}
SOURCE_DIR ${BOOST_USE_MY_BOOST_DIRECTORY} PATCHES ${patches_for_boost}
FIND_PACKAGE_ARGUMENTS "COMPONENTS ${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}"
OPTIONS "BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}"
)
endif()
if(patches_for_boost AND NOT boost_is_old) # is it 1.80.0 < x < 1.84.0
CPMAddPackage(
NAME Boost
VERSION ${${TRY_BOOST_VERSION}}
URL ${BOOST_URL} PATCHES ${patches_for_boost}
FIND_PACKAGE_ARGUMENTS "COMPONENTS ${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}"
OPTIONS "BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}"
)
cpmaddpackage(
NAME
Boost
VERSION
${${TRY_BOOST_VERSION}}
SOURCE_DIR
${BOOST_USE_MY_BOOST_DIRECTORY}
PATCHES
${patches_for_boost}
FIND_PACKAGE_ARGUMENTS
"COMPONENTS ${ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
OPTIONS
"BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}")
elseif(patches_for_boost AND NOT boost_is_old) # is it 1.80.0 < x < 1.84.0
cpmaddpackage(
NAME
Boost
VERSION
${${TRY_BOOST_VERSION}}
URL
${BOOST_URL}
PATCHES
${patches_for_boost}
FIND_PACKAGE_ARGUMENTS
"COMPONENTS ${ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
OPTIONS
"BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}")
elseif(NOT boost_is_old) # is it 1.85.0+ ?
CPMAddPackage(
NAME Boost
VERSION ${${TRY_BOOST_VERSION}}
URL ${BOOST_URL}
FIND_PACKAGE_ARGUMENTS "COMPONENTS ${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}"
OPTIONS "BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}"
)
cpmaddpackage(
NAME
Boost
VERSION
${${TRY_BOOST_VERSION}}
URL
${BOOST_URL}
FIND_PACKAGE_ARGUMENTS
"COMPONENTS ${ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
OPTIONS
"BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}")
else() # is it <1.80.0 ?
CPMAddPackage(
NAME Boost
VERSION ${${TRY_BOOST_VERSION}}
GIT_REPOSITORY "https://github.com/boostorg/boost"
GIT_TAG "boost-${${TRY_BOOST_VERSION}}" PATCHES ${patches_for_boost}
FIND_PACKAGE_ARGUMENTS "COMPONENTS ${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}}"
OPTIONS "BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}"
)
cpmaddpackage(
NAME
Boost
VERSION
${${TRY_BOOST_VERSION}}
GIT_REPOSITORY
"https://github.com/boostorg/boost"
GIT_TAG
"boost-${${TRY_BOOST_VERSION}}"
PATCHES
${patches_for_boost}
FIND_PACKAGE_ARGUMENTS
"COMPONENTS ${ESCAPED_BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
OPTIONS
"BOOST_ENABLE_CMAKE ON;BOOST_SKIP_INSTALL_RULES OFF;${BOOST_MY_OPTIONS}")
endif()

# set(IS_BOOST_LOCAL OFF) endif()
Expand All @@ -131,16 +165,26 @@ macro(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
# Link dependencies
foreach(target IN LISTS targets_to_link_to)
message(DEBUG "Boost_ADDED is defined : ${Boost_ADDED}")
if(NOT DEFINED ADDBOOSTCMAKE_LINK_TYPE)
get_target_property(type ${target} TYPE)
if("${type}" STREQUAL "INTERFACE_LIBRARY")
set(ADDBOOSTCMAKE_LINK_TYPE "INTERFACE")
else()
set(ADDBOOSTCMAKE_LINK_TYPE "PUBLIC")
endif()
endif()
if(Boost_ADDED STREQUAL "")
target_link_libraries(${target} PUBLIC Boost::boost)
target_link_libraries(${target} ${ADDBOOSTCMAKE_LINK_TYPE} Boost::boost)
else()
foreach(a_lib ${${BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}})
target_link_libraries(${target} PUBLIC Boost::${a_lib})
target_link_libraries(${target} ${ADDBOOSTCMAKE_LINK_TYPE}
Boost::${a_lib})
endforeach()
endif()

foreach(a_lib ${${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}})
target_link_libraries(${target} PUBLIC Boost::${a_lib})
target_link_libraries(${target} ${ADDBOOSTCMAKE_LINK_TYPE}
Boost::${a_lib})
endforeach()
endforeach()
if(Boost_ADDED STREQUAL "")
Expand All @@ -154,23 +198,16 @@ macro(add_boost TRY_BOOST_VERSION BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED
)
foreach(dep ${BOOST_ALL_DEPENDENCIES})
string(APPEND ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS
"boost_${dep} ${${TRY_BOOST_VERSION}};"
)
"boost_${dep} ${${TRY_BOOST_VERSION}};")
endforeach()
string(LENGTH "${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS}"
ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH
)
ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH)
math(EXPR ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH
"${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH}-1" OUTPUT_FORMAT DECIMAL
)
"${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH}-1"
OUTPUT_FORMAT DECIMAL)
string(SUBSTRING "${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS}" 0
${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH}
ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS
)
message(
DEBUG
"ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS ${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS_LENGTH}: ${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS}"
)
ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS)
endif()

message(
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Versions tested: from 1.79.0 upto 1.85.0 .
```cmake
CPMAddPackage(
NAME AddBoost.CMake
GIT_TAG 3.0
GIT_TAG 3.1
GITHUB_REPOSITORY Arniiiii/AddBoost.cmake
)
```
Expand Down Expand Up @@ -76,6 +76,7 @@ packageProject(
- [x] If you have your own Boost directory, set `BOOST_USE_MY_BOOST_DIRECTORY` to be the path with your Boost.
- [x] If you want, you can link Boost libs yourself, since the code is macro, not a function.
- [x] You can link Boost libs automagically to multiple targets just by adding them to the end of the `addboost(...)` macro.
- [x] You can use `ADDBOOSTCMAKE_LINK_TYPE` to override default behaviour of linking: if target is INTERFACE, use INTERFACE, if else: PUBLIC
- [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:
```
patches/
Expand Down

0 comments on commit bfdd849

Please sign in to comment.