Browse Source

Merge pull request #103 from ddobrev/master

Added automatic building of the generated source files
pull/106/merge
João Matos 12 years ago
parent
commit
658a9db1f6
  1. 48
      src/Generator/Driver.cs
  2. 4
      src/Generator/Options.cs
  3. 2
      src/Runtime/SymbolResolver.cs

48
src/Generator/Driver.cs

@ -1,13 +1,16 @@ @@ -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 @@ -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<string, string>();
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<CompilerError>().Where(error => !error.IsWarning))
Diagnostics.EmitError(error.ToString());
}
public void AddTranslationUnitPass(TranslationUnitPass pass)
{
TranslationUnitPasses.AddPass(pass);
@ -318,6 +356,8 @@ namespace CppSharp @@ -318,6 +356,8 @@ namespace CppSharp
}
driver.WriteCode(outputs);
if (driver.Options.IsCSharpGenerator)
driver.CompileCode();
}
}
}

4
src/Generator/Options.cs

@ -42,6 +42,8 @@ namespace CppSharp @@ -42,6 +42,8 @@ namespace CppSharp
CommentPrefix = "///";
Encoding = Encoding.ASCII;
CodeFiles = new List<string>();
}
// General options
@ -120,6 +122,8 @@ namespace CppSharp @@ -120,6 +122,8 @@ namespace CppSharp
}
public bool Is32Bit { get { return true; } }
public List<string> CodeFiles { get; private set; }
}
public class InvalidOptionException : Exception

2
src/Runtime/SymbolResolver.cs

@ -24,7 +24,7 @@ using System.Runtime.InteropServices; @@ -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<string, IntPtr> loadImage;

Loading…
Cancel
Save