1

I have integrated a tool that generates some code from a text file in CMake based build environment.

This tool is also able to generate the dependency file in the GNU make format (pretty much like gcc -MD does it for c files).

I would like to include this dependency file in makefiles generated by CMake in order to correctly rebuild when necessary. Unfortunately, I could not find the correct way to do this.

I tried:

Has anyone had a similar issue?

Louis Caron
  • 1,043
  • 1
  • 11
  • 17
  • I added an answer to your first link. Go see if it works for you. – starball Sep 16 '22 at 04:10
  • If there is an existing question that would solve yours and just doesn't have a solution, don't create a new question. Wait for a solution to the existing one, or [put a bounty on it](https://stackoverflow.com/help/bounty). Is your question really different than [the existing one](https://stackoverflow.com/q/45169207/11107541)? If not. This should be closed as a duplicate. – starball Sep 16 '22 at 04:10

1 Answers1

1

try to define your file as dependency of your add_custom_target:

cmake_minimum_required(VERSION 3.0)
project(Custom_Command_TEST)
add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/your_gen_file.txt"
                   COMMAND /bin/date > "${CMAKE_BINARY_DIR}/your_gen_file.txt"
                   COMMAND /bin/echo "RUNNING COMMAND")

add_custom_target(GenerateFile
                  /bin/echo "RUNNING TARGET"
                  DEPENDS "${CMAKE_BINARY_DIR}/your_gen_file.txt")

add_executable(${PROJECT_NAME} main.cpp)
add_dependencies(${PROJECT_NAME} GenerateFile)

will only rebuild the target GenerateFile. You could put your .txt generation step as COMMAND in add_custom_command.

output of first make:

[ 33%] Generating your_gen_file.txt
RUNNING COMMAND
RUNNING TARGET
[ 33%] Built target GenerateFile
[ 66%] Building CXX object CMakeFiles/Custom_Command_TEST.dir/main.cpp.o
[100%] Linking CXX executable Custom_Command_TEST
[100%] Built target Custom_Command_TEST

output second make:

RUNNING TARGET
[ 33%] Built target GenerateFile
[100%] Built target Custom_Command_TEST

In case you want to rebuild everything again, you'll want to make clean.

You could also check this older post

dboy
  • 1,004
  • 2
  • 16
  • 24
  • In this case, I would rebuild everytime, regardless of the content of the file which indicates the dependencies – Louis Caron Apr 11 '20 at 08:21