Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add how to add boost #575

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,24 +414,9 @@ CPMAddPackage(

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

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.
Copy link
Contributor

@ScottBailey ScottBailey Jul 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removed bit is a very important part of this example. It coaches the user in best practices.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I'll try to save these lines of notes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if add them to ## Tips that would be ok

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this related to CPM? And why worry about installation?

Copy link
Author

@Arniiiii Arniiiii Jul 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because assume there's no boost on system. What should cmake install when you asked to install your project that uses a boost library, when there's no boost on the specific system? Right, it installs the specific boost libraries too. That's why thinking about installation targets in this context is important.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... In my use cases, I compile boost as a static library so there's no boost installation. In the case of a library, this gets more complicated. But I think the fix to patch Boost instead of add another dependency.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... In my use cases, I compile boost as a static library so there's no boost installation. In the case of a library, this gets more complicated. But I think the fix to patch Boost instead of add another dependency.

I support your idea. I'll try realize it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the script allows more than patching, but also easy linking, easy installing and so on, no matter is boost installed on the system or not, which is great.


```CMake
# 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.84.0
URL https://github.com/boostorg/boost/releases/download/boost-1.84.0/boost-1.84.0.tar.xz
URL_HASH SHA256=2e64e5d79a738d0fa6fb546c6e5c2bd28f88d268a2a080546f74e5ff98f29d0e
OPTIONS "BOOST_ENABLE_CMAKE ON"
)
```

For a working example of using CPM to download and configure the Boost C++ Libraries see [here](examples/boost).
Also, to get install target if you use `add_subdirectory(dir_with_boost_source)`, you need to apply a patch for 1.80.0, and another 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).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this comment mean and how is it realted to nominal CPM usage?

Copy link
Author

@Arniiiii Arniiiii Jul 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a problem with cmake for boost from versions from 1.80.0 upto 1.84.0 that doesn't allow using it via add_subdirectory in terms of not generating install targets. And for version 1.80.0 it's a little bit different patch than for other versions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are using CPM, why would we call add_subdirectory? I don't in any of my projects...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are using CPM, why would we call add_subdirectory? I don't in any of my projects...

I'm using CPM. Though the logic behind CPM is actually a kinda smart add_subdirectory. I don't use add_subdirectory manually in my projects too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand.


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

Expand Down
29 changes: 22 additions & 7 deletions examples/boost/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,34 @@ project(CPMExampleBoost)

# ---- Create binary ----

add_executable(CPMExampleBoost main.cpp)
add_executable(${PROJECT_NAME} main.cpp)
target_compile_features(CPMExampleBoost PRIVATE cxx_std_17)

# ---- Dependencies ----

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

CPMAddPackage(
NAME Boost
VERSION 1.84.0
URL https://github.com/boostorg/boost/releases/download/boost-1.84.0/boost-1.84.0.tar.xz
URL_HASH SHA256=2e64e5d79a738d0fa6fb546c6e5c2bd28f88d268a2a080546f74e5ff98f29d0e
OPTIONS "BOOST_ENABLE_CMAKE ON" "BOOST_INCLUDE_LIBRARIES container\\\;asio" # Note the escapes!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a simple, secure, working example that demonstrates including container and asio. For me security and simplicity are important.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this is important.

NAME AddBoost.CMake
GIT_TAG main
GITHUB_REPOSITORY Arniiiii/AddBoost.cmake
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example lacks security and a specific version of boost. When developing a commercial application, there's often a need to call out a specific revision of boost.

These lines are neither equivalent nor better than the removed lines for my purposes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security? Not sure. I admit the problem with specific version. I'll try to solve it

Copy link
Author

@Arniiiii Arniiiii Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've thought a little bit about version handling. If to think about AddBoost.cmake not as wrapper for Boost but just another cmake script, In the end it's still find_package(Boost ${TRY_BOOST_VERSION} COMPONENTS ${BOOST_NOT_HEADER_ONLY_COMPONENTS}) or CPMAddPackage(NAME Boost VERSION ${TRY_BOOST_VERSION} PATCHES ... OPTIONS __CMAKE_BOOST_INSTALL_OR__BOOST_ENABLE_INSTALL) .

If to think about specific revision of boost there's two options:

  1. Use specific boost version for which there's tag and patch it
  2. If to think about specifying SOURCE_DIR for CPM to get a separate boost.
    It's easy to add the ability to do the second one to the AddBoost.cmake

)

target_link_libraries(CPMExampleBoost PRIVATE Boost::asio Boost::container)
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} boost_install_targets
)

# notice ${boost_install_targets} in the next lines:

# CPMAddPackage( NAME PackageProject.cmake VERSION 1.11.2 GIT_REPOSITORY
# "https://github.com/TheLartians/PackageProject.cmake.git" )
#
# packageProject( NAME ${PROJECT_NAME} VERSION ${PROJECT_VERSION} NAMESPACE ${PROJECT_NAME}
# BINARY_DIR ${PROJECT_BINARY_DIR} INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include INCLUDE_DESTINATION
# include/${PROJECT_NAME} VERSION_HEADER "${VERSION_HEADER_LOCATION}" EXPORT_HEADER
# "${EXPORT_HEADER_LOCATION}" COMPATIBILITY "AnyNewerVersion" DISABLE_VERSION_SUFFIX ON DEPENDENCIES
# "${boost_install_targets}" )
Loading