Open tengai650 opened 4 months ago
CMAKE_BUILD_TYPE
is not set by CMake when using a multi-config generator such as "Visual Studio 17 2022." This is because all build types will be generated in a single configuration pass. If you would like to select which build types are generated, you can use the CMAKE_CONFIGURATION_TYPES
variable. Otherwise, if you need for CMAKE_BUILD_TYPE
to exist during generation, you will need to switch to a single-config generator, such as Ninja.
Ok, but VC is only building a debug exe. Why wouldn't it be buidling all types? How do I get a release build?
And what about this CMAKE_BUILD_TYPE_INT being set to debug? What is its purpose and how is it set to "release"?
Ok, but VC is only building a debug exe. Why wouldn't it be buidling all types? How do I get a release build?
I'll need to set up a test case to double check, but to answer your question quickly, I believe you will need to add "buildPresets"
to your CMakePresets.json. When you use a multi-config generator but don't provide any build presets, I believe CMake just picks the first one in the CMAKE_CONFIGURATION_TYPES
variable when you try to build.
Starting with the information you shared earlier, it would like something like this (not tested, there could be a stray comma somewhere 😄)
{
"version": 3,
"configurePresets": [
{
"name": "vs",
"displayName": "VS Custom preset",
"description": "Sets Visual Studio generator, build and install directory",
"generator": "Visual Studio 17 2022",
"binaryDir": "${workspaceFolder}/build/${presetName}",
"architecture": {
"value": "x64",
"strategy": "set"
},
"cacheVariables": {
"BOOST_ROOT": "G:/dev/boost_1_85_0"
}
}
],
"buildPresets": [
{
"name": "vs_debug",
"configurePreset": "vs",
"configuration": "Debug"
},
{
"name": "vs_release",
"configurePreset": "vs",
"configuration": "Release"
}
]
}
And what about this CMAKE_BUILD_TYPE_INT being set to debug? What is its purpose and how is it set to "release"?
I don't see any documentation for it, so I suppose it is an internal variable and probably matches the first build type in the CMAKE_CONFIGURATION_TYPES
list. You could probably test out that theory by changing CMAKE_CONFIGURATION_TYPES
to something else. (e.g. Release;Debug;RelWithDebInfo;MinSizeRel
)
Ok. I think I'm starting to understand how "configurePresets" and "buildPresets" work.
I believe the understanding is: "many to one", many "buildPresets" to one "configurePresets". Originally, I thought it was "one to one".
For example here are Release/Debug "buildPresets" for different "configurePresets".
{
"version": 6,
"configurePresets": [
{
"name": "VS_x64",
"hidden": false,
"generator": "Visual Studio 17 2022",
"binaryDir": "${workspaceFolder}/build/${presetName}",
"description": "Visual build.",
"cacheVariables": {
"BOOST_ROOT": "G:/dev/boost/boost_1_85_0"
}
},
{
"name": "Ninja_x64",
"hidden": false,
"generator": "Ninja",
"binaryDir": "${workspaceFolder}/build/${presetName}",
"description": "Ninja build.",
"cacheVariables": {
"CMAKE_EXPORT_COMPILE_COMMANDS": "YES",
"BOOST_ROOT": "G:/dev/boost/boost_1_85_0"
}
}
],
"buildPresets": [
{
"name": "Release_VC",
"hidden": false,
"description": "",
"displayName": "",
"configuration": "Release",
"configurePreset": "VS_x64"
},
{
"name": "Debug_VC",
"hidden": false,
"description": "",
"displayName": "",
"configuration": "Debug",
"configurePreset": "VS_x64"
},
{
"name": "Release_Ninja",
"hidden": false,
"description": "",
"displayName": "",
"configuration": "Release",
"configurePreset": "Ninja_x64"
},
{
"name": "Debug_Ninja",
"hidden": false,
"description": "",
"displayName": "",
"configuration": "Debug",
"configurePreset": "Ninja_x64"
}
]
}
I believe the understanding is: "many to one", many "buildPresets" to one "configurePresets". Originally, I thought it was "one to one".
You're almost there. The one correction is that it is actually one to one for single-config generators like Ninja. So your presets file would look more like this:
{
"version": 6,
"configurePresets": [
{
"name": "VS_x64",
"hidden": false,
"generator": "Visual Studio 17 2022",
"binaryDir": "${workspaceFolder}/build/${presetName}",
"description": "Visual build.",
"cacheVariables": {
"BOOST_ROOT": "G:/dev/boost/boost_1_85_0"
}
},
{
"name": "Ninja_x64_Debug",
"hidden": false,
"generator": "Ninja",
"binaryDir": "${workspaceFolder}/build/${presetName}",
"description": "Ninja build.",
"cacheVariables": {
"CMAKE_EXPORT_COMPILE_COMMANDS": "YES",
"BOOST_ROOT": "G:/dev/boost/boost_1_85_0",
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "Ninja_x64_Release",
"hidden": false,
"generator": "Ninja",
"binaryDir": "${workspaceFolder}/build/${presetName}",
"description": "Ninja build.",
"cacheVariables": {
"CMAKE_EXPORT_COMPILE_COMMANDS": "YES",
"BOOST_ROOT": "G:/dev/boost/boost_1_85_0",
"CMAKE_BUILD_TYPE": "Release"
}
}
],
"buildPresets": [
{
"name": "Release_VC",
"hidden": false,
"description": "",
"displayName": "",
"configuration": "Release",
"configurePreset": "VS_x64"
},
{
"name": "Debug_VC",
"hidden": false,
"description": "",
"displayName": "",
"configuration": "Debug",
"configurePreset": "VS_x64"
}
]
}
It's somewhat confusing but an unfortunate artifact of the difference between single-config generators and multi-config generators.
Ok, now this makes sense. However, I need to urge Microsoft they need to do a better job at explaining how "buildPresets" and "configurePresets" work in the IDE.
For example, this needs to be better explained:
Unfortunately, the CMake documentation doesn't make these real-use-case very clear.
I was coming from Mac Visual Code and everything is working correctly. But this is because it uses "Unix makefiles" which is single-config generator and I was convinced the Visual Code and CMake on Windows was broken.
Thanks for the clarification!
Patrick
Ok, now this makes sense. However, I need to urge Microsoft they need to do a better job at explaining how "buildPresets" and "configurePresets" work in the IDE.
For example, this needs to be better explained: ... Unfortunately, the CMake documentation doesn't make these real-use-case very clear.
I've almost encountered the same issue as you, and these discussions have now helped me understand. I couldn't agree with you more; the documentation indeed lacks some crucial (and conspicuous) explanations.
@inkbottle-9 Thank you very much for your reply, our developers will continue to improve this document!
@inkbottle-9 @tengai650 Thanks for the feedback. We've made notes on our side, but to best help move this forward, could you also create an issue on the Kitware repository so that they can be made aware of this feedback as well? https://gitlab.kitware.com/cmake/cmake/-/issues
Brief Issue Summary
Target is Release by setting "CMAKE_BUILD_TYPE": "Release" in the CMakePresets.json file.
using the CMake Debugger I see inconsistent values set:
Output from build: [build] Building Custom Rule G:/dev/cmaketest/CMakeLists.txt [build] main.cpp [build] cmaketest.vcxproj -> G:\dev\cmaketest\bin\Debug\cmaketest.exe
CMake Tools Diagnostics
Debug Log
No response
Additional Information
configurePresets": [ { "name": "vs_release", "displayName": "VS Release Custom preset", "description": "Sets Visual Studio generator, build and install directory", "generator": "Visual Studio 17 2022", "binaryDir": "${workspaceFolder}/build/${presetName}", "architecture": { "value": "x64", "strategy": "external" }, "cacheVariables": { "BOOST_ROOT": "G:/dev/boost_1_85_0", "CMAKE_BUILD_TYPE": "Release" } } ]