|
| 1 | +/* -------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. |
| 3 | + * See 'LICENSE' in the project root for license information. |
| 4 | + * ------------------------------------------------------------------------------------------ */ |
| 5 | + |
| 6 | +import { promises as fs } from 'fs'; |
| 7 | +import { vcvars } from 'node-vcvarsall'; |
| 8 | +import { vswhere } from 'node-vswhere'; |
| 9 | +import * as path from 'path'; |
| 10 | +import * as vscode from 'vscode'; |
| 11 | + |
| 12 | +export async function setEnvironment(context?: vscode.ExtensionContext) { |
| 13 | + if (!context) { |
| 14 | + throw new Error('No context provided'); |
| 15 | + } |
| 16 | + |
| 17 | + const vses = await getVSInstallations(); |
| 18 | + if (!vses) { |
| 19 | + throw new Error('A Visual Studio installation with the C++ compiler was not found'); |
| 20 | + } |
| 21 | + |
| 22 | + let vs = await chooseVSInstallation(vses); |
| 23 | + let options: vcvars.Options | undefined; |
| 24 | + if (!vs) { |
| 25 | + const compiler = await getAdvancedConfiguration(vses); |
| 26 | + vs = compiler.vs; |
| 27 | + options = compiler.options; |
| 28 | + } |
| 29 | + const vars = await vscode.window.withProgress({ |
| 30 | + cancellable: false, |
| 31 | + location: vscode.ProgressLocation.Notification, |
| 32 | + title: 'Configuring Developer Environment...' |
| 33 | + }, () => vcvars.getVCVars(vs, options)); |
| 34 | + |
| 35 | + if (!vars || !vars['INCLUDE']) { |
| 36 | + throw new Error(`Something went wrong: ${JSON.stringify(vars)}`); |
| 37 | + } |
| 38 | + |
| 39 | + const host = vars['VSCMD_ARG_HOST_ARCH']; |
| 40 | + const target = vars['VSCMD_ARG_TGT_ARCH']; |
| 41 | + const arch = vcvars.getArchitecture({ |
| 42 | + host: match(host, { 'x86': 'x86', 'x64': 'x64' }) ?? 'x64', |
| 43 | + target: match(target, { 'x86': 'x86', 'x64': 'x64', 'arm64': 'ARM64', 'arm': 'ARM' }) ?? 'x64' |
| 44 | + }); |
| 45 | + const persist = vscode.workspace.getConfiguration('devcmd').get<boolean>('persistEnvironment') === true; |
| 46 | + |
| 47 | + context.environmentVariableCollection.clear(); |
| 48 | + for (const key of Object.keys(vars)) { |
| 49 | + context.environmentVariableCollection.replace(key, vars[key].replace(`%${key}%`, '${env:' + key + '}')); |
| 50 | + } |
| 51 | + context.environmentVariableCollection.description = (arch ? `${arch} ` : '') + 'Developer Command Prompt for ' + vs.displayName; |
| 52 | + context.environmentVariableCollection.persistent = persist; |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +async function getVSInstallations() { |
| 57 | + const installations = await vswhere.getVSInstallations({ |
| 58 | + all: true, |
| 59 | + prerelease: true, |
| 60 | + sort: true, |
| 61 | + requires: ['Microsoft.VisualStudio.Component.VC.Tools.x86.x64'] |
| 62 | + }); |
| 63 | + |
| 64 | + if (installations.length === 0) { |
| 65 | + throw new Error('A Visual Studio installation with the C++ compiler was not found'); |
| 66 | + } |
| 67 | + return installations; |
| 68 | +} |
| 69 | + |
| 70 | +async function chooseVSInstallation(installations: vswhere.Installation[]): Promise<vswhere.Installation | undefined> { |
| 71 | + const items: vscode.QuickPickItem[] = installations.map(installation => <vscode.QuickPickItem>{ |
| 72 | + label: installation.displayName, |
| 73 | + description: `Default settings for ${installation.displayName}` |
| 74 | + }); |
| 75 | + items.push({ |
| 76 | + label: 'Advanced options...', |
| 77 | + description: 'Select a specific host/target architecture, toolset version, etc.' |
| 78 | + }); |
| 79 | + const selection = await vscode.window.showQuickPick(items, { |
| 80 | + placeHolder: 'Select a Visual Studio installation' |
| 81 | + }); |
| 82 | + if (!selection) { |
| 83 | + throw new Error('The operation was cancelled'); |
| 84 | + } |
| 85 | + |
| 86 | + return installations.find(installation => installation.displayName === selection.label); |
| 87 | +} |
| 88 | + |
| 89 | +async function getAdvancedConfiguration(vses: vswhere.Installation[]): Promise<Compiler> { |
| 90 | + const compiler = await chooseCompiler(vses); |
| 91 | + if (!compiler) { |
| 92 | + throw new Error('The operation was cancelled'); |
| 93 | + } |
| 94 | + await setOptions(compiler); |
| 95 | + return compiler; |
| 96 | +} |
| 97 | + |
| 98 | +interface Compiler { |
| 99 | + version: string; |
| 100 | + vs: vswhere.Installation; |
| 101 | + options: vcvars.Options; |
| 102 | +} |
| 103 | + |
| 104 | +async function chooseCompiler(vses: vswhere.Installation[]): Promise<Compiler | undefined> { |
| 105 | + const compilers: Compiler[] = []; |
| 106 | + for (const vs of vses) { |
| 107 | + const vcPath = path.join(vs.installationPath, 'VC', 'Tools', 'MSVC'); |
| 108 | + const folders = await fs.readdir(vcPath); |
| 109 | + for (const version of folders) { |
| 110 | + const options: vcvars.Options = { |
| 111 | + // Don't set the version in the options if there is only one |
| 112 | + vcVersion: folders.length > 1 ? version : undefined |
| 113 | + }; |
| 114 | + compilers.push({ version, vs, options }); |
| 115 | + } |
| 116 | + } |
| 117 | + const items = compilers.map(compiler => <vscode.QuickPickItem>{ |
| 118 | + label: compiler.version, |
| 119 | + description: compiler.vs.displayName |
| 120 | + }); |
| 121 | + const selection = await vscode.window.showQuickPick(items, { |
| 122 | + placeHolder: 'Select a toolset version' |
| 123 | + }); |
| 124 | + if (!selection) { |
| 125 | + throw new Error('The operation was cancelled'); |
| 126 | + } |
| 127 | + return compilers.find(compiler => compiler.version === selection.label && compiler.vs.displayName === selection.description); |
| 128 | +} |
| 129 | + |
| 130 | +async function setOptions(compiler: Compiler): Promise<void> { |
| 131 | + const vcPath = path.join(compiler.vs.installationPath, 'VC', 'Tools', 'MSVC', compiler.version, 'bin'); |
| 132 | + const hostTargets = await getHostsAndTargets(vcPath); |
| 133 | + if (hostTargets.length > 1) { |
| 134 | + const items = hostTargets.map(ht => <vscode.QuickPickItem>{ |
| 135 | + label: vcvars.getArchitecture(ht), |
| 136 | + description: `host = ${ht.host}, target = ${ht.target}` |
| 137 | + }); |
| 138 | + const selection = await vscode.window.showQuickPick(items, { |
| 139 | + placeHolder: 'Select a host and target architecture' |
| 140 | + }); |
| 141 | + if (!selection) { |
| 142 | + throw new Error('The operation was cancelled'); |
| 143 | + } |
| 144 | + compiler.options.arch = <vcvars.Architecture>selection.label; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +async function getHostsAndTargets(vcPath: string): Promise<vcvars.HostTarget[]> { |
| 149 | + const hosts = await fs.readdir(vcPath); |
| 150 | + if (hosts.length === 0) { |
| 151 | + throw new Error('No hosts found'); |
| 152 | + } |
| 153 | + const hostTargets: vcvars.HostTarget[] = []; |
| 154 | + for (const host of hosts) { |
| 155 | + const h = match<'x86' | 'x64' | undefined>(host.toLowerCase(), { 'hostx86': 'x86', 'hostx64': 'x64' }); |
| 156 | + if (!h) { |
| 157 | + // skip any arm/arm64 folders because there is no arm compiler |
| 158 | + continue; |
| 159 | + } |
| 160 | + const targets = await fs.readdir(path.join(vcPath, host)); |
| 161 | + for (const target of targets) { |
| 162 | + hostTargets.push({ |
| 163 | + host: h, |
| 164 | + target: match(target, { 'x86': 'x86', 'x64': 'x64', 'arm64': 'ARM64', 'arm': 'ARM' }) ?? 'x64' |
| 165 | + }); |
| 166 | + } |
| 167 | + } |
| 168 | + return hostTargets; |
| 169 | +} |
| 170 | + |
| 171 | +export function deactivate() { |
| 172 | +} |
| 173 | + |
| 174 | +function match<T>(item: string, cases: { [key: string]: T }): T | undefined { |
| 175 | + return cases[item]; |
| 176 | +} |
0 commit comments