How can I use CMake to install a script?

5.9k views Asked by At

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?

2

There are 2 answers

0
Michael Kopp On BEST ANSWER

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.

0
MatrixManAtYrService On

I figured it would be simple, it just turned out to be hard to search for:

install(FILES "hello.sh"
    PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
    DESTINATION "bin"
    RENAME "hello")