async runBuild()

in src/cmake-tools.ts [1472:1558]


    async runBuild(targets_?: string[], showCommandOnly?: boolean): Promise<number> {
        if (!showCommandOnly) {
            log.info(localize('run.build', 'Building folder: {0}', this.folderName), (targets_ && targets_.length > 0) ? targets_.join(', ') : '');
        }
        let drv: CMakeDriver | null;
        if (showCommandOnly) {
            drv = await this.getCMakeDriverInstance();
            if (!drv) {
                throw new Error(localize('failed.to.get.cmake.driver', 'Failed to get CMake driver'));
            }
            const buildCmd = await drv.getCMakeBuildCommand(targets_ || await this.getDefaultBuildTargets());
            if (buildCmd) {
                log.showChannel();
                log.info(buildCmdStr(buildCmd.command, buildCmd.args));
            } else {
                throw new Error(localize('failed.to.get.build.command', 'Failed to get build command'));
            }
            return 0;
        }

        const config_retc = await this.ensureConfigured();
        if (config_retc === null) {
            throw new Error(localize('unable.to.configure', 'Build failed: Unable to configure the project'));
        } else if (config_retc !== 0) {
            return config_retc;
        }
        drv = await this.getCMakeDriverInstance();
        if (!drv) {
            throw new Error(localize('driver.died.after.successful.configure', 'CMake driver died immediately after successful configure'));
        }
        let targets = targets_;
        let targetName: string;
        const defaultBuildTargets = await this.getDefaultBuildTargets();
        if (this.useCMakePresets) {
            targets = (targets && targets.length > 0) ? targets : defaultBuildTargets;
            targetName = `${this.buildPreset?.displayName || this.buildPreset?.name || ''}${targets ? (': ' + targets.join(', ')) : ''}`;
            targetName = targetName || this.buildPreset?.displayName || this.buildPreset?.name || '';
        } else {
            targets = (targets && targets.length > 0) ? targets : defaultBuildTargets!;
            targetName = targets.join(', ');
        }

        this.updateDriverAndTargetsInTaskProvider(drv, targets);
        const consumer = new CMakeBuildConsumer(BUILD_LOGGER, drv.config);
        const IS_BUILDING_KEY = 'cmake:isBuilding';
        try {
            this._statusMessage.set(localize('building.status', 'Building'));
            this._isBusy.set(true);
            return await vscode.window.withProgress(
                {
                    location: vscode.ProgressLocation.Notification,
                    title: localize('building.target', 'Building: {0}', targetName),
                    cancellable: true
                },
                async (progress, cancel) => {
                    let old_progress = 0;
                    consumer.onProgress(pr => {
                        const increment = pr.value - old_progress;
                        if (increment >= 1) {
                            progress.report({ increment });
                            old_progress += increment;
                        }
                    });
                    cancel.onCancellationRequested(() => rollbar.invokeAsync(localize('stop.on.cancellation', 'Stop on cancellation'), () => this.stop()));
                    log.showChannel();
                    BUILD_LOGGER.info(localize('starting.build', 'Starting build'));
                    await setContextValue(IS_BUILDING_KEY, true);
                    const rc = await drv!.build(targets, consumer);
                    await setContextValue(IS_BUILDING_KEY, false);
                    if (rc === null) {
                        BUILD_LOGGER.info(localize('build.was.terminated', 'Build was terminated'));
                    } else {
                        BUILD_LOGGER.info(localize('build.finished.with.code', 'Build finished with exit code {0}', rc));
                    }
                    const file_diags = consumer.compileConsumer.resolveDiagnostics(drv!.binaryDir);
                    populateCollection(collections.build, file_diags);
                    await this._refreshCompileDatabase(drv!.expansionOptions);
                    return rc === null ? -1 : rc;
                }
            );
        } finally {
            await setContextValue(IS_BUILDING_KEY, false);
            this._statusMessage.set(localize('ready.status', 'Ready'));
            this._isBusy.set(false);
            consumer.dispose();
        }
    }