Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Allow building with .NET 6
Browse files Browse the repository at this point in the history
Ported projects to sdk-style and added ifdefs to allow compiling
with .NET 6
  • Loading branch information
slluis committed Aug 24, 2021
1 parent 60ac8e9 commit a6885a7
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ProjectGuid>{53DCA265-3C3C-42F9-B647-F72BA678122B}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -23,7 +23,10 @@
<NoWin32Manifest>False</NoWin32Manifest>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
<DocumentationFile>$(IntermediateOutputPath)ICSharpCode.NRefactory.CSharp.xml</DocumentationFile>
</PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<EnableDefaultCompileItems>False</EnableDefaultCompileItems>
<EnableDefaultEmbeddedResourceItems>False</EnableDefaultEmbeddedResourceItems>
<TargetFramework>net472</TargetFramework> </PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<RegisterForComInterop>False</RegisterForComInterop>
Expand Down Expand Up @@ -396,7 +399,6 @@
<Name>ICSharpCode.NRefactory</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup />
<ProjectExtensions>
<MonoDevelop>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

#if !NET6_0

using System;
using System.CodeDom;
using System.Collections.Generic;
Expand Down Expand Up @@ -1427,3 +1429,5 @@ CodeObject IAstVisitor<CodeObject>.VisitDocumentationReference(DocumentationRefe
}
}
}

#endif
34 changes: 31 additions & 3 deletions ICSharpCode.NRefactory.CSharp/Parser/mcs/assembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,11 @@ public virtual ModuleBuilder CreateModuleBuilder ()
// but returned ISymbolWriter does not have all what we need therefore some
// adaptor will be needed for now we alwayas emit MDB format when generating
// debug info
#if NET6_0
return Builder.DefineDynamicModule(module_name);
#else
return Builder.DefineDynamicModule (module_name, module_name, false);
#endif
}

public virtual void Emit ()
Expand Down Expand Up @@ -787,11 +791,13 @@ public void EmbedResources ()
//
// Add Win32 resources
//
#if !NET6_0
if (Compiler.Settings.Win32ResourceFile != null) {
Builder.DefineUnmanagedResource (Compiler.Settings.Win32ResourceFile);
} else {
Builder.DefineVersionInfoResource (vi_product, vi_product_version, vi_company, vi_copyright, vi_trademark);
}
#endif

if (Compiler.Settings.Win32IconFile != null) {
builder_extra.DefineWin32IconResource (Compiler.Settings.Win32IconFile);
Expand Down Expand Up @@ -819,9 +825,13 @@ public void EmbedResources ()
stream = new MemoryStream (File.ReadAllBytes (res.FileName));
}

#if !NET6_0
module.Builder.DefineManifestResource (res.Name, stream, res.Attributes);
#endif
} else {
#if !NET6_0
Builder.AddResourceFile (res.Name, Path.GetFileName (res.FileName), res.Attributes);
#endif
}
}
}
Expand Down Expand Up @@ -871,7 +881,11 @@ public void Save ()
if (Compiler.Settings.Target == Target.Module) {
SaveModule (pekind, machine);
} else {
#if NET6_0
throw new NotSupportedException();
#else
Builder.Save (module.Builder.ScopeName, pekind, machine);
#endif
}
} catch (Exception e) {
Report.Error (16, "Could not write to file `" + name + "', cause: " + e.Message);
Expand Down Expand Up @@ -964,7 +978,9 @@ void SetEntryPoint ()
return;
}

#if !NET6_0
Builder.SetEntryPoint (entry_point.MethodBuilder, file_kind);
#endif
}

void Error_ObsoleteSecurityAttribute (Attribute a, string option)
Expand Down Expand Up @@ -1043,14 +1059,14 @@ public AssemblyResource (string fileName, string name, bool isPrivate)
public string FileName { get; private set; }
public bool IsEmbeded { get; set; }

#region IEquatable<AssemblyResource> Members
#region IEquatable<AssemblyResource> Members

public bool Equals (AssemblyResource other)
{
return Name == other.Name;
}

#endregion
#endregion
}

//
Expand Down Expand Up @@ -1258,4 +1274,16 @@ protected void LoadReferencesCore (ModuleContainer module, out T corlib_assembly
compiler.TimeReporter.Stop (TimeReporter.TimerType.ReferencesLoading);
}
}
}

#if NET6_0

enum PEFileKinds
{
Dll = 1,
ConsoleApplication = 2,
WindowApplication = 3
}

#endif

}
2 changes: 1 addition & 1 deletion ICSharpCode.NRefactory.CSharp/Parser/mcs/class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2766,7 +2766,7 @@ public override void Emit ()
foreach (var de in declarative_security) {
#if STATIC
TypeBuilder.__AddDeclarativeSecurity (de);
#else
#elif !NET6_0
TypeBuilder.AddDeclarativeSecurity (de.Key, de.Value);
#endif
}
Expand Down
2 changes: 2 additions & 0 deletions ICSharpCode.NRefactory.CSharp/Parser/mcs/driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,10 @@ public bool Compile ()
if (!ctx.BuiltinTypes.CheckDefinitions (module))
return false;

#if !NET6_0
if (!assembly.Create (AppDomain.CurrentDomain, AssemblyBuilderAccess.Save))
return false;
#endif

module.CreateContainer ();

Expand Down
6 changes: 6 additions & 0 deletions ICSharpCode.NRefactory.CSharp/Parser/mcs/eval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,11 @@ CompiledMethod CompileBlock (Class host, Undo undo, Report Report)
AssemblyBuilderAccess access;

if (Environment.GetEnvironmentVariable ("SAVE") != null) {
#if NET6_0
access = AssemblyBuilderAccess.Run;
#else
access = AssemblyBuilderAccess.RunAndSave;
#endif
assembly = new AssemblyDefinitionDynamic (module, current_debug_name, current_debug_name);
assembly.Importer = importer;
} else {
Expand Down Expand Up @@ -786,8 +790,10 @@ CompiledMethod CompileBlock (Class host, Undo undo, Report Report)
if (host != null)
host.CloseContainer ();

#if !NET6_0
if (access == AssemblyBuilderAccess.RunAndSave)
assembly.Save ();
#endif

if (host == null)
return null;
Expand Down
18 changes: 11 additions & 7 deletions ICSharpCode.NRefactory.CSharp/Parser/mcs/method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public override void Emit ()
foreach (var de in declarative_security) {
#if STATIC
MethodBuilder.__AddDeclarativeSecurity (de);
#else
#elif !NET6_0
MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
#endif
}
Expand Down Expand Up @@ -1801,7 +1801,7 @@ public override void Emit ()
foreach (var de in declarative_security) {
#if STATIC
ConstructorBuilder.__AddDeclarativeSecurity (de);
#else
#elif !NET6_0
ConstructorBuilder.AddDeclarativeSecurity (de.Key, de.Value);
#endif
}
Expand Down Expand Up @@ -1861,6 +1861,7 @@ public override void WriteDebugSymbol (MonoSymbolFile file)
if (debug_builder == null)
return;

#if !NET6_0
var token = ConstructorBuilder.GetToken ();
int t = token.Token;
#if STATIC
Expand All @@ -1869,9 +1870,10 @@ public override void WriteDebugSymbol (MonoSymbolFile file)
#endif

debug_builder.DefineMethod (file, t);
#endif
}

#region IMethodData Members
#region IMethodData Members

public MemberName MethodName {
get {
Expand All @@ -1890,7 +1892,7 @@ EmitContext IMethodData.CreateEmitContext (ILGenerator ig, SourceMethodBuilder s
throw new NotImplementedException ();
}

#endregion
#endregion
}

/// <summary>
Expand Down Expand Up @@ -2191,6 +2193,7 @@ public void WriteDebugSymbol (MonoSymbolFile file)
if (debug_builder == null)
return;

#if !NET6_0
var token = builder.GetToken ();
int t = token.Token;
#if STATIC
Expand All @@ -2199,6 +2202,7 @@ public void WriteDebugSymbol (MonoSymbolFile file)
#endif

debug_builder.DefineMethod (file, t);
#endif
}
}

Expand Down Expand Up @@ -2337,7 +2341,7 @@ public void UpdateName (InterfaceMemberBase member)
SetMemberName (SetupName (prefix, member, Location));
}

#region IMethodData Members
#region IMethodData Members

public ToplevelBlock Block {
get {
Expand Down Expand Up @@ -2387,7 +2391,7 @@ MethodBase IMethodDefinition.Metadata {
public abstract ParametersCompiled ParameterInfo { get ; }
public abstract TypeSpec ReturnType { get; }

#endregion
#endregion

public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
{
Expand Down Expand Up @@ -2454,7 +2458,7 @@ public virtual void Emit (TypeDefinition parent)
foreach (var de in declarative_security) {
#if STATIC
method_data.MethodBuilder.__AddDeclarativeSecurity (de);
#else
#elif !NET6_0
method_data.MethodBuilder.AddDeclarativeSecurity (de.Key, de.Value);
#endif
}
Expand Down
16 changes: 11 additions & 5 deletions ICSharpCode.NRefactory.CSharp/Parser/mcs/reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,21 @@ public Module IncludeModule (string moduleFile)
public override ModuleBuilder CreateModuleBuilder ()
{
if (file_name == null)
#if NET6_0
return Builder.DefineDynamicModule(Name);
#else
return Builder.DefineDynamicModule (Name, false);
#endif

return base.CreateModuleBuilder ();
}
#endif
//
// Initializes the code generator
//
//
// Initializes the code generator
//
public bool Create (AppDomain domain, AssemblyBuilderAccess access)
{
#if STATIC || FULL_AOT_RUNTIME
#if STATIC || FULL_AOT_RUNTIME || NETCOREAPP
throw new NotSupportedException ();
#else
ResolveAssemblySecurityAttributes ();
Expand Down Expand Up @@ -261,10 +265,12 @@ protected override void SaveModule (PortableExecutableKinds pekind, ImageFileMac
base.SaveModule (pekind, machine);
}

#if !NETCOREAPP
Builder.Save (file_name, pekind, machine);
#endif
}
#endif
}
}

//
// Extension to System.Reflection.Emit.AssemblyBuilder to have fully compatible
Expand Down
14 changes: 7 additions & 7 deletions ICSharpCode.NRefactory.Cecil/ICSharpCode.NRefactory.Cecil.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -18,7 +18,12 @@
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
<DocumentationFile>$(IntermediateOutputPath)ICSharpCode.NRefactory.Cecil.xml</DocumentationFile>
<NoWarn>1591</NoWarn>
</PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<EnableDefaultCompileItems>False</EnableDefaultCompileItems>
<EnableDefaultEmbeddedResourceItems>False</EnableDefaultEmbeddedResourceItems>
<TargetFramework>net472</TargetFramework>
<Configurations>Debug;Release;net_4_5_Debug;net_4_5_Release</Configurations>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>Full</DebugType>
Expand All @@ -27,7 +32,6 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>PdbOnly</DebugType>
Expand All @@ -36,7 +40,6 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_4_5_Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -99,11 +102,8 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="CecilLoader.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">
<Project>{3B2A5653-EC97-4001-BB9B-D90F1AF2C371}</Project>
<Name>ICSharpCode.NRefactory</Name>
</ProjectReference>
</ItemGroup>
</Project>
Loading

0 comments on commit a6885a7

Please sign in to comment.