Add option to load Driver options from a YAML config file

This commit is contained in:
josetr 2022-03-16 20:02:30 +00:00
Родитель b1c6e07476
Коммит 1db675d3ea
6 изменённых файлов: 124 добавлений и 9 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@ -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; }
}
}

Просмотреть файл

@ -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();
}
}
}
}

Просмотреть файл

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

Просмотреть файл

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