feat(deps): add FindBoost module with components

Fetch the Boost CMake release archive (default 1.92.0) and build only
the libraries requested via find_package(Boost COMPONENTS ...), plus
their dependencies. Each component is exposed as Boost::<name>, e.g.
Boost::serialization. cmat now uses this module instead of a system
Boost install, so the Boost exercises are always available.

Also fix SolutionEx62Bonus: Employee.h used base_object without
including <boost/serialization/base_object.hpp>. This was not an
intentional exercise bug; it likely relied on a transitive include
in older Boost versions.

Co-authored-by: Claude Opus 5.5 <claude.opus5.5@anthropic.com>
This commit is contained in:
2026-10-06 08:11:54 +02:00
co-authored by Claude Opus 5.5
parent 38473eb734
commit c681ebab2b
4 changed files with 94 additions and 5 deletions
+1 -1
View File
@@ -100,7 +100,7 @@ cppcourse/
- `Materials/`: supplementary course material PDFs.
- Each exercise directory holds a `CMakeLists.txt` that defines its target via
`cmat_add_exercise()` (see `cmat/CMakeLists.txt`). Exercises that need a
missing dependency (e.g. Boost) are reported as not available at configure
missing dependency (e.g. curses) are reported as not available at configure
time and skipped.
## Behavioral Guidelines
+2 -4
View File
@@ -56,10 +56,8 @@ else()
# Windows-specific; there is nothing portable to build with.
endif()
# Exercises using Boost.Serialization (system install; not fetched). CONFIG
# mode uses Boost's own CMake package, since the FindBoost module was removed
# in CMake 4.
find_package(Boost CONFIG QUIET COMPONENTS serialization)
# Exercises using Boost.Serialization (fetched via deps/FindBoost.cmake).
find_package(Boost QUIET COMPONENTS serialization)
# ------------------------------------------------------------------------------
# Target helpers
+2
View File
@@ -2,6 +2,8 @@
#define EMPLYOEE_H
#include "Person.h"
#include <boost/serialization/base_object.hpp>
class Employee : public Person {
public:
void askForInfo();
+89
View File
@@ -0,0 +1,89 @@
# ==============================================================================
# Find Boost
# ==============================================================================
# This module fetches the Boost libraries (CMake release archive) and builds
# only the requested components and their dependencies.
#
# Usage:
# find_package(Boost REQUIRED COMPONENTS serialization program_options)
# target_link_libraries(app PRIVATE Boost::serialization Boost::program_options)
#
# Components are Boost library names as they appear under libs/ (lowercase),
# e.g. serialization, filesystem, json, asio. Header-only libraries work too.
# Without components every Boost library is configured (slower configure);
# targets are only built when linked.
#
# All components must be requested in the first find_package(Boost) call:
# Boost is configured once, later calls can only use what was configured.
#
# Targets provided:
# Boost::<component> - One target per requested component
# Boost::headers - All configured header-only libraries
#
# Variables set:
# Boost_FOUND - TRUE if Boost and all required components are available
# Boost_<component>_FOUND - TRUE if the component is available
# Boost_LIBRARIES - The component targets (Boost::<component>...)
# Boost_VERSION - Version of Boost
# ==============================================================================
# Use the version passed to find_package(), or default to 1.92.0
# The version must be the full x.y.z form used by the release archive names
if (DEFINED Boost_FIND_VERSION AND NOT Boost_FIND_VERSION STREQUAL "")
set(BOOST_VERSION "${Boost_FIND_VERSION}")
else()
set(BOOST_VERSION "1.92.0")
endif()
if (NOT DEFINED _FINDBOOST_INCLUDED)
set(_FINDBOOST_INCLUDED TRUE)
message(STATUS "Fetching Boost ${BOOST_VERSION} (components: ${Boost_FIND_COMPONENTS})")
include(FetchContent)
# Mark Boost includes as SYSTEM to suppress warnings from its headers
set(_boost_fetch_options "")
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.25)
list(APPEND _boost_fetch_options SYSTEM)
endif()
FetchContent_Declare(
boost
URL https://github.com/boostorg/boost/releases/download/boost-${BOOST_VERSION}/boost-${BOOST_VERSION}-cmake.tar.xz
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
EXCLUDE_FROM_ALL
${_boost_fetch_options}
)
# Read by the Boost superproject: build only these libraries (and their
# dependencies). Empty means all libraries.
set(BOOST_INCLUDE_LIBRARIES ${Boost_FIND_COMPONENTS})
FetchContent_MakeAvailable(boost)
unset(BOOST_INCLUDE_LIBRARIES)
set(BOOST_LICENSE_FILE "${boost_SOURCE_DIR}/LICENSE_1_0.txt" CACHE FILEPATH "Path to Boost license file")
endif()
set(Boost_LIBRARIES "")
foreach (_boost_comp IN LISTS Boost_FIND_COMPONENTS)
if (TARGET Boost::${_boost_comp})
set(Boost_${_boost_comp}_FOUND TRUE)
list(APPEND Boost_LIBRARIES Boost::${_boost_comp})
else()
set(Boost_${_boost_comp}_FOUND FALSE)
if (NOT Boost_FIND_QUIETLY)
message(STATUS "Boost component '${_boost_comp}' is not available: "
"unknown library, or not requested in the first find_package(Boost) call")
endif()
endif()
endforeach()
set(Boost_VERSION "${BOOST_VERSION}")
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Boost
REQUIRED_VARS boost_SOURCE_DIR
VERSION_VAR Boost_VERSION
HANDLE_COMPONENTS
)