Skip to content

Commit

Permalink
default to c++14 on mac for build and run active file (#11300)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbrow authored Aug 8, 2023
1 parent 4080514 commit 90f922d
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions Extension/src/LanguageServer/cppBuildTaskProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export class CppBuildTask extends Task {
isDefault?: boolean;
}

interface BuildOptions {
taskUsesActiveFile: boolean;
insertStd?: boolean;
}

export class CppBuildTaskProvider implements TaskProvider {
static CppBuildScriptType: string = 'cppbuild';

Expand Down Expand Up @@ -166,6 +171,7 @@ export class CppBuildTaskProvider implements TaskProvider {
private getTask: (compilerPath: string, appendSourceToName: boolean, compilerArgs?: string[], definition?: CppBuildTaskDefinition, detail?: string) => Task = (compilerPath: string, appendSourceToName: boolean, compilerArgs?: string[], definition?: CppBuildTaskDefinition, detail?: string) => {
const compilerPathBase: string = path.basename(compilerPath);
const isCl: boolean = compilerPathBase.toLowerCase() === "cl.exe";
const isClang: boolean = !isCl && compilerPathBase.toLowerCase().includes("clang");
// Double-quote the command if it is not already double-quoted.
let resolvedcompilerPath: string = isCl ? compilerPathBase : compilerPath;
if (resolvedcompilerPath && !resolvedcompilerPath.startsWith("\"") && resolvedcompilerPath.includes(" ")) {
Expand All @@ -177,12 +183,12 @@ export class CppBuildTaskProvider implements TaskProvider {
const taskLabel: string = ((appendSourceToName && !compilerPathBase.startsWith(ext.configPrefix)) ?
ext.configPrefix : "") + compilerPathBase + " " + localize("build_active_file", "build active file");
const programName: string = util.defaultExePath();
const isClang: boolean = !isCl && compilerPathBase.toLowerCase().includes("clang");
let args: string[] = isCl ?
['/Zi', '/EHsc', '/nologo', `/Fe${programName}`, '${file}'] :
isClang ?
['-fcolor-diagnostics', '-fansi-escape-codes', '-g', '${file}', '-o', programName] :
['-fdiagnostics-color=always', '-g', '${file}', '-o', programName];

if (compilerArgs && compilerArgs.length > 0) {
args = args.concat(compilerArgs);
}
Expand All @@ -205,7 +211,7 @@ export class CppBuildTaskProvider implements TaskProvider {
const task: CppBuildTask = new Task(definition, scope, definition.label, ext.CppSourceStr,
new CustomExecution(async (resolvedDefinition: TaskDefinition): Promise<Pseudoterminal> =>
// When the task is executed, this callback will run. Here, we setup for running the task.
new CustomBuildTaskTerminal(resolvedcompilerPath, resolvedDefinition.args, resolvedDefinition.options, taskUsesActiveFile)
new CustomBuildTaskTerminal(resolvedcompilerPath, resolvedDefinition.args, resolvedDefinition.options, {taskUsesActiveFile, insertStd: isClang && os.platform() === 'darwin'})
), isCl ? '$msCompile' : '$gcc');

task.group = TaskGroup.Build;
Expand Down Expand Up @@ -352,15 +358,20 @@ class CustomBuildTaskTerminal implements Pseudoterminal {
public get onDidClose(): Event<number> { return this.closeEmitter.event; }
private endOfLine: string = "\r\n";

constructor(private command: string, private args: string[], private options: cp.ExecOptions | cp.SpawnOptions | undefined, private taskUsesActiveFile: boolean) {
constructor(private command: string, private args: string[], private options: cp.ExecOptions | cp.SpawnOptions | undefined, private buildOptions: BuildOptions) {
}

async open(_initialDimensions: TerminalDimensions | undefined): Promise<void> {
if (this.taskUsesActiveFile && !util.isCppOrCFile(window.activeTextEditor?.document.uri)) {
if (this.buildOptions.taskUsesActiveFile && !util.isCppOrCFile(window.activeTextEditor?.document.uri)) {
this.writeEmitter.fire(localize("cannot.build.non.cpp", 'Cannot build and debug because the active file is not a C or C++ source file.') + this.endOfLine);
this.closeEmitter.fire(-1);
return;
}

// TODO: Remove when compiler query work goes in and we can determine the standard version from TypeScript
if (this.buildOptions.taskUsesActiveFile && window.activeTextEditor?.document.languageId === 'cpp' && this.buildOptions.insertStd) {
this.args.unshift('-std=gnu++14');
}
telemetry.logLanguageServerEvent("cppBuildTaskStarted");
// At this point we can start using the terminal.
this.writeEmitter.fire(localize("starting_build", "Starting build...") + this.endOfLine);
Expand Down

0 comments on commit 90f922d

Please sign in to comment.