cmake文件(RELATIVE_PATH ...)用于不能相对的文件

时间:2016-12-14 19:05:58

标签: windows cmake

在Windows下,可能存在无法相对于彼此形成的目录,因为它们存在于不同的驱动器上。制作一个最小的CMake脚本,尝试这个:

cmake_minimum_required(VERSION 3.6)
set(FILEA "C:/Directory/MyFile")
set(FILEB "E:/AnotherDirectory/AnotherFile")
file(RELATIVE_PATH relativePath ${FILEA} ${FILEB})
message("relativePath = ${relativePath}")

制作(运行cmake .):

...
relativePath = E:/AnotherDirectory/AnotherFile
...

这显然不正确。有没有办法确定对file(RELATIVE_PATH ...)的调用是否“失败”?

1 个答案:

答案 0 :(得分:3)

我刚刚查看了CMake的源代码 - 在本例中为SystemTools::RelativePath() - 如果Windows上的驱动器号不相同,它只是按设计返回“远程”路径:

// If there is nothing in common at all then just return the full
// path.  This is the case only on windows when the paths have
// different drive letters.  On unix two full paths always at least
// have the root "/" in common so we will return a relative path
// that passes through the root directory.
if (sameCount == 0) {
  return remote;
}

因为在你的情况下没有抛出错误,你可以做一些比较输入和结果并自己抛出错误的东西:

if (NOT FILEA STREQUAL FILEB AND FILEB STREQUAL relativePath)
    message(FATAL_ERROR "... your text goes here ...")
endif()