[CMake] Generate and install a file

Norton Allen allen at huarp.harvard.edu
Thu Mar 28 14:58:24 EDT 2019


I have spent a few hours trying to solve what seems like a simple problem.

I need to generate a file with some dependencies and install it.

If I want cmake to figure out how to compile and link it, I can

    add_executable(mytarget mytarget.c)
    install(TARGETS mytarget DESTINATION bin)

I also know that if I get cmake to configure a file, I can install it:

    configure_file (
       ${CMAKE_CURRENT_SOURCE_DIR}/config.h.in
       ${CMAKE_CURRENT_BINARY_DIR}/config.h )
    intall(FILES ${CMAKE_CURRENT_BINARY_DIR}/config.h
       DESTINATION include )

But what if I have a tool of my own that will generate the file? I know 
I can:

    add_custom_command(
       OUTPUT mygeneratedfile
       COMMAND mytool -o mygeneratedfile
       DEPENDS mysourcefile
    )

and this works well *provided* that mygeneratedfile is in turn a source 
file for another derivation, but I cannot follow that with:

    install(FILES mygeneratedfile DESTINATION bin)

because mygeneratedfile is not a 'target', and I can't make it into a 
target with add_custom_target() because that's talking about something 
else entirely (commands that will be run unconditionally, not based on 
dependencies)

Am I missing something?

In my specific case, mytool actually creates an executable via it's own 
build system, but it could also be creating a script of some kind that 
needs to be installed. Of course in that case, I could possibly trick 
CMake by generating mygeneratedfile.in and using configure_file() with 
no substitutions (pretty dubious), but I can't do that with a binary 
file. Is this not possible?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190328/809d3acc/attachment.html>


More information about the CMake mailing list