[CMake] add_custom_target and PRE_BUILD commands added in sub-dir not executing

Brett Delle Grazie brett.dellegrazie at gmail.com
Wed Sep 12 06:36:29 EDT 2012


Hi,

I've come across some strange behaviour when using add_custom_target and an
add_custom_command(  PRE_BUILD or PRE_LINK ) in nested projects.

We have a number of static analysis checks that were working using cppcheck.
The technique was to add a non-automatic global custom target called
'all_static_checks' like:

add_custom_target(all_static_checks)
set_target_properties(all_static_checks PROPERTIES EXCLUDE_FROM_ALL TRUE)

This make target 'all_static_checks' is run by the build system and
generates output from
the static analysis tool on a per-project basis.

For each sub-project we would:
add_custom_command(TARGET all_static_checks PRE_BUILD
COMMAND custom command goes here
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM)

This works when the add_custom_command and the add_custom_target are
defined within the same CMakeLists.txt
But it fails in the nested case (when the add_custom_command part is
added in a sub-directory). In the nested case
no PRE_BUILD commands are executed.

Attached are two test cases that have no 'C/C++' code (only
CMakeLists.txt) and simply execute an echo of A or B
when running the 'static checks'. The static check is defined in a
module and is identical for both tests.
The tests are compressed in .7z format so please let me know if that's
not appropriate.

The simple-test should succeed, the nested-test should fail.

My questions are:
(1) Is this correct? Should it fail in the nested case? My perception
is that it _used_ to work on earlier versions of CMake (2.8.6 for
example)
(2) Is there a better way?
I have already worked around this by using an add_custom_command and
add_dependencies but that solution is somewhat clunky and clutters
the set of targets.

Thanks in advance,

-- 
Best Regards,

Brett Delle Grazie
-------------- next part --------------
A non-text attachment was scrubbed...
Name: add_custom_target-test.7z
Type: application/octet-stream
Size: 815 bytes
Desc: not available
URL: <http://www.cmake.org/pipermail/cmake/attachments/20120912/d0d41906/attachment.obj>


More information about the CMake mailing list