本文介绍了cmake 3.15在SOMETIMES中将JOB_POOL添加到add_custom_command的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于使用cmake 3.15或更高版本并且还将Ninja用作生成器的用户,我想将新的JOB_POOL参数设置为一些较大的 add_custom_command()块.对于其他用户,我希望我的add_custom_command()保持相同(没有JOB_POOL).

For users that are using cmake 3.15 or later and are also using Ninja as a generator, I want to set the new JOB_POOL argument to some large add_custom_command() blocks. For other users, I want to keep my add_custom_command() the same (no JOB_POOL).

在前面的步骤中,我检查版本和生成器并设置$ {JOB_POOLS},并且还设置了一个变量,以便应使用池的用户可以看到(类似):

In earlier steps, I check the version and the generator and set ${JOB_POOLS} and I also set a variable such that users who should use a pool will see (something like):

出于历史原因,尽管@Tsyvarev,我还是将其留在这里指出这是我问题的根源!此处不需要双引号!

set(USE_POOL "JOB_POOL pool_A")

不使用池的用户将不会设置该变量.

Users that are not using a pool will not have that variable set.

现在如何在我的自定义命令中利用该变量...?

Now how to leverage that variable in my custom command...?

1.)生成器表达式不起作用,仅包含前一行的文本...

1.) Generator expressions don't work, just including the text with the previous line...

add_custom_command(
  ...
  $<USE_POOL>
  )

2.)我似乎不能简单地将变量放置在命令中,而只是在前一行中包含变量内容.例如,将$ {JOB_POOL}设置为字符串"JOB_POOL pool_A"时,此代码...

2.) I can't seem to simply place the variable in the command, again just including the variable contents on the previous line. For example, when ${JOB_POOL} is set to the string "JOB_POOL pool_A", this code...

出于历史原因,尽管@Tsyvarev,我还是将其留在这里指出这是我问题的根源!不要使用STRING!没有双引号!

add_custom_command(
  OUTPUT foo
  DEPENDS bar
  # Comment line here...
  ${USE_POOL}
  COMMAND
    ${CMAKE_COMMAND} -E ...
  )

给出此错误...

ninja: error: '../path/to/src/dir/JOB_POOL pool_A', needed by 'path/to/src/dir/foo', missing and no known rule to make it

它只是认为$ {JOB_POOL}字符串是另一个依赖项!

It simply considers the ${JOB_POOL} string to be another dependency!

3.)我不能使用add_custom_command()的"APPEND"功能.只是被忽略了...

3.) I can't use the "APPEND" feature of add_custom_command(). It's just ignored...

if (${USE_POOL})
  add_custom_command(
    ...
    APPEND
    JOB_POOL pool_A
    )
endif()

似乎唯一可行的方法是在我的周围加上一个如果"整个命令,因为我不想重复太多代码,这冒犯了我的敏感性...

The only thing that seems to work is to put an "if" around myentire command, which offends my sensibility since I don't like to duplicate so much code...

if(${USE_POOL})
  add_custom_command(
    ...many lines...
    JOB_POOL pool_A
    )
else()
  add_custom_command(
    ...many lines...
    )
endif()

您有更好的主意吗??

这是@tsyvarev的独立示例:

Here's a standalone example for @tsyvarev:

cmake_minimum_required(VERSION 3.15)
project foo

set_property(GLOBAL PROPERTY JOB_POOLS pool_A=2)
# For historical reasons, I leave this here, although @Tsyvarev
# points out that this is the source of my problem!
# Don't use a STRING!  No double-quotes!
set(USE_POOL "JOB_POOL pool_A")

add_custom_command(
  OUTPUT  foo.out
  DEPENDS foo.in
  ${USE_POOL}
  COMMAND
    ${CMAKE_COMMAND} -E copy foo.in foo.out
  COMMENT "Converting foo.in -> foo.out"
  VERBATIM
  )
add_custom_target(foo-out
  DEPENDS foo.out
  )
% cmake -GNinja .
% ninja foo-out
ninja: error: 'JOB_POOL pool_A', needed by 'foo.out', missing and no known rule to make it

它将字符串视为依赖项...如果将USE_POOL移至注释后,则将其视为注释的一部分...如果将其移至命令后,则将其视为命令的一部分...

It considers the string to be a dependency... If I move the USE_POOL to after the comment, it considers it part of the comment... If I move it to after the command, it considers it part of the command...

推荐答案

您的 JOB_POOL option 供用户选择.您可以为 add_custom_command 创建另一个包含相关参数序列的变量:

Your JOB_POOL option serves for the user's choice. You may create another variable which contains sequence of related parameters for add_custom_command:

if(JOB_POOL)
  set(JOB_POOL_PARAMS JOB_POOL pool_A) # Will add 'JOB_POOL pool_A' sequence of params
else()
  set(JOB_POOL_PARAMS) # Will add nothing
endif()

然后直接在 add_custom_command 调用中使用新变量:

Then use new variable directly in add_custom_command call:

add_custom_command(
  ...
  ${JOB_POOL_PARAMS} # Will expand to additional parameters when needed
)

这篇关于cmake 3.15在SOMETIMES中将JOB_POOL添加到add_custom_command的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:49