Skip to content

Commit b21cc30

Browse files
authored
Add cmake.cacheVariable command (#4422)
* Add cmake.cacheVariable for command substitution * Add PR #4422 to CHANGELOG * cacheVariable: report usage errors * cacheVariable: configure before lookup if needed * Fix "second argument of a localize call must be a string literal." * move quotes into main localization string
1 parent 65880d8 commit b21cc30

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Features:
88
- Add a setting to enable/disable our built-in language services. [#4290](https://github.com/microsoft/vscode-cmake-tools/issues/4290)
99
- Add an option to group the default build target dropdown using CMake groups [#3953](https://github.com/microsoft/vscode-cmake-tools/pull/3953) [@itzandroidtab](https://github.com/itzandroidtab)
1010
- Add `cmake.exclude` setting that allows users to set folders that they want the CMake Tools extension to ignore. [#4112](https://github.com/microsoft/vscode-cmake-tools/issues/4112)
11+
- Add a command to substitute CMake Cache variables in `launch.json` and `tasks.json`. [#4422](https://github.com/microsoft/vscode-cmake-tools/pull/4422)
1112

1213
Improvements:
1314

docs/debug-launch.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,28 @@ Here are minimal examples of a `launch.json` file that uses `cmake.launchTargetP
186186

187187
The value of the `program` attribute is expanded by CMake Tools to be the absolute path of the program to run.
188188

189+
### Cache variable substitution
190+
191+
You can substitute the value of any variable in the CMake cache by adding a `command`-type input for the `cmake.cacheVariable` command to the `inputs` section of `launch.json` with `args.name` as the name of the cache variable. That input can then be used with input variable substitution of values in the `configuration` section of `launch.json`. The optional `args.default` can provide a default value if the named variable isn't found in the CMake cache.
192+
193+
```jsonc
194+
{
195+
"version": "0.2.0",
196+
"inputs": [
197+
{
198+
"id": "cmakeCache.TOOLCHAIN_FILE",
199+
"type": "command",
200+
"command": "cmake.cacheVariable",
201+
"args": {
202+
"name": "CMAKE_TOOLCHAIN_FILE"
203+
}
204+
}
205+
]
206+
}
207+
```
208+
189209
> **Note:**
190-
> You must successfully [configure](configure.md) before `cmake.launchTargetPath` and `cmake.getLaunchTargetDirectory` will resolve correctly.
210+
> You must successfully [configure](configure.md) before `cmake.launchTargetPath`, `cmake.getLaunchTargetDirectory` and `cmake.cacheVariable` will resolve correctly.
191211
192212
## Debugging tests
193213

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
},
3636
"icon": "res/logo.png",
3737
"activationEvents": [
38+
"onCommand:cmake.cacheVariable",
3839
"onCommand:cmake.activeFolderName",
3940
"onCommand:cmake.activeFolderPath",
4041
"onCommand:cmake.activeConfigurePresetName",

src/cmakeProject.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,6 +2874,22 @@ export class CMakeProject {
28742874
return env;
28752875
}
28762876

2877+
async cacheVariable(name?: string, defaultValue?: string): Promise<string | null> {
2878+
if (await this.needsReconfigure()) {
2879+
await this.configureInternal(ConfigureTrigger.api, [], ConfigureType.Normal);
2880+
}
2881+
if (!name) {
2882+
void vscode.window.showErrorMessage(localize('cachevariable.missing.required.argument.name', 'cacheVariable: missing required argument "name".'));
2883+
return null;
2884+
}
2885+
const value = (await CMakeCache.fromPath(await this.cachePath))?.get(name)?.value ?? defaultValue;
2886+
if (value === null || value === undefined) {
2887+
void vscode.window.showErrorMessage(localize('cachevariable.not.found', 'Variable "{0}" not found in CMake cache, and no default provided.', name));
2888+
return null;
2889+
}
2890+
return value;
2891+
}
2892+
28772893
/**
28782894
* Implementation of `cmake.debugTarget`
28792895
*/

src/extension.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,11 @@ export class ExtensionManager implements vscode.Disposable {
18091809
return this.queryCMakeProject(cmakeProject => cmakeProject.tasksBuildCommand(), folder);
18101810
}
18111811

1812+
cacheVariable(args: { name?: string; default?: string }) {
1813+
telemetry.logEvent("substitution", { input: "cacheVariable", ...args });
1814+
return this.queryCMakeProject(cmakeProject => cmakeProject.cacheVariable(args.name, args.default));
1815+
}
1816+
18121817
debugTarget(folder?: vscode.WorkspaceFolder, name?: string, sourceDir?: string): Promise<vscode.DebugSession | null> {
18131818
telemetry.logEvent("debug", { all: "false" });
18141819
return this.runCMakeCommand(cmakeProject => cmakeProject.debugTarget(name), folder, undefined, true, sourceDir);
@@ -2371,6 +2376,7 @@ async function setup(context: vscode.ExtensionContext, progress?: ProgressHandle
23712376
'buildType',
23722377
'buildDirectory',
23732378
'executableTargets',
2379+
'cacheVariable',
23742380
'debugTarget',
23752381
'debugTargetAll',
23762382
'launchTarget',

0 commit comments

Comments
 (0)