From 721314b3c063dfc15ad02cddc4e918f922ddf2ce Mon Sep 17 00:00:00 2001 From: Elaheh Rashedi Date: Fri, 9 Jul 2021 16:46:10 -0700 Subject: [PATCH] adding buildPreset config to "debug" and "launch" target (#1977) --- src/cmake-tools.ts | 14 +++++++++++++- src/drivers/driver.ts | 15 ++++++++------- src/preset.ts | 2 +- src/util.ts | 10 ++++++++++ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/cmake-tools.ts b/src/cmake-tools.ts index e463b6af..753ee246 100644 --- a/src/cmake-tools.ts +++ b/src/cmake-tools.ts @@ -1802,9 +1802,15 @@ export class CMakeTools implements vscode.Disposable, api.CMakeToolsAPI { return null; } - // add debug configuration from settings + // Add debug configuration from settings. const user_config = this.workspaceContext.config.debugConfig; Object.assign(debug_config, user_config); + // Add environment variables from buildPreset. + if (this.buildPreset?.environment) { + const build_preset_environment = await drv.getConfigureEnvironment(); + debug_config.environment = debug_config.environment ? debug_config.environment.concat(util.splitEnvironmentVars(build_preset_environment)) : {}; + } + log.debug(localize('starting.debugger.with', 'Starting debugger with following configuration.'), JSON.stringify({ workspace: this.folder.uri.toString(), config: debug_config @@ -1846,6 +1852,12 @@ export class CMakeTools implements vscode.Disposable, api.CMakeToolsAPI { return null; } const user_config = this.workspaceContext.config.debugConfig; + const drv = await this.getCMakeDriverInstance(); + // Add environment variables from buildPreset. + if (drv && this.buildPreset?.environment) { + const build_preset_environment = await drv.getConfigureEnvironment(); + user_config.environment = user_config.environment ? user_config.environment.concat(util.splitEnvironmentVars(build_preset_environment)) : {}; + } const termOptions: vscode.TerminalOptions = { name: 'CMake/Launch', cwd: (user_config && user_config.cwd) || path.dirname(executable.path) diff --git a/src/drivers/driver.ts b/src/drivers/driver.ts index c3c12df7..5b9c5016 100644 --- a/src/drivers/driver.ts +++ b/src/drivers/driver.ts @@ -207,15 +207,16 @@ export abstract class CMakeDriver implements vscode.Disposable { * Get the environment variables that should be set at CMake-build time. */ async getCMakeBuildCommandEnvironment(in_env: proc.EnvironmentVariables): Promise { + let envs: proc.EnvironmentVariables; if (this.useCMakePresets) { - return this._buildPreset?.environment as proc.EnvironmentVariables; + envs = util.mergeEnvironment(in_env, this._buildPreset?.environment as proc.EnvironmentVariables); } else { - let envs = util.mergeEnvironment(in_env, getKitEnvironmentVariablesObject(this._kitEnvironmentVariables)); - envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this.config.environment, envs)); - envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this.config.buildEnvironment, envs)); - envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this._variantEnv, envs)); - return envs; + envs = util.mergeEnvironment(in_env, getKitEnvironmentVariablesObject(this._kitEnvironmentVariables)); } + envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this.config.environment, envs)); + envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this.config.buildEnvironment, envs)); + envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this._variantEnv, envs)); + return envs; } /** @@ -223,7 +224,7 @@ export abstract class CMakeDriver implements vscode.Disposable { */ async getCTestCommandEnvironment(): Promise { if (this.useCMakePresets) { - return this._testPreset?.environment as proc.EnvironmentVariables; + return (this._testPreset?.environment ? this._testPreset?.environment : {}) as proc.EnvironmentVariables; } else { let envs = getKitEnvironmentVariablesObject(this._kitEnvironmentVariables); envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this.config.environment, envs)); diff --git a/src/preset.ts b/src/preset.ts index 7aefe29d..ae5988a8 100644 --- a/src/preset.ts +++ b/src/preset.ts @@ -34,7 +34,7 @@ export interface Preset { environment?: { [key: string]: null | string }; vendor?: VendorType; - __expanded?: boolean; // Private field to indicate if we have already expanded thie preset. + __expanded?: boolean; // Private field to indicate if we have already expanded this preset. } export interface ValueStrategy { diff --git a/src/util.ts b/src/util.ts index a641bdc0..77a7ae80 100644 --- a/src/util.ts +++ b/src/util.ts @@ -359,6 +359,16 @@ export function* flatMap(rng: Iterable, fn: (item: In) => Iterable< } } +export function splitEnvironmentVars(env: EnvironmentVariables): EnvironmentVariables[] { + const converted_env: EnvironmentVariables[] = Object.entries(env).map( + ([key, value]) => ({ + name: key, + value + }) + ); + return converted_env; +} + export function mergeEnvironment(...env: EnvironmentVariables[]): EnvironmentVariables { return env.reduce((acc, vars) => { if (process.platform === 'win32') {