Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow building in different directories per build type #151

Closed
droidmonkey opened this issue May 6, 2017 · 15 comments
Closed

Allow building in different directories per build type #151

droidmonkey opened this issue May 6, 2017 · 15 comments
Labels

Comments

@droidmonkey
Copy link

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.

@s-martin
Copy link
Contributor

s-martin commented May 6, 2017

I'd like that too.

@DoDoENT
Copy link

DoDoENT commented May 28, 2017

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?).

@DoDoENT
Copy link

DoDoENT commented May 29, 2017

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}"

@maddouri
Copy link
Contributor

@DoDoENT Great!

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

@DoDoENT
Copy link

DoDoENT commented May 29, 2017

@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...

@droidmonkey
Copy link
Author

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.

@teknoman117
Copy link

teknoman117 commented Nov 20, 2018

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.

@vector-of-bool
Copy link
Contributor

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.

@gentooise
Copy link

gentooise commented Sep 17, 2019

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.)

@DoDoENT
Copy link

DoDoENT commented Sep 17, 2019

@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}",

@gentooise
Copy link

gentooise commented Sep 18, 2019

@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?

@DoDoENT
Copy link

DoDoENT commented Sep 18, 2019

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.

@gentooise
Copy link

gentooise commented Sep 19, 2019

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).

@bobbrow
Copy link
Member

bobbrow commented Sep 19, 2019

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

@gentooise
Copy link

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

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

@github-actions github-actions bot locked and limited conversation to collaborators Feb 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants