microsoft / vscode-cmake-tools

CMake integration in Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=vector-of-bool.cmake-tools
MIT License
1.43k stars 430 forks source link

CMakePresets.json misbehaves, always wants to build a debug build #3721

Closed Croydon closed 2 months ago

Croydon commented 2 months ago

Brief Issue Summary

I have create a CMakePresets.json for a project, which can be found here: https://github.com/Croydon/enet/commit/538f4ec28c0ebca8bf9c23996d7d9173d17ffb89

{
    "version": 3,
    "cmakeMinimumRequired": {
        "major": 3,
        "minor": 21,
        "patch": 0
    },
    "configurePresets": [
        {
            "name": "release-shared",
            "displayName": "Release-Shared",
            "description": "Release Shared build with default CMake Generator and architecture",
            "binaryDir": "${sourceDir}/build/${presetName}",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release",
                "BUILD_SHARED_LIBS": "ON"
            }
        },    
        {
            "name": "release-static",
            "displayName": "Release-Static",
            "description": "Release Static with default CMake Generator and architecture",
            "binaryDir": "${sourceDir}/build/${presetName}",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release",
                "BUILD_SHARED_LIBS": "OFF"
            }
        },
        {
            "name": "debug-shared",
            "displayName": "Debug-Shared",
            "description": "Debug Shared with default CMake Generator and architecture",
            "binaryDir": "${sourceDir}/build/${presetName}",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "BUILD_SHARED_LIBS": "ON"
            }
        },
        {
            "name": "debug-static",
            "displayName": "Debug-Static",
            "description": "Debug Static with default CMake Generator and architecture",
            "binaryDir": "${sourceDir}/build/${presetName}",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "BUILD_SHARED_LIBS": "OFF"
            }
        }
    ],
    "buildPresets": [
        {
            "name": "release-shared",
            "displayName": "Release-Shared",
            "description": "Release Shared build with default CMake Generator and architecture",
            "configurePreset": "release-shared"
        },
        {
            "name": "release-static",
            "displayName": "Release-Static",
            "description": "Release Static with default CMake Generator and architecture",
            "configurePreset": "release-static"
        },
        {
            "name": "debug-shared",
            "displayName": "Debug-Shared",
            "description": "Debug Shared with default CMake Generator and architecture",
            "configurePreset": "debug-shared"
        },
        {
            "name": "debug-static",
            "displayName": "Debug-Static",
            "description": "Debug Static with default CMake Generator and architecture",
            "configurePreset": "debug-static"
        }
    ]
}

With Visual Studio 2022, this Presets file does exactly what I am expecting from it. All four targets build what I want.

However, in Visual Studio Code (latest stable version with latest stable version of CMake Tools) it misbehaves, hence I am assuming that it might be a bug in the CMake Tools for Code.

When I select one of the release- targets and build it in Code, I will always get a debug build (again; works correctly in VS).

It also sets an install path of ${sourceDir}/build/${presetName}/Debug as some obvious indicator that it actually performs a debug build.

For release-shared target:

[main] Building folder: enet 
[build] Starting build
[proc] Executing command: C:\Users\Micha\AppData\Local\Programs\Python\Python312\Scripts\cmake.EXE --build E:/git/enet/build/release-shared
[build] MSBuild version 17.9.8+b34f75857 for .NET Framework
[build] 
[build]   1>Checking Build System
[build]   Building Custom Rule E:/git/enet/CMakeLists.txt
[build]   enet.vcxproj -> E:\git\enet\build\release-shared\Debug\enet.dll
[build]   Building Custom Rule E:/git/enet/CMakeLists.txt
[driver] Build completed: 00:00:02.973
[build] Build finished with exit code 0

For debug-shared target:

[main] Building folder: enet 
[build] Starting build
[proc] Executing command: C:\Users\Micha\AppData\Local\Programs\Python\Python312\Scripts\cmake.EXE --build E:/git/enet/build/debug-shared
[build] MSBuild version 17.9.8+b34f75857 for .NET Framework
[build] 
[build]   1>Checking Build System
[build]   Building Custom Rule E:/git/enet/CMakeLists.txt
[build]   callbacks.c
[build]   compress.c
[build]   host.c
[build]   list.c
[build]   packet.c
[build]   peer.c
[build]   protocol.c
[build]   unix.c
[build]   win32.c
[build]   Generating Code...
[build] E:\git\enet\win32.c(94,17): warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings [E:\git\enet\build\debug-shared\enet.vcxproj]
[build] E:\git\enet\win32.c(128,17): warning C4996: 'gethostbyaddr': Use getnameinfo() or GetNameInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings [E:\git\enet\build\debug-shared\enet.vcxproj]
[build]      Creating library E:/git/enet/build/debug-shared/Debug/enet.lib and object E:/git/enet/build/debug-shared/Debug/enet.exp
[build]   enet.vcxproj -> E:\git\enet\build\debug-shared\Debug\enet.dll
[build]   Building Custom Rule E:/git/enet/CMakeLists.txt
[driver] Build completed: 00:00:06.133
[build] Build finished with exit code 0

CMake Tools Diagnostics

{
  "os": "win32",
  "vscodeVersion": "1.88.1",
  "cmtVersion": "1.17.17",
  "configurations": [
    {
      "folder": "e:\\git\\enet",
      "cmakeVersion": "3.29.2",
      "configured": true,
      "generator": "Visual Studio 17 2022",
      "usesPresets": true,
      "compilers": {}
    }
  ],
  "cpptoolsIntegration": {
    "isReady": true,
    "hasCodeModel": true,
    "activeBuildType": "Debug",
    "buildTypesSeen": [
      "Debug",
      "Release",
      "MinSizeRel",
      "RelWithDebInfo"
    ],
    "requests": [
      "file:///e%3A/git/enet/callbacks.c",
      "file:///e%3A/git/enet/include/enet/win32.h"
    ],
    "responses": [
      {
        "uri": "file:///e%3A/git/enet/callbacks.c",
        "configuration": {
          "includePath": [
            "e:/git/enet/include"
          ],
          "defines": [
            "ENET_BUILDING_LIB",
            "ENET_DLL=1",
            "enet_EXPORTS"
          ],
          "compilerPath": "c:/program files/microsoft visual studio/2022/community/vc/tools/msvc/14.39.33519/bin/hostx64/x64/cl.exe",
          "compilerArgs": [],
          "compilerFragments": [
            "/DWIN32 /D_WINDOWS /Ob0 /Od /RTC1 -MDd -Zi   -W3"
          ]
        }
      },
      {
        "uri": "file:///e%3A/git/enet/include/enet/win32.h",
        "configuration": {
          "includePath": [
            "e:/git/enet/include"
          ],
          "defines": [
            "ENET_BUILDING_LIB",
            "ENET_DLL=1",
            "enet_EXPORTS"
          ],
          "compilerPath": "c:/program files/microsoft visual studio/2022/community/vc/tools/msvc/14.39.33519/bin/hostx64/x64/cl.exe",
          "compilerArgs": [],
          "compilerFragments": [
            "/DWIN32 /D_WINDOWS /Ob0 /Od /RTC1 -MDd -Zi   -W3"
          ]
        }
      }
    ],
    "partialMatches": [],
    "targetCount": 3,
    "executablesCount": 0,
    "librariesCount": 1,
    "targets": [
      {
        "name": "ALL_BUILD",
        "type": "UTILITY"
      },
      {
        "name": "ZERO_CHECK",
        "type": "UTILITY"
      },
      {
        "name": "enet",
        "type": "SHARED_LIBRARY"
      }
    ]
  },
  "settings": [
    {
      "communicationMode": "automatic",
      "useCMakePresets": "auto",
      "configureOnOpen": false
    }
  ]
}

Debug Log

Does not seem to contain more relevant data as the logs above and would expose too much personal information. If there is anything there in particular that might be relevant I can pick it out.

I have made a quick search through it with the keywords `debug`, `release` `build_type` and nothing interesting showed up.

Additional Information

Please let me know If I can provide any more useful information for that.

Thank you!

PS: On a second look, does this extension collide with another? I have The C/C++ Extension Pack (ms-vscode.cpptools-extension-pack) installed. And I see in the diagnostic data above that cpptoolsIntegration has "activeBuildType": "Debug", but where is this coming from? I can't find such setting anywere.

Evelyn-001 commented 2 months ago

@Croydon, could you please add property "configuration": "release" in buildPresets for release-shared/release-static configuration and verify your issue again? We find that select one of the release- targets and build it in Code, we could get a release build, please see the below video. Thanks. ENV: VS code version: 1.88.1 CMake Extension version: v 1.17.17 image

video0426

Croydon commented 2 months ago

Thank you @Evelyn-001!

Adding "configuration": "release" works for VS Code and continues to work for VS.

I am assuming that this difference between VS Code and VS is, that VS prefers the Ninja CMake generator which is single-configuration, while VS Code apparently(?) has no own preferences and uses whatever CMake itself prefers, which is the VS generator on Windows and the VS generator is a multi-generator. So specifying configuration again selects the desired one then.

It is still annoying that "CMAKE_BUILD_TYPE": "Release" from the configuration step is not respected automatically, but I am not sure which component would need to change for that. CMake itself, this extension here, ...?

Evelyn-001 commented 2 months ago

@Croydon, we are glad to be hearing your issue has been fixed. This is by design based on CMake upstream commandline. In multi-config scenarios one must define a Build Preset in which they define the configuration field. This matches the command-line cmake behavior where if you simply run cmake --build it will build the debug configuration, you must run cmake --build --config in order to build the correct configuration. This is done in VS by defining a build preset and defining configuration. please refer to this VS feedback ticket. Thanks for your support.