Skip to content

Commit

Permalink
Allow overriding of CMakePresets cache variables and arguments (#3537)
Browse files Browse the repository at this point in the history
* Update the already available settings to use in presets.

Turns out, we already have configure, build, buildTool, and test
args/envs that can override. I modified the code to use these in
presets. Also, I added a small sentence to the settings descriptions to
notify users that when using CMake Presets, they are designed for
overrides, not general configuration.

* pending further discussion, use general environment for all

* add temporary override string for presets to environment setting description

* don't remove configureSettings

* add text to specify that configureSettings isn't for presets scenario

* Add tentative treeview UI and output message for configure

Still need to do build/test/etc, and likely improve the strings
themselves.

* add output lines, improvement on the string to come, testing for location

* don't output message when we're using cache

* add handlers that update the nodes when a test or build setting is modified

* slight refactor and add * to status bar

* add button that only appears when overrides present that allows opening those settings

* share some code

* fix localize calls

* update tsconfig.json

* update eslint

* slightly reword the settings

* fix buildTool args

* avoid duplication

* ensure we only return -3 when testPreset isn't defined

* fix imports

* changelog and docs

* refactor to avoid circular dependency

* missed one

* fix imports
  • Loading branch information
gcampbell-msft authored Jan 19, 2024
1 parent 0c3de56 commit 0e48ef9
Show file tree
Hide file tree
Showing 18 changed files with 349 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module.exports = {
"no-trailing-spaces": "error",
"no-undef-init": "error",
"no-unsafe-finally": "error",
"no-unused-expressions": "error",
"no-unused-expressions": "off",
"no-unused-labels": "error",
"no-var": "error",
"one-var": [
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Improvements:

- Improve when the "Configure with Debugger" popup appears and allow for "Do Not Show Again". [#3343](https://github.com/microsoft/vscode-cmake-tools/issues/3343)
- Add option to disable "Not all open documents were saved" popup. [#2889](https://github.com/microsoft/vscode-cmake-tools/issues/2889)
- Allow overriding of CMakePresets cache variables and arguments [#1836](https://github.com/microsoft/vscode-cmake-tools/issues/1836)

Bug Fixes:

Expand Down
16 changes: 14 additions & 2 deletions docs/cmake-presets.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,14 @@ CMake Tools supports command substitution for launch commands when `CMakePresets

`CMakePresets.json` should be the source of truth for all settings related to configure, build, and test. This eliminates behavior specific to Visual Studio Code and ensures that your CMake and CTest invocations can be reproduced from the command line.

The following settings in `settings.json` either duplicate options in `CMakePresets.json` or no longer apply. These settings will be ignored when `CMakePresets.json` integration is enabled. Ignored settings will be logged to the Output Window when you run **CMake: Configure**.
The following settings in `settings.json` either duplicate options in `CMakePresets.json` or no longer apply. These settings will be ignored when `CMakePresets.json` integration is enabled.

| Ignored setting in `settings.json` | `CMakePresets.json` equivalent |
|--|--|
| `cmake.buildArgs` | Various options in `buildPreset` |
| `cmake.buildDirectory` | `configurePresets.binaryDir` |
| `cmake.buildEnvironment` | `buildPresets.environment` |
| `cmake.buildToolsArgs` | `buildPresets.nativeToolOptions` |
| `cmake.cmakePath` | `configurePresets.cmakeExecutable` |
| `cmake.configureArgs` | Various options in `configurePreset` |
| `cmake.configureEnvironment` | `configurePresets.environment` |
| `cmake.configureSettings` | `configurePresets.cacheVariables` |
Expand All @@ -249,6 +248,19 @@ The following settings in `settings.json` either duplicate options in `CMakePres
| `cmake.testEnvironment` | `testPresets.environment` |
| `cmake.toolset` | `configurePresets.toolset` |

## Settings that can be used to override CMakePresets.json settings for temporary testing

The following settings can be used temporarily when CMakePresets integration is enabled.

| Setting in `settings.json` | `CMakePresets.json` equivalent |
|--|--|
| `cmake.buildArgs` | Various options in `buildPreset` |
| `cmake.buildEnvironment` | `buildPresets.environment` |
| `cmake.buildToolsArgs` | `buildPresets.nativeToolOptions` |
| `cmake.configureArgs` | Various options in `configurePreset` |
| `cmake.configureEnvironment` | `configurePresets.environment` |
| `cmake.testEnvironment` | `testPresets.environment` |

## Unsupported commands

The following commands are not supported when `CMakePresets.json` integration is enabled:
Expand Down
54 changes: 51 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@
"when": "cmake:enableFullFeatureSet && useCMakePresets",
"category": "CMake"
},
{
"command": "cmake.projectStatus.viewConfigureSettings",
"title": "%cmake-tools.command.cmake.viewConfigureSettings.title%",
"when": "cmake:enabelFullFeatureSet && useCMakePresets",
"category": "CMake",
"icon": "$(settings-gear)"
},
{
"command": "cmake.projectStatus.selectConfigurePreset",
"title": "%cmake-tools.command.cmake.selectConfigurePreset.title%",
Expand All @@ -105,13 +112,27 @@
"when": "cmake:enableFullFeatureSet && useCMakePresets",
"category": "CMake"
},
{
"command": "cmake.projectStatus.viewBuildSettings",
"title": "%cmake-tools.command.cmake.viewBuildSettings.title%",
"when": "cmake:enableFullFeatureSet && useCMakePresets",
"category": "CMake",
"icon": "$(settings-gear)"
},
{
"command": "cmake.projectStatus.selectBuildPreset",
"title": "%cmake-tools.command.cmake.selectBuildPreset.title%",
"when": "cmake:enableFullFeatureSet && useCMakePresets",
"category": "CMake",
"icon": "$(edit)"
},
{
"command": "cmake.projectStatus.viewTestSettings",
"title": "%cmake-tools.command.cmake.viewTestSettings.title%",
"when": "cmake:enableFullFeatureSet && useCMakePresets",
"category": "CMake",
"icon": "$(settings-gear)"
},
{
"command": "cmake.selectTestPreset",
"title": "%cmake-tools.command.cmake.selectTestPreset.title%",
Expand Down Expand Up @@ -1304,10 +1325,18 @@
"command": "cmake.projectStatus.selectConfigurePreset",
"when": "never"
},
{
"command": "cmake.projectStatus.viewConfigureSettings",
"when": "never"
},
{
"command": "cmake.projectStatus.build",
"when": "never"
},
{
"command": "cmake.projectStatus.viewBuildSettings",
"when": "never"
},
{
"command": "cmake.projectStatus.setDefaultTarget",
"when": "never"
Expand All @@ -1316,6 +1345,10 @@
"command": "cmake.projectStatus.selectBuildPreset",
"when": "never"
},
{
"command": "cmake.projectStatus.viewTestSettings",
"when": "never"
},
{
"command": "cmake.projectStatus.ctest",
"when": "never"
Expand Down Expand Up @@ -1451,7 +1484,12 @@
},
{
"command": "cmake.projectStatus.selectConfigurePreset",
"when": "view == cmake.projectStatus && cmake:enableFullFeatureSet && viewItem == 'configPreset'",
"when": "view == cmake.projectStatus && cmake:enableFullFeatureSet && viewItem =~ /configPreset/",
"group": "inline"
},
{
"command": "cmake.projectStatus.viewConfigureSettings",
"when": "view == cmake.projectStatus && cmake:enableFullFeatureSet && viewItem == 'configPreset - overrides present'",
"group": "inline"
},
{
Expand All @@ -1466,9 +1504,14 @@
},
{
"command": "cmake.projectStatus.selectBuildPreset",
"when": "view == cmake.projectStatus && cmake:enableFullFeatureSet && viewItem == 'buildPreset'",
"when": "view == cmake.projectStatus && cmake:enableFullFeatureSet && viewItem =~ /buildPreset/",
"group": "inline"
},
{
"command": "cmake.projectStatus.viewBuildSettings",
"when": "view == cmake.projectStatus && cmake:enableFullFeatureSet && viewItem == 'buildPreset - overrides present'",
"group": "inline"
},
{
"command": "cmake.projectStatus.ctest",
"when": "view == cmake.projectStatus && cmake:enableFullFeatureSet && viewItem == 'test'",
Expand All @@ -1481,9 +1524,14 @@
},
{
"command": "cmake.projectStatus.selectTestPreset",
"when": "view == cmake.projectStatus && cmake:enableFullFeatureSet && viewItem == 'testPreset'",
"when": "view == cmake.projectStatus && cmake:enableFullFeatureSet && viewItem =~ /testPreset/",
"group": "inline"
},
{
"command": "cmake.projectStatus.viewTestSettings",
"when": "view == cmake.projectStatus && cmake:enableFullFeatureSet && viewItem == 'testPreset - overrides present'",
"group": "inline"
},
{
"command": "cmake.projectStatus.debugTarget",
"when": "view == cmake.projectStatus && cmake:enableFullFeatureSet && viewItem == 'debug'",
Expand Down
21 changes: 12 additions & 9 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
"cmake-tools.command.cmake.addBuildPreset.title": "Add Build Preset",
"cmake-tools.command.cmake.addTestPreset.title": "Add Test Preset",
"cmake-tools.command.cmake.selectConfigurePreset.title": "Select Configure Preset",
"cmake-tools.command.cmake.viewConfigureSettings.title": "View the settings overriding your Configure Preset",
"cmake-tools.command.cmake.selectBuildPreset.title": "Select Build Preset",
"cmake-tools.command.cmake.viewBuildSettings.title": "View the settings overriding your Build Preset",
"cmake-tools.command.cmake.selectTestPreset.title": "Select Test Preset",
"cmake-tools.command.cmake.viewTestSettings.title": "View the settings overriding your Test Preset",
"cmake-tools.command.cmake.viewLog.title": "Open the CMake Tools Log File",
"cmake-tools.command.cmake.logDiagnostics.title": "Log Diagnostics",
"cmake-tools.command.cmake.editKits.title": "Edit User-Local CMake Kits",
Expand Down Expand Up @@ -74,7 +77,7 @@
"cmake-tools.configuration.cmake.saveBeforeBuild.description": "Save open files before building.",
"cmake-tools.configuration.cmake.buildBeforeRun.description": "Build the target before running it.",
"cmake-tools.configuration.cmake.clearOutputBeforeBuild.description": "Clear build output before each build.",
"cmake-tools.configuration.cmake.configureSettings.description": "CMake variables to set on the command line.",
"cmake-tools.configuration.cmake.configureSettings.description": "CMake variables to set on the command line. This setting is specific to kits and will not be used for CMake Presets.",
"cmake-tools.configuration.cmake.cacheInit.string.description": "Path to a cache-initializing CMake file.",
"cmake-tools.configuration.cmake.cacheInit.array.description": "List of cache initializer files.",
"cmake-tools.configuration.cmake.cacheInit.array.string.description": "A cache initializing CMake file.",
Expand All @@ -83,9 +86,9 @@
"cmake-tools.configuration.cmake.generator.description": "The CMake generator to use.",
"cmake-tools.configuration.cmake.toolset.description": "The CMake toolset to use when configuring.",
"cmake-tools.configuration.cmake.platform.description": "The CMake platform to use when configuring.",
"cmake-tools.configuration.cmake.configureArgs.description": "Additional arguments to pass to CMake when configuring.",
"cmake-tools.configuration.cmake.buildArgs.description": "Additional arguments to pass to CMake when building.",
"cmake-tools.configuration.cmake.buildToolArgs.description": "Additional arguments to pass to the underlying build tool when building.",
"cmake-tools.configuration.cmake.configureArgs.description": "Additional arguments to pass to CMake when configuring. When using CMake Presets, these arguments are temporarily appended to the arguments provided by the active configure preset.",
"cmake-tools.configuration.cmake.buildArgs.description": "Additional arguments to pass to CMake when building. When using CMake Presets, these arguments are temporarily appended to the arguments provided by the active build preset.",
"cmake-tools.configuration.cmake.buildToolArgs.description": "Additional arguments to pass to the underlying build tool when building. When using CMake Presets, these arguments are temporarily appended to the arguments provided by the active build preset to invoke the build tool.",
"cmake-tools.configuration.cmake.parallelJobs.description": "The number of parallel build jobs. Use zero to automatically detect the number of CPUs. Setting this to 1 will omit the parallelism flag (-j) from the underlying build command, which has a generator-dependent effect on build parallelism.",
"cmake-tools.configuration.cmake.ctestPath.description": "Path to CTest executable. If null, will be inferred from cmake.cmakePath (recommended to leave null).",
"cmake-tools.configuration.cmake.ctest.parallelJobs.markdownDescription": {
Expand Down Expand Up @@ -136,13 +139,13 @@
"cmake-tools.configuration.cmake.defaultVariants.buildType.release.long": "Optimize for speed - exclude debug information.",
"cmake-tools.configuration.cmake.defaultVariants.buildType.minsize.long": "Optimize for smallest binary size - exclude debug information.",
"cmake-tools.configuration.cmake.defaultVariants.buildType.reldeb.long": "Optimize for speed - include debug information.",
"cmake-tools.configuration.cmake.ctestArgs.description": "Arguments to pass to CTest.",
"cmake-tools.configuration.cmake.ctestArgs.description": "Additional arguments to pass to CTest. When using CMake Presets, these arguments are temporarily added to the arguments provided by the active test preset.",
"cmake-tools.configuration.cmake.ctestDefaultArgs.description": "Arguments passed by default to CTest.",
"cmake-tools.configuration.cmake.environment.description": "Environment variables to set when running CMake commands.",
"cmake-tools.configuration.cmake.environment.description": "Environment variables to set when running CMake commands. When using CMake Presets, these are temporarily added to the environment used for CMake commands.",
"cmake-tools.configuration.cmake.environment.additionalProperties.description": "Value for the environment variable.",
"cmake-tools.configuration.cmake.configureEnvironment.description": "Environment variables to pass to CMake during configure.",
"cmake-tools.configuration.cmake.buildEnvironment.description": "Environment variables to pass to CMake during build.",
"cmake-tools.configuration.cmake.testEnvironment.description": "Environment variables to pass to CTest.",
"cmake-tools.configuration.cmake.configureEnvironment.description": "Environment variables to pass to CMake during configure. When using CMake Presets, these are temporarily added to the environment provided by the active configure preset.",
"cmake-tools.configuration.cmake.buildEnvironment.description": "Environment variables to pass to CMake during build. When using CMake Presets, these are temporarily added to the environment provided by the active build preset.",
"cmake-tools.configuration.cmake.testEnvironment.description": "Environment variables to pass to CTest. When using CMake Presets, these are temporarily added to the environment provided by the active test preset.",
"cmake-tools.configuration.cmake.mingwSearchDirs.description": "Directories where MinGW may be installed.",
"cmake-tools.configuration.cmake.additionalCompilerSearchDirs.description": "Additional directories to search for compilers.",
"cmake-tools.configuration.cmake.searchDirs.items.description": "Path to a directory.",
Expand Down
6 changes: 5 additions & 1 deletion src/cmakeProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { VariantManager } from './variant';
import * as nls from 'vscode-nls';
import { ConfigurationWebview } from './cacheView';
import { enableFullFeatureSet, extensionManager, updateFullFeatureSet } from './extension';
import { CMakeCommunicationMode, ConfigurationReader, OptionConfig, UseCMakePresets } from './config';
import { CMakeCommunicationMode, ConfigurationReader, OptionConfig, UseCMakePresets, checkConfigureOverridesPresent } from './config';
import * as preset from '@cmt/preset';
import * as util from '@cmt/util';
import { Environment, EnvironmentUtils } from './environmentVariables';
Expand Down Expand Up @@ -2348,6 +2348,10 @@ export class CMakeProject {
// Add environment variables from ConfigureEnvironment.
const configureEnv = await drv?.getConfigureEnvironment();

if ((drv?.useCMakePresets ?? false) && (checkConfigureOverridesPresent(this.workspaceContext.config) ?? false)) {
log.info(localize('launch.with.overrides', `NOTE: You are launching a target and there are some environment overrides being applied from your VS Code settings.`));
}

return EnvironmentUtils.merge([env, configureEnv]);
}

Expand Down
29 changes: 29 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,32 @@ const activeChangeEvents: PromiseTracker = new PromiseTracker();
export function getSettingsChangePromise(): Promise<any[]> {
return activeChangeEvents.getAwaiter();
}

export function checkConfigureOverridesPresent(config: ConfigurationReader): boolean {
if (config.configureArgs.length > 0 || Object.values(config.configureEnvironment).length > 0 || checkGeneralEnvironmentOverridesPresent(config)) {
return true;
}

return false;
}

export function checkBuildOverridesPresent(config: ConfigurationReader): boolean {
if (config.buildArgs.length > 0 || config.buildToolArgs.length > 0
|| Object.values(config.buildEnvironment).length > 0 || checkGeneralEnvironmentOverridesPresent(config)) {
return true;
}

return false;
}

export function checkTestOverridesPresent(config: ConfigurationReader): boolean {
if (Object.values(config.testEnvironment).length > 0 || config.ctestArgs.length > 0 || checkGeneralEnvironmentOverridesPresent(config)) {
return true;
}

return false;
}

export function checkGeneralEnvironmentOverridesPresent(config: ConfigurationReader): boolean {
return Object.values(config.environment).length > 0;
}
Loading

0 comments on commit 0e48ef9

Please sign in to comment.