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?



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)
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

Reply via email to