如果某个布尔条件为真(例如ENABLE_TESTING),那么我想运行如下:
COMMAND ./b2 --with-filesystem --with-program_options --with-test
Run Code Online (Sandbox Code Playgroud)
如果布尔条件为假,那么我想要:
COMMAND ""
Run Code Online (Sandbox Code Playgroud)
看起来我可以通过这个非常丑陋的代码实现这一点:
COMMAND $<$<BOOL:${ENABLE_TESTING}>:./b2> $<$<BOOL:${ENABLE_TESTING}>:--with-filesystem> $<$<BOOL:${ENABLE_TESTING}>:--with-program_options> $<$<BOOL:${ENABLE_TESTING}>:--with-test>
Run Code Online (Sandbox Code Playgroud)
但正如你所看到的,我必须一遍又一遍地重复布尔测试.如果我天真地尝试将它们组合成一个表达式:
COMMAND $<$<BOOL:${ENABLE_TESTING}>:./b2 --with-filesystem --with-program_options --with-test>
Run Code Online (Sandbox Code Playgroud)
然后我得到了错误/bin/sh: 1: $<1:./b2: not found.显然,在计算生成器表达式之前,会解析这些空格.所以我引用了整个事情:
COMMAND "$<$<BOOL:${ENABLE_TESTING}>:./b2 --with-filesystem --with-program_options --with-test>"
Run Code Online (Sandbox Code Playgroud)
但这会产生错误/bin/sh 1: /b2 --with-filesystem --with-program_options --with-test: not found.我想它将整个事物视为命令的名称而不是带有选项的命令.我怎样才能以干净利落的方式写出来呢?
编辑:我试图简化代码的最小例子,但我遗漏了一个额外的复杂性.这个命令发生在一个ExternalProject_Add部分,我需要依赖特殊的令牌,如<BINARY_DIR>.
COMMAND ./b2 "--stagedir=<BINARY_DIR>" --with-filesystem --with-program_options --with-test
Run Code Online (Sandbox Code Playgroud)
我无法将命令移动到其他地方的if语句中,因为如果我这样做,那么目录标记不会被扩展.
您可以在以下工具的帮助下完成此操作COMMAND_EXPAND_LISTS:
cmake_minimum_required(VERSION 3.11)
set(ENABLE_TESTING 1)
add_custom_target(
foo COMMAND "$<$<BOOL:${ENABLE_TESTING}>:./b2;--with-filesystem;--with-program_options;--with-test>"
COMMAND_EXPAND_LISTS)
Run Code Online (Sandbox Code Playgroud)
让我们检查一下它是否有效:
$ cmake .
$ VERBOSE=1 make foo
...
./b2 --with-filesystem --with-program_options --with-test
Built target foo
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
216 次 |
| 最近记录: |