Skip to content

Commit

Permalink
try to make it how ScottBailey recommends
Browse files Browse the repository at this point in the history
  • Loading branch information
Arniiiii committed Aug 19, 2024
1 parent f6665a6 commit 981b419
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 19 deletions.
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,6 @@ git config --global url."https://${USERNAME}:${TOKEN}@github.com".insteadOf "htt
git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com".insteadOf "https://gitlab.com"
```

## Tips

Use CPM_SOURCE_CACHE option to place downloaded project to specific folder and reuse them.

Consider using as much `URL` option as possible if there's a tarball which is compressed, to make CPM download it, then uncompress, instead of git cloning.

## Built with CPM.cmake

Some amazing projects that are built using the CPM.cmake package manager.
Expand Down Expand Up @@ -420,9 +414,41 @@ CPMAddPackage(

### [Boost](https://github.com/boostorg/boost)

Boost has incompatible targets: for boost installed via b2 (or via CMake upto version boost_1.84.0 or via CMake scripts from `boost/tools/boost_install`) there's no targets for header-only Boost's libraries. Starting from boost_1.85.0 there's b2 version **and CMake version** of *install* targets, and Boost CMake version install CMake target even for header-only libraries, which allows installing and using only necessary boost libraries.
Boost is a large project and will take a while to download. Using
`CPM_SOURCE_CACHE` is strongly recommended. Cloning moves much more
data than a source archive, so this sample will use a compressed
source archive (tar.xz) release from Boost's github page.

```CMake
# check https://github.com/boostorg/cmake if you want to add some libraries. Maybe they need some
# addtional options, like Boost::Python requires BOOST_ENABLE_PYTHON ON
set(BOOST_INCLUDE_LIBRARIES "container;asio;thread")
string(REPLACE ";" "\\\\\;" BOOST_INCLUDE_LIBRARIES "${BOOST_INCLUDE_LIBRARIES}")
# boost is a huge project and directly downloading the 'alternate release' from github is much
# faster than recursively cloning the repo.
CPMAddPackage(
NAME Boost
VERSION 1.85.0
URL https://github.com/boostorg/boost/releases/download/boost-1.85.0/boost-1.85.0-cmake.tar.xz
URL_HASH SHA256=0a9cc56ceae46986f5f4d43fe0311d90cf6d2fa9028258a95cab49ffdacf92ad
SYSTEM TRUE # to add boost's headers like they are system. Totally unnecessary
FIND_PACKAGE_ARGUMENTS
"COMPONENTS thread" # "COMPONENTS ${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
# Check if it's a header only lib here:
# https://www.boost.org/doc/libs/1_85_0/more/getting_started/unix-variants.html
OPTIONS "BOOST_ENABLE_CMAKE ON"
"BOOST_SKIP_INSTALL_RULES OFF" # if you want to make boost be
# installed on your system with your package ( Profits: install only what you need ; Cons:
# a little headache)
"BUILD_SHARED_LIBS OFF" # it should work whether build shared or not(static)
"BOOST_INCLUDE_LIBRARIES ${BOOST_INCLUDE_LIBRARIES}"
)
```

You should really use version only 1.85.0+ for Boost via CMake. About how add not only 1.85.0+ versions, check [Arniiiii/AddBoost.cmake](https://github.com/Arniiiii/AddBoost.cmake) repository or discussion at a [relevant PR](https://github.com/cpm-cmake/CPM.cmake/pull/575) or [relevant issue](https://github.com/cpm-cmake/CPM.cmake/issues/501).

Also, to get install target if you use anything that techincally does `add_subdirectory(dir_with_boost_source)` (which CPM does), you need to apply a patch for 1.80.0, and slightly different patch if you want a version of Boost from 1.81.0 upto 1.84.0. To solve such problems, there's a script [AddBoost.CMake](https://github.com/Arniiiii/AddBoost.cmake) example usage of which you can see here: [here](examples/boost) or [here](https://github.com/Arniiiii/ModernCppStarterExampleBoostCmake).
For a working example of using CPM to download and configure the Boost C++ Libraries see [here](examples/boost).

### [cxxopts](https://github.com/jarro2783/cxxopts)

Expand Down
54 changes: 43 additions & 11 deletions examples/boost/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(CPMExampleBoost)

set(FETCHCONTENT_QUIET OFF) # to see downloading progress

# ---- Create binary ----

add_executable(${PROJECT_NAME} main.cpp)
Expand All @@ -11,24 +13,54 @@ target_compile_features(CPMExampleBoost PRIVATE cxx_std_17)

include(../../cmake/CPM.cmake)

# check https://github.com/boostorg/cmake if you want to add some libraries. Maybe they need some
# addtional options, like Boost::Python requires BOOST_ENABLE_PYTHON ON
set(BOOST_INCLUDE_LIBRARIES "container;asio;thread")
string(REPLACE ";" "\\\\\;" BOOST_INCLUDE_LIBRARIES "${BOOST_INCLUDE_LIBRARIES}")

# boost is a huge project and directly downloading the 'alternate release' from github is much
# faster than recursively cloning the repo.
CPMAddPackage(
NAME AddBoost.CMake
GIT_TAG 2.0
GITHUB_REPOSITORY Arniiiii/AddBoost.cmake
NAME Boost
VERSION 1.85.0
URL https://github.com/boostorg/boost/releases/download/boost-1.85.0/boost-1.85.0-cmake.tar.xz
URL_HASH SHA256=0a9cc56ceae46986f5f4d43fe0311d90cf6d2fa9028258a95cab49ffdacf92ad
SYSTEM TRUE # to add boost's headers like they are system. Totally unnecessary
FIND_PACKAGE_ARGUMENTS
"COMPONENTS thread" # "COMPONENTS ${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}"
# Check if it's a header only lib here:
# https://www.boost.org/doc/libs/1_85_0/more/getting_started/unix-variants.html
OPTIONS "BOOST_ENABLE_CMAKE ON"
"BOOST_SKIP_INSTALL_RULES OFF" # if you want to make boost be
# installed on your system with your package ( Profits: install only what you need ; Cons:
# a little headache)
"BUILD_SHARED_LIBS OFF" # it should work whether build shared or not(static)
"BOOST_INCLUDE_LIBRARIES ${BOOST_INCLUDE_LIBRARIES}"
)

set(TRY_BOOST_VERSION "1.85.0")
set(BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED "thread")
set(BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED "asio")

add_boost(${TRY_BOOST_VERSION} ${BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}
${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED} ${PROJECT_NAME}
)
target_link_libraries(CPMExampleBoost PRIVATE Boost::asio Boost::container Boost::thread)

# notice ${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS} in the next lines:
# # or:

# cmake-format: off

# CPMAddPackage(
# NAME AddBoost.CMake
# GIT_TAG 2.1
# GITHUB_REPOSITORY Arniiiii/AddBoost.cmake
# )
#
# set(TRY_BOOST_VERSION "1.85.0")
# set(BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED "thread")
# set(BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED "asio")
# set(BOOST_ADD_MY_OPTIONS "BOOST_ENABLE_PYTHON ON;")
#
# add_boost(${TRY_BOOST_VERSION} ${BOOST_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED}
# ${BOOST_NOT_HEADER_ONLY_COMPONENTS_THAT_YOU_NEED} ${PROJECT_NAME}
# )
#
# # notice ${ADDBOOSTCMAKE_PACKAGEPROJECT_INSTALL_TARGETS} in the next lines:
#
# CPMAddPackage(
# NAME PackageProject.cmake
# VERSION 1.11.2
Expand Down

0 comments on commit 981b419

Please sign in to comment.