Skip to content

Commit

Permalink
Introduce FEATURE_ASSEMBLYLOADCONTEXT
Browse files Browse the repository at this point in the history
```sh-session
$ rep -r "#if !FEATURE_ASSEMBLY_LOADFROM" "#if FEATURE_ASSEMBLYLOADCONTEXT" *.cs
1679 files processed. 0 files changed. 0 replacements.
$ rep -r "#if FEATURE_ASSEMBLY_LOADFROM" "#if !FEATURE_ASSEMBLYLOADCONTEXT" *.cs
1679 files processed. 7 files changed. 29 replacements.
```

And manual move/rename of the definition.
  • Loading branch information
rainersigwald committed Nov 14, 2019
1 parent 6bd5e48 commit 5be281e
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/Build.UnitTests/BackEnd/TaskExecutionHost_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ private void InitializeHost(bool throwOnExecute)
ProjectInstance project = CreateTestProject();

TypeLoader typeLoader = new TypeLoader(IsTaskFactoryClass);
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
AssemblyLoadInfo loadInfo = AssemblyLoadInfo.Create(Assembly.GetAssembly(typeof(TaskBuilderTestTask.TaskBuilderTestTaskFactory)).FullName, null);
#else
AssemblyLoadInfo loadInfo = AssemblyLoadInfo.Create(typeof(TaskBuilderTestTask.TaskBuilderTestTaskFactory).GetTypeInfo().FullName, null);
Expand Down
12 changes: 6 additions & 6 deletions src/Build.UnitTests/BackEnd/TaskHostConfiguration_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void ConstructorWithNullLocation()
);
}

#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
/// <summary>
/// Test that an exception is thrown when the path to the task assembly is empty
/// </summary>
Expand Down Expand Up @@ -296,7 +296,7 @@ public void TestTranslationWithNullDictionary()
TaskHostConfiguration deserializedConfig = packet as TaskHostConfiguration;

Assert.Equal(config.TaskName, deserializedConfig.TaskName);
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
Assert.Equal(config.TaskLocation, config.TaskLocation);
#endif
Assert.Null(deserializedConfig.TaskParameters);
Expand Down Expand Up @@ -336,7 +336,7 @@ public void TestTranslationWithEmptyDictionary()
TaskHostConfiguration deserializedConfig = packet as TaskHostConfiguration;

Assert.Equal(config.TaskName, deserializedConfig.TaskName);
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
Assert.Equal(config.TaskLocation, config.TaskLocation);
#endif
Assert.NotNull(deserializedConfig.TaskParameters);
Expand Down Expand Up @@ -375,7 +375,7 @@ public void TestTranslationWithValueTypesInDictionary()
TaskHostConfiguration deserializedConfig = packet as TaskHostConfiguration;

Assert.Equal(config.TaskName, deserializedConfig.TaskName);
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
Assert.Equal(config.TaskLocation, config.TaskLocation);
#endif
Assert.NotNull(deserializedConfig.TaskParameters);
Expand Down Expand Up @@ -420,7 +420,7 @@ public void TestTranslationWithITaskItemInDictionary()
TaskHostConfiguration deserializedConfig = packet as TaskHostConfiguration;

Assert.Equal(config.TaskName, deserializedConfig.TaskName);
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
Assert.Equal(config.TaskLocation, config.TaskLocation);
#endif
Assert.NotNull(deserializedConfig.TaskParameters);
Expand Down Expand Up @@ -464,7 +464,7 @@ public void TestTranslationWithITaskItemArrayInDictionary()
TaskHostConfiguration deserializedConfig = packet as TaskHostConfiguration;

Assert.Equal(config.TaskName, deserializedConfig.TaskName);
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
Assert.Equal(config.TaskLocation, config.TaskLocation);
#endif
Assert.NotNull(deserializedConfig.TaskParameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.Build.BackEnd.SdkResolution
{
internal class SdkResolverLoader
{
#if !FEATURE_ASSEMBLY_LOADFROM
#if FEATURE_ASSEMBLYLOADCONTEXT
private readonly CoreClrAssemblyLoader _loader = new CoreClrAssemblyLoader();
#endif

Expand Down Expand Up @@ -135,7 +135,7 @@ protected virtual IEnumerable<Type> GetResolverTypes(Assembly assembly)

protected virtual Assembly LoadResolverAssembly(string resolverPath, LoggingContext loggingContext, ElementLocation location)
{
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
return Assembly.LoadFrom(resolverPath);
#else
return _loader.LoadFromPath(resolverPath);
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.BeforeCommon.targets
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<FeatureAppDomain>true</FeatureAppDomain>
<DefineConstants>$(DefineConstants);FEATURE_APPDOMAIN_UNHANDLED_EXCEPTION</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_ASPNET_COMPILER</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_ASSEMBLY_LOADFROM</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_ASSEMBLY_LOCATION</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_ASSEMBLY_GETENTRYASSEMBLY</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_ASSEMBLYNAME_CLONE</DefineConstants>
Expand Down Expand Up @@ -120,6 +119,7 @@

<PropertyGroup Condition="'$(NetCoreBuild)'=='true'">
<CompilerToolsDir>$([System.IO.Path]::Combine($(ToolPackagesDir)Microsoft.Net.Compilers, $(CompilerToolsVersion), "tools"))$([System.IO.Path]::DirectorySeparatorChar)</CompilerToolsDir>
<DefineConstants>$(DefineConstants);FEATURE_ASSEMBLYLOADCONTEXT</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_PROCESSSTARTINFO_ENVIRONMENT</DefineConstants>
<DefineConstants>$(DefineConstants);FEATURE_RUNTIMEINFORMATION</DefineConstants>
<DefineConstants>$(DefineConstants);USE_MSBUILD_DLL_EXTN</DefineConstants>
Expand Down
4 changes: 2 additions & 2 deletions src/Shared/AssemblyNameExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using System.Configuration.Assemblies;
using System.Runtime.Serialization;
using System.IO;
#if !FEATURE_ASSEMBLY_LOADFROM
#if FEATURE_ASSEMBLYLOADCONTEXT
using System.Reflection.PortableExecutable;
using System.Reflection.Metadata;
#endif
Expand Down Expand Up @@ -183,7 +183,7 @@ private AssemblyNameExtension(SerializationInfo info, StreamingContext context)
internal static AssemblyNameExtension GetAssemblyNameEx(string path)
{
AssemblyName assemblyName = null;
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
try
{
assemblyName = AssemblyName.GetAssemblyName(path);
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/TaskEngineAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Diagnostics;
using System.Globalization;

#if !FEATURE_ASSEMBLY_LOADFROM
#if FEATURE_ASSEMBLYLOADCONTEXT
using System.Runtime.Loader;
#endif
using Microsoft.Build.Shared;
Expand Down
6 changes: 3 additions & 3 deletions src/Shared/TypeLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Microsoft.Build.Shared
/// </summary>
internal class TypeLoader
{
#if !FEATURE_ASSEMBLY_LOADFROM
#if FEATURE_ASSEMBLYLOADCONTEXT
/// <summary>
/// AssemblyContextLoader used to load DLLs outside of msbuild.exe directory
/// </summary>
Expand All @@ -40,7 +40,7 @@ internal class TypeLoader
/// </summary>
private Func<Type, object, bool> _isDesiredType;

#if !FEATURE_ASSEMBLY_LOADFROM
#if FEATURE_ASSEMBLYLOADCONTEXT
static TypeLoader()
{
s_coreClrAssemblyLoader = new CoreClrAssemblyLoader();
Expand Down Expand Up @@ -155,7 +155,7 @@ private static Assembly LoadAssembly(AssemblyLoadInfo assemblyLoadInfo)
}
else
{
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
loadedAssembly = Assembly.UnsafeLoadFrom(assemblyLoadInfo.AssemblyFile);
#else
loadedAssembly = s_coreClrAssemblyLoader.LoadFromPath(assemblyLoadInfo.AssemblyFile);
Expand Down
30 changes: 15 additions & 15 deletions src/Tasks/AssemblyDependency/AssemblyInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

using Microsoft.Build.Shared;
using Microsoft.Build.Shared.FileSystem;
#if !FEATURE_ASSEMBLY_LOADFROM || MONO
#if FEATURE_ASSEMBLYLOADCONTEXT || MONO
using System.Reflection.PortableExecutable;
using System.Reflection.Metadata;
#endif
Expand All @@ -30,7 +30,7 @@ internal class AssemblyInformation : DisposableBase
{
private AssemblyNameExtension[] _assemblyDependencies;
private string[] _assemblyFiles;
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
private readonly IMetaDataDispenser _metadataDispenser;
private readonly IMetaDataAssemblyImport _assemblyImport;
private static Guid s_importerGuid = new Guid(((GuidAttribute)Attribute.GetCustomAttribute(typeof(IMetaDataImport), typeof(GuidAttribute), false)).Value);
Expand All @@ -39,14 +39,14 @@ internal class AssemblyInformation : DisposableBase
private readonly string _sourceFile;
private FrameworkName _frameworkName;

#if !FEATURE_ASSEMBLY_LOADFROM || MONO
#if FEATURE_ASSEMBLYLOADCONTEXT || MONO
private bool _metadataRead;
#endif

#if FEATURE_ASSEMBLY_LOADFROM && !MONO
#if !FEATURE_ASSEMBLYLOADCONTEXT && !MONO
private static string s_targetFrameworkAttribute = "System.Runtime.Versioning.TargetFrameworkAttribute";
#endif
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
// Borrowed from genman.
private const int GENMAN_STRING_BUF_SIZE = 1024;
private const int GENMAN_LOCALE_BUF_SIZE = 64;
Expand All @@ -56,7 +56,7 @@ static AssemblyInformation()
{
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += ReflectionOnlyAssemblyResolve;
}
#endif // FEATURE_ASSEMBLY_LOADFROM
#endif // !FEATURE_ASSEMBLYLOADCONTEXT

/// <summary>
/// Construct an instance for a source file.
Expand All @@ -68,7 +68,7 @@ internal AssemblyInformation(string sourceFile)
ErrorUtilities.VerifyThrowArgumentNull(sourceFile, nameof(sourceFile));
_sourceFile = sourceFile;

#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
if (NativeMethodsShared.IsWindows)
{
// Create the metadata dispenser and open scope on the source file.
Expand All @@ -82,7 +82,7 @@ internal AssemblyInformation(string sourceFile)
#endif
}

#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
private static Assembly ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
{
string[] nameParts = args.Name.Split(MSBuildConstants.CommaChar);
Expand Down Expand Up @@ -274,7 +274,7 @@ private FrameworkName GetFrameworkName()
// Assembly.GetCustomAttributes* for an attribute which belongs
// to an assembly that mono cannot find, causes a crash!
// Instead, opt for using PEReader and friends to get that info
#if FEATURE_ASSEMBLY_LOADFROM && !MONO
#if !FEATURE_ASSEMBLYLOADCONTEXT && !MONO
if (!NativeMethodsShared.IsWindows)
{
if (String.Equals(Environment.GetEnvironmentVariable("MONO29679"), "1", StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -345,7 +345,7 @@ private FrameworkName GetFrameworkName()
#endif
}

#if !FEATURE_ASSEMBLY_LOADFROM || MONO
#if FEATURE_ASSEMBLYLOADCONTEXT || MONO
/// <summary>
/// Read everything from the assembly in a single stream.
/// </summary>
Expand Down Expand Up @@ -454,7 +454,7 @@ private static AssemblyName GetAssemblyName(MetadataReader metadataReader, Assem

// Enabling this for MONO, because it's required by GetFrameworkName.
// More details are in the comment for that method
#if !FEATURE_ASSEMBLY_LOADFROM || MONO
#if FEATURE_ASSEMBLYLOADCONTEXT || MONO
// This method copied from DNX source: https://github.com/aspnet/dnx/blob/e0726f769aead073af2d8cd9db47b89e1745d574/src/Microsoft.Dnx.Tooling/Utils/LockFileUtils.cs#L385
// System.Reflection.Metadata 1.1 is expected to have an API that helps with this.
/// <summary>
Expand Down Expand Up @@ -514,7 +514,7 @@ private static List<string> GetFixedStringArguments(MetadataReader reader, Custo
}
#endif

#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
/// <summary>
/// Release interface pointers on Dispose().
/// </summary>
Expand Down Expand Up @@ -586,7 +586,7 @@ internal static string GetRuntimeVersion(string path)
/// <returns>The array of assembly dependencies.</returns>
private AssemblyNameExtension[] ImportAssemblyDependencies()
{
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
var asmRefs = new List<AssemblyNameExtension>();

if (!NativeMethodsShared.IsWindows)
Expand Down Expand Up @@ -683,7 +683,7 @@ private AssemblyNameExtension[] ImportAssemblyDependencies()
/// <returns>The extra files of assembly dependencies.</returns>
private string[] ImportFiles()
{
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
var files = new List<string>();
IntPtr fileEnum = IntPtr.Zero;
var fileTokens = new UInt32[GENMAN_ENUM_TOKEN_BUF_SIZE];
Expand Down Expand Up @@ -726,7 +726,7 @@ private string[] ImportFiles()
#endif
}

#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
/// <summary>
/// Allocate assembly metadata structure buffer.
/// </summary>
Expand Down
16 changes: 8 additions & 8 deletions src/Tasks/GenerateResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2280,7 +2280,7 @@ internal string StronglyTypedClassName
/// </summary>
private bool _stronglyTypedClassIsPublic;

#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
/// <summary>
/// Class that gets called by the ResxResourceReader to resolve references
/// to assemblies within the .RESX.
Expand Down Expand Up @@ -2428,7 +2428,7 @@ internal void Run(
_portableLibraryCacheInfo = new List<ResGenDependencies.PortableLibraryFile>();
_usePreserializedResources = usePreserializedResources;

#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
// If references were passed in, we will have to give the ResxResourceReader an object
// by which it can resolve types that are referenced from within the .RESX.
if ((_assemblyFiles != null) && (_assemblyFiles.Length > 0))
Expand All @@ -2439,7 +2439,7 @@ internal void Run(

try
{
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
// Install assembly resolution event handler.
_eventHandler = new ResolveEventHandler(ResolveAssembly);
AppDomain.CurrentDomain.AssemblyResolve += _eventHandler;
Expand All @@ -2461,15 +2461,15 @@ internal void Run(
}
finally
{
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
// Remove the event handler.
AppDomain.CurrentDomain.AssemblyResolve -= _eventHandler;
_eventHandler = null;
#endif
}
}

#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
/// <summary>
/// Callback to resolve assembly names to assemblies.
/// </summary>
Expand Down Expand Up @@ -2987,7 +2987,7 @@ private void ReadResources(String filename, bool shouldUseSourcePath, String out

if (format == Format.Assembly) // Multiple input .resources files within one assembly
{
#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
ReadAssemblyResources(filename, outFileOrDir);
#else
throw new InputFormatNotSupportedException("Reading resources from Assembly not supported on .NET Core MSBuild");
Expand Down Expand Up @@ -3080,7 +3080,7 @@ private void AddResourcesUsingMinimalCoreResxParsing(string filename, ReaderInfo
}
}

#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
/// <summary>
/// Reads resources from an assembly.
/// </summary>
Expand Down Expand Up @@ -3978,7 +3978,7 @@ internal int LinePosition
#endregion // Code from ResGen.EXE
}

#if FEATURE_ASSEMBLY_LOADFROM
#if !FEATURE_ASSEMBLYLOADCONTEXT
/// <summary>
/// This implemention of ITypeResolutionService is passed into the ResxResourceReader
/// class, which calls back into the methods on this class in order to resolve types
Expand Down

0 comments on commit 5be281e

Please sign in to comment.