CMake & CPack with multiple packages on a monorepo

Hello everyone,

we are building our packages on a monorepo which contains a set of common components and some specific components used only for specific packages.

To be more specific: assume we have three components A, B and C. There is package foo that uses A and B. Package bar requires A and C. CPack should be instructed to package only the neccessary libraries (as in: package bar contains only A and C).

Reality is even a bit more complex (C also requires D and B has a dependency on F plus external libraries; also the amount of libraries would exceed the alphabet). I hope that above abstraction provides enough information to serve as a minimal example.

Building with CMake works pretty straightforward - using a collector target and proper dependencies. Packaging is a bit more complex.

Currently all targets are flagged optional. Using that and the collector target, CPack will pick up only built targets and package them. Nevertheless, a reconfiguration is required when switching between packages.

What is the preferred / best practice to use CMake / CPack with such a layout?