[CMake] always execute CUSTOM_COMMAND

Jack Kelly endgame.dos at gmail.com
Tue Aug 21 04:36:41 EDT 2007


msk.conf at seznam.cz wrote:
> PROJECT ( test_project )
> ADD_CUSTOM_COMMAND (
> 	OUTPUT file.txt
> 	COMMAND cp /etc/passwd file.txt
> )
> ADD_CUSTOM_TARGET ( file_target DEPENDS file.txt )
> INSTALL ( FILES file.txt DESTINATION /tmp )
> 
> I need to create file "file.txt" and install it. But installation doesn't 
> affect dependency, so file.txt will never be created. That file isn't c code, 
> so it can't be included to any executable or library to create dependency.
> 
> What is the right syntax to create and install that file, please?
You were pretty close. You only really missed 2 things:

1. The `ALL' option in ADD_CUSTOM_TARGET. In the CMake docs for 
ADD_CUSTOM_TARGET:

  > If the ALL option is specified it indicates  that  this
  > target  should  be  added to the default build target so that it
  > will be run every time (the command cannot be called  ALL).

2. INSTALL should probably have a specific path to your generated file. 
CMake didn't pick up on it unless I gave a more specific path.

3. ADD_CUSTOM_TARGET can contain embedded commands.

Here's a CMakeLists.txt that I used:

PROJECT(test NONE)
ADD_CUSTOM_TARGET(
   "file.txt" ALL
   COMMAND "touch" ARGS "file.txt"
   VERBATIM
)
INSTALL(
   FILES "${CMAKE_BINARY_DIR}/file.txt"
   DESTINATION "/tmp"
)

-- Jack


More information about the CMake mailing list