Browse Source

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

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

53
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,36 +264,48 @@ 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(); var compilerOptions = new StringBuilder();
compilerOptions.Append(" /doc:" + Path.ChangeExtension(Path.GetFileName(assemblyFile), ".xml")); compilerOptions.Append(" /doc:" + docFile);
compilerOptions.Append(" /debug:pdbonly"); compilerOptions.Append(" /debug:pdbonly");
compilerOptions.Append(" /unsafe"); compilerOptions.Append(" /unsafe");
var compilerParameters = new CompilerParameters(); var compilerParameters = new CompilerParameters
compilerParameters.GenerateExecutable = false; {
compilerParameters.TreatWarningsAsErrors = false; GenerateExecutable = false,
compilerParameters.OutputAssembly = assemblyFile; TreatWarningsAsErrors = false,
compilerParameters.GenerateInMemory = false; OutputAssembly = assemblyFile,
compilerParameters.CompilerOptions = compilerOptions.ToString(); GenerateInMemory = false,
compilerParameters.ReferencedAssemblies.Add(typeof(object).Assembly.Location); CompilerOptions = compilerOptions.ToString()
};
compilerParameters.ReferencedAssemblies.Add(typeof (object).Assembly.Location);
var location = Assembly.GetExecutingAssembly().Location; var location = Assembly.GetExecutingAssembly().Location;
var locationRuntime = Path.Combine(Path.GetDirectoryName(location), "CppSharp.Runtime.dll"); var locationRuntime = Path.Combine(Path.GetDirectoryName(location),
"CppSharp.Runtime.dll");
compilerParameters.ReferencedAssemblies.Add(locationRuntime); compilerParameters.ReferencedAssemblies.Add(locationRuntime);
var providerOptions = new Dictionary<string, string>(); var codeProvider = new CSharpCodeProvider(
providerOptions.Add("CompilerVersion", "v4.0"); new Dictionary<string, string> {{"CompilerVersion", "v4.0"}});
var csharp = new CSharpCodeProvider(providerOptions); var compilerResults = codeProvider.CompileAssemblyFromFile(
var cr = csharp.CompileAssemblyFromFile(compilerParameters, Options.CodeFiles.ToArray()); compilerParameters, Options.CodeFiles.ToArray());
foreach (var error in cr.Errors.Cast<CompilerError>().Where(error => !error.IsWarning)) var errors = compilerResults.Errors.Cast<CompilerError>();
foreach (var error in errors.Where(error => !error.IsWarning))
Diagnostics.EmitError(error.ToString()); 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