Skip to content

Commit a8144f5

Browse files
Update Boost example (#531)
* Update CMake and main to add another library and download FASTER. * Apply style formatters * Update README.md's boost example and add information on determining source archive location at GitHub. * Update README.md --------- Co-authored-by: Lars Melchior <TheLartians@users.noreply.github.com>
1 parent 3c25130 commit a8144f5

File tree

3 files changed

+111
-12
lines changed

3 files changed

+111
-12
lines changed

README.md

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,19 +406,27 @@ CPMAddPackage(
406406
)
407407
```
408408

409-
### [Boost ](https://github.com/boostorg/boost)
409+
### [Boost](https://github.com/boostorg/boost)
410+
411+
Boost is a large project and will take a while to download. Using
412+
`CPM_SOURCE_CACHE` is strongly recomended. Cloning moves much more
413+
data than a source archive, so this sample will use a compressed
414+
source archive (tar.xz) release from Boost's github page.
410415

411416
```CMake
412-
# boost is a huge project and will take a while to download
413-
# using `CPM_SOURCE_CACHE` is strongly recommended
417+
# boost is a huge project and directly downloading the 'alternate release'
418+
# from github is much faster than recursively cloning the repo.
414419
CPMAddPackage(
415420
NAME Boost
416-
VERSION 1.81.0
417-
GITHUB_REPOSITORY "boostorg/boost"
418-
GIT_TAG "boost-1.81.0"
421+
VERSION 1.84.0
422+
URL https://github.com/boostorg/boost/releases/download/boost-1.84.0/boost-1.84.0.tar.xz
423+
URL_HASH SHA256=2e64e5d79a738d0fa6fb546c6e5c2bd28f88d268a2a080546f74e5ff98f29d0e
424+
OPTIONS "BOOST_ENABLE_CMAKE ON"
419425
)
420426
```
421427

428+
For a working example of using CPM to download and configure the Boost C++ Libraries see [here](examples/boost).
429+
422430
### [cxxopts](https://github.com/jarro2783/cxxopts)
423431

424432
```cmake
@@ -475,3 +483,85 @@ For a full example on using CPM to download and configure lua with sol2 see [her
475483
### Full Examples
476484

477485
See the [examples directory](https://github.com/cpm-cmake/CPM.cmake/tree/master/examples) for full examples with source code and check out the [wiki](https://github.com/cpm-cmake/CPM.cmake/wiki/More-Snippets) for many more example snippets.
486+
487+
## Source Archives from GitHub
488+
489+
Using a compressed source archive is usually much faster than a shallow
490+
clone. Optionally, you can verify the integrity using
491+
[SHA256](https://en.wikipedia.org/wiki/SHA-2) or similar. Setting the hash is useful to ensure a
492+
specific source is imported, especially since tags, branches, and
493+
archives can change.
494+
495+
Let's look at adding [spdlog](https://github.com/gabime/spdlog) to a project:
496+
497+
```cmake
498+
CPMAddPackage(
499+
NAME spdlog
500+
URL https://github.com/gabime/spdlog/archive/refs/tags/v1.12.0.zip
501+
URL_HASH SHA256=6174bf8885287422a6c6a0312eb8a30e8d22bcfcee7c48a6d02d1835d7769232
502+
)
503+
```
504+
505+
URL_HASH is optional, but it's a good idea for releases.
506+
507+
508+
### Identifying the URL
509+
510+
Information for determining the URL is found
511+
[here](https://docs.github.com/en/repositories/working-with-files/using-files/downloading-source-code-archives#source-code-archive-urls).
512+
513+
514+
#### Release
515+
516+
Not every software package provides releases, but for those that do,
517+
they can be found on the release page of the project. In a browser,
518+
the URL of the specific release is determined in a browser is
519+
determined by right clicking and selecting `Copy link address` (or
520+
similar) for the desired release. This is the value you will use in
521+
the URL section.
522+
523+
This is the URL for spdlog release 1.13.0 in zip format:
524+
`https://github.com/gabime/spdlog/archive/refs/tags/v1.13.0.zip`
525+
526+
527+
#### Branch
528+
529+
The URL for branches is non-obvious from a browser. But it's still fairly easy to figure it out. The format is as follows:
530+
531+
`https://github.com/<user>/<name>/archive/refs/heads/<branch-name>.<archive-type>`
532+
533+
Archive type can be one of `tar.gz` or `zip`.
534+
535+
The URL for branch `v2.x` of spdlog is:
536+
`https://github.com/gabime/spdlog/archive/refs/heads/v2.x.tar.gz`
537+
538+
539+
#### Tag
540+
541+
Tags are simiar, but with this format:
542+
543+
`https://github.com/<user>/<name>/archive/refs/tags/<tag-name>.<archive-type>`
544+
545+
Tag `v1.8.5` of spdlog is this:
546+
547+
`https://github.com/gabime/spdlog/archive/refs/tags/v1.8.5.tar.gz`
548+
549+
Exactly like the release.
550+
551+
552+
#### Commit
553+
554+
If a specific commit contains the code you need, it's defined as follows:
555+
556+
`https://github.com/<user>/<name>/archive/<commit-hash>.<archive-type>`
557+
558+
Example:
559+
`https://github.com/gabime/spdlog/archive/c1569a3d293a6b511ecb9c18b2298826c9578d9f.tar.gz`
560+
561+
562+
### Determining the Hash
563+
564+
The following snipet illustrates determining the SHA256 hash on a linux machine using `wget` and `sha256sum`:
565+
```bash
566+
wget https://github.com/gabime/spdlog/archive/refs/tags/v1.13.0.zip -O - | sha256sum
567+
```

examples/boost/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ include(../../cmake/CPM.cmake)
1313

1414
CPMAddPackage(
1515
NAME Boost
16-
VERSION 1.81.0
17-
GITHUB_REPOSITORY "boostorg/boost"
18-
GIT_TAG "boost-1.81.0"
16+
VERSION 1.84.0
17+
URL https://github.com/boostorg/boost/releases/download/boost-1.84.0/boost-1.84.0.tar.xz
18+
URL_HASH SHA256=2e64e5d79a738d0fa6fb546c6e5c2bd28f88d268a2a080546f74e5ff98f29d0e
19+
OPTIONS "BOOST_ENABLE_CMAKE ON" "BOOST_INCLUDE_LIBRARIES container\\\;asio" # Note the escapes!
1920
)
2021

21-
target_link_libraries(CPMExampleBoost PRIVATE Boost::asio)
22+
target_link_libraries(CPMExampleBoost PRIVATE Boost::asio Boost::container)

examples/boost/main.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,26 @@
99
//
1010

1111
#include <boost/asio.hpp>
12+
#include <boost/container/devector.hpp>
1213
#include <boost/date_time/posix_time/posix_time.hpp>
1314
#include <iostream>
15+
#include <string>
1416

15-
void print(const boost::system::error_code& /*e*/) { std::cout << "Hello, world!" << std::endl; }
17+
boost::container::devector<std::string> strings;
18+
19+
void print(const boost::system::error_code& /*e*/) {
20+
for (const auto& a : strings) std::cout << a;
21+
}
1622

1723
int main() {
1824
boost::asio::io_service io;
1925

26+
strings.push_back("Hello, world!\n");
27+
2028
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
2129
t.async_wait(&print);
2230

2331
io.run();
2432

2533
return 0;
26-
}
34+
}

0 commit comments

Comments
 (0)