| 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.Reflection; |
| 20 |
using NUnitLite; |
| 21 |
using System; |
| 22 |
using System.IO; |
| 23 |
using System.Runtime.InteropServices; |
| 24 |
|
| 25 |
namespace Grpc.Tools.Tests |
| 26 |
{ |
| 27 |
static class MsBuildAssemblyHelper |
| 28 |
{ |
| 29 |
[DllImport("__Internal")] |
| 30 |
extern static void mono_set_assemblies_path(string path); |
| 31 |
|
| 32 |
public static void TweakAssemblyPathIfOnMono() |
| 33 |
{ |
| 34 |
// Below is a hack to allow the tests to run under Mono Framework build. |
| 35 |
// Mono unfortunately comes with broken Microsoft.Build.* assemblies installed in |
| 36 |
// the GAC, so we need to tweak the assembly search path to make sure the right |
| 37 |
// msbuild assemblies are loaded (and the tests work). |
| 38 |
#if NET45 |
| 39 |
// only run this under .NET framework; under mono |
| 40 |
bool isMono = Type.GetType("Mono.Runtime") != null; |
| 41 |
if (isMono) |
| 42 |
{ |
| 43 |
var mscorlibDir = Path.GetDirectoryName(typeof(Array).Assembly.Location); |
| 44 |
// Construct the location of MsBuild assemblies from the location of mscorlib assembly. |
| 45 |
var msbuildToolPath = Path.Combine(mscorlibDir, "..", "msbuild", "Current", "bin"); |
| 46 |
|
| 47 |
if (!Directory.Exists(msbuildToolPath)) |
| 48 |
{ |
| 49 |
// with older versions of mono for Mac (e.g. mono 5.16.0 which is currently |
| 50 |
// installed on the kokoro mac workers) the "Current" symlink doesn't exist |
| 51 |
// so also try specifying the msbuild version explicitly |
| 52 |
msbuildToolPath = Path.Combine(mscorlibDir, "..", "msbuild", "15.0", "bin"); |
| 53 |
} |
| 54 |
|
| 55 |
// To make sure we've constructed the right path, make sure the assemblies we're interested |
| 56 |
// in are there. |
| 57 |
foreach(var assemblyName in new [] {"Microsoft.Build.Framework.dll", "Microsoft.Build.Utilities.v4.0.dll", "Microsoft.Build.Utilities.Core.dll"}) |
| 58 |
{ |
| 59 |
if (!File.Exists(Path.Combine(msbuildToolPath, assemblyName))) |
| 60 |
{ |
| 61 |
throw new InvalidOperationException($"Could not locate assembly {assemblyName} under {msbuildToolPath}"); |
| 62 |
} |
| 63 |
} |
| 64 |
// Normally the assembly search path can be changed by MONO_PATH environment variable, but it needs to be done |
| 65 |
// before the process starts. The following internal method allows us to do the same thing. |
| 66 |
mono_set_assemblies_path(msbuildToolPath); |
| 67 |
} |
| 68 |
#endif |
| 69 |
} |
| 70 |
} |
| 71 |
} |