Browse Source

UseHeaderDirectories when true generated files will be outputted to original relative paths, and include will use relative path between headers. When NoGenIncludePrefix is set a string is prepended to the include files that are not generated.

pull/224/head
marcos henrich 11 years ago committed by triton
parent
commit
56f772a8c8
  1. 16
      src/Generator/Driver.cs
  2. 31
      src/Generator/Generators/CLI/CLITypeReferences.cs
  3. 2
      src/Generator/Options.cs
  4. 15
      src/Generator/Utils/Utils.cs

16
src/Generator/Driver.cs

@ -320,7 +320,7 @@ namespace CppSharp @@ -320,7 +320,7 @@ namespace CppSharp
public void WriteCode(List<GeneratorOutput> outputs)
{
var outputPath = Options.OutputDir;
var outputPath = Path.GetFullPath(Options.OutputDir);
if (!Directory.Exists(outputPath))
Directory.CreateDirectory(outputPath);
@ -329,16 +329,22 @@ namespace CppSharp @@ -329,16 +329,22 @@ namespace CppSharp
{
var fileBase = output.TranslationUnit.FileNameWithoutExtension;
if (Options.UseHeaderDirectories)
{
var dir = Path.Combine(outputPath, output.TranslationUnit.FileRelativeDirectory);
Directory.CreateDirectory(dir);
fileBase = Path.Combine(output.TranslationUnit.FileRelativeDirectory, fileBase);
}
if (Options.GenerateName != null)
fileBase = Options.GenerateName(output.TranslationUnit);
foreach (var template in output.Templates)
{
var fileName = string.Format("{0}.{1}", fileBase, template.FileExtension);
Diagnostics.EmitMessage("Generated '{0}'", fileName);
var fileRelativePath = string.Format("{0}.{1}", fileBase, template.FileExtension);
Diagnostics.EmitMessage("Generated '{0}'", fileRelativePath);
var filePath = Path.Combine(outputPath, fileName);
string file = Path.GetFullPath(filePath);
var file = Path.Combine(outputPath, fileRelativePath);
File.WriteAllText(file, template.Generate());
Options.CodeFiles.Add(file);
}

31
src/Generator/Generators/CLI/CLITypeReferences.cs

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using CppSharp.AST;
using CppSharp.AST.Extensions;
using CppSharp.Generators.AST;
@ -109,9 +110,9 @@ namespace CppSharp.Generators.CLI @@ -109,9 +110,9 @@ namespace CppSharp.Generators.CLI
return;
}
var declFile = decl.Namespace.TranslationUnit.FileName;
var translationUnit = decl.Namespace.TranslationUnit;
if(decl.Namespace.TranslationUnit.IsSystemHeader)
if (translationUnit.IsSystemHeader)
return;
if(decl.ExplicityIgnored)
@ -125,15 +126,35 @@ namespace CppSharp.Generators.CLI @@ -125,15 +126,35 @@ namespace CppSharp.Generators.CLI
{
typeRef.Include = new Include
{
File = declFile,
TranslationUnit = decl.Namespace.TranslationUnit,
Kind = Include.IncludeKind.Quoted,
File = GetIncludePath(translationUnit),
TranslationUnit = translationUnit,
Kind = translationUnit.IsGenerated
? Include.IncludeKind.Quoted
: Include.IncludeKind.Angled,
};
}
typeRef.Include.InHeader |= IsIncludeInHeader(record);
}
private string GetIncludePath(TranslationUnit translationUnit)
{
if (!translationUnit.IsGenerated)
return DriverOptions.NoGenIncludePrefix + translationUnit.FileRelativePath;
if (!DriverOptions.UseHeaderDirectories)
return translationUnit.FileName;
var rel = PathHelpers.GetRelativePath(
TranslationUnit.FileRelativeDirectory,
translationUnit.FileRelativeDirectory);
if (string.IsNullOrEmpty(rel))
return translationUnit.FileName;
return Path.Combine(rel, translationUnit.FileName);
}
private bool IsBuiltinTypedef(Declaration decl)
{
var typedefDecl = decl as TypedefDecl;

2
src/Generator/Options.cs

@ -95,9 +95,11 @@ namespace CppSharp @@ -95,9 +95,11 @@ namespace CppSharp
public bool GenerateProperties;
public bool GenerateInternalImports;
public bool GenerateClassMarshals;
public bool UseHeaderDirectories;
//List of include directories that are used but not generated
public List<string> NoGenIncludeDirs;
public string NoGenIncludePrefix = "";
/// <summary>
/// Enable this option to enable generation of finalizers.

15
src/Generator/Utils/Utils.cs

@ -235,4 +235,19 @@ namespace CppSharp @@ -235,4 +235,19 @@ namespace CppSharp
return assembly.GetTypes().Where(baseType.IsAssignableFrom);
}
}
public static class PathHelpers
{
public static string GetRelativePath(string fromPath, string toPath)
{
var path1 = fromPath.Trim('\\', '/');
var path2 = toPath.Trim('\\', '/');
var uri1 = new System.Uri("c:\\" + path1 + "\\");
var uri2 = new System.Uri("c:\\" + path2 + "\\");
return uri1.MakeRelativeUri(uri2).ToString();
}
}
}

Loading…
Cancel
Save