Jan Tattermusch | afab541 | 2016-01-12 17:57:25 -0800 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | afab541 | 2016-01-12 17:57:25 -0800 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Jan Tattermusch | afab541 | 2016-01-12 17:57:25 -0800 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | afab541 | 2016-01-12 17:57:25 -0800 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | afab541 | 2016-01-12 17:57:25 -0800 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
| 19 | using System; |
| 20 | using System.Collections.Concurrent; |
| 21 | using System.Diagnostics; |
| 22 | using System.IO; |
| 23 | using System.Reflection; |
| 24 | using System.Runtime.InteropServices; |
| 25 | using System.Threading; |
Jan Tattermusch | d35471a | 2018-03-07 20:37:02 +0100 | [diff] [blame] | 26 | using Grpc.Core.Utils; |
Jan Tattermusch | afab541 | 2016-01-12 17:57:25 -0800 | [diff] [blame] | 27 | |
| 28 | namespace Grpc.Core.Internal |
| 29 | { |
| 30 | /// <summary> |
| 31 | /// Utility methods for detecting platform and architecture. |
| 32 | /// </summary> |
| 33 | internal static class PlatformApis |
| 34 | { |
Jan Tattermusch | 660547d | 2018-03-07 21:49:23 +0100 | [diff] [blame] | 35 | const string UnityEngineApplicationClassName = "UnityEngine.Application, UnityEngine"; |
Jan Tattermusch | 0b907fc | 2018-07-23 19:13:41 +0200 | [diff] [blame^] | 36 | const string XamarinAndroidObjectClassName = "Java.Lang.Object, Mono.Android"; |
| 37 | const string XamarinIOSObjectClassName = "Foundation.NSObject, Xamarin.iOS"; |
| 38 | |
Jan Tattermusch | afab541 | 2016-01-12 17:57:25 -0800 | [diff] [blame] | 39 | static readonly bool isLinux; |
| 40 | static readonly bool isMacOSX; |
| 41 | static readonly bool isWindows; |
Jan Tattermusch | dd133c1 | 2016-01-31 17:07:58 -0800 | [diff] [blame] | 42 | static readonly bool isMono; |
Jan Tattermusch | 4d2df96 | 2016-09-21 19:57:10 +0200 | [diff] [blame] | 43 | static readonly bool isNetCore; |
Jan Tattermusch | a1b0ee2 | 2018-03-06 09:20:38 +0100 | [diff] [blame] | 44 | static readonly bool isUnity; |
Alexander Houben | 7a7e09c | 2018-06-30 20:45:33 +0200 | [diff] [blame] | 45 | static readonly bool isXamarin; |
Jan Tattermusch | 0b907fc | 2018-07-23 19:13:41 +0200 | [diff] [blame^] | 46 | static readonly bool isXamarinIOS; |
Alexander Houben | 7a7e09c | 2018-06-30 20:45:33 +0200 | [diff] [blame] | 47 | static readonly bool isXamarinAndroid; |
Jan Tattermusch | afab541 | 2016-01-12 17:57:25 -0800 | [diff] [blame] | 48 | |
| 49 | static PlatformApis() |
| 50 | { |
Jan Tattermusch | 317b8ac | 2016-06-16 09:36:11 -0700 | [diff] [blame] | 51 | #if NETSTANDARD1_5 |
Chris Bacon | c65ceef | 2016-02-05 09:04:46 +0000 | [diff] [blame] | 52 | isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); |
| 53 | isMacOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); |
| 54 | isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); |
Jan Tattermusch | 4d2df96 | 2016-09-21 19:57:10 +0200 | [diff] [blame] | 55 | isNetCore = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core"); |
Chris Bacon | c65ceef | 2016-02-05 09:04:46 +0000 | [diff] [blame] | 56 | #else |
Jan Tattermusch | afab541 | 2016-01-12 17:57:25 -0800 | [diff] [blame] | 57 | var platform = Environment.OSVersion.Platform; |
| 58 | |
| 59 | // PlatformID.MacOSX is never returned, commonly used trick is to identify Mac is by using uname. |
| 60 | isMacOSX = (platform == PlatformID.Unix && GetUname() == "Darwin"); |
| 61 | isLinux = (platform == PlatformID.Unix && !isMacOSX); |
| 62 | isWindows = (platform == PlatformID.Win32NT || platform == PlatformID.Win32S || platform == PlatformID.Win32Windows); |
Jan Tattermusch | 4d2df96 | 2016-09-21 19:57:10 +0200 | [diff] [blame] | 63 | isNetCore = false; |
Chris Bacon | c65ceef | 2016-02-05 09:04:46 +0000 | [diff] [blame] | 64 | #endif |
Jan Tattermusch | dd133c1 | 2016-01-31 17:07:58 -0800 | [diff] [blame] | 65 | isMono = Type.GetType("Mono.Runtime") != null; |
Jan Tattermusch | 660547d | 2018-03-07 21:49:23 +0100 | [diff] [blame] | 66 | isUnity = Type.GetType(UnityEngineApplicationClassName) != null; |
Jan Tattermusch | 0b907fc | 2018-07-23 19:13:41 +0200 | [diff] [blame^] | 67 | isXamarinIOS = Type.GetType(XamarinIOSObjectClassName) != null; |
| 68 | isXamarinAndroid = Type.GetType(XamarinAndroidObjectClassName) != null; |
| 69 | isXamarin = isXamarinIOS || isXamarinAndroid; |
Jan Tattermusch | afab541 | 2016-01-12 17:57:25 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | public static bool IsLinux |
| 73 | { |
| 74 | get { return isLinux; } |
| 75 | } |
| 76 | |
| 77 | public static bool IsMacOSX |
| 78 | { |
| 79 | get { return isMacOSX; } |
| 80 | } |
| 81 | |
| 82 | public static bool IsWindows |
| 83 | { |
| 84 | get { return isWindows; } |
| 85 | } |
| 86 | |
Jan Tattermusch | dd133c1 | 2016-01-31 17:07:58 -0800 | [diff] [blame] | 87 | public static bool IsMono |
| 88 | { |
| 89 | get { return isMono; } |
| 90 | } |
| 91 | |
Jan Tattermusch | 4d2df96 | 2016-09-21 19:57:10 +0200 | [diff] [blame] | 92 | /// <summary> |
Jan Tattermusch | a1b0ee2 | 2018-03-06 09:20:38 +0100 | [diff] [blame] | 93 | /// true if running on Unity platform. |
| 94 | /// </summary> |
| 95 | public static bool IsUnity |
| 96 | { |
| 97 | get { return isUnity; } |
| 98 | } |
| 99 | |
| 100 | /// <summary> |
Alexander Houben | 7a7e09c | 2018-06-30 20:45:33 +0200 | [diff] [blame] | 101 | /// true if running on a Xamarin platform (either Xamarin.Android or Xamarin.iOS), |
| 102 | /// false otherwise. |
| 103 | /// </summary> |
| 104 | public static bool IsXamarin |
| 105 | { |
| 106 | get { return isXamarin; } |
| 107 | } |
| 108 | |
| 109 | /// <summary> |
| 110 | /// true if running on Xamarin.iOS, false otherwise. |
| 111 | /// </summary> |
Jan Tattermusch | 0b907fc | 2018-07-23 19:13:41 +0200 | [diff] [blame^] | 112 | public static bool IsXamarinIOS |
Alexander Houben | 7a7e09c | 2018-06-30 20:45:33 +0200 | [diff] [blame] | 113 | { |
Jan Tattermusch | 0b907fc | 2018-07-23 19:13:41 +0200 | [diff] [blame^] | 114 | get { return isXamarinIOS; } |
Alexander Houben | 7a7e09c | 2018-06-30 20:45:33 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /// <summary> |
| 118 | /// true if running on Xamarin.Android, false otherwise. |
| 119 | /// </summary> |
| 120 | public static bool IsXamarinAndroid |
| 121 | { |
| 122 | get { return isXamarinAndroid; } |
| 123 | } |
| 124 | |
| 125 | /// <summary> |
Jan Tattermusch | 4d2df96 | 2016-09-21 19:57:10 +0200 | [diff] [blame] | 126 | /// true if running on .NET Core (CoreCLR), false otherwise. |
| 127 | /// </summary> |
| 128 | public static bool IsNetCore |
| 129 | { |
| 130 | get { return isNetCore; } |
| 131 | } |
| 132 | |
Jan Tattermusch | afab541 | 2016-01-12 17:57:25 -0800 | [diff] [blame] | 133 | public static bool Is64Bit |
| 134 | { |
| 135 | get { return IntPtr.Size == 8; } |
| 136 | } |
| 137 | |
Jan Tattermusch | d35471a | 2018-03-07 20:37:02 +0100 | [diff] [blame] | 138 | /// <summary> |
| 139 | /// Returns <c>UnityEngine.Application.platform</c> as a string. |
| 140 | /// See https://docs.unity3d.com/ScriptReference/Application-platform.html for possible values. |
| 141 | /// Value is obtained via reflection to avoid compile-time dependency on Unity. |
| 142 | /// This method should only be called if <c>IsUnity</c> is <c>true</c>. |
| 143 | /// </summary> |
| 144 | public static string GetUnityRuntimePlatform() |
| 145 | { |
| 146 | GrpcPreconditions.CheckState(IsUnity, "Not running on Unity."); |
Jan Tattermusch | 660547d | 2018-03-07 21:49:23 +0100 | [diff] [blame] | 147 | #if NETSTANDARD1_5 |
| 148 | return Type.GetType(UnityEngineApplicationClassName).GetTypeInfo().GetProperty("platform").GetValue(null).ToString(); |
| 149 | #else |
| 150 | return Type.GetType(UnityEngineApplicationClassName).GetProperty("platform").GetValue(null).ToString(); |
| 151 | #endif |
Jan Tattermusch | d35471a | 2018-03-07 20:37:02 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Jan Tattermusch | afab541 | 2016-01-12 17:57:25 -0800 | [diff] [blame] | 154 | [DllImport("libc")] |
| 155 | static extern int uname(IntPtr buf); |
| 156 | |
| 157 | static string GetUname() |
| 158 | { |
| 159 | var buffer = Marshal.AllocHGlobal(8192); |
| 160 | try |
| 161 | { |
| 162 | if (uname(buffer) == 0) |
| 163 | { |
| 164 | return Marshal.PtrToStringAnsi(buffer); |
| 165 | } |
| 166 | return string.Empty; |
| 167 | } |
| 168 | catch |
| 169 | { |
| 170 | return string.Empty; |
| 171 | } |
| 172 | finally |
| 173 | { |
| 174 | if (buffer != IntPtr.Zero) |
| 175 | { |
| 176 | Marshal.FreeHGlobal(buffer); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |