Skip to content

Commit 5979f1c

Browse files
authored
Expand Mac support (#192)
1 parent b990d03 commit 5979f1c

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

source/icu.net/NativeMethods/NativeMethods.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,12 @@ private static bool CheckDirectoryForIcuBinaries(string directory, string librar
223223
// Do a reverse sort so that we use the highest version
224224
files.Sort((x, y) => string.CompareOrdinal(y, x));
225225
var filePath = files[0];
226+
var libNameLen = libraryName.Length;
226227
var version = IsWindows
227-
? Path.GetFileNameWithoutExtension(filePath).Substring(5) // strip icuuc
228+
? Path.GetFileNameWithoutExtension(filePath).Substring(libNameLen) // strip icuuc
228229
: IsMac
229-
? Path.GetFileNameWithoutExtension(filePath).Substring(9) // strip libicuuc.
230-
: Path.GetFileName(filePath).Substring(12); // strip libicuuc.so.
230+
? Path.GetFileNameWithoutExtension(filePath).Substring(libNameLen + 4) // strip libicuuc.
231+
: Path.GetFileName(filePath).Substring(libNameLen + 7); // strip libicuuc.so.
231232
Trace.WriteLineIf(Verbose, $"icu.net: Extracted version '{version}' from '{filePath}'");
232233
if (int.TryParse(version, out var icuVersion))
233234
{
@@ -255,38 +256,45 @@ private static bool LocateIcuLibrary(string libraryName)
255256
}
256257

257258
var arch = IsRunning64Bit ? "x64" : "x86";
258-
// Look for ICU binaries in lib/{win,linux}-{x86,x64} subdirectory first
259-
var platform = IsWindows ? "win" : "linux";
259+
var platform = IsWindows ? "win" : IsMac? "osx" : "linux";
260+
261+
// Look for ICU binaries in lib/{win,osx,linux}-{x86,x64} subdirectory first
260262
if (CheckDirectoryForIcuBinaries(
261263
Path.Combine(DirectoryOfThisAssembly, "lib", $"{platform}-{arch}"),
262264
libraryName))
263265
return true;
264266

265-
// Next look in lib/x86 or lib/x64 subdirectory
267+
// Next look in lib/{x86,x64} subdirectory
266268
if (CheckDirectoryForIcuBinaries(
267269
Path.Combine(DirectoryOfThisAssembly, "lib", arch),
268270
libraryName))
269271
return true;
270272

271-
// next try just {win,linux}-x86/x64 subdirectory
273+
// Next try just {win,osx,linux}-{x86,x64} subdirectory
272274
if (CheckDirectoryForIcuBinaries(
273275
Path.Combine(DirectoryOfThisAssembly, $"{platform}-{arch}"),
274276
libraryName))
275277
return true;
276278

277-
// next try just x86/x64 subdirectory
279+
// Next try just {x86,x64} subdirectory
278280
if (CheckDirectoryForIcuBinaries(
279281
Path.Combine(DirectoryOfThisAssembly, arch),
280282
libraryName))
281283
return true;
282284

283-
// Might also be in runtimes/win7-x64/native
285+
// Might be in runtimes/{win,osx,linux}/native
286+
if (CheckDirectoryForIcuBinaries(
287+
Path.Combine(DirectoryOfThisAssembly, "runtimes", platform, "native"),
288+
libraryName))
289+
return true;
290+
291+
// Might also be in runtimes/win7-{x86,x64}/native
284292
if (CheckDirectoryForIcuBinaries(
285293
Path.Combine(DirectoryOfThisAssembly, "runtimes", $"win7-{arch}", "native"),
286294
libraryName))
287295
return true;
288296

289-
// otherwise check the current directory
297+
// Otherwise check the current directory
290298
// If we don't find it here we rely on it being in the PATH somewhere...
291299
return CheckDirectoryForIcuBinaries(DirectoryOfThisAssembly, libraryName);
292300
}

source/icu.net/Platform.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2013 SIL International
22
// This software is licensed under the MIT license (http://opensource.org/licenses/MIT)
33
using System;
4-
#if NETSTANDARD1_6
4+
#if NET || NETSTANDARD
55
using System.Runtime.InteropServices;
66
#endif
77

@@ -33,7 +33,7 @@ public static string ProcessArchitecture
3333
{
3434
get {
3535

36-
#if NETSTANDARD1_6
36+
#if NET || NETSTANDARD
3737
// Workaround described here since the API does not exist:
3838
// https://github.com/dotnet/corefx/issues/999#issuecomment-75907756
3939
return IntPtr.Size == 4 ? x86 : x64;
@@ -47,7 +47,16 @@ public static OperatingSystemType OperatingSystem
4747
{
4848
get
4949
{
50-
#if !NETSTANDARD1_6
50+
#if NET || NETSTANDARD
51+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
52+
return OperatingSystemType.Windows;
53+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
54+
return OperatingSystemType.Unix;
55+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
56+
return OperatingSystemType.MacOSX;
57+
else
58+
throw new NotSupportedException("Cannot get OperatingSystemType from: " + RuntimeInformation.OSDescription);
59+
#else
5160
// See http://www.mono-project.com/docs/faq/technical/#how-to-detect-the-execution-platform
5261
switch ((int)Environment.OSVersion.Platform)
5362
{
@@ -59,15 +68,6 @@ public static OperatingSystemType OperatingSystem
5968
default:
6069
return OperatingSystemType.Windows;
6170
}
62-
#else
63-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
64-
return OperatingSystemType.Windows;
65-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
66-
return OperatingSystemType.Unix;
67-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
68-
return OperatingSystemType.MacOSX;
69-
else
70-
throw new NotSupportedException("Cannot get OperatingSystemType from: " + RuntimeInformation.OSDescription);
7171
#endif
7272
}
7373
}

0 commit comments

Comments
 (0)