Skip to content

Commit

Permalink
Expand Mac support (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Mar 27, 2024
1 parent b990d03 commit 5979f1c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
28 changes: 18 additions & 10 deletions source/icu.net/NativeMethods/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -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);
}
Expand Down
24 changes: 12 additions & 12 deletions source/icu.net/Platform.cs
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -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
}
}
Expand Down

0 comments on commit 5979f1c

Please sign in to comment.