如何使用 CMake 安装脚本?

How can I use CMake to install a script?

我有一个构建多个可执行文件并安装它们的项目。对于可执行文件,相关的 CMake 代码是:

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

然后我创建一个 .deb 包,当它安装时我可以从命令行 运行 foobar。我想做这样的事情:

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

...因此 hello 可以从命令行执行。但实际上并没有名为 add_script 的 CMake 命令。我怎样才能做到这一点?

本来以为很简单,结果发现很难搜索:

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

您可以使用

install(PROGRAMS hello.sh DESTINATION bin RENAME hello)

这将自动使您的脚本可执行。见 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.