Skip to content

Commit 93fbdfb

Browse files
committed
Emit compiler version to Windows PDB
1 parent b88948f commit 93fbdfb

File tree

7 files changed

+76
-2
lines changed

7 files changed

+76
-2
lines changed

src/Compilers/Core/Portable/DiaSymReader/Metadata/IMetadataEmit.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#nullable disable
6-
75
#pragma warning disable 436 // SuppressUnmanagedCodeSecurityAttribute defined in source and mscorlib
86

97
using System;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
namespace Microsoft.DiaSymReader
9+
{
10+
[ComImport]
11+
[Guid("2ae6a06a-92ba-4c2d-a64e-7e9fa421a330")]
12+
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
13+
[ComVisible(false)]
14+
internal interface ISymUnmanagedCompilerInfoWriter
15+
{
16+
/// <summary>
17+
/// Adds compiler version number and name.
18+
/// </summary>
19+
[PreserveSig]
20+
int AddCompilerInfo(ushort major, ushort minor, ushort build, ushort revision, [MarshalAs(UnmanagedType.LPWStr)] string name);
21+
}
22+
}

src/Compilers/Core/Portable/DiaSymReader/Writer/SymUnmanagedWriter.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,5 +208,20 @@ public abstract void SetAsyncInfo(
208208
/// <exception cref="InvalidOperationException">Writes are not allowed to the underlying stream.</exception>
209209
/// <exception cref="SymUnmanagedWriterException">Error occurred while writing PDB data.</exception>
210210
public abstract void CloseTokensToSourceSpansMap();
211+
212+
/// <summary>
213+
/// Writes compiler version and name to the PDB.
214+
/// </summary>
215+
/// <param name="major">Major version</param>
216+
/// <param name="minor">Minor version</param>
217+
/// <param name="build">Build</param>
218+
/// <param name="revision">Revision</param>
219+
/// <param name="name">Compiler name</param>
220+
/// <exception cref="ObjectDisposedException">Object has been disposed.</exception>
221+
/// <exception cref="SymUnmanagedWriterException">Error occurred while writing PDB data.</exception>
222+
/// <exception cref="NotSupportedException">The PDB writer does not support adding compiler info.</exception>
223+
/// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
224+
public virtual void AddCompilerInfo(ushort major, ushort minor, ushort build, ushort revision, string name)
225+
=> throw new NotSupportedException();
211226
}
212227
}

src/Compilers/Core/Portable/DiaSymReader/Writer/SymUnmanagedWriterImpl.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,5 +749,28 @@ public override unsafe void GetSignature(out Guid guid, out uint stamp, out int
749749
// we need to go through IPdbWriter interface to get it.
750750
((IPdbWriter)symWriter).GetSignatureAge(out stamp, out age);
751751
}
752+
753+
public override void AddCompilerInfo(ushort major, ushort minor, ushort build, ushort revision, string name)
754+
{
755+
if (name == null)
756+
{
757+
throw new ArgumentNullException(nameof(name));
758+
}
759+
760+
var symWriter = GetSymWriter();
761+
if (symWriter is not ISymUnmanagedCompilerInfoWriter infoWriter)
762+
{
763+
return;
764+
}
765+
766+
try
767+
{
768+
infoWriter.AddCompilerInfo(major, minor, build, revision, name);
769+
}
770+
catch (Exception ex)
771+
{
772+
throw PdbWritingException(ex);
773+
}
774+
}
752775
}
753776
}

src/Compilers/Core/Portable/NativePdbWriter/PdbWriter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Diagnostics;
1111
using System.IO;
1212
using System.Linq;
13+
using System.Reflection;
1314
using System.Reflection.Metadata;
1415
using System.Reflection.Metadata.Ecma335;
1516
using System.Security.Cryptography;
@@ -768,5 +769,13 @@ public void WriteRemainingDebugDocuments(IReadOnlyDictionary<string, DebugSource
768769
AddDocumentIndex(kvp.Value);
769770
}
770771
}
772+
773+
public void WriteCompilerVersion(string language)
774+
{
775+
var compilerAssembly = typeof(Compilation).Assembly;
776+
var fileVersion = Version.Parse(compilerAssembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version);
777+
var versionString = compilerAssembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
778+
_symWriter.AddCompilerInfo((ushort)fileVersion.Major, (ushort)fileVersion.Minor, (ushort)fileVersion.Build, (ushort)fileVersion.Revision, $"{language} - {versionString}");
779+
}
771780
}
772781
}

src/Compilers/Core/Portable/PEWriter/PeWriter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ internal static bool WritePeToStream(
108108
}
109109

110110
nativePdbWriterOpt.WriteRemainingDebugDocuments(mdWriter.Module.DebugDocumentsBuilder.DebugDocuments);
111+
112+
nativePdbWriterOpt.WriteCompilerVersion(context.Module.CommonCompilation.Language);
111113
}
112114

113115
Stream peStream = getPeStream();

src/Test/PdbUtilities/Writer/MockSymUnmanagedWriter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,10 @@ public override void WriteTo(Stream stream)
126126
{
127127
throw MakeException();
128128
}
129+
130+
public override void AddCompilerInfo(ushort major, ushort minor, ushort build, ushort revision, string name)
131+
{
132+
throw MakeException();
133+
}
129134
}
130135
}

0 commit comments

Comments
 (0)