1 #region Copyright notice and license 2 3 // Copyright 2018 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #endregion 18 19 using System; 20 using System.IO; 21 using System.Runtime.CompilerServices; 22 using System.Runtime.InteropServices; 23 using System.Security; 24 25 namespace Grpc.Tools 26 { 27 // Metadata names (MSBuild item attributes) that we refer to often. 28 static class Metadata 29 { 30 // On output dependency lists. 31 public static string Source = "Source"; 32 // On Protobuf items. 33 public static string ProtoRoot = "ProtoRoot"; 34 public static string OutputDir = "OutputDir"; 35 public static string GrpcServices = "GrpcServices"; 36 public static string GrpcOutputDir = "GrpcOutputDir"; 37 }; 38 39 // A few flags used to control the behavior under various platforms. 40 internal static class Platform 41 { 42 public enum OsKind { Unknown, Windows, Linux, MacOsX }; 43 public static readonly OsKind Os; 44 45 public enum CpuKind { Unknown, X86, X64 }; 46 public static readonly CpuKind Cpu; 47 48 // This is not necessarily true, but good enough. BCL lacks a per-FS 49 // API to determine file case sensitivity. 50 public static bool IsFsCaseInsensitive => Os == OsKind.Windows; 51 public static bool IsWindows => Os == OsKind.Windows; 52 Platform()53 static Platform() 54 { 55 #if NETCORE 56 Os = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? OsKind.Windows 57 : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? OsKind.Linux 58 : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? OsKind.MacOsX 59 : OsKind.Unknown; 60 61 switch (RuntimeInformation.ProcessArchitecture) 62 { 63 case Architecture.X86: Cpu = CpuKind.X86; break; 64 case Architecture.X64: Cpu = CpuKind.X64; break; 65 // We do not have build tools for other architectures. 66 default: Cpu = CpuKind.Unknown; break; 67 } 68 #else 69 // Running under either Mono or full MS framework. 70 Os = OsKind.Windows; 71 if (Type.GetType("Mono.Runtime", throwOnError: false) != null) 72 { 73 // Congratulations. We are running under Mono. 74 var plat = Environment.OSVersion.Platform; 75 if (plat == PlatformID.MacOSX) 76 { 77 Os = OsKind.MacOsX; 78 } 79 else if (plat == PlatformID.Unix || (int)plat == 128) 80 { 81 // This is how Mono detects OSX internally. 82 Os = File.Exists("/usr/lib/libc.dylib") ? OsKind.MacOsX : OsKind.Linux; 83 } 84 } 85 86 // Hope we are not building on ARM under Xamarin! 87 Cpu = Environment.Is64BitProcess ? CpuKind.X64 : CpuKind.X86; 88 #endif 89 } 90 }; 91 92 // Exception handling helpers. 93 static class Exceptions 94 { 95 // Returns true iff the exception indicates an error from an I/O call. See 96 // https://github.com/Microsoft/msbuild/blob/v15.4.8.50001/src/Shared/ExceptionHandling.cs#L101 97 static public bool IsIoRelated(Exception ex) => 98 ex is IOException || 99 (ex is ArgumentException && !(ex is ArgumentNullException)) || 100 ex is SecurityException || 101 ex is UnauthorizedAccessException || 102 ex is NotSupportedException; 103 }; 104 105 // String helpers. 106 static class Strings 107 { 108 // Compare string to argument using OrdinalIgnoreCase comparison. EqualNoCase(this string a, string b)109 public static bool EqualNoCase(this string a, string b) => 110 string.Equals(a, b, StringComparison.OrdinalIgnoreCase); 111 } 112 } 113