From f152c992bfdc648e7b8dfca879f03d163ce6afd4 Mon Sep 17 00:00:00 2001 From: Jared Parsons Date: Sun, 6 Dec 2015 19:36:37 -0800 Subject: [PATCH] Replace timestamp with hash in generated files Some of our generators were embedding timestamps in the output files as comments. This was indirectly preventing us from producing determistic builds as every rebuild came from different content. Issue #7270 is tracking changing determinism to not be influenced by the comments. Short term though changing this from using a time stamp (which changes and doesn't have meaning between machines) to outputing the checksum of the input file it ran against. --- .../VisualBasicSyntaxGenerator/Program.vb | 22 ++++++++++++++----- .../Tests/TestWriter.vb | 6 +++-- .../Util/WriteDumper.vb | 6 +++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicSyntaxGenerator/Program.vb b/src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicSyntaxGenerator/Program.vb index 7938edbe47f..6e727d92247 100644 --- a/src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicSyntaxGenerator/Program.vb +++ b/src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicSyntaxGenerator/Program.vb @@ -4,6 +4,7 @@ Imports System.IO Imports System.Collections.Generic Imports System.Console Imports System.Runtime.InteropServices +Imports System.Security.Cryptography ''' ''' Contains the startup code, command line argument processing, and driving the execution of the tool. @@ -19,7 +20,7 @@ Friend Module Program Dim outputKind As String = Nothing Dim paths As New List(Of String)() - For Each arg in args + For Each arg In args Dim c = arg.ToLowerInvariant() If c = "/test" OrElse c = "/source" OrElse c = "/gettext" Then If outputKind IsNot Nothing Then @@ -27,7 +28,7 @@ Friend Module Program Return exitWithErrors End If outputKind = c - Else If c = "/?" Then + ElseIf c = "/?" Then PrintUsage() Return exitWithErrors Else @@ -54,7 +55,8 @@ Friend Module Program Return exitWithErrors End If - WriteOutput(outputFile, definition, outputKind) + Dim checksum = GetChecksum(inputFile) + WriteOutput(outputFile, definition, outputKind, checksum) Return exitWithoutErrors @@ -67,6 +69,14 @@ Friend Module Program End Function + Private Function GetChecksum(filePath As String) As String + Dim fileBytes = File.ReadAllBytes(filePath) + Dim func = SHA256.Create() + Dim hashBytes = func.ComputeHash(fileBytes) + Dim data = BitConverter.ToString(hashBytes) + Return data.Replace("-", "") + End Function + Private Sub PrintUsage() WriteLine("VBSyntaxGenerator.exe input output [/source] [/test]") WriteLine(" /source Generates syntax model source code.") @@ -84,11 +94,11 @@ Friend Module Program Return True End Function - Public Sub WriteOutput(outputFile As String, definition As ParseTree, outputKind As String) + Public Sub WriteOutput(outputFile As String, definition As ParseTree, outputKind As String, checksum As String) Using output As New StreamWriter(outputFile) output.WriteLine("' Definition of syntax model.") - output.WriteLine("' Generated by a tool on {0:g}", Date.Now) + output.WriteLine("' Generated by a tool from SHA256 content {0}", checksum) output.WriteLine("' DO NOT HAND EDIT") @@ -102,7 +112,7 @@ Friend Module Program output.WriteLine("Imports Roslyn.Utilities") output.WriteLine("Imports Xunit") - Dim testWriter As New TestWriter(definition) + Dim testWriter As New TestWriter(definition, checksum) testWriter.WriteTestCode(output) Case "/gettext" diff --git a/src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicSyntaxGenerator/Tests/TestWriter.vb b/src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicSyntaxGenerator/Tests/TestWriter.vb index 0c32658cb93..576411a2fb8 100644 --- a/src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicSyntaxGenerator/Tests/TestWriter.vb +++ b/src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicSyntaxGenerator/Tests/TestWriter.vb @@ -11,12 +11,14 @@ Imports System.IO Public Class TestWriter Inherits WriteUtils + Private ReadOnly _checksum As String Private _writer As TextWriter 'output is sent here. Private Const s_externalSourceDirectiveString As String = "ExternalSourceDirective" ' Initialize the class with the parse tree to write. - Public Sub New(parseTree As ParseTree) + Public Sub New(parseTree As ParseTree, checksum As String) MyBase.New(parseTree) + _checksum = checksum End Sub ' Write out the code defining the tree to the give file. @@ -28,7 +30,7 @@ Public Class TestWriter Private Sub GenerateFile() _writer.WriteLine("' Tests for parse trees.") - _writer.WriteLine("' Generated by a tool on {0:g}", DateTime.Now) + _writer.WriteLine("' Generated by a tool from SHA256 content {0}", _checksum) _writer.WriteLine("' DO NOT HAND EDIT") _writer.WriteLine() diff --git a/src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicSyntaxGenerator/Util/WriteDumper.vb b/src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicSyntaxGenerator/Util/WriteDumper.vb index c2246c6a94f..20c7ec6df19 100644 --- a/src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicSyntaxGenerator/Util/WriteDumper.vb +++ b/src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicSyntaxGenerator/Util/WriteDumper.vb @@ -11,10 +11,12 @@ Imports System.IO Friend Class WriteDumper Inherits WriteUtils + Private ReadOnly _checksum As String Private _writer As TextWriter 'output is sent here. - Public Sub New(parseTree As ParseTree) + Public Sub New(parseTree As ParseTree, checksum As String) MyBase.New(parseTree) + _checksum = checksum End Sub ' Write the dumper utility function to the given file. @@ -29,7 +31,7 @@ Friend Class WriteDumper ' Write the whole contents of the file. Private Sub GenerateFile() _writer.WriteLine("' Definition of parse trees dumper.") - _writer.WriteLine("' Generated by a tool on {0:g}", DateTime.Now) + _writer.WriteLine("' Generated by a tool from SHA256 content {0}", _checksum) _writer.WriteLine("' DO NOT HAND EDIT") _writer.WriteLine() -- GitLab