diff --git a/src/Generator/Driver.cs b/src/Generator/Driver.cs index 70a9cb85..54b57c2a 100644 --- a/src/Generator/Driver.cs +++ b/src/Generator/Driver.cs @@ -1,13 +1,16 @@ -using CppSharp.AST; +using System.CodeDom.Compiler; +using System.Linq; +using System.Reflection; +using System.Text; +using CppSharp.AST; using CppSharp.Generators; using CppSharp.Generators.CLI; using CppSharp.Generators.CSharp; -using CppSharp.Parser; using CppSharp.Passes; using CppSharp.Types; -using System; using System.Collections.Generic; using System.IO; +using Microsoft.CSharp; #if !OLD_PARSER using Std; @@ -250,11 +253,46 @@ namespace CppSharp Diagnostics.EmitMessage(DiagnosticId.FileGenerated, "Generated '{0}'", fileName); var filePath = Path.Combine(outputPath, fileName); - File.WriteAllText(Path.GetFullPath(filePath), template.Generate()); + string file = Path.GetFullPath(filePath); + File.WriteAllText(file, template.Generate()); + Options.CodeFiles.Add(file); } } } + public void CompileCode() + { + string assemblyFile; + if (string.IsNullOrEmpty(Options.LibraryName)) + assemblyFile = "out.dll"; + else + assemblyFile = Options.LibraryName + ".dll"; + + var compilerOptions = new StringBuilder(); + compilerOptions.Append(" /doc:" + Path.ChangeExtension(Path.GetFileName(assemblyFile), ".xml")); + compilerOptions.Append(" /debug:pdbonly"); + compilerOptions.Append(" /unsafe"); + + var compilerParameters = new CompilerParameters(); + compilerParameters.GenerateExecutable = false; + compilerParameters.TreatWarningsAsErrors = false; + compilerParameters.OutputAssembly = assemblyFile; + compilerParameters.GenerateInMemory = false; + compilerParameters.CompilerOptions = compilerOptions.ToString(); + compilerParameters.ReferencedAssemblies.Add(typeof(object).Assembly.Location); + var location = Assembly.GetExecutingAssembly().Location; + var locationRuntime = Path.Combine(Path.GetDirectoryName(location), "CppSharp.Runtime.dll"); + compilerParameters.ReferencedAssemblies.Add(locationRuntime); + + var providerOptions = new Dictionary(); + providerOptions.Add("CompilerVersion", "v4.0"); + var csharp = new CSharpCodeProvider(providerOptions); + var cr = csharp.CompileAssemblyFromFile(compilerParameters, Options.CodeFiles.ToArray()); + + foreach (var error in cr.Errors.Cast().Where(error => !error.IsWarning)) + Diagnostics.EmitError(error.ToString()); + } + public void AddTranslationUnitPass(TranslationUnitPass pass) { TranslationUnitPasses.AddPass(pass); @@ -318,6 +356,8 @@ namespace CppSharp } driver.WriteCode(outputs); + if (driver.Options.IsCSharpGenerator) + driver.CompileCode(); } } } \ No newline at end of file diff --git a/src/Generator/Options.cs b/src/Generator/Options.cs index b9325d2f..d7046774 100644 --- a/src/Generator/Options.cs +++ b/src/Generator/Options.cs @@ -42,6 +42,8 @@ namespace CppSharp CommentPrefix = "///"; Encoding = Encoding.ASCII; + + CodeFiles = new List(); } // General options @@ -120,6 +122,8 @@ namespace CppSharp } public bool Is32Bit { get { return true; } } + + public List CodeFiles { get; private set; } } public class InvalidOptionException : Exception diff --git a/src/Runtime/SymbolResolver.cs b/src/Runtime/SymbolResolver.cs index e2a180ac..dc3bb9a7 100644 --- a/src/Runtime/SymbolResolver.cs +++ b/src/Runtime/SymbolResolver.cs @@ -24,7 +24,7 @@ using System.Runtime.InteropServices; namespace CppSharp { - internal static class SymbolResolver + public static class SymbolResolver { static readonly string[] formats; static readonly Func loadImage;