Browse Source

Add option to load Driver options from a YAML config file

pg
josetr 3 years ago
parent
commit
1db675d3ea
  1. 1
      Directory.Packages.props
  2. 18
      src/AST/Module.cs
  3. 13
      src/Generator/Config/Cfg.cs
  4. 93
      src/Generator/Config/CfgLoader.cs
  5. 1
      src/Generator/CppSharp.Generator.csproj
  6. 7
      src/Generator/Driver.cs

1
Directory.Packages.props

@ -4,5 +4,6 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> <PackageVersion Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageVersion Include="NUnit" Version="3.13.2" /> <PackageVersion Include="NUnit" Version="3.13.2" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.0.0" /> <PackageVersion Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageVersion Include="YamlDotNet" Version="11.2.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>

18
src/AST/Module.cs

@ -5,17 +5,17 @@ namespace CppSharp.AST
{ {
public class Module public class Module
{ {
public List<string> IncludeDirs { get; } = new List<string>(); public List<string> IncludeDirs { get; set; } = new List<string>();
public List<string> Headers { get; } = new List<string>(); public List<string> Headers { get; set; } = new List<string>();
public List<string> LibraryDirs { get; } = new List<string>(); public List<string> LibraryDirs { get; } = new List<string>();
public List<string> Libraries { get; } = new List<string>(); public List<string> Libraries { get; set; } = new List<string>();
public List<string> Defines { get; } = new List<string>(); public List<string> Defines { get; set; } = new List<string>();
public List<string> Undefines { get; } = new List<string>(); public List<string> Undefines { get; set; } = new List<string>();
public string OutputNamespace { get; set; } public string OutputNamespace { get; set; }
public List<TranslationUnit> Units { get; } = new List<TranslationUnit>(); public List<TranslationUnit> Units { get; set; } = new List<TranslationUnit>();
public List<string> CodeFiles { get; } = new List<string>(); public List<string> CodeFiles { get; set; } = new List<string>();
public List<string> ReferencedAssemblies { get; } = new List<string>(); public List<string> ReferencedAssemblies { get; set; } = new List<string>();
public List<Module> Dependencies { get; } = new List<Module>(); public List<Module> Dependencies { get; set; } = new List<Module>();
[Obsolete("Use Module(string libraryName) instead.")] [Obsolete("Use Module(string libraryName) instead.")]
public Module() public Module()

13
src/Generator/Config/Cfg.cs

@ -0,0 +1,13 @@
namespace CppSharp.Config
{
using CppSharp.AST;
using CppSharp.Parser;
internal class Cfg
{
public bool SetupMSVC { get; set; }
public ParserOptions ParserOptions { get; set; }
public Module Module { get; set; }
public DriverOptions Options { get; set; }
}
}

93
src/Generator/Config/CfgLoader.cs

@ -0,0 +1,93 @@
namespace CppSharp.Config
{
using CppSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
internal static partial class CfgLoader
{
public static Cfg LoadConfig(string path, Driver driver)
{
var deserializer = new DeserializerBuilder()
.WithTypeConverter(new YamlTypeConverter())
.WithNodeDeserializer(new NodeDeserializer())
.WithNamingConvention(PascalCaseNamingConvention.Instance)
.Build();
var yaml = File.ReadAllText(path);
var config = deserializer.Deserialize<Cfg>(yaml);
if (config.SetupMSVC)
config.ParserOptions.SetupMSVC();
driver.Options = config.Options;
driver.ParserOptions = config.ParserOptions;
return config;
}
private class NodeDeserializer : INodeDeserializer
{
bool INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func<IParser, Type, object> nestedObjectDeserializer, out object value)
{
if (expectedType != typeof(string) || !parser.TryConsume<Scalar>(out var scalar))
{
value = null;
return false;
}
var template = scalar.Value;
value = Regex.Replace(template, @"\${(\w+)}", match =>
{
var result = Environment.GetEnvironmentVariable(match.Groups[1].Value);
return result;
});
return true;
}
}
private class YamlTypeConverter : IYamlTypeConverter
{
public bool Accepts(Type type)
{
if (type != typeof(List<string>))
return false;
return true;
}
public object ReadYaml(IParser parser, Type type)
{
if (parser.Current.GetType() != typeof(SequenceStart))
throw new InvalidDataException("Invalid YAML.");
var list = new List<string>();
parser.MoveNext();
do
{
if (parser.Current is not Scalar s)
throw new InvalidDataException("Invalid YAML.");
list.AddRange(s.Value.Split(';'));
parser.MoveNext();
} while (parser.Current.GetType() != typeof(SequenceEnd));
parser.MoveNext();
return list;
}
public void WriteYaml(IEmitter emitter, object value, Type type)
{
throw new NotImplementedException();
}
}
}
}

1
src/Generator/CppSharp.Generator.csproj

@ -11,5 +11,6 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Parser\CppSharp.Parser.csproj" /> <ProjectReference Include="..\Parser\CppSharp.Parser.csproj" />
<ProjectReference Include="$(NativeProjectsDir)Std-symbols.vcxproj" ReferenceOutputAssembly="false" Condition="$(IsWindows)" /> <ProjectReference Include="$(NativeProjectsDir)Std-symbols.vcxproj" ReferenceOutputAssembly="false" Condition="$(IsWindows)" />
<PackageReference Include="YamlDotNet" />
</ItemGroup> </ItemGroup>
</Project> </Project>

7
src/Generator/Driver.cs

@ -13,6 +13,7 @@ using CppSharp.Parser;
using CppSharp.Passes; using CppSharp.Passes;
using CppSharp.Utils; using CppSharp.Utils;
using CppSharp.Types; using CppSharp.Types;
using CppSharp.Config;
namespace CppSharp namespace CppSharp
{ {
@ -31,6 +32,12 @@ namespace CppSharp
ParserOptions = new ParserOptions(); ParserOptions = new ParserOptions();
} }
public void LoadConfig(string path)
{
var config = CfgLoader.LoadConfig(path, this);
Options.Modules.Add(config.Module);
}
Generator CreateGeneratorFromKind(GeneratorKind kind) Generator CreateGeneratorFromKind(GeneratorKind kind)
{ {
switch (kind) switch (kind)

Loading…
Cancel
Save