CMake:如何规范化路径?

时间:2017-12-20 16:14:17

标签: cmake

是否有一种强大的方法来规范化CMake中的路径?

示例:

# Let's assume that an environment variable MY_ROOT_DIR is set
# that points to some directory.
set(MYFILE "$ENV{MY_ROOT_DIR}/somefile.txt")
message(${MYFILE})
# This will result for example in 
# Win:         C:\path\to\my\root\dir/somefile.txt
# Unix based:  /path/to/my/root/dir/somefile.txt

在此示例中,在将其用作路径组件之前,需要对MY_ROOT_DIR(即用斜杠替换反斜杠)进行规范化。你会如何在CMake中做到这一点?

CMake(或工具链下方的工具)可能会处理带有混合分隔符(/\)的路径,或者可能不会。 CMake使用/作为标准分隔符。 CMake为具有错误路径分隔符\的路径生成的典型警告可能类似于:

CMake Warning (dev) at cmake_install.cmake:5 (set):
  Syntax error in cmake code at

    C:/path/to/my/root/build/cmake_install.cmake:5

  when parsing string

    C:\path\to\my\root/somefile.txt

  Invalid escape sequence \p

  Policy CMP0010 is not set: Bad variable reference syntax is an error.  Run
  "cmake --help-policy CMP0010" for policy details.  Use the cmake_policy
  command to set the policy and suppress this warning.

感谢你提供任何暗示!

1 个答案:

答案 0 :(得分:5)

您可以使用file(TO_CMAKE_PATH)命令。

  

{ "originalRequest": { "source": "google", "version": "2", "data": { "surface": { "capabilities": [ { "name": "actions.capability.AUDIO_OUTPUT" } ] }, "inputs": [ { "rawInputs": [ { "query": "Sample", "inputType": "VOICE" } ], "arguments": [ { "textValue": "1", "name": "is_health_check", "boolValue": true } ], "intent": "actions.intent.MAIN" } ], ... } 模式将原生TO_CMAKE_PATH转换为带有正斜杠(<path>)的cmake样式路径。输入可以是单个路径,也可以是系统搜索路径,如/。搜索路径将转换为由$ENV{PATH}个字符分隔的cmake样式列表。

以下是一个例子:

;