cmakecmake-custom-command

cmake add_custom_command is aways out of date


I have the problem that add_custom_command is always out of date and therefore runs on every build. the custom command runs a tool that is a target of the same project to generate a file that is used by another target:

add_executable(GeneratorTool main.cpp)

add_custom_command(
    OUTPUT generated.h
    COMMAND GeneratorTool
    DEPENDS main.cpp
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMENT "** GeneratorTool **"
)

add_library(MyLib STATIC generated.h ...)

In the build output (visual studio 2010) I always see ** GeneratorTool **. I would expect that it does not build again once generated.h exists and is newer than main.cpp. Any ideas?

Thanks, Jochen


Solution

  • First of all, you can put DEPENDS on GeneratorTool in your add_custom_command instead of main.cpp . GeneratorTool already depends main.cpp .

    Then most likely it is the location of generated.h which is ambiguous which forces the rebuilt of generated.h.

    Make sure that MyLib looks for the generated.h in the right place.

    My blind guess is to try:

    add_library(MyLib STATIC ${CMAKE_CURRENT_SOURCE_DIR}/generated.h ...)