1

I want all the files with a particular extension in CMake in a directory. These extension files are generated post running the build. I tried

file(GLOB_RECURSE GCOV_OBJECTS $ENV{OUTPUT_UT_DIR} *Test.cpp.o)

But this is getting executed before running the build. I want something like this

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                  COMMAND my_command_to_get_all_desired_files

Using file command inside add_custom_command is giving me a syntax error. Not sure how to proceed from here.

Jim Moriarty
  • 141
  • 1
  • 5
  • 12
  • `my_command_to_get_all_desired_files` "get" where? where do you want to "get" them? You want to _print_ them? Is this XY question? `Using file command` "file command inside add_custom_command"? – KamilCuk Jun 25 '21 at 09:15
  • Get files mean, I need all those files in a list. I need to execute a command on those files. like `gcov ${GCOV_OBJECTS}`. – Jim Moriarty Jun 25 '21 at 09:20

1 Answers1

2

Create a cmake script:

# mycmakescript.cmake
file(GLOB_RECURSE GCOV_OBJECTS $ENV{OUTPUT_UT_DIR} *Test.cpp.o)
message("${GCOV_OBJECTS}")
#  I need to execute a command on those files. like gcov ${GCOV_OBJECTS}
execute_process(COMMAND gvoc ${GCOV_OBJECTS})

and call it when building:

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
              COMMAND cmake -P mycmakescript.cmake)
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • can I make a function in .cmake file and call it to return this `${GCOV_OBJECTS}`? – Jim Moriarty Jun 25 '21 at 09:27
  • 2
    You seem to want to move back in time and set a variable? The order of events is: configure stage then build stage. You can't go back in time from build stage to set a variable at configure stage. `I need to execute a command on those files` Then you _do not need a variable_. Just execute the command, from inside `mycmakescript.cmake`. – KamilCuk Jun 25 '21 at 09:56