'How to check the git tag everytime during the make process? [duplicate]

I am currently using the CMake for a project. I would like to check the git tag when making the project, if the git tag does not satisfy a certain format, the making process shall fail.

Here is what I am doing in the CMakeList.txt

execute_process(
    COMMAND git describe --always --tags
    OUTPUT_VARIABLE git_tag
    OUTPUT_STRIP_TRAILING_WHITESPACE
    WORKING_DIRECTORY ${PX4_SOURCE_DIR}
    )

string(REPLACE "-" ";" git_tag_list ${git_tag})
list(GET git_tag_list 0 git_tag_short)
string(REPLACE "." ";" ver_list ${git_tag_short})
set(ver_check_fail_msg "The git tag must be in the format of X.X.XX (6 characters), where X are digits. The current is ${git_tag_short}.") 
list(LENGTH ver_list ver_len)
if (NOT "${ver_len}" STREQUAL "3")
    message(FATAL_ERROR "${ver_check_fail_msg}")
endif()

The problem is that the check is only executed every time I call cmake, but I would like the check to be executed every time I call make.

Any suggestions?



Solution 1:[1]

The answer is originally from: How to always run command when building regardless of any dependency?

One can use the following code:

add_custom_target(
    my_custom_target_that_always_runs ALL
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/__header.h
)

add_custom_command(
    OUTPUT
        ${CMAKE_CURRENT_BINARY_DIR}/__header.h  # fake! ensure we run!
        ${CMAKE_CURRENT_BINARY_DIR}/header.h    # real header, we write.
    # this command must generate: ${CMAKE_CURRENT_BINARY_DIR}/header.h
    COMMAND some_script
)

For some explanation, the key here is the file ${CMAKE_CURRENT_BINARY_DIR}/__header.h which should never exist.

Since it does not exist and the custom target depends on it, it will trigger the some_script in the add_custom_command to try generate the missing file.

Makesure that some_script never really generate the ${CMAKE_CURRENT_BINARY_DIR}/__header.h. As long as the file does not exist, the add_custom_target will always trigger the some_script.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nyaruko