run a shell command (ctags) in cmake and make

kirill_igum picture kirill_igum · Mar 22, 2012 · Viewed 11.5k times · Source

I'm coding a c++ project in vim.

I'd like to run a ctags command (ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .) to generate references when I run make.

I think the way to do it is to use add_custom_command but I get confused on how to integrate it into CMakeLists.txt .

Answer

Daniel Blezek picture Daniel Blezek · Mar 23, 2012

The most basic way to do this is:

set_source_files_properties( tags PROPERTIES GENERATED true)
add_custom_command ( OUTPUT tags
    COMMAND ctags -R --c++-kinds=+p --fields=+iaS --extra=+q . 
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} )
add_executable ( MyProjectOutput tags )

The first line tells CMake that tags will be generated. The add_custom_command is CMake will generate tags when needed, and finally, some target needs to depend on tags. The default working directory is in the build tree, so WORKING_DIRECTORY must be set to your source tree. This is equivalent a Makefile entry:

tags:
    ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .

MyProjectOutput: tags
    # Whatever here...