Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use System.CommandLine v2 in jit-dasm
  • Loading branch information
am11 committed Nov 9, 2022
commit f93be71b897a26bb96f557a1b2bf6dc523ef9856
125 changes: 125 additions & 0 deletions src/jit-dasm/JitDasmRootCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.CommandLine;
using System.IO;

namespace ManagedCodeGen
{
internal sealed class JitDasmRootCommand : RootCommand
{
public Option<string> AltJit { get; } =
new("--altjit", "If set, the name of the altjit to use (e.g., clrjit_win_arm64_x64.dll)");
public Option<string> CrossgenPath { get; } =
new(new[] { "--crossgen", "-c" }, result => result.Tokens.Count > 0 ? Path.GetFullPath(result.Tokens[0].Value) : null, true, "The crossgen or crossgen2 compiler exe.");
public Option<string> JitPath { get; } =
new(new[] { "--jit", "-j" }, result => result.Tokens.Count > 0 ? Path.GetFullPath(result.Tokens[0].Value) : null, true, "The full path to the jit library");
public Option<string> OutputPath { get; } =
new(new[] { "--output", "-o" }, "The output path");
public Option<string> Filename { get; } =
new(new[] { "--file", "-f" }, "Name of file to take list of assemblies from. Both a file and assembly list can be used");
public Option<bool> DumpGCInfo { get; } =
new("--gcinfo", "Add GC info to the disasm output");
public Option<bool> DumpDebugInfo { get; } =
new("--debuginfo", "Add Debug info to the disasm output");
public Option<bool> Verbose { get; } =
new(new[] { "--verbose", "-v" }, "Enable verbose output");
public Option<bool> NoDiffable { get; } =
new("--nodiffable", "Generate non-diffable asm (pointer values will be left in output)");
public Option<bool> Recursive { get; } =
new(new[] { "--recursive", "-r" }, "Search directories recursively");
public Option<List<string>> PlatformPaths { get; } =
new(new[] { "--platform", "-p" }, "Path to platform assemblies");
public Option<List<string>> Methods { get; } =
new(new[] { "--methods", "-m" }, "List of methods to disasm");
public Option<List<string>> AssemblyList { get; } =
new("--assembly", "The list of assemblies or directories to scan for assemblies");
public Option<bool> WaitForDebugger { get; } =
new(new[] { "--wait", "-w" }, "Wait for debugger to attach");

public ParseResult Result;
public bool CodeGeneratorV1 { get; private set; }

public JitDasmRootCommand(string[] args) : base("Managed codegen diff tool (crossgen/AOT)")
{
AddOption(AltJit);
AddOption(CrossgenPath);
AddOption(JitPath);
AddOption(OutputPath);
AddOption(Filename);
AddOption(DumpGCInfo);
AddOption(DumpDebugInfo);
AddOption(Verbose);
AddOption(NoDiffable);
AddOption(Recursive);
AddOption(PlatformPaths);
AddOption(Methods);
AddOption(AssemblyList);
AddOption(WaitForDebugger);

this.SetHandler(context =>
{
Result = context.ParseResult;

try
{
List<string> errors = new();
string crossgen = Result.GetValueForOption(CrossgenPath);
if (crossgen == null || !File.Exists(crossgen))
{
errors.Add("Can't find --crossgen tool.");
}

string crossgenFilename = Path.GetFileNameWithoutExtension(crossgen).ToLower();
if (crossgenFilename == "crossgen")
{
CodeGeneratorV1 = true;
}
else if (crossgenFilename != "crossgen2")
{
errors.Add("--crossgen tool should be crossgen or crossgen2.");
}

if (Result.FindResultFor(Filename) == null && Result.GetValueForOption(AssemblyList).Count == 0)
{
errors.Add("No input: Specify --file <arg> or list input assemblies.");
}

string jitPath = Result.GetValueForOption(JitPath);
if (jitPath != null && !File.Exists(jitPath))
{
errors.Add("Can't find --jit library.");
}

string filename = Result.GetValueForOption(Filename);
if (filename != null && !File.Exists(filename))
{
errors.Add($"Error reading input file {filename}, file not found.");
}

if (errors.Count > 0)
{
throw new Exception(string.Join(Environment.NewLine, errors));
}

context.ExitCode = new Program(this).Run();
}
catch (Exception e)
{
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Red;

Console.Error.WriteLine("Error: " + e.Message);
Console.Error.WriteLine(e.ToString());

Console.ResetColor();

context.ExitCode = 1;
}
});
}
}
}
Loading