export function extractCompilerPathAndArgs()

in Extension/src/common.ts [878:932]


export function extractCompilerPathAndArgs(inputCompilerPath?: string, inputCompilerArgs?: string[]): CompilerPathAndArgs {
    let compilerPath: string | undefined = inputCompilerPath;
    const compilerPathLowercase: string | undefined = inputCompilerPath?.toLowerCase();
    let compilerName: string = "";
    let additionalArgs: string[] = [];

    if (compilerPath) {
        if (compilerPathLowercase?.endsWith("\\cl.exe") || compilerPathLowercase?.endsWith("/cl.exe") || (compilerPathLowercase === "cl.exe")
            || compilerPathLowercase?.endsWith("\\cl") || compilerPathLowercase?.endsWith("/cl") || (compilerPathLowercase === "cl")) {
            compilerName = path.basename(compilerPath);
        } else if (compilerPath.startsWith("\"")) {
            // Input has quotes around compiler path
            const endQuote: number = compilerPath.substr(1).search("\"") + 1;
            if (endQuote !== -1) {
                additionalArgs = extractArgs(compilerPath.substr(endQuote + 1));
                compilerPath = compilerPath.substr(1, endQuote - 1);
                compilerName = path.basename(compilerPath);
            }
        } else {
            // Input has no quotes around compiler path
            let spaceStart: number = compilerPath.lastIndexOf(" ");
            if (checkFileExistsSync(compilerPath)) {
                // Get compiler name if there are no args but path is valid.
                compilerName = path.basename(compilerPath);
            } else if (spaceStart !== -1 && !checkFileExistsSync(compilerPath)) {
                // Get compiler name if compiler path has spaces and args.
                // Go from right to left checking if a valid path is to the left of a space.
                let potentialCompilerPath: string = compilerPath.substr(0, spaceStart);
                while (!checkFileExistsSync(potentialCompilerPath)) {
                    spaceStart = potentialCompilerPath.lastIndexOf(" ");
                    if (spaceStart === -1) {
                        // Reached the start without finding a valid path. Use the original value.
                        potentialCompilerPath = compilerPath;
                        break;
                    }
                    potentialCompilerPath = potentialCompilerPath.substr(0, spaceStart);
                }
                if (compilerPath !== potentialCompilerPath) {
                    // Found a valid compilerPath and args.
                    additionalArgs = extractArgs(compilerPath.substr(spaceStart + 1));
                    compilerPath = potentialCompilerPath;
                    compilerName = path.basename(potentialCompilerPath);
                }
            }
        }
    }
    // Combine args from inputCompilerPath and inputCompilerArgs and remove duplicates
    if (inputCompilerArgs && inputCompilerArgs.length) {
        additionalArgs = inputCompilerArgs.concat(additionalArgs.filter(
            function (item: string): boolean {
                return inputCompilerArgs.indexOf(item) < 0;
            }));
    }
    return { compilerPath, compilerName, additionalArgs };
}