From 5979f1c52814878336399b394146b73f32d1c075 Mon Sep 17 00:00:00 2001 From: "D. Ror" Date: Wed, 27 Mar 2024 12:25:00 -0400 Subject: [PATCH] Expand Mac support (#192) --- source/icu.net/NativeMethods/NativeMethods.cs | 28 ++++++++++++------- source/icu.net/Platform.cs | 24 ++++++++-------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/source/icu.net/NativeMethods/NativeMethods.cs b/source/icu.net/NativeMethods/NativeMethods.cs index 75c0fd85..6f4f4f64 100644 --- a/source/icu.net/NativeMethods/NativeMethods.cs +++ b/source/icu.net/NativeMethods/NativeMethods.cs @@ -223,11 +223,12 @@ private static bool CheckDirectoryForIcuBinaries(string directory, string librar // Do a reverse sort so that we use the highest version files.Sort((x, y) => string.CompareOrdinal(y, x)); var filePath = files[0]; + var libNameLen = libraryName.Length; var version = IsWindows - ? Path.GetFileNameWithoutExtension(filePath).Substring(5) // strip icuuc + ? Path.GetFileNameWithoutExtension(filePath).Substring(libNameLen) // strip icuuc : IsMac - ? Path.GetFileNameWithoutExtension(filePath).Substring(9) // strip libicuuc. - : Path.GetFileName(filePath).Substring(12); // strip libicuuc.so. + ? Path.GetFileNameWithoutExtension(filePath).Substring(libNameLen + 4) // strip libicuuc. + : Path.GetFileName(filePath).Substring(libNameLen + 7); // strip libicuuc.so. Trace.WriteLineIf(Verbose, $"icu.net: Extracted version '{version}' from '{filePath}'"); if (int.TryParse(version, out var icuVersion)) { @@ -255,38 +256,45 @@ private static bool LocateIcuLibrary(string libraryName) } var arch = IsRunning64Bit ? "x64" : "x86"; - // Look for ICU binaries in lib/{win,linux}-{x86,x64} subdirectory first - var platform = IsWindows ? "win" : "linux"; + var platform = IsWindows ? "win" : IsMac? "osx" : "linux"; + + // Look for ICU binaries in lib/{win,osx,linux}-{x86,x64} subdirectory first if (CheckDirectoryForIcuBinaries( Path.Combine(DirectoryOfThisAssembly, "lib", $"{platform}-{arch}"), libraryName)) return true; - // Next look in lib/x86 or lib/x64 subdirectory + // Next look in lib/{x86,x64} subdirectory if (CheckDirectoryForIcuBinaries( Path.Combine(DirectoryOfThisAssembly, "lib", arch), libraryName)) return true; - // next try just {win,linux}-x86/x64 subdirectory + // Next try just {win,osx,linux}-{x86,x64} subdirectory if (CheckDirectoryForIcuBinaries( Path.Combine(DirectoryOfThisAssembly, $"{platform}-{arch}"), libraryName)) return true; - // next try just x86/x64 subdirectory + // Next try just {x86,x64} subdirectory if (CheckDirectoryForIcuBinaries( Path.Combine(DirectoryOfThisAssembly, arch), libraryName)) return true; - // Might also be in runtimes/win7-x64/native + // Might be in runtimes/{win,osx,linux}/native + if (CheckDirectoryForIcuBinaries( + Path.Combine(DirectoryOfThisAssembly, "runtimes", platform, "native"), + libraryName)) + return true; + + // Might also be in runtimes/win7-{x86,x64}/native if (CheckDirectoryForIcuBinaries( Path.Combine(DirectoryOfThisAssembly, "runtimes", $"win7-{arch}", "native"), libraryName)) return true; - // otherwise check the current directory + // Otherwise check the current directory // If we don't find it here we rely on it being in the PATH somewhere... return CheckDirectoryForIcuBinaries(DirectoryOfThisAssembly, libraryName); } diff --git a/source/icu.net/Platform.cs b/source/icu.net/Platform.cs index ee9ded9b..9cd08a9e 100644 --- a/source/icu.net/Platform.cs +++ b/source/icu.net/Platform.cs @@ -1,7 +1,7 @@ // Copyright (c) 2013 SIL International // This software is licensed under the MIT license (http://opensource.org/licenses/MIT) using System; -#if NETSTANDARD1_6 +#if NET || NETSTANDARD using System.Runtime.InteropServices; #endif @@ -33,7 +33,7 @@ public static string ProcessArchitecture { get { -#if NETSTANDARD1_6 +#if NET || NETSTANDARD // Workaround described here since the API does not exist: // https://github.com/dotnet/corefx/issues/999#issuecomment-75907756 return IntPtr.Size == 4 ? x86 : x64; @@ -47,7 +47,16 @@ public static OperatingSystemType OperatingSystem { get { -#if !NETSTANDARD1_6 +#if NET || NETSTANDARD + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + return OperatingSystemType.Windows; + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + return OperatingSystemType.Unix; + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + return OperatingSystemType.MacOSX; + else + throw new NotSupportedException("Cannot get OperatingSystemType from: " + RuntimeInformation.OSDescription); +#else // See http://www.mono-project.com/docs/faq/technical/#how-to-detect-the-execution-platform switch ((int)Environment.OSVersion.Platform) { @@ -59,15 +68,6 @@ public static OperatingSystemType OperatingSystem default: return OperatingSystemType.Windows; } -#else - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - return OperatingSystemType.Windows; - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - return OperatingSystemType.Unix; - else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - return OperatingSystemType.MacOSX; - else - throw new NotSupportedException("Cannot get OperatingSystemType from: " + RuntimeInformation.OSDescription); #endif } }