Skip to content

Commit

Permalink
support non-trivial command-lines for Compile Active File
Browse files Browse the repository at this point in the history
in particular macro definitions containing quotes and spaces
fixes #969
fixes #1221
  • Loading branch information
Trass3r committed Oct 9, 2020
1 parent aa254ef commit 12f46d8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
14 changes: 6 additions & 8 deletions src/compdb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as shlex from '@cmt/shlex';

import {createLogger} from './logging';
import {fs} from './pr';
import * as util from './util';
Expand All @@ -16,7 +14,7 @@ interface BaseCompileCommand {
output?: string;
}

interface StringCompileCommand extends BaseCompileCommand {
export interface StringCompileCommand extends BaseCompileCommand {
command: string;
}

Expand All @@ -27,16 +25,16 @@ interface ArgsCompileCommand extends BaseCompileCommand {
export type CompileCommand = StringCompileCommand|ArgsCompileCommand;

export class CompilationDatabase {
private readonly _infoByFilePath: Map<string, ArgsCompileCommand>;
constructor(infos: CompileCommand[]) {
private readonly _infoByFilePath: Map<string, StringCompileCommand>;
constructor(infos: StringCompileCommand[]) {
this._infoByFilePath = infos.reduce(
(acc, cur) => acc.set(util.platformNormalizePath(cur.file), {
directory: cur.directory,
file: cur.file,
output: cur.output,
arguments: 'arguments' in cur ? cur.arguments : [...shlex.split(cur.command)],
command: cur.command,
}),
new Map<string, ArgsCompileCommand>(),
new Map<string, StringCompileCommand>(),
);
}

Expand All @@ -48,7 +46,7 @@ export class CompilationDatabase {
}
const data = await fs.readFile(dbpath);
try {
const content = JSON.parse(data.toString()) as CompileCommand[];
const content = JSON.parse(data.toString()) as StringCompileCommand[];
return new CompilationDatabase(content);
} catch (e) {
log.warning(localize('error.parsing.compilation.database', 'Error parsing compilation database "{0}": {1}', dbpath, util.errorToString(e)));
Expand Down
12 changes: 3 additions & 9 deletions src/drivers/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as vscode from 'vscode';
import * as api from '@cmt/api';
import {CMakeExecutable} from '@cmt/cmake/cmake-executable';
import * as codepages from '@cmt/code-pages';
import {CompileCommand} from '@cmt/compdb';
import {StringCompileCommand} from '@cmt/compdb';
import {ConfigurationReader} from '@cmt/config';
import {CMakeBuildConsumer, CompileOutputConsumer} from '@cmt/diagnostics/build';
import {CMakeOutputConsumer} from '@cmt/diagnostics/cmake';
Expand All @@ -21,7 +21,6 @@ import paths from '@cmt/paths';
import {fs} from '@cmt/pr';
import * as proc from '@cmt/proc';
import rollbar from '@cmt/rollbar';
import * as shlex from '@cmt/shlex';
import * as telemetry from '@cmt/telemetry';
import * as util from '@cmt/util';
import {ConfigureArguments, VariantOption} from '@cmt/variant';
Expand Down Expand Up @@ -261,11 +260,7 @@ export abstract class CMakeDriver implements vscode.Disposable {
* Launch the given compilation command in an embedded terminal.
* @param cmd The compilation command from a compilation database to run
*/
runCompileCommand(cmd: CompileCommand): vscode.Terminal {
if ('command' in cmd) {
const args = [...shlex.split(cmd.command)];
return this.runCompileCommand({directory: cmd.directory, file: cmd.file, arguments: args});
} else {
runCompileCommand(cmd: StringCompileCommand): vscode.Terminal {
const env = this.getEffectiveSubprocessEnvironment();
const key = `${cmd.directory}${JSON.stringify(env)}`;
let existing = this._compileTerms.get(key);
Expand All @@ -286,9 +281,8 @@ export abstract class CMakeDriver implements vscode.Disposable {
existing = term;
}
existing.show();
existing.sendText(cmd.arguments.map(s => shlex.quote(s)).join(' ') + '\r\n');
existing.sendText(cmd.command + '\r\n');
return existing;
}
}

/**
Expand Down

0 comments on commit 12f46d8

Please sign in to comment.