Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cmake: How to have add_custom_command() run after all project files are built

Tags:

build

cmake

Using Cmake v3.8, I need my custom command to run only after my newly built .hex, .map, and .elf files are produced. However, the command is not truly running after all of the *.hex, *.map, and *.elf files are produced. Here is what I have:

    add_custom_command(
    POST_BUILD
    COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/performCrc32.py
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    OUTPUT  performCrc32.out
    COMMENT "Running CRC32 check..."
)
add_custom_target(
    performCrc32 ALL
    DEPENDS performCrc32.py
    performCrc32.out
) 

What am I missing, if anything?

like image 691
S_Balb Avatar asked May 27 '17 01:05

S_Balb


1 Answers

There is no way for add commands to be executed "after the build is entirely complete".

  1. You may add commands to be executed after specific target is built:

    add_custom_command(TARGET <kernel-target> POST_BUILD
        COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/performCrc32.py
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        COMMENT "Running CRC32 check..."
    )
    

This would add command to be executed after <kernel-target> and all its dependencies will be built. Note on the absence of OUTPUT option in this case.

This is preferrable way for post-build checks, as a check will be performed every time target is actually (re)built.

  1. You may bind your custom command to your custom target (as usual), and add dependencies for the target:

    add_custom_command(
        COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/performCrc32.py
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        OUTPUT  performCrc32.out
        COMMENT "Running CRC32 check..."
    )
    add_custom_target(
        performCrc32 ALL
        DEPENDS performCrc32.py
        performCrc32.out
    ) 
    
    add_dependencies(performCrc32 <hex-targets...> <map-targets> <elf-target>)
    

    In this case command will be executed after all dependent targets are built. However, the command will be executed only first build: once OUTPUT file will be created, the command won't be executed again.


POST_BUILD option for add_custom_command is applicable only for TARGET flow of this command, as described above. I am curious why CMake doesn't emit error for your case, when you use POST_BUILD without TARGET.

like image 108
Tsyvarev Avatar answered Oct 16 '22 12:10

Tsyvarev