1*cc02d7e2SAndroid Build Coastguard Worker #region Copyright notice and license 2*cc02d7e2SAndroid Build Coastguard Worker 3*cc02d7e2SAndroid Build Coastguard Worker // Copyright 2018 gRPC authors. 4*cc02d7e2SAndroid Build Coastguard Worker // 5*cc02d7e2SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); 6*cc02d7e2SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License. 7*cc02d7e2SAndroid Build Coastguard Worker // You may obtain a copy of the License at 8*cc02d7e2SAndroid Build Coastguard Worker // 9*cc02d7e2SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0 10*cc02d7e2SAndroid Build Coastguard Worker // 11*cc02d7e2SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 12*cc02d7e2SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, 13*cc02d7e2SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*cc02d7e2SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and 15*cc02d7e2SAndroid Build Coastguard Worker // limitations under the License. 16*cc02d7e2SAndroid Build Coastguard Worker 17*cc02d7e2SAndroid Build Coastguard Worker #endregion 18*cc02d7e2SAndroid Build Coastguard Worker 19*cc02d7e2SAndroid Build Coastguard Worker using System.Reflection; 20*cc02d7e2SAndroid Build Coastguard Worker using NUnitLite; 21*cc02d7e2SAndroid Build Coastguard Worker using System; 22*cc02d7e2SAndroid Build Coastguard Worker using System.IO; 23*cc02d7e2SAndroid Build Coastguard Worker using System.Runtime.InteropServices; 24*cc02d7e2SAndroid Build Coastguard Worker 25*cc02d7e2SAndroid Build Coastguard Worker namespace Grpc.Tools.Tests 26*cc02d7e2SAndroid Build Coastguard Worker { 27*cc02d7e2SAndroid Build Coastguard Worker static class MsBuildAssemblyHelper 28*cc02d7e2SAndroid Build Coastguard Worker { 29*cc02d7e2SAndroid Build Coastguard Worker [DllImport("__Internal")] mono_set_assemblies_path(string path)30*cc02d7e2SAndroid Build Coastguard Worker extern static void mono_set_assemblies_path(string path); 31*cc02d7e2SAndroid Build Coastguard Worker TweakAssemblyPathIfOnMono()32*cc02d7e2SAndroid Build Coastguard Worker public static void TweakAssemblyPathIfOnMono() 33*cc02d7e2SAndroid Build Coastguard Worker { 34*cc02d7e2SAndroid Build Coastguard Worker // Below is a hack to allow the tests to run under Mono Framework build. 35*cc02d7e2SAndroid Build Coastguard Worker // Mono unfortunately comes with broken Microsoft.Build.* assemblies installed in 36*cc02d7e2SAndroid Build Coastguard Worker // the GAC, so we need to tweak the assembly search path to make sure the right 37*cc02d7e2SAndroid Build Coastguard Worker // msbuild assemblies are loaded (and the tests work). 38*cc02d7e2SAndroid Build Coastguard Worker #if NET45 39*cc02d7e2SAndroid Build Coastguard Worker // only run this under .NET framework; under mono 40*cc02d7e2SAndroid Build Coastguard Worker bool isMono = Type.GetType("Mono.Runtime") != null; 41*cc02d7e2SAndroid Build Coastguard Worker if (isMono) 42*cc02d7e2SAndroid Build Coastguard Worker { 43*cc02d7e2SAndroid Build Coastguard Worker var mscorlibDir = Path.GetDirectoryName(typeof(Array).Assembly.Location); 44*cc02d7e2SAndroid Build Coastguard Worker // Construct the location of MsBuild assemblies from the location of mscorlib assembly. 45*cc02d7e2SAndroid Build Coastguard Worker var msbuildToolPath = Path.Combine(mscorlibDir, "..", "msbuild", "Current", "bin"); 46*cc02d7e2SAndroid Build Coastguard Worker 47*cc02d7e2SAndroid Build Coastguard Worker if (!Directory.Exists(msbuildToolPath)) 48*cc02d7e2SAndroid Build Coastguard Worker { 49*cc02d7e2SAndroid Build Coastguard Worker // with older versions of mono for Mac (e.g. mono 5.16.0 which is currently 50*cc02d7e2SAndroid Build Coastguard Worker // installed on the kokoro mac workers) the "Current" symlink doesn't exist 51*cc02d7e2SAndroid Build Coastguard Worker // so also try specifying the msbuild version explicitly 52*cc02d7e2SAndroid Build Coastguard Worker msbuildToolPath = Path.Combine(mscorlibDir, "..", "msbuild", "15.0", "bin"); 53*cc02d7e2SAndroid Build Coastguard Worker } 54*cc02d7e2SAndroid Build Coastguard Worker 55*cc02d7e2SAndroid Build Coastguard Worker // To make sure we've constructed the right path, make sure the assemblies we're interested 56*cc02d7e2SAndroid Build Coastguard Worker // in are there. 57*cc02d7e2SAndroid Build Coastguard Worker foreach(var assemblyName in new [] {"Microsoft.Build.Framework.dll", "Microsoft.Build.Utilities.v4.0.dll", "Microsoft.Build.Utilities.Core.dll"}) 58*cc02d7e2SAndroid Build Coastguard Worker { 59*cc02d7e2SAndroid Build Coastguard Worker if (!File.Exists(Path.Combine(msbuildToolPath, assemblyName))) 60*cc02d7e2SAndroid Build Coastguard Worker { 61*cc02d7e2SAndroid Build Coastguard Worker throw new InvalidOperationException($"Could not locate assembly {assemblyName} under {msbuildToolPath}"); 62*cc02d7e2SAndroid Build Coastguard Worker } 63*cc02d7e2SAndroid Build Coastguard Worker } 64*cc02d7e2SAndroid Build Coastguard Worker // Normally the assembly search path can be changed by MONO_PATH environment variable, but it needs to be done 65*cc02d7e2SAndroid Build Coastguard Worker // before the process starts. The following internal method allows us to do the same thing. 66*cc02d7e2SAndroid Build Coastguard Worker mono_set_assemblies_path(msbuildToolPath); 67*cc02d7e2SAndroid Build Coastguard Worker } 68*cc02d7e2SAndroid Build Coastguard Worker #endif 69*cc02d7e2SAndroid Build Coastguard Worker } 70*cc02d7e2SAndroid Build Coastguard Worker } 71*cc02d7e2SAndroid Build Coastguard Worker } 72