-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathpaths.ts
More file actions
367 lines (319 loc) · 12.4 KB
/
paths.ts
File metadata and controls
367 lines (319 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* This module defines important directories and paths to the extension
*/
import { DirectoryContext } from '@cmt/workspace';
import * as path from 'path';
import * as which from 'which';
import * as vscode from 'vscode';
import { vsInstallations } from '@cmt/installs/visualStudio';
import { expandString } from '@cmt/expand';
import { fs } from '@cmt/pr';
import * as util from '@cmt/util';
interface VSCMakePaths {
cmake?: string;
ninja?: string;
}
export interface PathWithTrust {
path: string;
isTrusted: boolean;
}
class WindowsDefaultCompilerPaths {
constructor(private readonly _env: WindowsEnvironment) {
this._env = _env;
}
get LLVM(): PathWithTrust[] {
return [
this._env.ProgramFiles! + "\\LLVM\\bin",
this._env.ProgramFilesX86! + "\\LLVM\\bin"
].map(p => ({ path: p, isTrusted: true }));
}
get MSYS2(): PathWithTrust[] {
return [
paths.windows.SystemDrive! + '\\msys64\\mingw32\\bin',
paths.windows.SystemDrive! + '\\msys64\\mingw64\\bin',
paths.windows.SystemDrive! + '\\msys64\\clang32\\bin',
paths.windows.SystemDrive! + '\\msys64\\clang64\\bin',
paths.windows.SystemDrive! + '\\msys64\\clangarm64\\bin',
paths.windows.SystemDrive! + '\\msys64\\ucrt64\\bin'
].map(p => ({ path: p, isTrusted: false }));
}
}
class WindowsEnvironment {
readonly defaultCompilerPaths: WindowsDefaultCompilerPaths = new WindowsDefaultCompilerPaths(this);
get AppData(): string | undefined {
if (util.isTestMode()) {
return path.join(vscode.workspace.workspaceFolders![0].uri.fsPath, '.vscode');
}
return process.env['APPDATA'];
}
get LocalAppData(): string | undefined {
if (util.isTestMode()) {
return path.join(vscode.workspace.workspaceFolders![0].uri.fsPath, '.vscode');
}
return process.env['LOCALAPPDATA'];
}
get AllUserProfile(): string | undefined {
return process.env['ProgramData'];
}
get ComSpec(): string {
let comSpec = process.env['ComSpec'];
if (undefined === comSpec) {
comSpec = this.SystemRoot! + '\\system32\\cmd.exe';
}
return comSpec;
}
get HomeDrive(): string | undefined {
return process.env['HOMEDRIVE'];
}
get HomePath(): string | undefined {
return process.env['HOMEPATH'];
}
get ProgramFilesX86(): string | undefined {
return process.env['ProgramFiles(x86)'];
}
get ProgramFiles(): string | undefined {
return process.env['ProgramFiles'];
}
get SystemDrive(): string | undefined {
return process.env['SystemDrive'];
}
get SystemRoot(): string | undefined {
return process.env['SystemRoot'];
}
get Temp(): string | undefined {
return process.env['TEMP'];
}
}
/**
* Directory class.
*/
class Paths {
private _ninjaPath?: string;
readonly windows: WindowsEnvironment = new WindowsEnvironment();
/**
* The current user's home directory
*/
get userHome(): string {
if (process.platform === 'win32') {
return path.join(process.env['HOMEDRIVE'] || 'C:', process.env['HOMEPATH'] || 'Users\\Public');
} else {
return process.env['HOME'] || process.env['PROFILE']!;
}
}
/**
* The user-local data directory. This is where user-specific persistent
* application data should be stored.
*/
get userLocalDir(): string {
if (util.isTestMode()) {
return path.join(vscode.workspace.workspaceFolders![0].uri.fsPath, '.vscode');
}
if (process.platform === 'win32') {
return this.windows.LocalAppData!;
} else {
const xdg_dir = process.env['XDG_DATA_HOME'];
if (xdg_dir) {
return xdg_dir;
}
const home = this.userHome;
return path.join(home, '.local/share');
}
}
get userRoamingDir(): string {
if (process.platform === 'win32') {
return this.windows.AppData!;
} else {
const xdg_dir = process.env['XDG_CONFIG_HOME'];
if (xdg_dir) {
return xdg_dir;
}
const home = this.userHome;
return path.join(home, '.config');
}
}
/**
* The directory where CMake Tools should store user-specific persistent
* data.
*/
get dataDir(): string {
return path.join(this.userLocalDir, 'CMakeTools');
}
/**
* The "roaming" directory where CMake Tools stores roaming configuration
* data.
*/
get roamingDataDir(): string {
return path.join(this.userRoamingDir, 'CMakeTools');
}
/**
* Get the platform-specific temporary directory
*/
get tmpDir(): string {
if (process.platform === 'win32') {
return this.windows.Temp!;
} else {
return '/tmp';
}
}
get ninjaPath() {
return this._ninjaPath;
}
async which(name: string, searchPATH?: string): Promise<string | null> {
return new Promise<string | null>(resolve => {
const options: which.AsyncOptions & which.OptionsFirst = { all: false };
if (searchPATH) {
options.path = searchPATH;
}
which(name, options, (err, resolved) => {
if (err) {
resolve(null);
} else {
console.assert(resolved, '`which` didn\'t do what it should have.');
resolve(resolved!);
}
});
});
}
async getCTestPath(wsc: DirectoryContext, overWriteCMakePathSetting?: string, searchPATH?: string): Promise<string | null> {
const ctestPath = await this.expandStringPath(wsc.config.rawCTestPath, wsc);
if (!ctestPath || ctestPath === 'auto' || overWriteCMakePathSetting) {
const cmake = await this.getCMakePath(wsc, overWriteCMakePathSetting, searchPATH);
if (cmake === null) {
return null;
} else {
try {
// Check if CTest is a sibling executable in the same directory
const ctestName = process.platform === 'win32' ? 'ctest.exe' : 'ctest';
const ctestSibling = path.join(path.dirname(cmake), ctestName);
await fs.access(ctestSibling, fs.constants.X_OK);
return ctestSibling;
} catch {
// The best we can do.
return 'ctest';
}
}
} else {
return ctestPath;
}
}
async getCPackPath(wsc: DirectoryContext, overWriteCMakePathSetting?: string, searchPATH?: string): Promise<string | null> {
const cpackPath = await this.expandStringPath(wsc.config.rawCPackPath, wsc);
if (!cpackPath || cpackPath === 'auto' || overWriteCMakePathSetting) {
const cmake = await this.getCMakePath(wsc, overWriteCMakePathSetting, searchPATH);
if (cmake === null) {
return null;
} else {
try {
// Check if CPack is a sibling executable in the same directory
const cpackName = process.platform === 'win32' ? 'cpack.exe' : 'cpack';
const cpackSibling = path.join(path.dirname(cmake), cpackName);
await fs.access(cpackSibling, fs.constants.X_OK);
return cpackSibling;
} catch {
// The best we can do.
return 'cpack';
}
}
} else {
return cpackPath;
}
}
async getCMakePath(wsc: DirectoryContext, overWriteCMakePathSetting?: string, searchPATH?: string): Promise<string | null> {
this._ninjaPath = undefined;
let raw = overWriteCMakePathSetting;
if (!raw) {
raw = await this.expandStringPath(wsc.config.rawCMakePath, wsc);
}
if (raw === 'auto' || raw === 'cmake') {
// We start by searching $PATH for cmake
const on_path = await this.which('cmake', searchPATH);
if (on_path) {
return on_path;
}
if (process.platform === 'win32') {
// We didn't find it on the $PATH. Try some good guesses
const cmake_relative_path = '\\CMake\\bin\\cmake.exe';
const default_cmake_paths = [
this.windows.ProgramFiles! + cmake_relative_path,
this.windows.ProgramFilesX86! + cmake_relative_path
];
for (const cmake_path of default_cmake_paths) {
if (await fs.exists(cmake_path)) {
return cmake_path;
}
}
// Look for bundled CMake executables in Visual Studio install paths
const bundled_tools_paths = await this.vsCMakePaths();
if (bundled_tools_paths.cmake) {
this._ninjaPath = bundled_tools_paths.ninja;
return bundled_tools_paths.cmake!;
}
}
return null;
}
return raw;
}
async expandStringPath(raw_path: string, wsc: DirectoryContext): Promise<string> {
return expandString(raw_path, {
vars: {
buildKit: '${buildKit}',
buildType: '${buildType}',
buildKitVendor: '${buildKitVendor}',
buildKitTriple: '${buildKitTriple}',
buildKitVersion: '${buildKitVersion}',
buildKitHostOs: '${buildKitVendor}',
buildKitTargetOs: '${buildKitTargetOs}',
buildKitTargetArch: '${buildKitTargetArch}',
buildKitVersionMajor: '${buildKitVersionMajor}',
buildKitVersionMinor: '${buildKitVersionMinor}',
generator: '${generator}',
userHome: this.userHome,
workspaceFolder: wsc.folder.uri.fsPath,
workspaceFolderBasename: path.basename(wsc.folder.uri.fsPath),
sourceDir: '${sourceDir}',
workspaceHash: util.makeHashString(wsc.folder.uri.fsPath),
workspaceRoot: wsc.folder.uri.fsPath,
workspaceRootFolderName: path.basename(wsc.folder.uri.fsPath)
}
});
}
async vsCMakePaths(preferredInstanceId?: string): Promise<VSCMakePaths> {
const vsCMakePaths: VSCMakePaths = {};
const vs_installations = await vsInstallations();
if (vs_installations.length > 0) {
const bundled_tool_paths = [] as { cmake: string; ninja: string; instanceId: string; version: util.Version }[];
for (const install of vs_installations) {
const bundled_tool_path = {
cmake: install.installationPath + '\\Common7\\IDE\\CommonExtensions\\Microsoft\\CMake\\CMake\\bin\\cmake.exe',
ninja: install.installationPath + '\\Common7\\IDE\\CommonExtensions\\Microsoft\\CMake\\Ninja\\ninja.exe',
instanceId: install.instanceId,
version: util.parseVersion(install.installationVersion)
};
bundled_tool_paths.push(bundled_tool_path);
}
bundled_tool_paths.sort((a, b) => {
if (preferredInstanceId === a.instanceId) {
return -1;
} else if (preferredInstanceId === b.instanceId) {
return 1;
}
return util.versionGreater(a.version, b.version) ? -1 : util.versionEquals(a.version, b.version) ? 0 : 1;
});
for (const tool_path of bundled_tool_paths) {
if (await fs.exists(tool_path.cmake)) {
// CMake can be still used without Ninja
vsCMakePaths.cmake = tool_path.cmake;
// Check for Ninja in case it was removed in later VS versions
if (await fs.exists(tool_path.ninja)) {
vsCMakePaths.ninja = tool_path.ninja;
// Return the first CMake/Ninja set found
break;
}
}
}
}
return vsCMakePaths;
}
}
const paths = new Paths();
export default paths;