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

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

问题描述

我想运行一个分析整个源代码树的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$ 

如您所见,即使先前触摸过file2,在第三次运行时它也不会生成file1。有时每隔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,即只需复制粘贴post-build命令,它就可以可靠地工作。也许它每运行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 ;-)

推荐答案

虽然我一点也不

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

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.

请参见使用示例

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

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