xref: /aosp_15_r20/external/grpc-grpc/src/csharp/Grpc.Tools/ProtoToolsPlatform.cs (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 Microsoft.Build.Framework;
20 using Microsoft.Build.Utilities;
21 using Grpc.Core.Internal;
22 
23 namespace Grpc.Tools
24 {
25     /// <summary>
26     /// A helper task to resolve actual OS type and bitness.
27     /// </summary>
28     public class ProtoToolsPlatform : Task
29     {
30         /// <summary>
31         /// Return one of 'linux', 'macosx' or 'windows'.
32         /// If the OS is unknown, the property is not set.
33         /// </summary>
34         [Output]
35         public string Os { get; set; }
36 
37         /// <summary>
38         /// Return one of 'x64', 'x86', 'arm64'.
39         /// If the CPU is unknown, the property is not set.
40         /// </summary>
41         [Output]
42         public string Cpu { get; set; }
43 
44 
Execute()45         public override bool Execute()
46         {
47             switch (Platform.Os)
48             {
49                 case CommonPlatformDetection.OSKind.Linux: Os = "linux"; break;
50                 case CommonPlatformDetection.OSKind.MacOSX: Os = "macosx"; break;
51                 case CommonPlatformDetection.OSKind.Windows: Os = "windows"; break;
52                 default: Os = ""; break;
53             }
54 
55             switch (Platform.Cpu)
56             {
57                 case CommonPlatformDetection.CpuArchitecture.X86: Cpu = "x86"; break;
58                 case CommonPlatformDetection.CpuArchitecture.X64: Cpu = "x64"; break;
59                 case CommonPlatformDetection.CpuArchitecture.Arm64: Cpu = "arm64"; break;
60                 default: Cpu = ""; break;
61             }
62 
63             // Use x64 on macosx arm64 until a native protoc is shipped
64             if (Os == "macosx" && Cpu == "arm64")
65             {
66                 Cpu = "x64";
67             }
68             // Use x86 on Windows arm64 until a native protoc is shipped
69             else if (Os == "windows" && Cpu == "arm64")
70             {
71                 Cpu = "x86";
72             }
73 
74             return true;
75         }
76     };
77 }
78