0

I'm using Ubuntu Linux.

I've been trying to get the following cscope command to run when I run "make" from my project directory, so it recompiles cscope and gets updated name information when I make my project.

cscope -b -q -U -R

Per my research and a bit of reading, I should be able to get CMake to run a command when you do 'make' by using the add_custom_command function in CMakeLists.txt.

However, many attempts and variations of it, have not been successful. Is it possible to run this as I want it with add_custom_command?

Simply doing this doesn't seem to work:

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/cscope.in.out ${CMAKE_CURRENT_BINARY_DIR}/cscope.out ${CMAKE_CURRENT_BINARY_DIR}/cscope.po.out COMMAND cscope -b -q -U -R)

I've tried using the TARGET overload of add_custom_command as well, and making a custom target with a dependency on either ALL or the main output file of the project, but that doesn't do anything either.

Ideally this would run after the project has been built, if could tell me what I'm doing wrong or if this is at all the way to do this, I'd be grateful?

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • 1
    `add_custom_command` is useless until some **target** depends from the file which command produces. – Tsyvarev Sep 26 '16 at 20:20
  • Did you actually check the latest [`add_custom_command` documentation](https://cmake.org/cmake/help/latest/command/add_custom_command.html)? – Antonio Sep 27 '16 at 12:29

1 Answers1

3

I've tried using the TARGET overload of add_custom_command as well, and making a custom target with a dependency on either ALL or the main output file of the project, but that doesn't do anything either.

This seems to be the problem - when a CMake commands requires to be passed a target, they refer to the name of a target you've created previously by using any of add_executable, add_library or add_custom_target, which doesn't necessarily map to an actual artifact file generated by the command.

Here's my take on the problem, and it seems to generate the three cscope files in the build directory.

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(te)

add_executable(main main.cpp asdf.cpp)
add_custom_command(TARGET main POST_BUILD COMMAND cscope -b -q -U -R)

Note here that the name of the target here is whatever I've passed as the first argument to the add_executable command.

milleniumbug
  • 15,379
  • 3
  • 47
  • 71
  • This is the correct answer as long as `POST_BUILD` is acceptable. Otherwise, a target dependent on one of the `OUTPUT` file of `add_custom_command` will trigger the command before being built. – Antonio Sep 27 '16 at 12:38