cppast/src/refl/BuildOption.cs

95 lines
3.4 KiB
C#
Raw Normal View History

2024-04-18 22:17:22 +08:00
using CommandLine;
using CppAst;
2024-04-28 22:15:01 +08:00
using System.Collections.Generic;
2024-04-18 22:17:22 +08:00
namespace refl
{
[Verb("build",HelpText = "Generate custom code based on the parsed AST.")]
internal class CmdBuildOption
{
[Value(0, MetaName = "", Required = true, Min = 2, HelpText = "Input file(s) for parsing.")]
2024-04-21 21:44:53 +08:00
public IEnumerable<string>? InputFiles { get; set; }
2024-04-18 22:17:22 +08:00
2024-04-25 21:47:15 +08:00
[Option('o', "output", Required = true, HelpText = "Output file for generated code.")]
public string Output { get; set; } = "";
2024-04-18 22:17:22 +08:00
2024-04-25 21:47:15 +08:00
[Option('t', "template", Required = true, HelpText = "the code template need to render")]
public string Template { get; set; } = "";
[Option('i', "include", Required = false, HelpText = "link to the include dir")]
public string IncludeDir { get; set; } = "";
2024-04-18 22:17:22 +08:00
2024-04-28 22:15:01 +08:00
[Option('m', "macros", Required = false, HelpText = "add the macros file")]
public string MacroFile { get; set; } = "";
2024-04-18 22:17:22 +08:00
[Option('d', "define", Required = false, HelpText = "define a macro on the command line")]
2024-04-21 21:44:53 +08:00
public string Define { get; set; } = "";
2024-04-18 22:17:22 +08:00
2024-04-25 21:47:15 +08:00
[Option('v', "verbose", Required = false, HelpText = "output compile info")]
public bool Verbose { get; set; } = false;
2024-04-18 22:17:22 +08:00
[Option('h', "help", HelpText = "Display this help message.")]
2024-04-21 21:44:53 +08:00
public bool Help { get; set; } = false;
2024-04-18 22:17:22 +08:00
static CppParserOptions MakeParserOptions(CmdBuildOption opt)
{
var parse = new CppParserOptions
{
AdditionalArguments = { "-std=c++20" },
2024-04-19 22:23:40 +08:00
//ParseTokenAttributes = true,
//ParseCommentAttribute = true,
2024-04-18 22:17:22 +08:00
};
if (!string.IsNullOrWhiteSpace(opt.Define))
{
var defines = opt.Define.Split(";");
foreach (var def in defines)
{
parse.Defines.Add(def);
}
}
2024-04-25 21:47:15 +08:00
if (!string.IsNullOrWhiteSpace(opt.IncludeDir))
2024-04-18 22:17:22 +08:00
{
2024-04-25 21:47:15 +08:00
var links = opt.IncludeDir.Split(";");
2024-04-18 22:17:22 +08:00
foreach (var ink in links)
{
parse.IncludeFolders.Add(ink);
}
}
return parse;
}
public static int CMD_CheckBuildOption(CmdBuildOption opt)
{
2024-04-29 01:07:26 +08:00
if(opt.InputFiles == null || !Gen.InitTemplate(opt.Template))
2024-04-21 21:44:53 +08:00
{
return -1;
}
2024-04-18 22:17:22 +08:00
var parse_opt = MakeParserOptions(opt);
2024-04-28 22:15:01 +08:00
List<string>? file_list = null;
if (Path.Exists(opt.MacroFile))
{
file_list = opt.InputFiles.ToList();
file_list[0] = opt.MacroFile;
}
else
{
file_list = opt.InputFiles.Skip(1).ToList();
}
var compilation = CppAst.CppParser.ParseFiles(file_list, parse_opt);
2024-04-25 21:47:15 +08:00
if (opt.Verbose)
{
// Print diagnostic messages
foreach (var message in compilation.Diagnostics.Messages)
if (message.Type.Equals(CppLogMessageType.Error))
Console.WriteLine(message);
}
2024-04-21 21:44:53 +08:00
var module = ModuleMeta.ParseCompileInfo(compilation);
2024-04-29 01:07:26 +08:00
Gen gen = new Gen();
gen.GenCppMeta(module, opt.Output.ToLower());
2024-04-25 21:47:15 +08:00
if (opt.Verbose)
{
Console.WriteLine("gen success!");
}
2024-04-18 22:17:22 +08:00
return 0;
}
}
}