MsBuildAssemblyHelper.cs 3.1 KB

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