Search code examples
cmakecpack

CMake check '/etc/hosts' for RPM installation


I'm trying to write a simple execute_process command for my CMakeLists.txt to check for the presence of a certain piece of text in the hosts file when the RPM is being installed, and if it isn't there, append it to the hosts file.

However, when running CMake, I get the message:

grep ||: No such file or directory
grep: echo: No such file or directory
grep: 172.16.10.43 UPSSERVER: No such file or directory
grep: >>: No such file or directory

It seems to think each piece of my executed command is a file? Here is the command in question:

set(HOSTS_UPSSERVER "172.16.10.43 UPSSERVER")
execute_process(COMMAND grep -qxF ${HOSTS_UPSSERVER} /etc/hosts || echo ${HOSTS_UPSSERVER} >> /etc/hosts)

I know the command itself works because I've tested it on its own outside the command line. Is there an error in the syntax for the way I've defined the HOSTS_UPSSERVER variable? Or in the way I'm using the execute_process command?

I haven't had much luck finding useful information on CMake, as the documentation is vague and good tutorials are practically non-existent.


Solution

  • Checking the /etc/hosts from within cmake makes no sense as you would only check the file on your build host. To run a script during RPM install you need to include the code in the RPM itself by providing a spec file. During generation of the RPM via cpack you can configure a spec file via CPACK_RPM_USER_BINARY_SPECFILE

    Within the spec file you have to define several parameters as package name and dependencies. To run a script after installation you need to add it to the %post section:

    %post
    grep -qxF ${HOSTS_UPSSERVER} /etc/hosts || echo ${HOSTS_UPSSERVER} >> /etc/hosts
    
    

    To use cmake variables within the spec file you need to generate it using configure_file.