# Controlling archive creation with CPack

**URL:** https://discourse.cmake.org/t/controlling-archive-creation-with-cpack/4570
**Category:** Usage
**Tags:** cpack:archive
**Created:** [November 29, 2021, 11:56am UTC](https://discourse.cmake.org/t/controlling-archive-creation-with-cpack/4570 "2021-11-29T11:56:03Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![jjELT](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@jjELT](https://discourse.cmake.org/u/jjELT)
#### Post date: [November 29, 2021, 11:56am UTC](https://discourse.cmake.org/t/controlling-archive-creation-with-cpack/4570/1 "2021-11-29T11:56:03Z")

</div>

# Introduction

I read the [CPack tutorials](https://gitlab.kitware.com/cmake/community/-/wikis/home#cpack), 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

```auto
# 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

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

```

To configure and build I run the following commands:

```auto
# 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.

```auto
# 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.

```auto
# 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`

```auto
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](https://cmake.org/cmake/help/latest/cpack_gen/archive.html?highlight=CPACK_ARCHIVE_%3Ccomponent%3E_FILE_NAME#variable:CPACK_ARCHIVE_%3Ccomponent%3E_FILE_NAME).

# Sample Project

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

[cpack-create-archives.zip](https://discourse.cmake.org/uploads/short-url/1c6LrWm71cLaK2Z2xPJdLC6prVI.zip) (1.8 KB)

# Question

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