CmakePresets使用
在vscode下进行交叉编译时,使用cmake套件,很多配置依赖于vscode的插件,插件将配置参数以命令行的形式传递给cmake命令。不方便命令行使用。
配置文件
cmake可识别项目根目录下的CMakePresets.json
and CMakeUserPresets.json
文件。其中CMakePresets.json
会引用CMakeUserPresets.json
文件,类似于C语言中的include用法。一般CMakePresets.json
用于通用配置,CMakeUserPresets.json
用于特殊配置。
配置语法
可配置的presets有:
- configurePresets:用于cmake --preset configurePreset_name
- buildPresets:用于cmake --build --preset buildPreset_name命令
- testPresets: 用于ctest --preset testPresets_name
- packagePresets:用于cpack --preset packagePresets_name
- workflowPresets:用于一键式运行cmake --workflow --preset workflowPresets_name
示例配置(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"
}
]
}
]
}
常用选项说明
"hidden": true
表示隐藏,在命令--list-presets
中不会显示"inherits": "cross_gcc_base"
: 表示继承配置,新配置会覆盖父配置"configurePreset": "cross_gcc_Release",
:buildPreset中的configurePreset表示对应的configurePreset,只有在configure时使用对应的preset才能在build时使用。如在configure时使用cross_gcc_Debug,则在build时只能使用build_all,不能使用build_install
命令行使用
cmake --list-presets
:列出configure presetscmake --preset cross_gcc_Debug
:运行cross_gcc_Debug配置cmake --build --list-presets
:列出build presetscmake --build --presets build_all
:编译build_all配置cmake --workflow --list-presets
:列出所有的workflow presetscmake --workflow --preset work_Debug
:运行work_Debugcmake --workflow --fresh --preset work_Debug
:移除cache全新config并编译
存在的问题
- 目前vscode不识别workflowPresets,需要在
settings.json
文件中配置"cmake.allowUnsupportedPresetsVersions": true
- 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",