Generate individual package for each component

I want to build multiple packages from a single project, and I’m trying to use cpack component to do it.

I have a toy project set up as follows:

# CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(Sandbox VERSION 0.2)

add_executable(main main.cpp)
add_executable(main2 main.cpp)
install(TARGETS main
  COMPONENT FirstComp)
install(TARGETS main2
  COMPONENT SecondComp)

set(CPACK_PACKAGE_CONTACT "Name Surname <name@domain.com>")
set(CPACK_GENERATOR DEB)
include(CPack)

cpack_add_component(FirstComp
  DISPLAY_NAME first_comp
  )

cpack_add_component(SecondComp
  DISPLAY_NAME SecondComp
  )
//main.cpp
#include <iostream>

int main(){
  std::cout << "Hello, World\n";
}

Normally, if I don’t use component, i can just run the following command to generate the .deb package.

cmake -B build -S .
cmake --build build --target package

But I’m not sure what I should do to generate a seperate .deb package for my FristComp and SecondComp components.

You have to tell CPack to do so.

# tell CPack generator to use component packaging
set(CPACK_DEB_COMPONENT_INSTALL 1)
# tell CPack which kind of grouping you want
set(CPACK_COMPONENTS_GROUPING ONE_PER_GROUP)

see:
https://cmake.org/cmake/help/latest/module/CPackComponent.html#module:CPackComponent

1 Like

Thank you very much