Browse Source

Improved error handling of the code compiler code (and minor reformatting).

pull/123/head
triton 12 years ago
parent
commit
ed67b741ae
  1. 73
      src/Generator/Driver.cs

73
src/Generator/Driver.cs

@ -1,4 +1,5 @@
using System.CodeDom.Compiler; using System;
using System.CodeDom.Compiler;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
@ -263,35 +264,47 @@ namespace CppSharp
public void CompileCode() public void CompileCode()
{ {
string assemblyFile; try
if (string.IsNullOrEmpty(Options.LibraryName)) {
assemblyFile = "out.dll"; var assemblyFile = string.IsNullOrEmpty(Options.LibraryName) ?
else "out.dll" : Options.LibraryName + ".dll";
assemblyFile = Options.LibraryName + ".dll";
var docFile = Path.ChangeExtension(Path.GetFileName(assemblyFile), ".xml");
var compilerOptions = new StringBuilder();
compilerOptions.Append(" /doc:" + Path.ChangeExtension(Path.GetFileName(assemblyFile), ".xml")); var compilerOptions = new StringBuilder();
compilerOptions.Append(" /debug:pdbonly"); compilerOptions.Append(" /doc:" + docFile);
compilerOptions.Append(" /unsafe"); compilerOptions.Append(" /debug:pdbonly");
compilerOptions.Append(" /unsafe");
var compilerParameters = new CompilerParameters();
compilerParameters.GenerateExecutable = false; var compilerParameters = new CompilerParameters
compilerParameters.TreatWarningsAsErrors = false; {
compilerParameters.OutputAssembly = assemblyFile; GenerateExecutable = false,
compilerParameters.GenerateInMemory = false; TreatWarningsAsErrors = false,
compilerParameters.CompilerOptions = compilerOptions.ToString(); OutputAssembly = assemblyFile,
compilerParameters.ReferencedAssemblies.Add(typeof(object).Assembly.Location); GenerateInMemory = false,
var location = Assembly.GetExecutingAssembly().Location; CompilerOptions = compilerOptions.ToString()
var locationRuntime = Path.Combine(Path.GetDirectoryName(location), "CppSharp.Runtime.dll"); };
compilerParameters.ReferencedAssemblies.Add(locationRuntime);
compilerParameters.ReferencedAssemblies.Add(typeof (object).Assembly.Location);
var providerOptions = new Dictionary<string, string>(); var location = Assembly.GetExecutingAssembly().Location;
providerOptions.Add("CompilerVersion", "v4.0"); var locationRuntime = Path.Combine(Path.GetDirectoryName(location),
var csharp = new CSharpCodeProvider(providerOptions); "CppSharp.Runtime.dll");
var cr = csharp.CompileAssemblyFromFile(compilerParameters, Options.CodeFiles.ToArray()); compilerParameters.ReferencedAssemblies.Add(locationRuntime);
foreach (var error in cr.Errors.Cast<CompilerError>().Where(error => !error.IsWarning)) var codeProvider = new CSharpCodeProvider(
Diagnostics.EmitError(error.ToString()); new Dictionary<string, string> {{"CompilerVersion", "v4.0"}});
var compilerResults = codeProvider.CompileAssemblyFromFile(
compilerParameters, Options.CodeFiles.ToArray());
var errors = compilerResults.Errors.Cast<CompilerError>();
foreach (var error in errors.Where(error => !error.IsWarning))
Diagnostics.EmitError(error.ToString());
}
catch (Exception exception)
{
Diagnostics.EmitError("Could not compile the generated source code");
Diagnostics.EmitMessage(exception.ToString());
}
} }
public void AddTranslationUnitPass(TranslationUnitPass pass) public void AddTranslationUnitPass(TranslationUnitPass pass)

Loading…
Cancel
Save