Controlling archive creation with CPack

Introduction

I read the CPack tutorials, but did not find a solution for my specific problem.

I have a project with an optional target I add with the EXCLUDE_FROM_ALL option

# This is an optional target.
add_executable(my-optional-target EXCLUDE_FROM_ALL)

, so I have to explicitly built it on demand.

I am also only installing this target on demand only

# Install the target. 
install(TARGETS my-optional-target 
		EXCLUDE_FROM_ALL
		COMPONENT optional
...
	)

To configure and build I run the following commands:

# Configure the project
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=build/install
# Build the main components
cmake --build build
# Install the main components
cmake --install build

# Build the optional component
cmake --build build --target my-optional-target
# Install the optional component
cmake --install build --component optional

So far everything works as expected. Now comes the problematic part.

CPack Archive Generator

My goal is to call CPack from command line in order to create the following:

  • Single archive with only the main components
  • Single archive with only the optional component
  • Two archives, one with the main the other with the optional components in a single call
  • Single archive with both the main and optional components

For multi-config CMake generators, I would also like to specify the configurations.

In the CMakeLists.txt I set(CPACK_ARCHIVE_COMPONENT_INSTALL true). With that set (among others) I can call CPack to create two archives, one with the main the other with the optional components in a single call.

# Create two archives, one with the main the other with the optional components in a single call
cpack -B build --config build\CPackConfig.cmake

I cannot create any of the other archives from command line, as all arguments passed to CPack are ignored.

# The result is always two archives, one for each component.
cpack -B build --config build\CPackConfig.cmake -DCPACK_COMPONENTS_ALL=optional
cpack -B build --config build\CPackConfig.cmake -DCPACK_MONOLITHIC_INSTALL=true

If I set these values in the CMakeLists.txt

set(CPACK_COMPONENTS_ALL optional)
#set(CPACK_COMPONENTS_ALL Unspecified)
#set(CPACK_COMPONENTS_ALL Unspecified optional)

they do have an effect, just not when passed as an argument to CPack, which would mean I would always have to edit my CMakeLists.txt and reconfigure the project before I can build the archive I need.

I found no solution to build a single archive with both the main and the optional components.

I found a few settings which have no effect at all, like CPACK_ARCHIVE_<component>_FILE_NAME.

Sample Project

I created a minimal sample project to show the problems with CPack.

cpack-create-archives.zip (1.8 KB)

Question

Can anyone please tell me how to create the mentioned archives calling CPack from command line?