Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add app-local support for ICU #35383

Merged
merged 8 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ libSystem.Globalization.Native!GlobalizationNative_GetSortVersion
libSystem.Globalization.Native!GlobalizationNative_GetTimeZoneDisplayName
libSystem.Globalization.Native!GlobalizationNative_IndexOf
libSystem.Globalization.Native!GlobalizationNative_IndexOfOrdinalIgnoreCase
libSystem.Globalization.Native!GlobalizationNative_InitICUFunctions
libSystem.Globalization.Native!GlobalizationNative_IsNormalized
libSystem.Globalization.Native!GlobalizationNative_IsPredefinedLocale
libSystem.Globalization.Native!GlobalizationNative_LastIndexOf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Reflection;

namespace System.Globalization
{
internal static partial class GlobalizationMode
Expand All @@ -17,7 +19,7 @@ private static bool GetGlobalizationInvariantMode()
bool invariantEnabled = GetInvariantSwitchValue();
if (!invariantEnabled)
{
if (Interop.Globalization.LoadICU() == 0)
if (!LoadIcu())
{
string message = "Couldn't find a valid ICU package installed on the system. " +
"Set the configuration flag System.Globalization.Invariant to true if you want to run with no globalization support.";
Expand All @@ -26,5 +28,44 @@ private static bool GetGlobalizationInvariantMode()
}
return invariantEnabled;
}

private static bool LoadIcu()
{
if (!TryGetAppLocalIcuSwitchValue(out ReadOnlySpan<char> version, out ReadOnlySpan<char> suffix))
{
return Interop.Globalization.LoadICU() != 0;
}

#if TARGET_OSX
string extension = ".dylib";
bool versionAtEnd = false;
#else
string extension = "so.";
safern marked this conversation as resolved.
Show resolved Hide resolved
bool versionAtEnd = true;
#endif

// Append '.' to suffix since in Unix the version is separated by '.'
safern marked this conversation as resolved.
Show resolved Hide resolved
int suffixLength = suffix.Length + 1;
Span<char> suffixWithSeparator = stackalloc char[suffixLength];
suffix.CopyTo(suffixWithSeparator);
suffixWithSeparator[suffixLength - 1] = '.';

Assembly assembly = Assembly.GetExecutingAssembly();
safern marked this conversation as resolved.
Show resolved Hide resolved

#if !TARGET_OSX
// In Linux we need to load libicudata first because libicuuc and libicui18n depend on it. In order for the loader to find
safern marked this conversation as resolved.
Show resolved Hide resolved
// it on the same path, we load it before loading the other two libraries.
string icudataBase = "libicudata";
LoadLibrary(CreateLibraryName(icudataBase, suffixWithSeparator, extension, version, versionAtEnd), assembly, failOnLoadFailure: true);
#endif

string icuucBase = "libicuuc";
string icuinBase = "libicui18n";
safern marked this conversation as resolved.
Show resolved Hide resolved
IntPtr icuucLib = LoadLibrary(CreateLibraryName(icuucBase, suffixWithSeparator, extension, version, versionAtEnd), assembly, failOnLoadFailure: true);
IntPtr icuinLib = LoadLibrary(CreateLibraryName(icuinBase, suffixWithSeparator, extension, version, versionAtEnd), assembly, failOnLoadFailure: true);

Interop.Globalization.InitICU(icuucLib, icuinLib, version.ToString(), suffix.Length > 0 ? suffix.ToString() : null);
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Reflection;
using System.Runtime.InteropServices;

namespace System.Globalization
{
internal static partial class GlobalizationMode
Expand All @@ -12,6 +15,47 @@ internal static partial class GlobalizationMode

internal static bool UseNls { get; } = !Invariant &&
(GetSwitchValue("System.Globalization.UseNls", "DOTNET_SYSTEM_GLOBALIZATION_USENLS") ||
Interop.Globalization.LoadICU() == 0);
!LoadIcu());

private static bool LoadIcu()
{
if (!TryGetAppLocalIcuSwitchValue(out ReadOnlySpan<char> version, out ReadOnlySpan<char> suffix))
{
return Interop.Globalization.LoadICU() != 0;
}

string extension = ".dll";
string icuucBase = "icuuc";
string icuinBase = "icuin";
safern marked this conversation as resolved.
Show resolved Hide resolved
IntPtr icuucLib = IntPtr.Zero;
IntPtr icuinLib = IntPtr.Zero;
Assembly assembly = Assembly.GetExecutingAssembly();

int index = version.ToString().IndexOf('.', StringComparison.Ordinal);
safern marked this conversation as resolved.
Show resolved Hide resolved

if (index != -1)
safern marked this conversation as resolved.
Show resolved Hide resolved
{
ReadOnlySpan<char> truncatedVersion = version.Slice(0, index);
icuucLib = LoadLibrary(CreateLibraryName(icuucBase, suffix, extension, truncatedVersion), assembly, failOnLoadFailure: false);

if (icuucLib != IntPtr.Zero)
{
icuinLib = LoadLibrary(CreateLibraryName(icuinBase, suffix, extension, truncatedVersion), assembly, failOnLoadFailure: false);
}
}

if (icuucLib == IntPtr.Zero)
{
icuucLib = LoadLibrary(CreateLibraryName(icuucBase, suffix, extension, version), assembly, failOnLoadFailure: true);
}

if (icuinLib == IntPtr.Zero)
{
icuinLib = LoadLibrary(CreateLibraryName(icuinBase, suffix, extension, version), assembly, failOnLoadFailure: true);
}

Interop.Globalization.InitICU(icuucLib, icuinLib, version.ToString(), suffix.Length > 0 ? suffix.ToString() : null);
safern marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,62 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.InteropServices;

namespace System.Globalization
{
internal static partial class GlobalizationMode
{
private static bool GetInvariantSwitchValue() =>
GetSwitchValue("System.Globalization.Invariant", "DOTNET_SYSTEM_GLOBALIZATION_INVARIANT");

private static bool TryGetAppLocalIcuSwitchValue(out ReadOnlySpan<char> version, out ReadOnlySpan<char> icuSuffix)
{
icuSuffix = default;

if (!TryGetStringValue("System.Globalization.AppLocalIcu", "DOTNET_SYSTEM_GLOBALIZATION_APPLOCALICU", out string? value))
{
version = default;
return false;
}

// Custom built ICU can have a suffix on the name, i.e: libicuucmyapp.so.67.1
// So users would set the runtime switch as: myapp:67.1

int indexOfSeparator = value.IndexOf(':', StringComparison.Ordinal);
safern marked this conversation as resolved.
Show resolved Hide resolved
if (indexOfSeparator != -1)
{
ReadOnlySpan<char> valueAsSpan = value.AsSpan();
icuSuffix = valueAsSpan.Slice(0, indexOfSeparator);
safern marked this conversation as resolved.
Show resolved Hide resolved

if (icuSuffix.Length > 20)
safern marked this conversation as resolved.
Show resolved Hide resolved
{
Environment.FailFast($"The resolved \"{icuSuffix.ToString()}\" suffix from System.Globalization.AppLocalIcu switch has to be < 20 chars long.");
safern marked this conversation as resolved.
Show resolved Hide resolved
}

version = valueAsSpan.Slice(icuSuffix.Length + 1);
safern marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
version = value;
}

if (version.Length > 33)
{
Environment.FailFast($"The resolved version \"{version.ToString()}\" from System.Globalization.AppLocalIcu switch has to be < 33 chars long.");
}

return true;
}

// GetSwitchValue calls CLRConfig first to detect if the switch is defined in the config file.
// if the switch is defined we just use the value of this switch. otherwise, we'll try to get the switch
// value from the environment variable if it is defined.
private static bool GetSwitchValue(string switchName, string envVariable)
{
bool ret = CLRConfig.GetBoolValue(switchName, out bool exist);
if (!exist)
if (!AppContext.TryGetSwitch(switchName, out bool ret))
{
string? switchValue = Environment.GetEnvironmentVariable(envVariable);
if (switchValue != null)
Expand All @@ -26,5 +68,61 @@ private static bool GetSwitchValue(string switchName, string envVariable)

return ret;
}

private static bool TryGetStringValue(string switchName, string envVariable, [NotNullWhen(true)] out string? value)
{
value = AppContext.GetData(switchName) as string;
if (string.IsNullOrEmpty(value))
{
value = Environment.GetEnvironmentVariable(envVariable);
if (string.IsNullOrEmpty(value))
{
return false;
}
}

return true;
}

private static string CreateLibraryName(ReadOnlySpan<char> baseName, ReadOnlySpan<char> suffix, ReadOnlySpan<char> extension, ReadOnlySpan<char> version, bool versionAtEnd = false)
{
int length = baseName.Length + suffix.Length + version.Length + extension.Length;

// We validate that suffix and version are not larger than 53 characters.
safern marked this conversation as resolved.
Show resolved Hide resolved
Span<char> result = stackalloc char[length];
baseName.CopyTo(result);

Span<char> secondPart = result.Slice(baseName.Length);
suffix.CopyTo(secondPart);

Span<char> middle = secondPart.Slice(suffix.Length);

if (!versionAtEnd)
{
version.CopyTo(middle);

Span<char> end = middle.Slice(version.Length);
extension.CopyTo(end);
}
else
{
extension.CopyTo(middle);

Span<char> end = middle.Slice(extension.Length);
version.CopyTo(end);
}

return result.ToString();
}

private static IntPtr LoadLibrary(string library, Assembly assembly, bool failOnLoadFailure)
{
if (!NativeLibrary.TryLoad(library, assembly, DllImportSearchPath.ApplicationDirectory, out IntPtr lib) && failOnLoadFailure)
{
Environment.FailFast($"Failed to load app-local ICU: {library}");
safern marked this conversation as resolved.
Show resolved Hide resolved
}

return lib;
safern marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
1 change: 1 addition & 0 deletions src/coreclr/src/libraries-native/entrypoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ FCFuncStart(gPalGlobalizationNative)
QCFuncElement("GetTimeZoneDisplayName", GlobalizationNative_GetTimeZoneDisplayName)
QCFuncElement("IndexOf", GlobalizationNative_IndexOf)
QCFuncElement("IndexOfOrdinalIgnoreCase", GlobalizationNative_IndexOfOrdinalIgnoreCase)
QCFuncElement("InitICU", GlobalizationNative_InitICUFunctions)
QCFuncElement("IsNormalized", GlobalizationNative_IsNormalized)
QCFuncElement("IsPredefinedLocale", GlobalizationNative_IsPredefinedLocale)
QCFuncElement("LastIndexOf", GlobalizationNative_LastIndexOf)
Expand Down
1 change: 1 addition & 0 deletions src/libraries/Common/src/Interop/Interop.Collation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal static partial class Globalization

[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_IndexOfOrdinalIgnoreCase")]
internal static extern unsafe int IndexOfOrdinalIgnoreCase(string target, int cwTargetLength, char* pSource, int cwSourceLength, bool findLast);

[DllImport(Libraries.GlobalizationNative, CharSet = CharSet.Unicode, EntryPoint = "GlobalizationNative_IndexOfOrdinalIgnoreCase")]
internal static extern unsafe int IndexOfOrdinalIgnoreCase(char* target, int cwTargetLength, char* pSource, int cwSourceLength, bool findLast);

Expand Down
4 changes: 4 additions & 0 deletions src/libraries/Common/src/Interop/Interop.ICU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
Expand All @@ -11,6 +12,9 @@ internal static partial class Globalization
[DllImport(Libraries.GlobalizationNative, EntryPoint = "GlobalizationNative_LoadICU")]
internal static extern int LoadICU();

[DllImport(Libraries.GlobalizationNative, EntryPoint = "GlobalizationNative_InitICUFunctions")]
safern marked this conversation as resolved.
Show resolved Hide resolved
internal static extern void InitICU(IntPtr icuuc, IntPtr icuin, string version, string? suffix);
stephentoub marked this conversation as resolved.
Show resolved Hide resolved

[DllImport(Libraries.GlobalizationNative, EntryPoint = "GlobalizationNative_GetICUVersion")]
internal static extern int GetICUVersion();
}
Expand Down
Loading