Skip to content

Commit

Permalink
Merge branch 'develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbrow authored Mar 19, 2020
2 parents aa507a1 + ccec317 commit 33ac423
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@
"gulp-sourcemaps": "^2.6.5",
"gulp-typescript": "^5.0.1",
"jsonc-parser": "^2.1.1",
"minimist": "^1.2.0",
"minimist": "^1.2.2",
"mocha": "^4.1.0",
"module-alias": "^2.2.1",
"sinon": "~5.0.7",
Expand Down
41 changes: 33 additions & 8 deletions src/cpptools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface CompileFlagInformation {
class MissingCompilerException extends Error {}

interface TargetDefaults {
name: string;
includePath: string[];
compileFlags: string[];
defines: string[];
Expand Down Expand Up @@ -170,6 +171,10 @@ export interface CodeModelParams {
* property.
*/
clCompilerPath?: string|null;
/**
* The active target
*/
activeTarget: string|null;
}

/**
Expand Down Expand Up @@ -202,7 +207,12 @@ export class CppConfigurationProvider implements cpt.CustomConfigurationProvider
*/
private _getConfiguration(uri: vscode.Uri): cpt.SourceFileConfigurationItem|undefined {
const norm_path = util.platformNormalizePath(uri.fsPath);
return this._fileIndex.get(norm_path);
const configurations = this._fileIndex.get(norm_path);
if (this._activeTarget && configurations?.has(this._activeTarget)) {
return configurations!.get(this._activeTarget);
} else {
return configurations?.values().next().value; // Any value is fine if the target doesn't match
}
}

/**
Expand Down Expand Up @@ -243,9 +253,14 @@ export class CppConfigurationProvider implements cpt.CustomConfigurationProvider

/**
* Index of files to configurations, using the normalized path to the file
* as the key.
* as the key to the <target,configuration>.
*/
private readonly _fileIndex = new Map<string, Map<string, cpt.SourceFileConfigurationItem>>();

/**
* If a source file configuration exists for the active target, we will prefer that one when asked.
*/
private readonly _fileIndex = new Map<string, cpt.SourceFileConfigurationItem>();
private _activeTarget: string|null = null;

/**
* Create a source file configuration for the given file group.
Expand Down Expand Up @@ -315,10 +330,19 @@ export class CppConfigurationProvider implements cpt.CustomConfigurationProvider
for (const src of grp.sources) {
const abs = path.isAbsolute(src) ? src : path.join(sourceDir, src);
const abs_norm = util.platformNormalizePath(abs);
this._fileIndex.set(abs_norm, {
uri: vscode.Uri.file(abs).toString(),
configuration,
});
if (this._fileIndex.has(abs_norm)) {
this._fileIndex.get(abs_norm)!.set(target.name, {
uri: vscode.Uri.file(abs).toString(),
configuration
});
} else {
const data = new Map<string, cpt.SourceFileConfigurationItem>();
data.set(target.name, {
uri: vscode.Uri.file(abs).toString(),
configuration,
});
this._fileIndex.set(abs_norm, data);
}
const dir = path.dirname(abs_norm);
if (this._workspaceBrowseConfiguration.browsePath.indexOf(dir) < 0) {
this._workspaceBrowseConfiguration.browsePath.push(dir);
Expand All @@ -333,6 +357,7 @@ export class CppConfigurationProvider implements cpt.CustomConfigurationProvider
updateConfigurationData(opts: CodeModelParams) {
let hadMissingCompilers = false;
this._workspaceBrowseConfiguration = {browsePath: []};
this._activeTarget = opts.activeTarget;
for (const config of opts.codeModel.configurations) {
for (const project of config.projects) {
for (const target of project.targets) {
Expand All @@ -347,12 +372,12 @@ export class CppConfigurationProvider implements cpt.CustomConfigurationProvider
const sysroot = target.sysroot || '';
for (const grp of target.fileGroups || []) {
try {

this._updateFileGroup(
target.sourceDirectory || '',
grp,
opts,
{
name: target.name,
compileFlags,
includePath,
defines,
Expand Down
7 changes: 6 additions & 1 deletion src/drivers/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,13 @@ export abstract class CMakeDriver implements vscode.Disposable {
const env = this.getEffectiveSubprocessEnvironment();
const key = `${cmd.directory}${JSON.stringify(env)}`;
let existing = this._compileTerms.get(key);
const shellPath = process.platform === 'win32' ? 'cmd.exe' : undefined;
if (existing && this.config.clearOutputBeforeBuild) {
this._compileTerms.delete(key);
existing.dispose();
existing = undefined;
}
if (!existing) {
const shellPath = process.platform === 'win32' ? 'cmd.exe' : undefined;
const term = vscode.window.createTerminal({
name: localize('file.compilation', 'File Compilation'),
cwd: cmd.directory,
Expand Down
6 changes: 4 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ class ExtensionManager implements vscode.Disposable {
}
}

private cpptoolsReady: boolean = false;
private _updateCodeModel(folder: CMakeToolsFolder) {
const cmt = folder.cmakeTools;
this._projectOutlineProvider.updateCodeModel(
Expand Down Expand Up @@ -482,10 +483,11 @@ class ExtensionManager implements vscode.Disposable {
const opts = drv ? drv.expansionOptions : undefined;
const env = await effectiveKitEnvironment(kit, opts);
const clCompilerPath = await findCLCompilerPath(env);
this._configProvider.updateConfigurationData({cache, codeModel, clCompilerPath});
this._configProvider.updateConfigurationData({cache, codeModel, clCompilerPath, activeTarget: cmt.defaultBuildTarget});
await this.ensureCppToolsProviderRegistered();
if (cpptools.notifyReady) {
if (cpptools.notifyReady && !this.cpptoolsReady) {
cpptools.notifyReady(this._configProvider);
this.cpptoolsReady = true;
} else {
cpptools.didChangeCustomConfiguration(this._configProvider);
}
Expand Down
64 changes: 63 additions & 1 deletion test/unit-tests/cpptools.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import {parseCompileFlags} from '@cmt/cpptools';
import {parseCompileFlags, CppConfigurationProvider} from '@cmt/cpptools';
import {expect} from '@test/util';
import { CMakeCache } from '@cmt/cache';
import * as path from 'path';
import * as codemodel_api from '@cmt/drivers/codemodel-driver-interface';
import * as vscode from 'vscode';

// tslint:disable:no-unused-expression

const here = __dirname;
function getTestResourceFilePath(filename: string): string {
return path.normalize(path.join(here, '../../../test/unit-tests', filename));
}

suite('CppTools tests', () => {
test('Parse some compiler flags', () => {
let info = parseCompileFlags(['-DFOO=BAR']);
Expand All @@ -15,4 +24,57 @@ suite('CppTools tests', () => {
info = parseCompileFlags(['-std=c++03']);
expect(info.standard).to.eql('c++03');
});

test('Validate code model', async () => {
const provider = new CppConfigurationProvider();
const cache = await CMakeCache.fromPath(getTestResourceFilePath('TestCMakeCache.txt'));
const sourceFile = path.join(here, 'main.cpp');
const uri = vscode.Uri.file(sourceFile);
const codeModel: codemodel_api.CodeModelContent = {
configurations: [{
projects: [{
name: 'cpptools-test',
sourceDirectory: here,
targets: [
{
name: 'target1',
type: 'EXECUTABLE',
fileGroups: [{
sources: [sourceFile],
isGenerated: false,
compileFlags: '-DFLAG1',
language: 'CXX'
}]
},
{
name: 'target2',
type: 'EXECUTABLE',
fileGroups: [{
sources: [sourceFile],
isGenerated: false,
compileFlags: '-DFLAG2',
language: 'CXX'
}]
}
]
}]
}]
};

provider.updateConfigurationData({cache, codeModel, activeTarget: 'target1'});
let configurations = await provider.provideConfigurations([uri]);
expect(configurations.length).to.eq(1);
expect(configurations[0].configuration.defines).to.contain('FLAG1');

provider.updateConfigurationData({cache, codeModel, activeTarget: 'target2'});
configurations = await provider.provideConfigurations([uri]);
expect(configurations.length).to.eq(1);
expect(configurations[0].configuration.defines).to.contain('FLAG2');

provider.updateConfigurationData({cache, codeModel, activeTarget: 'all'});
configurations = await provider.provideConfigurations([uri]);
expect(configurations.length).to.eq(1);
expect(configurations[0].configuration.defines.some(def => def === 'FLAG1' || def === 'FLAG2')).to.be.true;
expect(configurations[0].configuration.defines).to.not.have.all.members(['FLAG1', 'FLAG2']);
});
});
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,9 @@
integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==

acorn@5.X, acorn@^5.0.3:
version "5.7.3"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.7.3.tgz#67aa231bf8812974b85235a96771eb6bd07ea279"
integrity sha512-T/zvzYRfbVojPWahDsE5evJdHb3oJoQfFbsrKM7w5Zcs++Tr257tia3BmMP8XYVjp1S9RZXQMh7gao96BlqZOw==
version "5.7.4"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.7.4.tgz#3e8d8a9947d0599a1796d10225d7432f4a4acf5e"
integrity sha512-1D++VG7BhrtvQpNbBzovKNc1FLGGEE/oGe7b9xJm/RFHMBeUaUGpluV9RLjZa47YFdPcDAenEYuq9pQPcMdLJg==

acorn@^6.2.1:
version "6.4.0"
Expand Down Expand Up @@ -3224,10 +3224,10 @@ minimist@0.0.8:
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
integrity sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=

minimist@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
integrity sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=
minimist@^1.2.0, minimist@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.2.tgz#b00a00230a1108c48c169e69a291aafda3aacd63"
integrity sha512-rIqbOrKb8GJmx/5bc2M0QchhUouMXSpd1RTclXsB41JdL+VtnojfaJR+h7F9k18/4kHUsBFgk80Uk+q569vjPA==

minimist@~0.0.1:
version "0.0.10"
Expand Down

0 comments on commit 33ac423

Please sign in to comment.