首页 > 解决方案 > Linux cmake在文件(GLOB)命令中停留了几个小时

问题描述

当我运行 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 Linux 上使用较旧的 cmake 版本运行:

$ 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 版本中是否存在错误?我搜索了谷歌,没有找到关于这个“错误”的描述。但它在旧/新版本之间的行为确实不同。

标签: linuxcmakebitbake

解决方案


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

bash: syntax error near unexpected token `('

因此,在旧版本的 CMake 上,该file()命令将永远不会返回,因为它用于bash执行特定操作。

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

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()

推荐阅读