How to install a custom target ?

I am creating a target to generate a script using add_custom_target(). Now I want to install it, but:

  • install(TARGETS) fails with “install TARGETS given target “script” which is not an executable, library, or module.” and I can’t find a way to tell cmake that this target has TYPE EXECUTABLE
  • install(PROGRAMS) would allow me to specify “TYPE EXECUTABLE”, but does not appear to let me specify a file from build tree, only source tree

What did I miss ?

Moreover, the “correct way” to use add_custom_target() seems to be together with add_custom_command() if we want proper dependency handling. But then it seems I have to give different names to the “command” (which takes the output file name) and to the target. And then I’m at a loss as to how install(TARGETS) can be used at all: specifying the file name does not work as it is not a target name, and specifying the target name naturally does not work as it’s not the file name. So what ?

What’s make you think so? The doc: https://cmake.org/cmake/help/v3.13/command/install.html#programs
says:
File names given as relative paths are interpreted with respect to the current source directory

I bet that if you provide absolute path you can install any file in the build tree.
Did you try something like?
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/yourscript.sh DESTINATION bin)

I’m pretty sure you cannot use install(TARGETS) for custom target.

1 Like

Right, this works, thanks. It would be useful to get this example in the doc, I’d say.
Also, if we go with install(PROGRAMS), then better use “TYPE BIN” than “DESTINATION bin”

That seems pretty counter-intuitive, to say the list. If this is so, it should surely be mentionned in the doc too.

Using install(PROGRAMS) still feels like a hack, and anyway it looks like we can’t use the “proper” add_custom_cammand() idiom.

1 Like

I completely agree with you. I have spent the last 15 mins trying to wrestle with install(TARGETS) to install a custom target and failed. What was frustrating was I cotinued to try using install(TARGETS) over install(PROGRAMS) because of the following line in the docs

This form is intended to install programs that are not targets, such as shell scripts. Use the TARGETS form to install targets built within the project.
That is until I found this thread and @erk mentioned he is pretty sure custom targets cannot be installed with install(TARGETS)