86 lines
3.4 KiB
C#
Raw Normal View History

2019-10-19 13:52:16 +02:00
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.IO;
2020-01-30 21:53:08 +01:00
using System.Linq;
2019-10-19 13:52:16 +02:00
using System.Reflection;
namespace CapnpC.CSharp.Generator.Tests.Util
{
class InlineAssemblyCompiler
{
2020-01-30 21:53:08 +01:00
public enum CompileSummary
{
2020-01-30 21:53:08 +01:00
Success,
SuccessWithWarnings,
Error
}
2020-01-30 21:53:08 +01:00
public static CompileSummary TryCompileCapnp(NullableContextOptions nullableContextOptions, params string[] code)
2019-10-19 13:52:16 +02:00
{
var options = new CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary,
2020-01-30 21:53:08 +01:00
optimizationLevel: OptimizationLevel.Debug,
nullableContextOptions: nullableContextOptions);
2019-10-19 13:52:16 +02:00
string assemblyRoot = Path.GetDirectoryName(typeof(object).Assembly.Location);
string capnpRuntimePath = Path.GetFullPath(Path.Combine(
Assembly.GetExecutingAssembly().Location,
2019-12-10 17:12:49 +00:00
"..", "..", "..", "..", "..",
"Capnp.Net.Runtime",
"bin",
"Debug",
2021-10-02 21:22:03 +02:00
"netcoreapp3.1",
2019-12-10 17:12:49 +00:00
"Capnp.Net.Runtime.dll"));
2019-10-19 13:52:16 +02:00
2020-02-08 17:23:07 +01:00
var parseOptions = CSharpParseOptions.Default;
if (nullableContextOptions == NullableContextOptions.Disable)
parseOptions = parseOptions.WithLanguageVersion(LanguageVersion.CSharp7_1);
2019-10-19 13:52:16 +02:00
var compilation = CSharpCompilation.Create(
"CompilationTestAssembly",
options: options,
references: new MetadataReference[] {
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "mscorlib.dll")),
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.dll")),
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Core.dll")),
2020-04-01 22:15:46 +02:00
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Diagnostics.Tools.dll")),
2019-10-19 13:52:16 +02:00
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Runtime.dll")),
MetadataReference.CreateFromFile(Path.Combine(assemblyRoot, "System.Private.CoreLib.dll")),
MetadataReference.CreateFromFile(capnpRuntimePath) },
2020-02-08 17:23:07 +01:00
syntaxTrees: Array.ConvertAll(code, new Converter<string, SyntaxTree>(c => CSharpSyntaxTree.ParseText(c, parseOptions))));
2019-10-19 13:52:16 +02:00
using (var stream = new MemoryStream())
{
var emitResult = compilation.Emit(stream);
2019-12-10 19:07:49 +00:00
foreach (var diag in emitResult.Diagnostics)
Console.WriteLine($"{diag}");
if (!emitResult.Success)
{
foreach (var c in code)
{
string path = Path.ChangeExtension(Path.GetTempFileName(), ".capnp.cs");
File.WriteAllText(path, c);
Console.WriteLine($"[See {path} for generated code]");
}
2019-12-10 19:07:49 +00:00
}
2020-01-30 21:53:08 +01:00
if (emitResult.Success)
{
if (emitResult.Diagnostics.Any(diag => diag.Severity == DiagnosticSeverity.Warning))
return CompileSummary.SuccessWithWarnings;
else
return CompileSummary.Success;
}
else
{
return CompileSummary.Error;
}
2019-10-19 13:52:16 +02:00
}
}
}
}