Skip to content

Commit

Permalink
Add StableStringHash method
Browse files Browse the repository at this point in the history
This method hashes a string without taking target framework or bitness into account. Fixes dotnet#4986
  • Loading branch information
Forgind committed Sep 8, 2020
1 parent c7790e1 commit 007602d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Build/Evaluation/Expander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4017,6 +4017,14 @@ private bool TryExecuteWellKnownFunction(out object returnVal, object objectInst
return true;
}
}
else if (string.Equals(_methodMethodName, nameof(IntrinsicFunctions.StableStringHash), StringComparison.OrdinalIgnoreCase))
{
if (TryGetArg(args, out string arg0))
{
returnVal = IntrinsicFunctions.StableStringHash(arg0);
return true;
}
}
}
else if (_receiverType == typeof(Path))
{
Expand Down
8 changes: 8 additions & 0 deletions src/Build/Evaluation/IntrinsicFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ internal static string ValueOrDefault(string conditionValue, string defaultValue
}
}

///<summary>
/// Hash the string independent of bitness and target framework.
/// </summary>
internal static int StableStringHash(string toHash)
{
return CommunicationsUtilities.GetHashCode(toHash);
}

/// <summary>
/// Returns true if a task host exists that can service the requested runtime and architecture
/// values, and false otherwise.
Expand Down
4 changes: 2 additions & 2 deletions src/Shared/CommunicationsUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal Handshake(HandshakeOptions nodeType)
options = (int)nodeType | (((int)CommunicationsUtilities.handshakeVersion) << 24);
string handshakeSalt = Environment.GetEnvironmentVariable("MSBUILDNODEHANDSHAKESALT");
string toolsDirectory = (nodeType & HandshakeOptions.X64) == HandshakeOptions.X64 ? BuildEnvironmentHelper.Instance.MSBuildToolsDirectory64 : BuildEnvironmentHelper.Instance.MSBuildToolsDirectory32;
salt = CommunicationsUtilities.GetHandshakeHashCode(handshakeSalt + toolsDirectory);
salt = CommunicationsUtilities.GetHashCode(handshakeSalt + toolsDirectory);
Version fileVersion = new Version(FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion);
fileVersionMajor = fileVersion.Major;
fileVersionMinor = fileVersion.Minor;
Expand Down Expand Up @@ -620,7 +620,7 @@ internal static void Trace(int nodeId, string format, params object[] args)
/// but stripped out architecture specific defines
/// that causes the hashcode to be different and this causes problem in cross-architecture handshaking
/// </summary>
internal static int GetHandshakeHashCode(string fileVersion)
internal static int GetHashCode(string fileVersion)
{
unsafe
{
Expand Down

0 comments on commit 007602d

Please sign in to comment.