3

我认为它应该是非常微不足道的,但似乎不受支持......在 CMake 中甚至有可能让列表的一个值包含分号吗?

原因非常简单——因为我在 Windows 上运行,而在 Windows 上,分号是一个环境变量(即PATH)中两个或多个文件夹之间的分隔符。

    list(
      APPEND
      MY_TEST_ENVIRONMENT
      "MY_FLAG=1"
    )

    # <...>

    list(
      APPEND
      MY_TEST_ENVIRONMENT
      "PATH=first_folder_path;second_folder_path"
    #                        ^--- here is the problem
    )

    # <...>


   set_property(TEST MyTests PROPERTY ENVIRONMENT ${MY_TEST_ENVIRONMENT})

我尝试删除和添加双引号,我尝试转义\;,我尝试添加相同的环境变量两次 - 但这些都不起作用!

4

2 回答 2

5

通过用反斜杠转义分号,您走在了正确的轨道上。但是,在ENVIRONMENT测试属性中设置列表时,这不是列表将被使用(并由 CMake 解释)的最后一个位置。然后该MY_TEST_ENVIRONMENT列表用于填充CTestTestfile.cmake,稍后由 CTest 读取以设置您的测试环境。

简而言之,您需要更多的转义字符来将分号传播到测试环境。具体来说,使用双反斜杠\\额外的反斜杠以及\;转义的分号转义到列表中。这是 CMake 的转义字符文档供参考。总共应该使用三个反斜杠:

list(
  APPEND
  MY_TEST_ENVIRONMENT
  "MY_FLAG=1"
)

# <...>

list(
  APPEND
  MY_TEST_ENVIRONMENT
  "PATH=first_folder_path\\\;second_folder_path"
#                         ^--- Use three backslashes here
)

# <...>

set_property(TEST MyTests PROPERTY ENVIRONMENT ${MY_TEST_ENVIRONMENT})
于 2020-01-22T19:18:50.773 回答
0

Unfortunately lists in CMake internally are just strings separated with semicolons. If you do:

set(MyList
    ABC
    DEF;GHI
)

You're defining a list with three elements. If you peek the contents:

message(${MyList})
message("${MyList}")

You'll get ABCDEFGHI and ABC;DEF;GHI respectively, so no way to know how many elements you actually wanted to have in either case. Most of CMake commands, like add_executable or target_compile_definitions interpret passed arguments this way and you will not be able to pass them lists with elements containing semicolons.

Note, that semicolons are not removed when you put your variable in quotes (see example above), so if you're invoking some external command, like a powershell script, then the PATH variable should be passed correctly. But then you wouldn't use a list for your arguments list (because elements are semicolon-separater), but rather build your own space-separated string.

于 2020-01-22T16:29:01.813 回答