Evan Teran
Evan Teran

Reputation: 90523

cmake run script for install target?

I have a project where "installing" the code is not quite as simple as just copying some files. With a traditional Makefile, I would just create a make install target that runs a series of shell commands to do what I need.

But googling around has resulting in no examples of this (some things close, but not quite... i think). So basically, I want a custom command, that depends on the target executables, but produces nothing and runs a script that need not be portable to accomplish the "install"

Anyone have any examples of something like this?

Upvotes: 20

Views: 26551

Answers (2)

AngryDane
AngryDane

Reputation: 89

This worked for me: use add_custom_target, then add the main target as a dependency to the custom target target.

# create custom target for setcap to be executed
add_custom_target(setcap ALL
    WORKING_DIRECTORY ${OUTPUT_DIR}/bin
    COMMAND ${CMAKE_COMMAND} -E 'sudo setcap cap_net_raw,cap_net_admin+eip ${}/bin/<executable name>)
# create a dependency on the custom target for main target, setcap depends on ${proj_name}
add_dependencies( ${proj_name} setcap)

Upvotes: 1

Torbj&#246;rn
Torbj&#246;rn

Reputation: 5820

CMake's install command allows for custom scripts. See the official documentation: install - Custom Installation Logic:

install([[SCRIPT <file>] [CODE <code>]]
        [COMPONENT <component>] [...])

The SCRIPT form will invoke the given CMake script files during installation. If the script file name is a relative path it will be interpreted with respect to the current source directory. The CODE form will invoke the given CMake code during installation. Code is specified as a single argument inside a double-quoted string. For example, the code

install(CODE "MESSAGE(\"Sample install message.\")")

will print a message during installation.

To run custom shell script (or whatever program), combine install(CODE ...) with execute_process:

install(CODE "execute_process(COMMAND my_script.sh)")

Upvotes: 37

Related Questions