microsoft / vscode-cmake-tools

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

[Feature] Add option to specify `cmake.buildDirectory` for single- and multi-config generators, respectively #2426

Open hwhsu1231 opened 2 years ago

hwhsu1231 commented 2 years ago

Problem Description

In CMake Generator, there are single- and multi-config generator to choose. If we use single-config, such as MinGW Makefiles, I would like to separate build directories for Configurations and Architectures. Thanks to the Substitution Variable provided by CMake-Tools, we can design our custom build directory.

1. Single-Config Generator

If I use single-config Generator, for example MinGW Makefiles or NMake Makefiles, one Makefile file only supports one configuration. So in order to differentiate Debug and Release configurations, I will set cmake.buildDirectory like this:

{
  "cmake.buildDirectory": 
    "${workspaceFolder}/build/${buildKitTargetOs}-${buildKitVendor}-${buildKitTargetArch}-${buildType}"
}

The possible results of build directories will look like this:

build/
    win32-MSVC-x64-Debug/
    win32-MSVC-x64-Release/
    win32-MSVC-x86-Debug/
    win32-MSVC-x86-Release/
    win32-GCC-x86-Debug/
    win32-GCC-x86-Release/
    win32-GCC-x86-MinSizeRel/

2. Multi-Config Generator

However, if I choose to use multi-config generators such as Ninja Multi-Config, it is a waste to separate different configurations into individual sub-folders. So I will delete ${buildType} variable from the single-config format. Here is my format of cmake.buildDirectory:

{
  "cmake.buildDirectory": 
    "${workspaceFolder}/build/${buildKitTargetOs}-${buildKitVendor}-${buildKitTargetArch}"
}

The possible results of build directories will look like:

build/
    win32-MSVC-x64/
    win32-MSVC-x86/
    win32-GCC-x64/
    win32-GCC-x86/

Expected Setting

I hope CMake-Tools can give us an option to set cmake.buildDirectory for single-config and multi-config, respectively. And Then CMake-Tools will auto-detect which type our currently-used generator belongs to, single-config or multi-config.

For example:

{
  "cmake.buildDirectory": {
    "singleConfig": "${workspaceFolder}/build",
    "multiConfig": "${workspaceFolder}/build",
  }
}

Of course, we can still retain the original format of cmake.buildDirectory. That means both single- and multi-config will use the same format of cmake.buildDirectory:

{
  "cmake.buildDirectory": "${workspaceFolder}/build/${buildKitHostOs}_${buildKitTargetOs}"
}

In my case, I would like to set this settings like this:

{
  "cmake.buildDirectory": {
    "singleConfig": 
      "${workspaceFolder}/build/${buildKitTargetOs}-${buildKitVendor}-${buildKitTargetArch}-${buildType}",
    "multiConfig": 
      "${workspaceFolder}/build/${buildKitTargetOs}-${buildKitVendor}-${buildKitTargetArch}"
  }
}

Platform and Version:

bobbrow commented 2 years ago

Thank you for the feature suggestion. We may not be able to get to it immediately, but we would accept a PR if someone from the community would like to work on this and contribute. I think the settings change as proposed is fine. Acceptable values for cmake.buildDirectory can be a string or that new object with two properties.

AkhilTThomas commented 1 year ago

Just putting it out there, what i was looking for and did not find directly online.

This creates build artifacts in the current active folder


"cmake.sourceDirectory": [
        "${workspaceFolder}/cpp/src/project1",
        "${workspaceFolder}/cpp/src/project2"
    ],
    "cmake.buildDirectory": "${command:cmake.activeFolderPath}/build",