Skip to content

Commit ffabd1e

Browse files
author
Elaheh Rashedi
authored
Enhance config, install, clean tasks (#2716)
1 parent ffc315d commit ffabd1e

File tree

6 files changed

+57
-76
lines changed

6 files changed

+57
-76
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Improvements:
55
- Support for presets version 4. [#2492](https://github.com/microsoft/vscode-cmake-tools/issues/2492) [@chausner](https://github.com/chausner)
66
- Triggering reconfigure after changes are made to included files. [#2526](https://github.com/microsoft/vscode-cmake-tools/issues/2526) [@chausner](https://github.com/chausner)
77
- Add target name to terminal window name for launch. [#2613](https://github.com/microsoft/vscode-cmake-tools/issues/2613)
8-
- Add support for "preset" and "env" in task provider. [#2636](https://github.com/microsoft/vscode-cmake-tools/issues/2636) [#2553](https://github.com/microsoft/vscode-cmake-tools/issues/2553)
8+
- Add support for "preset" and "env" in task provider. [#2636](https://github.com/microsoft/vscode-cmake-tools/issues/2636) [#2553](https://github.com/microsoft/vscode-cmake-tools/issues/2553) [#2714](https://github.com/microsoft/vscode-cmake-tools/issues/2714) [#2706](https://github.com/microsoft/vscode-cmake-tools/issues/2706)
99
- Add Craig Scott's "Professional CMake" book to the list of resources in doc/faq.md for learning CMake. [#2679](https://github.com/microsoft/vscode-cmake-tools/pull/2679) [@david-fong](https://github.com/david-fong)
1010

1111
Bug Fixes:

src/cmakeTaskProvider.ts

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,16 @@ export class CustomBuildTaskTerminal implements vscode.Pseudoterminal, proc.Outp
182182
await this.runConfigTask();
183183
break;
184184
case CommandType.build:
185-
await this.runBuildTask();
185+
await this.runBuildTask(CommandType.build);
186186
break;
187187
case CommandType.install:
188-
await this.runInstallTask();
188+
await this.runBuildTask(CommandType.install);
189189
break;
190190
case CommandType.test:
191191
await this.runTestTask();
192192
break;
193193
case CommandType.clean:
194-
await this.runCleanTask();
194+
await this.runBuildTask(CommandType.clean);
195195
break;
196196
case CommandType.cleanRebuild:
197197
await this.runCleanRebuildTask();
@@ -272,7 +272,7 @@ export class CustomBuildTaskTerminal implements vscode.Pseudoterminal, proc.Outp
272272

273273
this.preset = await this.resolvePresetName(this.preset, cmakeTools.useCMakePresets, CommandType.config);
274274
const configPreset: preset.ConfigurePreset | undefined = await cmakeTools?.expandConfigPresetbyName(this.preset);
275-
const result = await cmakeDriver.configure(ConfigureTrigger.taskProvider, [], this, false, false, configPreset);
275+
const result = await cmakeDriver.configure(ConfigureTrigger.taskProvider, [], this, false, false, configPreset, this.options);
276276
if (result === undefined || result === null) {
277277
this.writeEmitter.fire(localize('configure.terminated', 'Configure was terminated') + endOfLine);
278278
this.closeEmitter.fire(-1);
@@ -287,7 +287,16 @@ export class CustomBuildTaskTerminal implements vscode.Pseudoterminal, proc.Outp
287287
}
288288
}
289289

290-
private async runBuildTask(): Promise<any> {
290+
private async runBuildTask(commandType: CommandType): Promise<any> {
291+
let targets = this.targets;
292+
const taskName: string = localizeCommandType(commandType);
293+
if (commandType === CommandType.install) {
294+
this.checkTargets(true);
295+
targets = ['install'];
296+
} else if (commandType === CommandType.clean) {
297+
this.checkTargets(true);
298+
targets = ['clean'];
299+
}
291300
let fullCommand: proc.BuildCommand | null;
292301
let args: string[] = [];
293302
const cmakeTools: CMakeTools | undefined = this.getCMakeTools();
@@ -306,18 +315,18 @@ export class CustomBuildTaskTerminal implements vscode.Pseudoterminal, proc.Outp
306315
const buildPreset: preset.BuildPreset | undefined = await cmakeTools?.expandBuildPresetbyName(this.preset);
307316
if (!buildPreset) {
308317
log.debug(localize("build.preset.not.found", 'Build preset not found.'));
309-
this.writeEmitter.fire(localize("build.failed", "Build preset {0} not found. Build failed.", this.preset) + endOfLine);
318+
this.writeEmitter.fire(localize("build.failed", "Build preset {0} not found. {1} failed.", this.preset, taskName) + endOfLine);
310319
this.closeEmitter.fire(-1);
311320
return;
312321
}
313-
fullCommand = await cmakeDriver.generateBuildCommandFromPreset(buildPreset, this.targets);
322+
fullCommand = await cmakeDriver.generateBuildCommandFromPreset(buildPreset, targets);
314323
if (fullCommand) {
315324
cmakePath = fullCommand.command;
316325
args = fullCommand.args || [];
317326
this.options.environment = EnvironmentUtils.merge([ fullCommand.build_env, this.options.environment], {preserveNull: true});
318327
}
319328
} else {
320-
fullCommand = await cmakeDriver.generateBuildCommandFromSettings(this.targets);
329+
fullCommand = await cmakeDriver.generateBuildCommandFromSettings(targets);
321330
if (fullCommand) {
322331
cmakePath = fullCommand.command;
323332
args = fullCommand.args ? fullCommand.args : [];
@@ -326,44 +335,27 @@ export class CustomBuildTaskTerminal implements vscode.Pseudoterminal, proc.Outp
326335
}
327336
} else {
328337
log.debug(localize("cmake.driver.not.found", 'CMake driver not found.'));
329-
this.writeEmitter.fire(localize("build.failed", "Build failed.") + endOfLine);
338+
this.writeEmitter.fire(localize("build.failed", "{0} failed.", taskName) + endOfLine);
330339
this.closeEmitter.fire(-1);
331340
return;
332341
}
333-
this.writeEmitter.fire(localize("build.started", "Build task started....") + endOfLine);
342+
this.writeEmitter.fire(localize("build.started", "{0} task started....", taskName) + endOfLine);
334343
this.writeEmitter.fire(proc.buildCmdStr(cmakePath, args) + endOfLine);
335344
try {
336345
const result: proc.ExecutionResult = await proc.execute(cmakePath, args, this, this.options).result;
337346
if (result.retc) {
338-
this.writeEmitter.fire(localize("build.finished.with.error", "Build finished with error(s).") + endOfLine);
347+
this.writeEmitter.fire(localize("build.finished.with.error", "{0} finished with error(s).", taskName) + endOfLine);
339348
} else if (result.stderr && !result.stdout) {
340-
this.writeEmitter.fire(localize("build.finished.with.warnings", "Build finished with warning(s).") + endOfLine);
349+
this.writeEmitter.fire(localize("build.finished.with.warnings", "{0} finished with warning(s).", taskName) + endOfLine);
341350
} else if (result.stdout && result.stdout.includes("warning")) {
342-
this.writeEmitter.fire(localize("build.finished.with.warnings", "Build finished with warning(s).") + endOfLine);
351+
this.writeEmitter.fire(localize("build.finished.with.warnings", "{0} finished with warning(s).", taskName) + endOfLine);
343352
} else {
344-
this.writeEmitter.fire(localize("build.finished.successfully", "Build finished successfully.") + endOfLine);
353+
this.writeEmitter.fire(localize("build.finished.successfully", "{0} finished successfully.", taskName) + endOfLine);
345354
}
346355
this.closeEmitter.fire(0);
347356
} catch {
348-
this.writeEmitter.fire(localize("build.finished.with.error", "Build finished with error(s).") + endOfLine);
349-
this.closeEmitter.fire(-1);
350-
}
351-
}
352-
353-
private async runInstallTask(): Promise<any> {
354-
this.writeEmitter.fire(localize("install.started", "Install task started...") + endOfLine);
355-
this.checkTargets(true);
356-
const cmakeTools: CMakeTools | undefined = this.getCMakeTools();
357-
if (!cmakeTools) {
358-
return;
359-
}
360-
const result: number | undefined = await cmakeTools.runBuild(['install'], false, this);
361-
if (result === undefined) {
362-
this.writeEmitter.fire(localize('install.terminated', 'Install was terminated') + endOfLine);
357+
this.writeEmitter.fire(localize("build.finished.with.error", "{0} finished with error(s).", taskName) + endOfLine);
363358
this.closeEmitter.fire(-1);
364-
} else {
365-
this.writeEmitter.fire(localize('install.finished.with.code', 'Install finished with return code {0}', result) + endOfLine);
366-
this.closeEmitter.fire(result);
367359
}
368360
}
369361

@@ -402,28 +394,10 @@ export class CustomBuildTaskTerminal implements vscode.Pseudoterminal, proc.Outp
402394
}
403395
}
404396

405-
private async runCleanTask(ignoreTargets: boolean = true): Promise<any> {
406-
this.writeEmitter.fire(localize("clean.started", "Clean task started...") + endOfLine);
407-
this.checkTargets(ignoreTargets);
408-
const cmakeTools: CMakeTools | undefined = this.getCMakeTools();
409-
if (!cmakeTools) {
410-
return;
411-
}
412-
const result: number | undefined = await cmakeTools.runBuild(['clean'], false, this);
413-
if (result === undefined || result !== 0) {
414-
this.writeEmitter.fire(localize("clean.failed", "Clean task failed.") + endOfLine);
415-
this.closeEmitter.fire(-1);
416-
} else {
417-
this.writeEmitter.fire(localize("clean.finished.with.code", "Clean finished with return code {0}", result) + endOfLine);
418-
this.closeEmitter.fire(result);
419-
}
420-
return result;
421-
}
422-
423397
private async runCleanRebuildTask(): Promise<any> {
424-
const cleanResult = await this.runCleanTask(false);
398+
const cleanResult = await this.runBuildTask(CommandType.clean);
425399
if (cleanResult === 0) {
426-
await this.runBuildTask();
400+
await this.runBuildTask(CommandType.build);
427401
}
428402
}
429403
}

src/drivers/cmakeDriver.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export abstract class CMakeDriver implements vscode.Disposable {
7373
*
7474
* @returns The exit code from CMake
7575
*/
76-
protected abstract doConfigure(extra_args: string[], consumer?: proc.OutputConsumer, showCommandOnly?: boolean, configurePreset?: preset.ConfigurePreset | null): Promise<number>;
76+
protected abstract doConfigure(extra_args: string[], consumer?: proc.OutputConsumer, showCommandOnly?: boolean, configurePreset?: preset.ConfigurePreset | null, options?: proc.ExecutionOptions): Promise<number>;
7777
protected abstract doCacheConfigure(): Promise<number>;
7878

7979
private _isConfiguredAtLeastOnce = false;
@@ -199,18 +199,22 @@ export abstract class CMakeDriver implements vscode.Disposable {
199199
/**
200200
* Get the environment variables that should be set at CMake-configure time.
201201
*/
202-
async getConfigureEnvironment(configurePreset?: preset.ConfigurePreset | null): Promise<Environment> {
202+
async getConfigureEnvironment(configurePreset?: preset.ConfigurePreset | null, extraEnvironmentVariables?: Environment): Promise<Environment> {
203+
let envs;
203204
if (this.useCMakePresets) {
204-
return EnvironmentUtils.create(configurePreset ? configurePreset.environment : this._configurePreset?.environment);
205+
envs = EnvironmentUtils.create(configurePreset ? configurePreset.environment : this._configurePreset?.environment);
205206
} else {
206-
let envs = this._kitEnvironmentVariables;
207+
envs = this._kitEnvironmentVariables;
207208
/* NOTE: By mergeEnvironment one by one to enable expanding self containd variable such as PATH properly */
208209
/* If configureEnvironment and environment both configured different PATH, doing this will preserve them all */
209210
envs = EnvironmentUtils.merge([envs, await this.computeExpandedEnvironment(this.config.environment, envs)]);
210211
envs = EnvironmentUtils.merge([envs, await this.computeExpandedEnvironment(this.config.configureEnvironment, envs)]);
211212
envs = EnvironmentUtils.merge([envs, await this.computeExpandedEnvironment(this._variantEnv, envs)]);
212-
return envs;
213213
}
214+
if (extraEnvironmentVariables) {
215+
envs = EnvironmentUtils.merge([envs, await this.computeExpandedEnvironment(extraEnvironmentVariables, envs)]);
216+
}
217+
return envs;
214218
}
215219

216220
/**
@@ -1219,7 +1223,7 @@ export abstract class CMakeDriver implements vscode.Disposable {
12191223
return Promise.all(expanded_flags_promises);
12201224
}
12211225

1222-
async configure(trigger: ConfigureTrigger, extra_args: string[], consumer?: proc.OutputConsumer, withoutCmakeSettings: boolean = false, showCommandOnly?: boolean, presetOverride?: preset.ConfigurePreset): Promise<number> {
1226+
async configure(trigger: ConfigureTrigger, extra_args: string[], consumer?: proc.OutputConsumer, withoutCmakeSettings: boolean = false, showCommandOnly?: boolean, presetOverride?: preset.ConfigurePreset, options?: proc.ExecutionOptions): Promise<number> {
12231227
// Check if the configuration is using cache in the first configuration and adjust the logging messages based on that.
12241228
const shouldUseCachedConfiguration: boolean = this.shouldUseCachedConfiguration(trigger);
12251229

@@ -1279,7 +1283,7 @@ export abstract class CMakeDriver implements vscode.Disposable {
12791283
this._isConfiguredAtLeastOnce = true;
12801284
return retc;
12811285
} else {
1282-
retc = await this.doConfigure(expanded_flags, consumer, showCommandOnly, presetOverride);
1286+
retc = await this.doConfigure(expanded_flags, consumer, showCommandOnly, presetOverride, options);
12831287
this._isConfiguredAtLeastOnce = true;
12841288
}
12851289
const timeEnd: number = new Date().getTime();

src/drivers/cmakeFileApiDriver.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export class CMakeFileApiDriver extends CMakeDriver {
221221
return 0;
222222
}
223223

224-
async doConfigure(args_: string[], outputConsumer?: proc.OutputConsumer, showCommandOnly?: boolean, configurePreset?: ConfigurePreset | null): Promise<number> {
224+
async doConfigure(args_: string[], outputConsumer?: proc.OutputConsumer, showCommandOnly?: boolean, configurePreset?: ConfigurePreset | null, options?: proc.ExecutionOptions): Promise<number> {
225225
const api_path = this.getCMakeFileApiPath(configurePreset?.binaryDir);
226226
await createQueryFileForApi(api_path);
227227

@@ -269,17 +269,20 @@ export class CMakeFileApiDriver extends CMakeDriver {
269269
} else {
270270
log.debug(`Configuring using ${this.useCMakePresets ? 'preset' : 'kit'}`);
271271
log.debug('Invoking CMake', cmake, 'with arguments', JSON.stringify(args));
272-
const env = await this.getConfigureEnvironment(configurePreset);
273-
const res = await this.executeCommand(cmake, args, outputConsumer, { environment: env, cwd: binaryDir }).result;
274-
log.trace(res.stderr);
275-
log.trace(res.stdout);
276-
if (res.retc === 0) {
272+
const env = await this.getConfigureEnvironment(configurePreset, options?.environment);
273+
const result = await this.executeCommand(cmake, args, outputConsumer, {
274+
environment: env,
275+
cwd: options?.cwd ?? binaryDir
276+
}).result;
277+
log.trace(result.stderr);
278+
log.trace(result.stdout);
279+
if (result.retc === 0) {
277280
if (!configurePreset) {
278281
this._needsReconfigure = false;
279282
}
280283
await this.updateCodeModel(configurePreset?.binaryDir);
281284
}
282-
return res.retc === null ? -1 : res.retc;
285+
return result.retc === null ? -1 : result.retc;
283286
}
284287
}
285288

src/drivers/cmakeLegacyDriver.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class CMakeLegacyDriver extends CMakeDriver {
7676
this._cacheWatcher.dispose();
7777
}
7878

79-
async doConfigure(args_: string[], outputConsumer?: proc.OutputConsumer, showCommandOnly?: boolean, configurePreset?: ConfigurePreset | null): Promise<number> {
79+
async doConfigure(args_: string[], outputConsumer?: proc.OutputConsumer, showCommandOnly?: boolean, configurePreset?: ConfigurePreset | null, options?: proc.ExecutionOptions): Promise<number> {
8080
// Ensure the binary directory exists
8181
const binaryDir = configurePreset?.binaryDir ?? this.binaryDir;
8282
await fs.mkdir_p(binaryDir);
@@ -113,19 +113,19 @@ export class CMakeLegacyDriver extends CMakeDriver {
113113
return 0;
114114
} else {
115115
log.debug(localize('invoking.cmake.with.arguments', 'Invoking CMake {0} with arguments {1}', cmake, JSON.stringify(args)));
116-
const res = await this.executeCommand(cmake, args, outputConsumer, {
117-
environment: await this.getConfigureEnvironment(configurePreset),
118-
cwd: binaryDir
116+
const result = await this.executeCommand(cmake, args, outputConsumer, {
117+
environment: await this.getConfigureEnvironment(configurePreset, options?.environment),
118+
cwd: options?.cwd ?? binaryDir
119119
}).result;
120-
log.trace(res.stderr);
121-
log.trace(res.stdout);
122-
if (res.retc === 0 && !configurePreset) {
120+
log.trace(result.stderr);
121+
log.trace(result.stdout);
122+
if (result.retc === 0 && !configurePreset) {
123123
this._needsReconfigure = false;
124124
}
125125
if (!configurePreset) {
126126
await this._reloadPostConfigure();
127127
}
128-
return res.retc === null ? -1 : res.retc;
128+
return result.retc === null ? -1 : result.retc;
129129
}
130130
}
131131

src/drivers/cmakeServerDriver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export class CMakeServerDriver extends CMakeDriver {
154154
})();
155155
}
156156

157-
protected async doConfigure(args: string[], consumer?: proc.OutputConsumer, showCommandOnly?: boolean, configurePreset?: ConfigurePreset | null) {
157+
protected async doConfigure(args: string[], consumer?: proc.OutputConsumer, showCommandOnly?: boolean, configurePreset?: ConfigurePreset | null, _options?: proc.ExecutionOptions) {
158158
await this._clientChangeInProgress;
159159
const cl = await this.getClient();
160160
const sub = this.onMessage(msg => {

0 commit comments

Comments
 (0)