Search code examples
cmakeinstallationdebcpack

How can I use CMake to install a script?


I have a project that builds several executables and also installs them. For the executables the relevant CMake code is:

add_executable(foo "foo.cpp")
add_executable(bar "bar.cpp;qux.cpp")
install(TARGETS foo bar "/usr/bin")

Then I create a .deb package and when it installs I can run foo and bar from the command line. I want to do something like this:

add_executable(foo "foo.cpp")
add_executable(bar "bar.cpp;qux.cpp")
add_script(hello "hello.sh")
install(TARGETS foo bar hello)

...so that hello is executable from the command line. But there isn't actually a CMake command called add_script. How can I achieve this?


Solution

  • You can use

    install(PROGRAMS hello.sh DESTINATION bin RENAME hello)
    

    which will automatically make your script executable. See the docs for install(FILES):

    The PROGRAMS form is identical to the FILES form except that the default permissions for the installed file also include OWNER_EXECUTE, GROUP_EXECUTE, and WORLD_EXECUTE. This form is intended to install programs that are not targets, such as shell scripts.