不要扩展CMake列表变量 [英] Do not expand CMake list variable

查看:63
本文介绍了不要扩展CMake列表变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CMake脚本,该脚本通过 add_test()运行一些测试,该脚本在CMake 3.15中的Windows(Server 2008,不要问)下运行.调用这些测试时,它们在其运行的环境中的PYTHONPATH环境变量似乎已重置为环境默认值,并且不包含它需要的某些路径.

I have a CMake script that runs some tests via add_test(), running under Windows (Server 2008, don't ask) in CMake 3.15. When these tests are called, the PYTHONPATH environment variable in the environment they run in seems to get reset to the environment default, and doesn't contain some paths that it needs to.

因此,在运行测试时,我需要将PYTHONPATH设置为CMake运行时的$ ENV {PYTHONPATH}变量的值.它有许多用分号分隔的路径,因此CMake认为这是一个列表,并试图将其扩展为多个用空格分隔的字符串,显然,这些字符串的结尾很糟糕.

I therefore need to set PYTHONPATH when the tests are run to the value of the $ENV{PYTHONPATH} variable when CMake runs. This has a number of semicolon-separated paths, so CMake thinks it's a list and tries to expand it into a number of space-separated strings, which obviously ends badly.

我无法解决如何停止CMake这样做.从我所看到的所有内容中,您应该可以只用引号引起来:

I cannot work out how to stop CMake doing this. From everything I can see, you should be able to do just surround with quotes:

add_test(
  NAME mytest
  COMMAND cmake -E env PYTHONPATH="$ENV{PYTHONPATH}"
  run_test_here)

...但是它总是进行扩展.我也尝试使用set_tests_properties进行设置:

...but it always does the expansion. I also tried setting with set_tests_properties:

set_tests_properties(mytest PROPERTIES
    ENVIRONMENT PYTHONPATH="$ENV{PYTHONPATH}")

...但是这似乎根本没有做任何事情-测试时的PYTHONPATH并未改变.我以为是因为它是一个环境变量,但是通过 set()使用常规CMake变量没有什么区别,所以我做错了.请帮忙!

...but that didn't appear to do anything at all - PYTHONPATH at test time wasn't altered. I thought it was because it's an environment variable, but using a regular CMake variable via set() makes no difference, so I'm doing something wrong. Help please!

推荐答案

以下方法应该起作用:

COMMAND cmake -E env "PYTHONPATH=$ENV{PYTHONPATH}"

您需要引用命令行的全部内容,以使消息正确扩展.

You need to quote the full part of the command line, to make properly expanded message.

经过测试:

set(tmp "C:\\Python27\\Scripts;E:\\JenkinsMIDEBLD\\workspace\\...;...")
add_test(NAME MYtest1 COMMAND cmake -S . -E env "tmp=${tmp}") 
add_test(NAME MYtest2 COMMAND cmake -S . -E env tmp="${tmp}") 

运行 ctest 后,我得到:

1: Test command: /bin/cmake "-S" "." "-E" "env" "tmp=C:\Python27\Scripts;E:\JenkinsMIDEBLD\workspace\...;..."
2: Test command: /bin/cmake "-S" "." "-E" "env" "tmp="C:\Python27\Scripts" "E:\JenkinsMIDEBLD\workspace\..." "...""

第一个测试将正确的; 传递给了 var ,而第二个测试则通过了以空格分隔的列表.

The first test has proper ; passed to var, while the second one passes space separated list.

这是cmake解析带引号的参数.一个参数要么全引号要么根本不引号-部分引号被解释为文字".因此,假设:

This is how cmake parses quoted arguments. An argument is either fully quoted or not quoted at all - partial quotes are interpreted as a literal ". So assumnig that:

set(var a;b;c)

以下内容:

var="$var"

不是用引号引起来的参数,而"是按字面意义使用的;它将 $ var 列表扩展为空格分隔的列表,并且将" 保留,在 = a 之间有一个",最后还有一个" . var ="$ var" 等于:

Is not a quoted argument and " are taken literally! It expands the $var list into space separated list and the " stay, there is one " between = and a, and there is additional " on the end. The var="$var" is equal to:

var=\"a b c\"
    ^^     ^^    - the quotes stay!
^^^^^^^ ^ ^^^    - these are 3 arguments, the last one is `c"`

不带引号的是:

var=$var

等于(注意引号):

var=a c c

要用引号引起来,必须用引号将所有内容都引用起来,元素的第一个和最后一个字符为":

To quotes argument you have to quote it all, with first and last character of the element beeing ":

"var=$var"

将扩展为:

"var=a;b;c"

这篇关于不要扩展CMake列表变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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