vscode-compiler-explorer icon indicating copy to clipboard operation
vscode-compiler-explorer copied to clipboard

Offline-Mode Feature

Open TekuConcept opened this issue 4 years ago • 0 comments

Godbolt's Compiler-Explorer provides a great on-the-go convenience for side-by-side comparisons of C/C++ and assembly. However, it requires a network connection to work both in browser and in VSCode. What if you could add an offline mode that doesn't depend solely on the online Compiler-Explorer?

With any GCC or Clang toolchain, the commands are

gcc -S -o- ${filename} ${compileroptions}
clang -S -o- ${filename} ${compileroptions}
g++ and clang++ also work

For MSVC, I believe the command is cl /FAs /o - ${filename} ${compileroptions} but haven't confirmed.

You can use child_proc in place of node-fetch. The code looks something like:

export interface CompilerProperties {
    compiler: string
    flags: string
    includes: string[]
}

async compile(properties: CompilerProperties, file: string, lang: string) : Promise<string> {
    let vspath: string
    if (vscode.workspace.workspaceFolders == undefined) vspath = ''
    else vspath = vscode.workspace.workspaceFolders[0].uri.fsPath

    return new Promise((resolve, _reject) => {
        lang = lang.toUpperCase()
        if (lang !== 'C' && lang !== 'CXX' && lang !== 'C++' && lang !== 'CPP')
            resolve(`unsupported language ${lang}`)
        let command: string = `${properties.compiler} ${properties.flags} -S -o- ${file} `

        for (let i = 0; i < properties.includes.length; i++) {
            let include = properties.includes[i]
            if (include.startsWith('${workspaceFolder}'))
                include = include.replace('${workspaceFolder}', vspath)
            if (include.length === 0) continue
            command = command.concat(`-I${include} `)
        }

        // cp: child_proc
        cp.exec(command, (err:cp.ExecException | null, stdout:string, stderr:string) => {
            if (err) {
                console.log('error: ', err)
                resolve(`Error: ${err}`)
            }
            if (stderr.length > 0) resolve(`Command: ${command}\n\n${stderr}`)
            else resolve(`Command: ${command}\n\n${stdout}`)
        })
    })
}

TekuConcept avatar Mar 14 '20 01:03 TekuConcept