0
votes

I have a build which is currently set up with steps as follows:

cmake
make
cpack -G TGZ
cpack -G RPM

I now have a problem in that there are files I wish to include in the RPM but not the tarball. Is there a way to make the install command conditional according to the generator used?

The simple and obvious way is wrong:

if (NOT ${PACKAGE_TYPE} STREQUAL "TGZ")
   message("HELLO ${PACKAGE_TYPE}")
   install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/foobar DESTINATION "/usr/lib" COMPONENT core RENAME "/usr/lib/only-install-me-for-RPM")
endif()

I believe it is wrong because the configure stage (running cmake) evaluates the conditional but cpack does not. I do not want two builds as the install stage is the only part different. I do want more than one kind of installation package.

Background

Why do I want to do such an odd thing? I can think of other legitimate reasons but in this case it is because of the introduction of /usr/lib/.build-id. It is not possible to disable this behaviour from cmake (though it is possible in the .spec file see https://bugzilla.redhat.com/show_bug.cgi?id=1724153)

In RHEL8 rpmbuild installs files (actually links) in /usr/lib/.build-id which I have not specificed myself. In order to persuade cmake to make /usr/lib relocatable I have to install a dummy file in /usr/lib - see https://gitlab.kitware.com/cmake/cmake/-/issues/20691

This is not necessary for the tarball.

1
It turns out I can solve my problem using set(CPACK_RPM_SPEC_MORE_DEFINE "%define _build_id_links none") to disable .build-id in the RPM spec but the question of how to do it the other way still stands. - Bruce Adams
Not an answer, because I don't know cmake, but traditionally you'd need to have additional "Source" lines in the specfile, e.g. Source1: myfile.txt - Aaron D. Marasco

1 Answers

0
votes

Currently used CPack generator can be retrieved from CPACK_GENERATOR variable. But this meaning works only inside a script specified with CPACK_PROJECT_CONFIG_FILE variable. Inside CMakeLists.txt the variable CPACK_GENERATOR has other meaning.

Because install command can only be issued in CMakeLists.txt, this command cannot be made conditional (based on CPack generator). But you may assign a COMPONENT for this installation. This component can be excluded from the components list later.

CMakeLists.txt:

# Assign 'core_special' COMPONENT for installation
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/foobar DESTINATION "/usr/lib" COMPONENT core_special RENAME "/usr/lib/only-install-me-for-RPM")

# ...

# Set config script for CPack. 
set(CPACK_PROJECT_CONFIG_FILE "${CMAKE_SOURCE_DIR}/cpack_project_config.cmake")

cpack_project_config.cmake:

# Exclude component "core_special" for all CPack generators except TGZ.
if (NOT CPACK_GENERATOR STREQUAL "TGZ")
  list(REMOVE_ITEM CPACK_COMPONENTS_ALL "core_special")
endif()

# Need to set 'CMAKE_<GENERATOR>_COMPONENT_INSTALL' to ON, otherwise CPack ignores CPACK_COMPONENTS_ALL variable
set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)
set(CPACK_RPM_COMPONENT_INSTALL ON)

# E.g create single archive/package from all components
# (other values - "IGNORE", "ONE_PER_GROUP" - will also work)
set(CPACK_COMPONENTS_GROUPING "ALL_COMPONENTS_IN_ONE")