如何使用 CMake Tools 插件配置复杂的项目?

Posted

技术标签:

【中文标题】如何使用 CMake Tools 插件配置复杂的项目?【英文标题】:How to configure a complex project with CMake Tools plugin? 【发布时间】:2020-09-09 17:22:03 【问题描述】:

我正在尝试配置 vscode 以使用 CMake 工具插件开发 llvm/mlir 项目。 项目很大,需要将此配置传递给命令行:

git clone https://github.com/llvm/llvm-project.git
mkdir llvm-project/build
cd llvm-project/build
cmake -G Ninja ../llvm \
   -DLLVM_ENABLE_PROJECTS=mlir \
   -DLLVM_BUILD_EXAMPLES=ON \
   -DLLVM_TARGETS_TO_BUILD="X86;NVPTX;AMDGPU" \
   -DCMAKE_BUILD_TYPE=Release \
   -DLLVM_ENABLE_ASSERTIONS=ON \
   -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLLVM_ENABLE_LLD=ON \
   -DPYTHON_EXECUTABLE=python3

我尝试将此信息添加到.vscode/settings.json


    "cmake.sourceDirectory": "$workspaceFolder/llvm",
    "cmake.configureArgs": [
        "-DLLVM_ENABLE_PROJECTS=mlir",
        "-DLLVM_BUILD_EXAMPLES=ON",
        "-DLLVM_TARGETS_TO_BUILD='X86;NVPTX;AMDGPU'",
        "-DCMAKE_BUILD_TYPE=Release",
        "-DLLVM_ENABLE_ASSERTIONS=ON",
        "-DCMAKE_C_COMPILER=clang",
        "-DCMAKE_CXX_COMPILER=clang++",
        "-DLLVM_ENABLE_LLD=ON",
        "-DPYTHON_EXECUTABLE=python3",
    ]

但是配置不成功,好像没有检测到我的参数。

[main] Configuring folder: llvm-project 
[proc] Executing command: /usr/bin/cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=/bin/clang-10 -DCMAKE_CXX_COMPILER:FILEPATH=/bin/clang++-10 -H$HOME/Development/mlir/llvm-project/mlir -B$HOME/Development/mlir/llvm-project/build -G Ninja
[cmake] Not searching for unused variables given on the command line.
[cmake] CMake Warning (dev) in CMakeLists.txt:
[cmake]   No project() command is present.  The top-level CMakeLists.txt file must
[cmake]   contain a literal, direct call to the project() command.  Add a line of
[cmake]   code such as
[cmake] 
[cmake]     project(ProjectName)
[cmake] 
[cmake]   near the top of the file, but after cmake_minimum_required().
[cmake] 
[cmake]   CMake is pretending there is a "project(Project)" command on the first
[cmake]   line.
[cmake] This warning is for project developers.  Use -Wno-dev to suppress it.
[cmake] 
[cmake] -- The C compiler identification is Clang 10.0.0
[cmake] -- The CXX compiler identification is Clang 10.0.0
[cmake] -- Check for working C compiler: /bin/clang-10
[cmake] -- Check for working C compiler: /bin/clang-10 -- works
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Check for working CXX compiler: /bin/clang++-10
[cmake] -- Check for working CXX compiler: /bin/clang++-10 -- works
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] CMake Warning (dev) at CMakeLists.txt:26 (if):
[cmake]   Policy CMP0057 is not set: Support new IN_LIST if() operator.  Run "cmake
[cmake]   --help-policy CMP0057" for policy details.  Use the cmake_policy command to
[cmake]   set the policy and suppress this warning.
[cmake] 
[cmake]   IN_LIST will be interpreted as an operator when the policy is set to NEW.
[cmake]   Since the policy is not set the OLD behavior will be used.
[cmake] This warning is for project developers.  Use -Wno-dev to suppress it.
[cmake] 
[cmake] CMake Error at CMakeLists.txt:26 (if):
[cmake]   if given arguments:
[cmake] 
[cmake]     "NVPTX" "IN_LIST" "LLVM_TARGETS_TO_BUILD"
[cmake] 
[cmake]   Unknown arguments specified
[cmake] 
[cmake] 
[cmake] CMake Warning (dev) in CMakeLists.txt:
[cmake]   No cmake_minimum_required command is present.  A line of code such as
[cmake] 
[cmake]     cmake_minimum_required(VERSION 3.16)
[cmake] 
[cmake]   should be added at the top of the file.  The version specified may be lower
[cmake]   if you wish to support older CMake versions for this project.  For more
[cmake]   information run "cmake --help-policy CMP0000".
[cmake] This warning is for project developers.  Use -Wno-dev to suppress it.
[cmake] 
[cmake] -- Configuring incomplete, errors occurred!
[cmake] See also "$HOME/Development/mlir/llvm-project/build/CMakeFiles/CMakeOutput.log".

如何将配置行转换为 CMake Tools 插件可以理解的内容?

编辑:调整错误信息

EDIT2:

修改.vscode/settings.json使用cmake.configureSettings并没有解决问题:


    "cmake.sourceDirectory": "$workspaceFolder/llvm",
    "cmake.configureSettings": 
        "LLVM_ENABLE_PROJECTS" : "mlir",
        "LLVM_BUILD_EXAMPLES" : "ON",
        "LLVM_TARGETS_TO_BUILD" : "\"X86;NVPTX;AMDGPU\"",
        "CMAKE_BUILD_TYPE" : "Release",
        "LLVM_ENABLE_ASSERTIONS" : "ON",
        "CMAKE_C_COMPILER" : "clang",
        "CMAKE_CXX_COMPILER" : "clang++",
        "LLVM_ENABLE_LLD" : "ON",
        "PYTHON_EXECUTABLE" : "python3",
    

【问题讨论】:

此错误消息已在此站点上被询问 几次 次,您是否尝试过与这些相关的任何潜在解决方案?例如一个是here 刚刚找到此信息“警告总是更喜欢使用 cmake.configureSettings 或 CMake 变体。切勿使用此设置传递 -D 参数。”在此链接 (vector-of-bool.github.io/docs/vscode-cmake-tools/…)。但是我仍然不清楚如何使用这个功能。 @squareskittles 抱歉,我在第一次提交时粘贴了不正确的错误消息。我已更新错误消息以反映问题。 【参考方案1】:

我发现了问题/可能的错误。

似乎 vscode 和 CMake Tools 插件缓存了一些文件,并且即使您删除构建文件夹(包含 CMakeCache.txt),该项目也不会使用在 .vscode/settings.json 上修改的新配置参数.

所以,让我的项目正常运行。

    我将此信息添加到.vscode/seetings.json

    "cmake.sourceDirectory": "$workspaceFolder/llvm",
    "cmake.configureSettings": 
        "LLVM_ENABLE_PROJECTS" : "mlir",
        "LLVM_BUILD_EXAMPLES" : "ON",
        "LLVM_TARGETS_TO_BUILD" : "X86;NVPTX;AMDGPU",
        "CMAKE_BUILD_TYPE" : "Release",
        "LLVM_ENABLE_ASSERTIONS" : "ON",
        "CMAKE_C_COMPILER" : "clang",
        "CMAKE_CXX_COMPILER" : "clang++",
        "LLVM_ENABLE_LLD" : "ON",
        "PYTHON_EXECUTABLE" : "python3",
    

    关闭 vscode 已删除构建文件夹 打开 vscode 回答是:“您要配置项目 'llvm-project' 吗?”

并且配置成功。

现在您可以使用 vscode 选择目标。

不幸的是,每次打开 vscode 时,CMake 插件都会触发新配置,但最好在另一个问题/论坛中解决此问题。

【讨论】:

【参考方案2】:

.vscode/seetings.json 中,改为使用数组语法 来指定多个选项。例如,


    "cmake.sourceDirectory": "$workspaceFolder/llvm",
    "cmake.configureSettings": 
        //...
        "LLVM_TARGETS_TO_BUILD" : ["X86", "NVPTX", "AMDGPU"],
        //...
    

【讨论】:

以上是关于如何使用 CMake Tools 插件配置复杂的项目?的主要内容,如果未能解决你的问题,请参考以下文章

[Tools] [cmake] 调试笔记 --- 如何彻底清除cmake产生的缓存

[Tools] [cmake] 调试笔记 --- 如何彻底清除cmake产生的缓存

Vscode 在ubuntu下 cmake-tools提示有误

使用CMake构建复杂工程

如何让vscode中的cmakelist高亮显示

C++搭建集群聊天室:工程统帅神器 Cmake,我爱死它了