如何在制作过程中每次检查git标签?

发布于 2025-01-30 02:05:03 字数 749 浏览 1 评论 0原文

我目前正在将CMAKE用于一个项目。 我想在制作项目时检查git标签,如果git标签不满足某种格式,则制作过程将失败。

这是我在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()

问题是每次致电Cmake时才执行支票,但是我希望每次致电时执行检查。

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花浅忆 2025-02-06 02:05:03

答案最初来自:如何始终运行构建时命令不管有任何依赖性吗?

可以使用以下代码:

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
)

对于某些说明,此处的关键是文件$ {cmake_current_binary_dir}/__header.h永远不应该存在。

由于它不存在,并且自定义目标取决于它,因此它将在some_script中触发add_custom_command尝试生成缺失的文件。

使该some_script从未真正生成$ {cmake_current_binary_dir}/__ He​​ader.h。只要文件不存在,add_custom_target将始终触发some_script

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文