cmake总是运行命令,而不管任何依赖 [英] cmake always run command regardless of any dependency

查看:179
本文介绍了cmake总是运行命令,而不管任何依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

正如你可以看到的,在第三次运行它没有生成file1 ,即使file2以前被触摸过。有时它每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?

奇怪,但如果我添加 TWO 命令重新触摸file2,ie只需复制粘贴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 ;-)

推荐答案

很高兴与此解决方案,张贴,因为我偶然在这个页面,没有看到它提到。

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.

请参阅工作示例其中使用这个示例。

See a working example where this is used.

这篇关于cmake总是运行命令,而不管任何依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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