无论任何依赖项如何在构建时始终运行命令? [英] How to always run command when building regardless of any dependency?

查看:13
本文介绍了无论任何依赖项如何在构建时始终运行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个解析整个源代码树的 cmake 命令,所以我无法在 cmake 的 add_custom_command/add_custom_target 命令中列出所有可能的依赖项.

I want to run a cmake command that parses the whole source tree, so I can't list all possible dependencies in cmake's add_custom_command/add_custom_target commands.

是否可以告诉 cmake 只在没有任何条件的情况下运行命令?我尝试了网上找到的所有解决方案(包括 SO),但他们都假设该命令依赖于少数已知文件是最新的.

Is it possible to tell cmake just to run a command without any conditions? I tried all solutions found on the net (including SO) but they all assume that the command is dependent on few known files being up to date.

我找到了一个解决方案,但它不能可靠地工作:

I found a solution but it does not work reliably:

cmake_minimum_required(VERSION 2.6)

project(main)

add_custom_command(
   OUTPUT file1
   COMMAND echo touching file1
   COMMAND touch file1
   DEPENDS file2)
add_custom_target(dep ALL DEPENDS file1 file2)

# this command re-touches file2 after dep target is "built"
# and thus forces its rebuild
ADD_CUSTOM_COMMAND(TARGET dep
          POST_BUILD
          COMMAND echo touching file2
          COMMAND touch file2
)

这是输出:

queen3@queen3-home:~/testlib$ make
[100%] Generating file1
touching file1
touching file2
[100%] Built target dep
queen3@queen3-home:~/testlib$ make
[100%] Generating file1
touching file1
touching file2
[100%] Built target dep
queen3@queen3-home:~/testlib$ make
touching file2
[100%] Built target dep
queen3@queen3-home:~/testlib$ 

如您所见,在第三次运行时,它没有生成文件 1,即使之前接触过文件 2.有时每 2 次运行一次,有时每 3 次运行一次,有时每 4 次运行一次.这是一个错误吗?有没有另一种方法可以在不依赖 cmake 的情况下运行命令?

As you can see, on third run it did not generate file1, even though file2 was touched previously. Sometimes it happens every 2nd run, sometimes every 3rd, sometimes every 4th. Is it a bug? Is there another way to run a command without any dependency in cmake?

奇怪,但如果我添加两个命令来重新触摸 file2,即只是复制粘贴后构建命令,它可以可靠地工作.或者它可能每 1000 次运行就会失败,我还不确定;-)

Strange but if I add TWO commands to re-touch file2, i.e. just copy-paste the post-build command, it works reliably. Or maybe it will fail every 1000th run, I'm not sure yet ;-)

推荐答案

虽然我对这个解决方案并不满意,但由于我偶然发现了这个页面并且没有看到它被提及,所以发帖.

While I'm not at all pleased with this solution, posting since I stumbled on this page and didn't see it mentioned.

您可以添加引用丢失文件的自定义目标,

You can add a custom target that references a missing file,

例如:

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_command
)

这将继续运行自定义命令,因为未找到 __header.h.

This will keep running the custom command because __header.h is not found.

参见 working/a> 使用它的地方.

See a working example where this is used.

这篇关于无论任何依赖项如何在构建时始终运行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆