Vscode-cmake-tools: Allow building in different directories per build type

Created on 6 May 2017  路  15Comments  路  Source: microsoft/vscode-cmake-tools

Currently, it is only possible to build in one directory (default to build) no matter which variant you choose. This results in unnecessary rebuilds when switching between debug and release, for example.

Recommend adding a configuration variable similar to the following:

```
cmake.buildDirectoryByVariant {
"Release" : "build",
"Debug" : "build_debug"
}
````

Any undefined variants would result in using the default value or the value set in cmake.buildDirectory.

question

Most helpful comment

OK, I just found out that this is already possible. Simply put ${buildType} in your cmake.buildDirectory path.

For example:

"cmake.buildDirectory" : "${workspaceRoot}/build/${buildType}"

All 15 comments

I'd like that too.

I need that too. Also, it would be great to have ability to have different CMake build "kits", each preconfigured with specific environment and default CMake parameters and then having separate build directory for each variant-kit combination. This is how QtCreator manages CMake-based projects (I am actually using it for everyday work, but since VS code appears to have more advanced editor, I am considering a switch, but so far QtCreator has much better CMake-based project build and debug support - maybe take some ideas from them?).

OK, I just found out that this is already possible. Simply put ${buildType} in your cmake.buildDirectory path.

For example:

"cmake.buildDirectory" : "${workspaceRoot}/build/${buildType}"

@DoDoENT Great!

Is it also possible to specify the generator and and the compiler/toolchain ?

@maddouri, you can specify generator with cmake.generator (but only Visual Studio, Ninja and makefiles are supported, Xcode is not), but compiler/toolchain cannot be specified at the time. This needs to be developed, hopefully in a manner where each "kit" can specify its own variants, environment, default cmake parameters, default launch procedure (which will enable, for example, deploying of c++ android apps on device with ADB and attaching the debugger to it), etc. It is a lot of work to do to catch up with some other IDE's, like QtCreator, Xcode or proper Visual Studio...

Awesome, that trick totally solves this use case. Perhaps this can be put in the readme since it seems popular. Closing the issue as I don't think adding a new variable is necessary.

Sorry to bump an older thread, but using this method, is there any way to get compile_commands to work? ${buildType} doesn't seem to apply to the c_cpp_properties.json file.

The c_cpp_properties.json file is controlled by the cpptools extension, not CMake Tools. It won't know about your build type.

Even then, if you are using CMake Tools, the compile_commands.json file shouldn't be necessary, as CMake Tools will give cpptools all the necessary compile information directly.

OK, I just found out that this is already possible. Simply put ${buildType} in your cmake.buildDirectory path.

For example:

"cmake.buildDirectory" : "${workspaceRoot}/build/${buildType}"

Sorry to comment old thread, but to me this trick does not seem to cover all cases.

How do you handle the case when you have multiple combinations of variants like the example here? How can I specify a different build directory for each combination? (Following the example: bar-debug, bar-release, baz-debug, baz-release, etc.)

@gentooise, I use this:

"cmake.buildDirectory": "/path/to/my/Builds/${workspaceRootFolderName}/${buildKit}/${buildType}",

in global settings.json and it works like charm.

I prefer having all my cmake builds folder in one single folder, excluded from backup, but you can also use something like this:

"cmake.buildDirectory": "${workspaceRoot}/build/${buildKit}/${buildType}",

@DoDoENT I know I can use build kit and build type, but unfortunately that doesn't solve my issue.

I try to explain it better. Consider the following cmake-variants.json:

{
    "buildOpt" : {
        "default": "debug",
        "description": "Build type option (Debug/Release)",
        "choices": {
            "debug": {
                "short": "Debug",
                "long": "Debug build (code not optimized and with debug info)",
                "buildType": "Debug"
            },
            "release": {
                "short": "Release",
                "long": "Release build (code optimized without debug info) including final deliverables",
                "buildType": "Release"
            }
        }
    },

    "buildTarget" : {
        "default": "foo",
        "description": "Build target option (app, boot, bootstrap, flasher)",
        "choices": {
            "foo": {
                "short": "Foo",
                "long": "Foo build target",
                "settings": {
                    "TARGET": "foo"
                }
            },
            "bar": {
                "short": "Bar",
                "long": "Bar build target",
                "settings": {
                    "TARGET": "bar"
                }
            }
        }
    }
}

What I need is to have different build directories for each variant combination (i.e. foo-debug, bar-debug, foo-release, bar-release).

Right now I'm forced to have multiple .code-workspace files, but that defeats the purpose of defining cmake-variants in the first place.

It would be nice to have a way to combine variants' names into the buildDirectory, e.g.:

"cmake.buildDirectory": "${workspaceFolder}/${buildTarget}-${buildOpt}"

where ${buildTarget} and ${buildOpt} refer to user-defined cmake variants (e.g. foo and debug respectively). The variables could simply refer to corresponding keys inside choices map.

NB: I named the variant buildOpt (and not buildType) on purpose, to avoid confusion with the existing ${buildType}.

Is there a way to achieve that with the current implementation of CMake Tools?

Aha, I see the problem. Unfortunately, I don't know the solution. I actually have a similar problem and didn't find a solution. I am currently deleting the build folder each time I need to switch between different variants of the same build type.

The problem is that I need all build folders to exist at the same time because some builds are complex and depend on other simpler builds, so I cannot delete build folders every time.

The only solution I found is to define multiple .code-workspace files with different build directories, but that defeats the purpose of having cmake-variants, because at this point I could simply define different builds for each workspace and abandon the cmake-variants feature (which was quite comfortable).

@gentooise, did you try ${variant:buildTarget}-${variant:buildOpt}?

@gentooise, did you try ${variant:buildTarget}-${variant:buildOpt}?

It works like a charm! That's great, thank you :)

Was this page helpful?
0 / 5 - 0 ratings