HEKTO
HEKTO

Reputation: 4191

CMake - how to block executing installation scripts during packaging?

My CMakeLists.txt file contains commands, which should be executed by make install, and all this works fine. The sample CMakeLists.txt below is a short excerpt from my actual CMake file (the tm0001.cpp content is not important here - it might be any C++ program):

cmake_minimum_required(VERSION 3.12)

project(tm0001)

set(CMAKE_CXX_STANDARD 11)
add_executable(${PROJECT_NAME} tm0001.cpp)

install(
  TARGETS ${PROJECT_NAME}
  DESTINATION /usr/local/bin
  PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
)

install(CODE "message(\"-- This must be called during installation only\")")

set(CPACK_PACKAGE_CONTACT "HEKTO")
set(CPACK_GENERATOR "DEB")
include(CPack)

I see the message command is executed by make package as well, which is not I want.

How to tell CMake not to execute installation scripts by the make package command? I couldn't find any way to do that with the CMake if command.

Upvotes: 3

Views: 803

Answers (3)

Tomasz Rybicki
Tomasz Rybicki

Reputation: 21

What I did was to specify install commands with CODE/SCRIPT as separate component e.g. install(CODE ... COMPONENT post-install).

Then also added other non-code install commands as a different component e.g. install(FILES ... COMPONENT files-install)

The CPack then needs to be configured to package only files-install component (solution to this can be found easily - hint: use CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE, CPACK_COMPONENTS_ALL and CPACK_(RPM/DEB/...)_COMPONENT_INSTALL variables).

Of course then the resulting package won't run these CODE components during installing the package - they need to be added separately as a post install script.

Upvotes: 2

HEKTO
HEKTO

Reputation: 4191

I'm answering my own question, because the existing answer doesn't address my main problem. I couldn't find any way (on the CMake level) to block install commands from running during make package - even the postinst script is called by this command.

Fortunately, I could modify the postinst script itself to do nothing in case it's called not by the dpkg:

if [ -z ${DPKG_ADMINDIR} ]; then
  echo "postinst: missing 'dpkg' environment (not an error during packaging)"
  exit 0
fi

It's a trick of course, but it worked for me.

Upvotes: 2

zaufi
zaufi

Reputation: 7119

As it already said in the comment, it's an extremely bad idea to "work w/ systemd" (and doing anything not related to build or packaging of your project) from install commands. The install command (even SCRIPT and CODE signatures) are intended to be used for install actions and not for any other side effects.

The correct way to act here is to produce a native package (DEB/RPM) w/ post-install script, where using the system-provided macros (like described here), you can install your package properly. Take a look to CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA for the way to provide package install actions.

The other bad thing is to use the hardcoded path (/usr/bin/). And BTW, a better place for the (pure) daemon app I suggest /usr/sbin/. Take a look to GNUInstallDirs module shipped w/ CMake for further references.

Upvotes: 4

Related Questions