Linux cmake 在 file(GLOB) 命令中停留了几个小时

Linux cmake stuck in file(GLOB) command for several hours

当我 运行 bitbake 时,它​​卡在包的 do_configure 任务中。通过打印调试,发现卡在了 https://github.com/Kitware/CMake/blame/ae5f98a5e36da8cf3c75625ffb9a1d34aa2407cb/Modules/FindDoxygen.cmake,正是这一行:

file(GLOB _Doxygen_GRAPHVIZ_BIN_DIRS
  "$ENV{ProgramFiles}/Graphviz*/bin"
  "$ENV{ProgramFiles${_x86}}/Graphviz*/bin"
  )

然后我写了这个CMakeLists.txt文件:

set(_x86 "(x86)")
message(STATUS "Will run file GLOB...")
file(GLOB _Doxygen_GRAPHVIZ_BIN_DIRS
  "$ENV{ProgramFiles}/Graphviz*/bin"
  "$ENV{ProgramFiles${_x86}}/Graphviz*/bin"
  )
unset(_x86)
message(STATUS "file GLOB end")

运行 RedHat 上较旧的 cmake 版本 Linux:

$ cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 6.5 (Santiago)
$ cmake --version
cmake version 3.6.1
$ cmake .
-- The C compiler identification is GNU 4.4.7
-- The CXX compiler identification is GNU 4.4.7
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Will run file GLOB...
# stuck for several hours...

运行 Ubuntu 18.04.1 LTS 上较新的 cmake 版本:

$ cmake --version
cmake version 3.10.2
$ cmake .
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Will run file GLOB...
-- file GLOB end
...
-- Build files have been written to: xxx
# Finished in less than 1min.

那么 file(GLOB) 是旧版 cmake 中的错误吗?我搜索了 Google,没有找到关于这个 "bug" 的描述。但它在 older/newer 版本之间的行为确实不同。

当此代码在 Linux 上 运行 时,CMake 执行挂起,因为 file() 命令卡在路径 (x86) 中的括号中。当bash遇到括号,没有被单引号包围时,会抛出错误:

bash: syntax error near unexpected token `('

因此,在旧版本的 CMake 上,file() 命令永远不会 return,因为它使用 bash 来执行特定操作。

较新版本的 CMake 通过检查 Windows 围绕此 CMake 代码块避免了该问题。查看最新代码here:

if(WIN32)
    set(_x86 "(x86)")
    file(
        GLOB _Doxygen_GRAPHVIZ_BIN_DIRS
        "$ENV{ProgramFiles}/Graphviz*/bin"
        "$ENV{ProgramFiles${_x86}}/Graphviz*/bin"
    )
    unset(_x86)
else()
    set(_Doxygen_GRAPHVIZ_BIN_DIRS "")
endif()