2

In my CMakeLists.txt, I use add_custom_command in conjunction with add_custom_target to call a code generation step.

If the code generation step succeeds, but one of the files mentioned in the OUTPUT argument to add_custom_command is missing, make is removing the first file mentioned in OUTPUT, for reasons outlined in Why does GNU make delete a file

(The first OUTPUT file because that is the "main" file that is associated with the command in the generated Makefile)

So far, so good. The issue I have is that this does not fail the build immediately. Instead, when the generated code is actually being used in the compilation, the compiler complains about a missing header, which ironically is not the file that the generator did not generate, but the file make removed.

The message explaining that the file was removed also only appears when make is run with VERBOSE=1, leaving me in the dark for way too long.

Deleting primary custom command output "/app/tests/eclipseProject/projects/math/build/aragen/matrix2Component/matrix2/includes/serviceInterfaces/matrix/proxy_Mat_SI.h" because another output "/app/tests/eclipseProject/projects/math/build/aragen/matrix2Component/matrix2/vsomeip/serviceInterfaces/matrix/proxy_vsomeip_Mat_SI.h" does not exist.

How can I make sure the build fails when trying to build the generation target, before compilation is executed?

kutschkem
  • 7,826
  • 3
  • 21
  • 56

1 Answers1

0

If you only need to support POSIX platforms (e.g. Linux, Mac OS, Windows with Make toolchain) then you could add an explicit check to your add_custom_command:

set(output_file_check "test" "whether expected files were generated:")
foreach(output_file IN LISTS output_files)
    list(APPEND output_file_check "-a" "-e" "${output_file}")
endforeach()

add_custom_command(
    OUTPUT ${output_files}
    COMMAND "your_generator"
    COMMAND "${CMAKE_COMMAND}" -E echo "Checking whether all expected files have been generated..."
    COMMAND ${output_file_check}
    COMMAND "${CMAKE_COMMAND}" -E echo "Success: All expected files are present."
    VERBATIM
    )

But this is not 100% portable. It would probably be better if the add_custom_command had an option to check the output files.

NEOatNHNG
  • 904
  • 6
  • 9