[CMake] Generate and install a file

Norton Allen allen at huarp.harvard.edu
Thu Mar 28 16:07:34 EDT 2019


On 3/28/2019 3:33 PM, Kyle Edwards wrote:
> Using the DEPENDS argument of add_custom_command() will do this. 
> However, add_custom_command() on its own isn't enough to ensure that 
> the file is generated before installation. You need to pair 
> add_custom_command() with add_custom_target(). Example:
>
> add_custom_command(
>   OUTPUT mygeneratedfile
>   COMMAND mytool -o mygeneratedfile
>   DEPENDS mysourcefile
> )
> add_custom_target(mygeneratedtarget
>   DEPENDS mygeneratedfile
> )
>
> This is different from:
>
> add_custom_target(mygeneratedtarget
>   COMMAND mytool -o mygeneratedfile
>   BYPRODUCTS mygeneratedfile
>   DEPENDS mysourcefile
> )
>
> because it will only run mytool when it needs to (when mysourcefile 
> has changed).
>
Kyle,

What you say makes sense, and I can even understand why 
add_custom_target might do that, but I cannot get this to actually work. 
Here is a minimal CMakeLists.txt. mysourcefile is just and empty file.

I have these files in test/cmake, and I:

    cd test
    mkdir build-cmake
    cd build-cmake
    cmake ../cmake
    make

and there is no sign of mygeneratedfile. I then try

    make install

and I get:

    $ make install
    Install the project...
    -- Install configuration: ""
    CMake Error at cmake_install.cmake:31 (file):
       file INSTALL cannot find
    "/home/nort/test/build-cmake/mygeneratedfile".


    make: *** [Makefile:84: install] Error 1

Where is my mistake?



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190328/2248f101/attachment.html>
-------------- next part --------------
cmake_minimum_required(VERSION 2.8.8)
cmake_policy(SET CMP0048 NEW)
project(table  VERSION 2.0.0)

add_custom_command(
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/mygeneratedfile
  COMMAND touch mygeneratedfile
  DEPENDS mysourcefile
)
add_custom_target(mygeneratedtarget
  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/mygeneratedfile
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/mygeneratedfile DESTINATION bin)


More information about the CMake mailing list