Skip to content

Commit

Permalink
Include commit hash in compiler info (#131)
Browse files Browse the repository at this point in the history
* Include commit hash in compiler info

closes #130

* tests
  • Loading branch information
jaredpar committed May 10, 2024
1 parent 926eb22 commit cb27b6d
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/Basic.CompilerLog.UnitTests/CompilerLogReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,15 +452,15 @@ public void MetadataCompat(string resourceName)
[Fact]
public void Disposed()
{
var reader = CompilerLogReader.Create(Fixture.Console.Value.CompilerLogPath);
using var reader = CompilerLogReader.Create(Fixture.Console.Value.CompilerLogPath);
reader.Dispose();
Assert.Throws<ObjectDisposedException>(() => reader.ReadCompilationData(0));
}

[Fact]
public void VisualBasic()
{
var reader = CompilerLogReader.Create(Fixture.ConsoleVisualBasic.Value.CompilerLogPath);
using var reader = CompilerLogReader.Create(Fixture.ConsoleVisualBasic.Value.CompilerLogPath);
var data = reader.ReadCompilationData(0);
Assert.True(data.IsVisualBasic);
Assert.True(data.CompilerCall.IsVisualBasic);
Expand Down
5 changes: 3 additions & 2 deletions src/Basic.CompilerLog.UnitTests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,9 @@ public void PrintCompilers()
var tuple = reader.ReadAllCompilerAssemblies().Single();
Assert.Contains($"""
Compilers
{tuple.CompilerFilePath}
{tuple.AssemblyName}
{'\t'}File Path: {tuple.CompilerFilePath}
{'\t'}Assembly Name: {tuple.AssemblyName}
{'\t'}Commit Hash: {tuple.CommitHash}
""", output);
}

Expand Down
27 changes: 17 additions & 10 deletions src/Basic.CompilerLog.Util/CompilerLogBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public bool Return(MemoryStream stream)
private readonly Dictionary<Guid, (string FileName, AssemblyName AssemblyName)> _mvidToRefInfoMap = new();
private readonly Dictionary<string, Guid> _assemblyPathToMvidMap = new(PathUtil.Comparer);
private readonly HashSet<string> _contentHashMap = new(PathUtil.Comparer);
private readonly Dictionary<string, string> _compilerAssemblyNameMap = new(PathUtil.Comparer);
private readonly Dictionary<string, (string AssemblyName, string? CommitHash)> _compilerInfoMap = new(PathUtil.Comparer);
private readonly DefaultObjectPool<MemoryStream> _memoryStreamPool = new(new MemoryStreamPoolPolicy(), maximumRetained: 5);

private int _compilationCount;
Expand Down Expand Up @@ -71,9 +71,9 @@ internal void Add(CompilerCall compilerCall, CommandLineArguments commandLineArg
CompilerCallKind = compilerCall.Kind,
CommandLineArgsHash = AddContentMessagePack(compilerCall.GetArguments()),
CompilationDataPackHash = AddCompilationDataPack(commandLineArguments),
CompilerAssemblyName = AddCompilerAssemblyName(),
};

AddCompilerInfo(infoPack, compilerCall);
AddCompilationOptions(infoPack, commandLineArguments, compilerCall);

var index = _compilationCount;
Expand Down Expand Up @@ -138,22 +138,29 @@ void AddContentIf(CompilationDataPack dataPack, RawContentKind kind, string? fil
return Path.Combine(compilerCall.ProjectDirectory, filePath);
}

string? AddCompilerAssemblyName()
void AddCompilerInfo(CompilationInfoPack infoPack, CompilerCall compilerCall)
{
if (compilerCall.CompilerFilePath is null)
{
return null;
Diagnostics.Add($"Cannot find compiler for {compilerCall.GetDiagnosticName()}");
return;
}

if (_compilerAssemblyNameMap.TryGetValue(compilerCall.CompilerFilePath, out var assemblyName))
if (!_compilerInfoMap.TryGetValue(compilerCall.CompilerFilePath, out var compilerInfo))
{
return assemblyName;
var name = AssemblyName.GetAssemblyName(compilerCall.CompilerFilePath);
compilerInfo.AssemblyName = name.ToString();
compilerInfo.CommitHash = RoslynUtil.ReadCompilerCommitHash(compilerCall.CompilerFilePath);
if (compilerInfo.CommitHash is null)
{
Diagnostics.Add($"Cannot find commit hash for {compilerCall.CompilerFilePath}");
}

_compilerInfoMap[compilerCall.CompilerFilePath] = compilerInfo;
}

var name = AssemblyName.GetAssemblyName(compilerCall.CompilerFilePath);
assemblyName = name.ToString();
_compilerAssemblyNameMap[compilerCall.CompilerFilePath] = assemblyName;
return assemblyName;
infoPack.CompilerAssemblyName = compilerInfo.AssemblyName;
infoPack.CompilerCommitHash = compilerInfo.CommitHash;
}

void AddCompilationOptions(CompilationInfoPack infoPack, CommandLineArguments args, CompilerCall compilerCall)
Expand Down
8 changes: 4 additions & 4 deletions src/Basic.CompilerLog.Util/CompilerLogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ public List<CompilerCall> ReadAllCompilerCalls(Func<CompilerCall, bool>? predica
return list;
}

public List<(string CompilerFilePath, AssemblyName AssemblyName)> ReadAllCompilerAssemblies()
public List<(string CompilerFilePath, AssemblyName AssemblyName, string? CommitHash)> ReadAllCompilerAssemblies()
{
var list = new List<(string CompilerFilePath, AssemblyName AssemblyName)>();
var map = new Dictionary<string, AssemblyName>(PathUtil.Comparer);
var map = new Dictionary<string, (AssemblyName, string?)>(PathUtil.Comparer);
for (int i = 0; i < Count; i++)
{
var pack = GetOrReadCompilationInfo(i);
Expand All @@ -246,13 +246,13 @@ pack.CompilerAssemblyName is not null &&
!map.ContainsKey(pack.CompilerFilePath))
{
var name = new AssemblyName(pack.CompilerAssemblyName);
map[pack.CompilerFilePath] = name;
map[pack.CompilerFilePath] = (name, pack.CompilerCommitHash);
}
}

return map
.OrderBy(x => x.Key, PathUtil.Comparer)
.Select(x => (x.Key, x.Value))
.Select(x => (x.Key, x.Value.Item1, x.Value.Item2))
.ToList();
}

Expand Down
29 changes: 29 additions & 0 deletions src/Basic.CompilerLog.Util/RoslynUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,4 +599,33 @@ static int GetNewlineLength(Span<char> span) =>
_ => 0
};
}

internal static string? ReadCompilerCommitHash(string assemblyFilePath)
{
using var stream = new FileStream(assemblyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var peReader = new PEReader(stream);
var metadataReader = peReader.GetMetadataReader();
var attributes = metadataReader.GetAssemblyDefinition().GetCustomAttributes();
foreach (var attributeHandle in attributes)
{
var attribute = metadataReader.GetCustomAttribute(attributeHandle);
if (attribute.Constructor.Kind is HandleKind.MemberReference)
{
var ctor = metadataReader.GetMemberReference((MemberReferenceHandle)attribute.Constructor);
if (ctor.Parent.Kind is HandleKind.TypeReference)
{
var typeNameHandle = metadataReader.GetTypeReference((TypeReferenceHandle)ctor.Parent).Name;
var typeName = metadataReader.GetString(typeNameHandle);
if (typeName.EndsWith("CommitHashAttribute"))
{
var value = metadataReader.GetBlobReader(attribute.Value);
_ = value.ReadBytes(2); // prolog
return value.ReadSerializedString();
}
}
}
}

return null;
}
}
2 changes: 2 additions & 0 deletions src/Basic.CompilerLog.Util/Serialize/MessagePackTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ public class CompilationInfoPack
public string? CompilerFilePath { get; set; }
[Key(10)]
public string? CompilerAssemblyName { get; set; }
[Key(11)]
public string? CompilerCommitHash { get; set; }
}

[MessagePackObject]
Expand Down
7 changes: 4 additions & 3 deletions src/Basic.CompilerLog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,17 @@ int RunPrint(IEnumerable<string> args)
WriteLine("Projects");
foreach (var compilerCall in compilerCalls)
{
WriteLine(compilerCall.GetDiagnosticName());
WriteLine($"\t{compilerCall.GetDiagnosticName()}");
}

if (compilers)
{
WriteLine("Compilers");
foreach (var tuple in reader.ReadAllCompilerAssemblies())
{
WriteLine(tuple.CompilerFilePath);
WriteLine(tuple.AssemblyName.ToString());
WriteLine($"\tFile Path: {tuple.CompilerFilePath}");
WriteLine($"\tAssembly Name: {tuple.AssemblyName}");
WriteLine($"\tCommit Hash: {tuple.CommitHash}");
}
}

Expand Down
39 changes: 37 additions & 2 deletions src/Scratch/Scratch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Diagnostics;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Threading.Tasks;
using Basic.CompilerLog;
using Basic.CompilerLog.Util;
Expand Down Expand Up @@ -38,7 +42,7 @@

// Profile();

TestBinaryLogReader();
ReadAttribute();
// ExportScratch();
// await WorkspaceScratch();
// RoslynScratch();
Expand Down Expand Up @@ -89,6 +93,37 @@
}
*/

void ReadAttribute()
{
var assemblyPath = @"c:\Program Files\dotnet\sdk\8.0.204\Roslyn\bincore\csc.dll";
using (var stream = File.OpenRead(assemblyPath))
using (var peReader = new PEReader(stream))
{
var metadataReader = peReader.GetMetadataReader();
var attributes = metadataReader.GetAssemblyDefinition().GetCustomAttributes();
foreach (var attributeHandle in attributes)
{
var attribute = metadataReader.GetCustomAttribute(attributeHandle);
if (attribute.Constructor.Kind is HandleKind.MemberReference)
{
var ctor = metadataReader.GetMemberReference((MemberReferenceHandle)attribute.Constructor);
if (ctor.Parent.Kind is HandleKind.TypeReference)
{
var typeNameHandle = metadataReader.GetTypeReference((TypeReferenceHandle)ctor.Parent).Name;
var typeName = metadataReader.GetString(typeNameHandle);
if (typeName.EndsWith("CommitHashAttribute"))
{
var value = metadataReader.GetBlobReader(attribute.Value);
_ = value.ReadBytes(2); // prolog
var str = value.ReadSerializedString();
Console.WriteLine("here");
}
}
}
}
}
}

void TestBinaryLogReader()
{
var binlogPath = @"e:\temp\console\build.binlog";
Expand Down

0 comments on commit cb27b6d

Please sign in to comment.