mirror of https://github.com/mono/CppSharp.git
Browse Source
CodeDom doesn't work in .NET Core (https://github.com/dotnet/runtime/issues/18768) and Roslyn turns out to be too low level (just a compiler and not a building framework) so the best approach is to invoke msbuild as a process. Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>instantiate-types-nested-templates
15 changed files with 157 additions and 113 deletions
@ -0,0 +1,56 @@
@@ -0,0 +1,56 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using CppSharp.AST; |
||||
|
||||
namespace CppSharp.Generators |
||||
{ |
||||
public class MSBuildGenerator : CodeGenerator |
||||
{ |
||||
public MSBuildGenerator(BindingContext context, Module module, |
||||
Dictionary<Module, string> libraryMappings) |
||||
: base(context) |
||||
{ |
||||
this.module = module; |
||||
this.libraryMappings = libraryMappings; |
||||
} |
||||
|
||||
public override string FileExtension => "csproj"; |
||||
|
||||
public override void Process() |
||||
{ |
||||
var location = System.Reflection.Assembly.GetExecutingAssembly().Location; |
||||
Write($@"
|
||||
<Project Sdk=""Microsoft.NET.Sdk""> |
||||
<PropertyGroup> |
||||
<TargetFramework>netstandard2.0</TargetFramework> |
||||
<PlatformTarget>{(Context.TargetInfo.PointerWidth == 64 ? "x64" : "x86")}</PlatformTarget> |
||||
<OutputPath>{Options.OutputDir}</OutputPath> |
||||
<DocumentationFile>{module.LibraryName}.xml</DocumentationFile> |
||||
<Configuration>Release</Configuration> |
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> |
||||
<EnableDefaultNoneItems>false</EnableDefaultNoneItems> |
||||
<EnableDefaultItems>false</EnableDefaultItems> |
||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> |
||||
</PropertyGroup> |
||||
<ItemGroup> |
||||
{string.Join(Environment.NewLine, module.CodeFiles.Select(c => |
||||
$"<Compile Include=\"{c}\" />"))} |
||||
</ItemGroup> |
||||
<ItemGroup> |
||||
{string.Join(Environment.NewLine, |
||||
new[] { Path.Combine(Path.GetDirectoryName(location), "CppSharp.Runtime.dll") } |
||||
.Union(module.Dependencies.Where(libraryMappings.ContainsKey).Select(d => libraryMappings[d])) |
||||
.Select(reference => |
||||
$@"<Reference Include=""{Path.GetFileNameWithoutExtension(reference)}"">
|
||||
<HintPath>{reference}</HintPath> |
||||
</Reference>"))}
|
||||
</ItemGroup> |
||||
</Project>".Trim());
|
||||
} |
||||
|
||||
private readonly Module module; |
||||
private readonly Dictionary<Module, string> libraryMappings; |
||||
} |
||||
} |
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
using System; |
||||
using System.Runtime.InteropServices; |
||||
using System.Text; |
||||
|
||||
namespace CppSharp.Runtime |
||||
{ |
||||
// HACK: .NET Standard 2.0 which we use in auto-building to support .NET Framework, lacks UnmanagedType.LPUTF8Str
|
||||
public class UTF8Marshaller : ICustomMarshaler |
||||
{ |
||||
public void CleanUpManagedData(object ManagedObj) |
||||
{ |
||||
} |
||||
|
||||
public void CleanUpNativeData(IntPtr pNativeData) |
||||
=> Marshal.FreeHGlobal(pNativeData); |
||||
|
||||
public int GetNativeDataSize() => -1; |
||||
|
||||
public IntPtr MarshalManagedToNative(object managedObj) |
||||
{ |
||||
if (managedObj == null) |
||||
return IntPtr.Zero; |
||||
if (!(managedObj is string)) |
||||
throw new MarshalDirectiveException( |
||||
"UTF8Marshaler must be used on a string."); |
||||
|
||||
// not null terminated
|
||||
byte[] strbuf = Encoding.UTF8.GetBytes((string) managedObj); |
||||
IntPtr buffer = Marshal.AllocHGlobal(strbuf.Length + 1); |
||||
Marshal.Copy(strbuf, 0, buffer, strbuf.Length); |
||||
|
||||
// write the terminating null
|
||||
Marshal.WriteByte(buffer + strbuf.Length, 0); |
||||
return buffer; |
||||
} |
||||
|
||||
public unsafe object MarshalNativeToManaged(IntPtr str) |
||||
{ |
||||
if (str == IntPtr.Zero) |
||||
return null; |
||||
|
||||
int byteCount = 0; |
||||
var str8 = (byte*) str; |
||||
while (*(str8++) != 0) byteCount += sizeof(byte); |
||||
|
||||
return Encoding.UTF8.GetString((byte*) str, byteCount); |
||||
} |
||||
|
||||
public static ICustomMarshaler GetInstance(string pstrCookie) |
||||
{ |
||||
if (marshaler == null) |
||||
marshaler = new UTF8Marshaller(); |
||||
return marshaler; |
||||
} |
||||
|
||||
private static UTF8Marshaller marshaler; |
||||
} |
||||
} |
@ -1,12 +0,0 @@
@@ -1,12 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk"> |
||||
<PropertyGroup> |
||||
<TestGeneratorName>NamespacesDerived.Gen</TestGeneratorName> |
||||
<TestGeneratorProjectDir>$(MSBuildProjectDirectory)\..\NamespacesDerived\</TestGeneratorProjectDir> |
||||
<EnableGeneratorCompileItems>false</EnableGeneratorCompileItems> |
||||
<OutputPath>$(GenDir)NamespacesDerived</OutputPath> |
||||
</PropertyGroup> |
||||
|
||||
<ItemGroup> |
||||
<Compile Include="$(GenDir)NamespacesDerived\NamespacesBase.cs" /> |
||||
</ItemGroup> |
||||
</Project> |
@ -1,14 +0,0 @@
@@ -1,14 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk"> |
||||
<PropertyGroup> |
||||
<EnableGeneratorCompileItems>false</EnableGeneratorCompileItems> |
||||
<DocumentationFile>NamespacesDerived.CSharp.xml</DocumentationFile> |
||||
</PropertyGroup> |
||||
|
||||
<ItemGroup> |
||||
<Compile Include="$(GenDir)NamespacesDerived\NamespacesDerived.cs" /> |
||||
</ItemGroup> |
||||
|
||||
<ItemGroup> |
||||
<ProjectReference Include="..\NamespacesBase\NamespacesBase.CSharp.csproj" /> |
||||
</ItemGroup> |
||||
</Project> |
@ -1 +1,13 @@
@@ -1 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk" /> |
||||
<Project Sdk="Microsoft.NET.Sdk"> |
||||
<ItemGroup> |
||||
<ProjectReference Include="NamespacesDerived.Gen.csproj" ReferenceOutputAssembly="false" /> |
||||
</ItemGroup> |
||||
<ItemGroup> |
||||
<Reference Include="NamespacesBase"> |
||||
<HintPath>..\..\build\gen\NamespacesDerived\NamespacesBase.dll</HintPath> |
||||
</Reference> |
||||
<Reference Include="NamespacesDerived"> |
||||
<HintPath>..\..\build\gen\NamespacesDerived\NamespacesDerived.dll</HintPath> |
||||
</Reference> |
||||
</ItemGroup> |
||||
</Project> |
Loading…
Reference in new issue