CmakePresets使用

在vscode下进行交叉编译时,使用cmake套件,很多配置依赖于vscode的插件,插件将配置参数以命令行的形式传递给cmake命令。不方便命令行使用。

配置文件

cmake可识别项目根目录下的CMakePresets.json and CMakeUserPresets.json文件。其中CMakePresets.json会引用CMakeUserPresets.json文件,类似于C语言中的include用法。一般CMakePresets.json用于通用配置,CMakeUserPresets.json用于特殊配置。

配置语法

可配置的presets有:

示例配置(cmake文档中的示例):

{
  "version": 6,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 23,
    "patch": 0
  },
  "include": [
    "otherThings.json",
    "moreThings.json"
  ],
  "configurePresets": [
    {
      "name": "default",
      "displayName": "Default Config",
      "description": "Default build using Ninja generator",
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/build/default",
      "cacheVariables": {
        "FIRST_CACHE_VARIABLE": {
          "type": "BOOL",
          "value": "OFF"
        },
        "SECOND_CACHE_VARIABLE": "ON"
      },
      "environment": {
        "MY_ENVIRONMENT_VARIABLE": "Test",
        "PATH": "$env{HOME}/ninja/bin:$penv{PATH}"
      },
      "vendor": {
        "example.com/ExampleIDE/1.0": {
          "autoFormat": true
        }
      }
    },
    {
      "name": "ninja-multi",
      "inherits": "default",
      "displayName": "Ninja Multi-Config",
      "description": "Default build using Ninja Multi-Config generator",
      "generator": "Ninja Multi-Config"
    },
    {
      "name": "windows-only",
      "inherits": "default",
      "displayName": "Windows-only configuration",
      "description": "This build is only available on Windows",
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Windows"
      }
    }
  ],
  "buildPresets": [
    {
      "name": "default",
      "configurePreset": "default"
    }
  ],
  "testPresets": [
    {
      "name": "default",
      "configurePreset": "default",
      "output": {"outputOnFailure": true},
      "execution": {"noTestsAction": "error", "stopOnFailure": true}
    }
  ],
  "packagePresets": [
    {
      "name": "default",
      "configurePreset": "default",
      "generators": [
        "TGZ"
      ]
    }
  ],
  "workflowPresets": [
    {
      "name": "default",
      "steps": [
        {
          "type": "configure",
          "name": "default"
        },
        {
          "type": "build",
          "name": "default"
        },
        {
          "type": "test",
          "name": "default"
        },
        {
          "type": "package",
          "name": "default"
        }
      ]
    }
  ],
  "vendor": {
    "example.com/ExampleIDE/1.0": {
      "autoFormat": false
    }
  }
}

交叉编译项目中的配置:

{
    "version": 6,
    "cmakeMinimumRequired": {
        "major": 3,
        "minor": 23,
        "patch": 0
    },
    "configurePresets": [
        {
            "name": "cross_gcc_base",
            "hidden": true,
            "displayName": "cross_gcc",
            "binaryDir": "${sourceDir}/out/build/${presetName}",
            // "installDir": "${sourceDir}/out/install/${presetName}",
            "installDir": "${sourceDir}/../../win_nfs/zynq_app",
            "generator": "Ninja",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "CMAKE_EXPORT_COMPILE_COMMANDS": "TRUE",
                "CMAKE_TOOLCHAIN_FILE": "~/work/tools/cmake/cmake_toolchain/linaro-gcc7.cmake"
            }
        },
        {
            "name": "cross_gcc_Debug",
            "displayName": "cross_gcc_Debug",
            "inherits": "cross_gcc_base"
        },
        {
            "name": "cross_gcc_Release",
            "displayName": "cross_gcc_Release",
            "inherits": "cross_gcc_base",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release"
            }
        }
    ],
    "buildPresets": [
        {
            "name": "build_base",
            "hidden": true,
            "jobs": 8,
            "cleanFirst": true
        },
        {
            "name": "build_Release",
            "inherits": "build_base",
            "configurePreset": "cross_gcc_Release",
            "targets":["install", "all"]
        },
        // {
        //     "name": "build_install",
        //     "inherits": "build_base",
        //     "configurePreset": "cross_gcc_Release",
        //     "targets": "install"
        // },
        {
            "name": "build_Debug",
            "inherits": "build_base",
            "configurePreset": "cross_gcc_Debug",
            "targets":["install", "all"]
        }
    ],
    "workflowPresets": [
        {
            "name": "work_Release",
            "steps": [
                {
                    "type": "configure",
                    "name": "cross_gcc_Release"
                },
                {
                    "type": "build",
                    "name": "build_install"
                }
            ]
        },
        {
            "name": "work_Debug",
            "steps": [
                {
                    "type": "configure",
                    "name": "cross_gcc_Debug"
                },
                {
                    "type": "build",
                    "name": "build_all"
                }
            ]
        }
    ]
}

常用选项说明

命令行使用

存在的问题

  1. 目前vscode不识别workflowPresets,需要在settings.json文件中配置"cmake.allowUnsupportedPresetsVersions": true
  2. cmake生成的compile_commands.json在presets指定的binaryDir目录下,对于Clangd,要复制到根目录下。vscode要配置"cmake.copyCompileCommands": "${workspaceFolder}/compile_commands.json",

vscode下的cmake配置

"cmake.configureOnOpen": false,
"cmake.buildTask": false,
"cmake.clearOutputBeforeBuild": true,
"cmake.configureOnEdit": false,
// "cmake.exportCompileCommandsFile": true,
"cmake.allowCommentsInPresetsFile": true,
"cmake.allowUnsupportedPresetsVersions": true,
"cmake.copyCompileCommands": "${workspaceFolder}/out/build/compile_commands.json",
"cmake.useCMakePresets": "auto",