首页 > 解决方案 > 如何使用 CMake Tools 插件配置复杂的项目?

问题描述

我正在尝试配置 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 插件可以理解的内容?

编辑:调整错误信息

编辑2:

修改.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",
    }
}

标签: visual-studio-codecmakevscode-settings

解决方案


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

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

推荐阅读