vscode-cmake-tools icon indicating copy to clipboard operation
vscode-cmake-tools copied to clipboard

Add support for "args" in task provider

Open MaJerle opened this issue 2 years ago • 6 comments

Brief Issue Summary

I would like to have default build command to do cmake --build --preset presetName - to build currently selected user preset. How can I know its name? I cannot find command to do ${command:cmake.getPresetName} or something similar.

CMake Tools Diagnostics

No response

Debug Log

No response

Additional Information

No response

MaJerle avatar May 17 '22 20:05 MaJerle

So far I may be solve this with "args": ["--build", "${command:cmake.buildDirectory}", "-j", "8"], in the build task. Do you confirm?

MaJerle avatar May 17 '22 20:05 MaJerle

currently, the args element is not being supported in the build task.

This is a bug, when there is a default build preset defined, CMakeTools should pick the default build preset settings. (In the current design for the configure task, CMakeTools will pick the default configure preset settings to configure the project. The same should be applied to build tasks.

elahehrashedi avatar May 17 '22 20:05 elahehrashedi

One option that can be added to the configure or build task can be the name of the preset (instead of always picking the default). for example:

        {
            "type": "cmake",
            "label": "CMake: build",
            "command": "build",
            "targets": [
                "all"
            ],
            **"preset": "presetName",**
            "group": "build",
            "problemMatcher": [],
            "detail": "CMake template build task"
        }

elahehrashedi avatar May 17 '22 21:05 elahehrashedi

My initial goal is to have 3 tasks - build, clean and rebuild, all operating at the currently selected preset. As I know, but may be wrong, you can run command for build with cmake --build --preset presetName or cmake --build pathToBuildDir.

For the time being I did it that way - and it seems to work well:

{
	"version": "2.0.0",
	"tasks": [
        {
            "type": "cppbuild",
            "label": "Build project",
            "command": "cmake",
            "args": ["--build", "${command:cmake.buildDirectory}", "-j", "8"],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": ["$gcc"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "type": "shell",
            "label": "Re-build project",
            "command": "cmake",
            "args": ["--build", "${command:cmake.buildDirectory}", "--clean-first", "-v", "-j", "8"],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": ["$gcc"],
        },
        {
            "type": "shell",
            "label": "Clean project",
            "command": "cmake",
            "args": ["--build", "${command:cmake.buildDirectory}", "--target", "clean"],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": []
        }
    ]
}

MaJerle avatar May 18 '22 05:05 MaJerle

I can see you have used the "shell" task as a workaround to pass "args" to the task. I will use this issue to track adding this feature (i.e. supporting args) to "cmake" tasks in future releases. I opened another feature request to track adding the "clean" and "clean-rebuild" tasks #2555

elahehrashedi avatar May 18 '22 17:05 elahehrashedi

There is an upcoming fix for this issue in PR https://github.com/microsoft/vscode-cmake-tools/pull/2636. In this PR we added the suport for "env" variables as well as "preset".

after this fix goes in, you can have two ways of defining a build task:

  1. the first approach, using presets:
		{
			"type": "cmake",
			"label": "CMake: build",
			"command": "build",
			"preset": "${command:cmake.activeBuildPresetName}",
			"group": "build",
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"detail": "CMake: build"
		}

This task is equal to running cmake --build --preset <buildPresetName>. Note that when using presets, the args that are defined in settings.json will be ignored.

  1. the second approach:
		{
			"type": "cmake",
			"label": "CMake: build",
			"command": "build",
			"targets": [
				"all"
			],
			"options": {},
			"group": "build",
			"problemMatcher": [],
			"detail": "CMake template build task"
		}

In this approach, any args that you define the settings including "cmake.buildDirectory", "cmake.buildArgs", and "cmake.buildToolArgs" will be implicitly added to the build task.

currently, we don't support adding additional args to the task.

elahehrashedi avatar Jul 05 '22 23:07 elahehrashedi