[CMake] problem with add_custom_command / add_custom_target in different directories

Brad King brad.king at kitware.com
Wed Feb 25 08:54:39 EST 2009


Michael Wild wrote:
> On 25. Feb, 2009, at 7:25, Philip Lowman wrote:
>> When I invoke add_custom_target() in the
>> parent directory it causes an error in the makefiles.
> To me this appears to be a bug in the Makefile generator. I also tried 
> to make a "untarFoo" custom target in test/CMakeLists.txt, which 
> depended on the output of the custom command and then have the "untar" 
> target depend on "untarFoo", but that didn't work out either.

Custom commands are registered only in the directory where they appear.
When Philip points the DEPENDS of add_custom_target at a file, CMake looks
only at custom commands added in that directory for a rule.  Since it
doesn't find one it assumes that the user is referring to a file that will
be on disk by the time the custom target builds.  This isn't the case, so
make complains it can't find the file.

> What works for me (CMake-CVS, Mac OS X 10.5, Makefile generator) is the 
> following:
> - add an empty custom target "untar" with no dependencies to 
> CMakeLists.txt BEFORE any of the add_subdirectory calls
> - in the test/CMakeLists.txt add a custom target "untarFoo" which 
> depends on the output of the custom command
> - in the test/CMakeLists.txt add a dependency of the top-level "untar" 
> target on the "untarFoo" helper target

This approach is correct, but could unfortunately create too many extra
targets that the user will see in IDE generators like VS and Xcode.  I
suggest instead doing all the untar rules in a single target defined in
one directory.  One can use the WORKING_DIRECTORY option to make each
rule execute in the proper directory.

Meanwhile, there are a few potential problems with the untar-in-source
approach in the first place:

   1.) The source tree might be read-only.  You can untar into the
       build tree and refer to things there.

   2.) Your example specifies a directory as the output of the custom
       command.  Since the directory timestamp gets updated by a
       variety of operations, the untar rule may not rerun reliably
       when the tarball changes.  I suggest making the extraction
       rule generate a stamp file with "cmake -E touch ...".

-Brad


More information about the CMake mailing list