forked from dotnet/msbuild
/
IntrinsicFunctions.cs
645 lines (553 loc) · 24.3 KB
/
IntrinsicFunctions.cs
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Microsoft.Build.Internal;
using Microsoft.Build.Shared;
using Microsoft.Build.Shared.FileSystem;
using Microsoft.Build.Utilities;
using Microsoft.Win32;
// Needed for DoesTaskHostExistForParameters
using NodeProviderOutOfProcTaskHost = Microsoft.Build.BackEnd.NodeProviderOutOfProcTaskHost;
namespace Microsoft.Build.Evaluation
{
/// <summary>
/// The Intrinsic class provides static methods that can be accessed from MSBuild's
/// property functions using $([MSBuild]::Function(x,y))
/// </summary>
internal static class IntrinsicFunctions
{
#if FEATURE_WIN32_REGISTRY
private static readonly object[] DefaultRegistryViews = new object[] { RegistryView.Default };
private static readonly Lazy<Regex> RegistrySdkRegex = new Lazy<Regex>(() => new Regex(@"^HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Microsoft SDKs\\Windows\\v(\d+\.\d+)$", RegexOptions.IgnoreCase));
#endif // FEATURE_WIN32_REGISTRY
private static readonly Lazy<NuGetFrameworkWrapper> NuGetFramework = new Lazy<NuGetFrameworkWrapper>(() => new NuGetFrameworkWrapper());
/// <summary>
/// Add two doubles
/// </summary>
internal static double Add(double a, double b)
{
return a + b;
}
/// <summary>
/// Add two longs
/// </summary>
internal static long Add(long a, long b)
{
return a + b;
}
/// <summary>
/// Subtract two doubles
/// </summary>
internal static double Subtract(double a, double b)
{
return a - b;
}
/// <summary>
/// Subtract two longs
/// </summary>
internal static long Subtract(long a, long b)
{
return a - b;
}
/// <summary>
/// Multiply two doubles
/// </summary>
internal static double Multiply(double a, double b)
{
return a * b;
}
/// <summary>
/// Multiply two longs
/// </summary>
internal static long Multiply(long a, long b)
{
return a * b;
}
/// <summary>
/// Divide two doubles
/// </summary>
internal static double Divide(double a, double b)
{
return a / b;
}
/// <summary>
/// Divide two longs
/// </summary>
internal static long Divide(long a, long b)
{
return a / b;
}
/// <summary>
/// Modulo two doubles
/// </summary>
internal static double Modulo(double a, double b)
{
return a % b;
}
/// <summary>
/// Modulo two longs
/// </summary>
internal static long Modulo(long a, long b)
{
return a % b;
}
/// <summary>
/// Escape the string according to MSBuild's escaping rules
/// </summary>
internal static string Escape(string unescaped)
{
return EscapingUtilities.Escape(unescaped);
}
/// <summary>
/// Unescape the string according to MSBuild's escaping rules
/// </summary>
internal static string Unescape(string escaped)
{
return EscapingUtilities.UnescapeAll(escaped);
}
/// <summary>
/// Perform a bitwise OR on the first and second (first | second)
/// </summary>
internal static int BitwiseOr(int first, int second)
{
return first | second;
}
/// <summary>
/// Perform a bitwise AND on the first and second (first & second)
/// </summary>
internal static int BitwiseAnd(int first, int second)
{
return first & second;
}
/// <summary>
/// Perform a bitwise XOR on the first and second (first ^ second)
/// </summary>
internal static int BitwiseXor(int first, int second)
{
return first ^ second;
}
/// <summary>
/// Perform a bitwise NOT on the first and second (~first)
/// </summary>
internal static int BitwiseNot(int first)
{
return ~first;
}
#if FEATURE_WIN32_REGISTRY
/// <summary>
/// Get the value of the registry key and value, default value is null
/// </summary>
internal static object GetRegistryValue(string keyName, string valueName)
{
return Registry.GetValue(keyName, valueName, null /* null to match the $(Regsitry:XYZ@ZBC) behaviour */);
}
/// <summary>
/// Get the value of the registry key and value
/// </summary>
internal static object GetRegistryValue(string keyName, string valueName, object defaultValue)
{
return Registry.GetValue(keyName, valueName, defaultValue);
}
internal static object GetRegistryValueFromView(string keyName, string valueName, object defaultValue, params object[] views)
{
if (views == null || views.Length == 0)
{
views = DefaultRegistryViews;
}
return GetRegistryValueFromView(keyName, valueName, defaultValue, new ArraySegment<object>(views));
}
/// <summary>
/// Get the value of the registry key from one of the RegistryView's specified
/// </summary>
internal static object GetRegistryValueFromView(string keyName, string valueName, object defaultValue, ArraySegment<object> views)
{
// We will take on handing of default value
// A we need to act on the null return from the GetValue call below
// so we can keep searching other registry views
object result = defaultValue;
// If we haven't been passed any views, then we'll just use the default view
if (views.Count == 0)
{
views = new ArraySegment<object>(DefaultRegistryViews);
}
foreach (object viewObject in views)
{
if (viewObject is string viewAsString)
{
string typeLeafName = typeof(RegistryView).Name + ".";
string typeFullName = typeof(RegistryView).FullName + ".";
// We'll allow the user to specify the leaf or full type name on the RegistryView enum
viewAsString = viewAsString.Replace(typeFullName, "").Replace(typeLeafName, "");
// This may throw - and that's fine as the user will receive a controlled version
// of that error.
RegistryView view = (RegistryView)Enum.Parse(typeof(RegistryView), viewAsString, true);
if (!NativeMethodsShared.IsWindows && !keyName.StartsWith("HKEY_CURRENT_USER", StringComparison.OrdinalIgnoreCase))
{
// Fake common requests to HKLM that we can resolve
// See if this asks for a specific SDK
var m = RegistrySdkRegex.Value.Match(keyName);
if (m.Success && m.Groups.Count >= 1 && valueName.Equals("InstallRoot", StringComparison.OrdinalIgnoreCase))
{
return Path.Combine(NativeMethodsShared.FrameworkBasePath, m.Groups[0].Value) + Path.DirectorySeparatorChar;
}
return string.Empty;
}
using (RegistryKey key = GetBaseKeyFromKeyName(keyName, view, out string subKeyName))
{
if (key != null)
{
using (RegistryKey subKey = key.OpenSubKey(subKeyName, false))
{
// If we managed to retrieve the subkey, then move onto locating the value
if (subKey != null)
{
result = subKey.GetValue(valueName);
}
// We've found a value, so stop looking
if (result != null)
{
break;
}
}
}
}
}
}
// We will have either found a result or defaultValue if one wasn't found at this point
return result;
}
#else // FEATURE_WIN32_REGISTRY is off, need to mock the function names to let scrips call these property functions and get NULLs rather than fail with errors
/// <summary>
/// Get the value of the registry key and value, default value is null
/// </summary>
internal static object GetRegistryValue(string keyName, string valueName)
{
return null; // FEATURE_WIN32_REGISTRY is off, need to mock the function names to let scrips call these property functions and get NULLs rather than fail with errors
}
/// <summary>
/// Get the value of the registry key and value
/// </summary>
internal static object GetRegistryValue(string keyName, string valueName, object defaultValue)
{
return defaultValue; // FEATURE_WIN32_REGISTRY is off, need to mock the function names to let scrips call these property functions and get NULLs rather than fail with errors
}
/// <summary>
/// Get the value of the registry key from one of the RegistryView's specified
/// </summary>
internal static object GetRegistryValueFromView(string keyName, string valueName, object defaultValue, params object[] views)
{
return defaultValue; // FEATURE_WIN32_REGISTRY is off, need to mock the function names to let scrips call these property functions and get NULLs rather than fail with errors
}
#endif
/// <summary>
/// Given the absolute location of a file, and a disc location, returns relative file path to that disk location.
/// Throws UriFormatException.
/// </summary>
/// <param name="basePath">
/// The base path we want to relativize to. Must be absolute.
/// Should <i>not</i> include a filename as the last segment will be interpreted as a directory.
/// </param>
/// <param name="path">
/// The path we need to make relative to basePath. The path can be either absolute path or a relative path in which case it is relative to the base path.
/// If the path cannot be made relative to the base path (for example, it is on another drive), it is returned verbatim.
/// </param>
/// <returns>relative path (can be the full path)</returns>
internal static string MakeRelative(string basePath, string path)
{
string result = FileUtilities.MakeRelative(basePath, path);
return result;
}
/// <summary>
/// Searches upward for a directory containing the specified file, beginning in the specified directory.
/// </summary>
/// <param name="startingDirectory">The directory to start the search in.</param>
/// <param name="fileName">The name of the file to search for.</param>
/// <param name="fileSystem">The file system abstraction to use that implements file system operations</param>
/// <returns>The full path of the directory containing the file if it is found, otherwise an empty string. </returns>
internal static string GetDirectoryNameOfFileAbove(string startingDirectory, string fileName, IFileSystem fileSystem)
{
return FileUtilities.GetDirectoryNameOfFileAbove(startingDirectory, fileName, fileSystem);
}
/// <summary>
/// Searches upward for the specified file, beginning in the specified <see cref="IElementLocation"/>.
/// </summary>
/// <param name="file">The name of the file to search for.</param>
/// <param name="startingDirectory">An optional directory to start the search in. The default location is the directory
/// <param name="fileSystem">The file system abstraction to use that implements file system operations</param>
/// of the file containing the property function.</param>
/// <returns>The full path of the file if it is found, otherwise an empty string.</returns>
internal static string GetPathOfFileAbove(string file, string startingDirectory, IFileSystem fileSystem)
{
return FileUtilities.GetPathOfFileAbove(file, startingDirectory, fileSystem);
}
/// <summary>
/// Return the string in parameter 'defaultValue' only if parameter 'conditionValue' is empty
/// else, return the value conditionValue
/// </summary>
internal static string ValueOrDefault(string conditionValue, string defaultValue)
{
if (String.IsNullOrEmpty(conditionValue))
{
return defaultValue;
}
else
{
return conditionValue;
}
}
///<summary>
/// Hash the string independent of bitness and target framework.
/// </summary>
internal static int StableStringHash(string toHash)
{
return CommunicationsUtilities.GetHashCode(toHash);
}
/// <summary>
/// Returns true if a task host exists that can service the requested runtime and architecture
/// values, and false otherwise.
/// </summary>
internal static bool DoesTaskHostExist(string runtime, string architecture)
{
if (runtime != null)
{
runtime = runtime.Trim();
}
if (architecture != null)
{
architecture = architecture.Trim();
}
if (!XMakeAttributes.IsValidMSBuildRuntimeValue(runtime))
{
ErrorUtilities.ThrowArgument("InvalidTaskHostFactoryParameter", runtime, "Runtime", XMakeAttributes.MSBuildRuntimeValues.clr2, XMakeAttributes.MSBuildRuntimeValues.clr4, XMakeAttributes.MSBuildRuntimeValues.currentRuntime, XMakeAttributes.MSBuildRuntimeValues.any);
}
if (!XMakeAttributes.IsValidMSBuildArchitectureValue(architecture))
{
ErrorUtilities.ThrowArgument("InvalidTaskHostFactoryParameter", architecture, "Architecture", XMakeAttributes.MSBuildArchitectureValues.x86, XMakeAttributes.MSBuildArchitectureValues.x64, XMakeAttributes.MSBuildArchitectureValues.currentArchitecture, XMakeAttributes.MSBuildArchitectureValues.any);
}
runtime = XMakeAttributes.GetExplicitMSBuildRuntime(runtime);
architecture = XMakeAttributes.GetExplicitMSBuildArchitecture(architecture);
IDictionary<string, string> parameters = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
parameters.Add(XMakeAttributes.runtime, runtime);
parameters.Add(XMakeAttributes.architecture, architecture);
HandshakeOptions desiredContext = CommunicationsUtilities.GetHandshakeOptions(taskHost: true, taskHostParameters: parameters);
string taskHostLocation = NodeProviderOutOfProcTaskHost.GetMSBuildLocationFromHostContext(desiredContext);
if (taskHostLocation != null && FileUtilities.FileExistsNoThrow(taskHostLocation))
{
return true;
}
return false;
}
/// <summary>
/// If the given path doesn't have a trailing slash then add one.
/// If the path is an empty string, does not modify it.
/// </summary>
/// <param name="path">The path to check.</param>
/// <returns>The specified path with a trailing slash.</returns>
internal static string EnsureTrailingSlash(string path)
{
return FileUtilities.EnsureTrailingSlash(path);
}
/// <summary>
/// Gets the canonicalized full path of the provided directory and ensures it contains the correct directory separator characters for the current operating system
/// while ensuring it has a trailing slash.
/// </summary>
/// <param name="path">One or more directory paths to combine and normalize.</param>
/// <returns>A canonicalized full directory path with the correct directory separators and a trailing slash.</returns>
internal static string NormalizeDirectory(params string[] path)
{
return EnsureTrailingSlash(NormalizePath(path));
}
/// <summary>
/// Gets the canonicalized full path of the provided path and ensures it contains the correct directory separator characters for the current operating system.
/// </summary>
/// <param name="path">One or more paths to combine and normalize.</param>
/// <returns>A canonicalized full path with the correct directory separators.</returns>
internal static string NormalizePath(params string[] path)
{
return FileUtilities.NormalizePath(path);
}
/// <summary>
/// Specify whether the current OS platform is <paramref name="platformString"/>
/// </summary>
/// <param name="platformString">The platform string. Must be a member of <see cref="OSPlatform"/>. Case Insensitive</param>
/// <returns></returns>
internal static bool IsOSPlatform(string platformString)
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Create(platformString.ToUpperInvariant()));
}
/// <summary>
/// True if current OS is a Unix system.
/// </summary>
/// <returns></returns>
internal static bool IsOsUnixLike()
{
return NativeMethodsShared.IsUnixLike;
}
/// <summary>
/// True if current OS is a BSD system.
/// </summary>
/// <returns></returns>
internal static bool IsOsBsdLike()
{
return NativeMethodsShared.IsBSD;
}
internal static bool VersionEquals(string a, string b)
{
return SimpleVersion.Parse(a) == SimpleVersion.Parse(b);
}
internal static bool VersionNotEquals(string a, string b)
{
return SimpleVersion.Parse(a) != SimpleVersion.Parse(b);
}
internal static bool VersionGreaterThan(string a, string b)
{
return SimpleVersion.Parse(a) > SimpleVersion.Parse(b);
}
internal static bool VersionGreaterThanOrEquals(string a, string b)
{
return SimpleVersion.Parse(a) >= SimpleVersion.Parse(b);
}
internal static bool VersionLessThan(string a, string b)
{
return SimpleVersion.Parse(a) < SimpleVersion.Parse(b);
}
internal static bool VersionLessThanOrEquals(string a, string b)
{
return SimpleVersion.Parse(a) <= SimpleVersion.Parse(b);
}
internal static string GetTargetFrameworkIdentifier(string tfm)
{
return NuGetFramework.Value.GetTargetFrameworkIdentifier(tfm);
}
internal static string GetTargetFrameworkVersion(string tfm, int versionPartCount = 2)
{
return NuGetFramework.Value.GetTargetFrameworkVersion(tfm, versionPartCount);
}
internal static bool IsTargetFrameworkCompatible(string target, string candidate)
{
return NuGetFramework.Value.IsCompatible(target, candidate);
}
internal static string GetTargetPlatformIdentifier(string tfm)
{
return NuGetFramework.Value.GetTargetPlatformIdentifier(tfm);
}
internal static string GetTargetPlatformVersion(string tfm, int versionPartCount = 2)
{
return NuGetFramework.Value.GetTargetPlatformVersion(tfm, versionPartCount);
}
internal static bool AreFeaturesEnabled(Version wave)
{
return ChangeWaves.AreFeaturesEnabled(wave);
}
public static string GetCurrentToolsDirectory()
{
return BuildEnvironmentHelper.Instance.CurrentMSBuildToolsDirectory;
}
public static string GetToolsDirectory32()
{
return BuildEnvironmentHelper.Instance.MSBuildToolsDirectory32;
}
public static string GetToolsDirectory64()
{
return BuildEnvironmentHelper.Instance.MSBuildToolsDirectory64;
}
public static string GetMSBuildSDKsPath()
{
return BuildEnvironmentHelper.Instance.MSBuildSDKsPath;
}
public static string GetVsInstallRoot()
{
return BuildEnvironmentHelper.Instance.VisualStudioInstallRootDirectory;
}
public static string GetProgramFiles32()
{
return FrameworkLocationHelper.programFiles32;
}
public static string GetMSBuildExtensionsPath()
{
return BuildEnvironmentHelper.Instance.MSBuildExtensionsPath;
}
public static bool IsRunningFromVisualStudio()
{
return BuildEnvironmentHelper.Instance.Mode == BuildEnvironmentMode.VisualStudio;
}
#region Debug only intrinsics
/// <summary>
/// returns if the string contains escaped wildcards
/// </summary>
internal static List<string> __GetListTest()
{
return new List<string> { "A", "B", "C", "D" };
}
#endregion
#if FEATURE_WIN32_REGISTRY
/// <summary>
/// Following function will parse a keyName and returns the basekey for it.
/// It will also store the subkey name in the out parameter.
/// If the keyName is not valid, we will throw ArgumentException.
/// The return value shouldn't be null.
/// Taken from: \ndp\clr\src\BCL\Microsoft\Win32\Registry.cs
/// </summary>
private static RegistryKey GetBaseKeyFromKeyName(string keyName, RegistryView view, out string subKeyName)
{
if (keyName == null)
{
throw new ArgumentNullException(nameof(keyName));
}
string basekeyName;
int i = keyName.IndexOf('\\');
if (i != -1)
{
basekeyName = keyName.Substring(0, i).ToUpperInvariant();
}
else
{
basekeyName = keyName.ToUpperInvariant();
}
RegistryKey basekey = null;
switch (basekeyName)
{
case "HKEY_CURRENT_USER":
basekey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, view);
break;
case "HKEY_LOCAL_MACHINE":
basekey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, view);
break;
case "HKEY_CLASSES_ROOT":
basekey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, view);
break;
case "HKEY_USERS":
basekey = RegistryKey.OpenBaseKey(RegistryHive.Users, view);
break;
case "HKEY_PERFORMANCE_DATA":
basekey = RegistryKey.OpenBaseKey(RegistryHive.PerformanceData, view);
break;
case "HKEY_CURRENT_CONFIG":
basekey = RegistryKey.OpenBaseKey(RegistryHive.CurrentConfig, view);
break;
#if FEATURE_REGISTRYHIVE_DYNDATA
case "HKEY_DYN_DATA":
basekey = RegistryKey.OpenBaseKey(RegistryHive.DynData, view);
break;
#endif
default:
ErrorUtilities.ThrowArgument(keyName);
break;
}
if (i == -1 || i == keyName.Length)
{
subKeyName = string.Empty;
}
else
{
subKeyName = keyName.Substring(i + 1, keyName.Length - i - 1);
}
return basekey;
}
#endif
}
}