Skip to content

Commit

Permalink
Add command line options as part of input hash (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescourtney authored Aug 1, 2023
1 parent 5f74de9 commit 4456849
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/FlatSharp.Compiler/FlatSharp.Compiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
23 changes: 16 additions & 7 deletions src/FlatSharp.Compiler/FlatSharpCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private static int RunCompiler(CompilerOptions options)
{
List<byte[]> bfbs = GetBfbs(options);

string inputHash = ComputeInputHash(bfbs);
string inputHash = ComputeInputHash(bfbs, options);

if (IsInputUnchanged(outputFullPath, inputHash))
{
Expand Down Expand Up @@ -230,21 +230,30 @@ private static bool IsInputUnchanged(string outputFullPath, string inputHash)
}

[ExcludeFromCodeCoverage]
private static string ComputeInputHash(List<byte[]> bfbs)
private static string ComputeInputHash(List<byte[]> bfbs, CompilerOptions options)
{
static void MergeHashes(byte[] hash, byte[] temp)
{
for (int i = 0; i < temp.Length; ++i)
{
hash[i] ^= temp[i];
}
}

string inputHash = AssemblyVersion;

using (var hash = SHA256.Create())
{
// Use the assembly hash as the base; this means each build will change the hash for the same schema.
byte[] hashBytes = hash.ComputeHash(File.ReadAllBytes(typeof(FlatSharpCompiler).Assembly.Location));

// Supplement with command line options.
MergeHashes(hashBytes, hash.ComputeHash(Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(options))));

// Merge each of the schema files.
foreach (var schema in bfbs)
{
var tempHash = hash.ComputeHash(schema);
for (int i = 0; i < 32; ++i)
{
hashBytes[i] ^= tempHash[i];
}
MergeHashes(hashBytes, hash.ComputeHash(schema));
}

inputHash += "." + Convert.ToBase64String(hashBytes);
Expand Down

0 comments on commit 4456849

Please sign in to comment.