-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make auto-compiling of C# work with .NET Core
CodeDom doesn't work in .NET Core (dotnet/runtime#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 <[email protected]>
- Loading branch information
Showing
15 changed files
with
157 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 13 additions & 1 deletion
14
tests/NamespacesDerived/NamespacesDerived.Tests.CSharp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters