Repository navigation
Build cleanly as a git-submodule project reference #3
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
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Reflection; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace NSpark.Signer; | ||
|
|
||
| /// <summary> | ||
| /// Locates <c>libspark_frost</c> under <c>runtimes/{rid}/native/</c> next to the application. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// A NuGet consumer resolves the library through <c>deps.json</c>. A consumer that references | ||
| /// NSpark as a project (for example from a git submodule) gets the same <c>runtimes/</c> tree | ||
| /// copied to its output but no <c>deps.json</c> entry for it, so default probing misses it. | ||
| /// This resolver covers that case and otherwise defers to default probing. | ||
| /// </remarks> | ||
| internal static class NativeLibraryResolver | ||
| { | ||
| internal const string LibraryName = "spark_frost"; | ||
|
|
||
| [ModuleInitializer] | ||
| [SuppressMessage("Usage", "CA2255:The 'ModuleInitializer' attribute should not be used in libraries", | ||
| Justification = "The resolver must be in place before the first P/Invoke into spark_frost, from any entry point.")] | ||
| internal static void Register() => | ||
| NativeLibrary.SetDllImportResolver(typeof(NativeLibraryResolver).Assembly, Resolve); | ||
|
|
||
| private static IntPtr Resolve(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) | ||
| { | ||
| if (!string.Equals(libraryName, LibraryName, StringComparison.Ordinal)) | ||
| { | ||
| return IntPtr.Zero; | ||
| } | ||
|
|
||
| foreach (var candidate in Candidates()) | ||
| { | ||
| if (File.Exists(candidate) && NativeLibrary.TryLoad(candidate, out var handle)) | ||
| { | ||
| return handle; | ||
| } | ||
| } | ||
|
|
||
| return IntPtr.Zero; | ||
| } | ||
|
|
||
| internal static IEnumerable<string> Candidates() | ||
| { | ||
| var fileName = OperatingSystem.IsWindows() ? "spark_frost.dll" | ||
| : OperatingSystem.IsMacOS() ? "libspark_frost.dylib" | ||
| : "libspark_frost.so"; | ||
|
|
||
| // The runtime's RID first; a distro-built (non-portable) runtime reports e.g. | ||
| // "ubuntu.24.04-x64", so fall back to the portable RID for this OS and architecture. | ||
| foreach (var rid in new[] { RuntimeInformation.RuntimeIdentifier, PortableRid() }.Distinct(StringComparer.Ordinal)) | ||
| { | ||
| yield return Path.Combine(AppContext.BaseDirectory, "runtimes", rid, "native", fileName); | ||
| } | ||
| } | ||
|
|
||
| private static string PortableRid() | ||
| { | ||
| var os = OperatingSystem.IsWindows() ? "win" : OperatingSystem.IsMacOS() ? "osx" : "linux"; | ||
| var arch = RuntimeInformation.ProcessArchitecture switch | ||
| { | ||
| Architecture.Arm64 => "arm64", | ||
| Architecture.X86 => "x86", | ||
| _ => "x64", | ||
| }; | ||
| return $"{os}-{arch}"; | ||
| } | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
tests/NSpark.UnitTests/Signer/NativeLibraryResolverTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| using System.Runtime.InteropServices; | ||
| using NSpark.Signer; | ||
|
|
||
| namespace NSpark.UnitTests.Signer; | ||
|
|
||
| /// <summary> | ||
| /// Tests for <see cref="NativeLibraryResolver"/>, which lets project-reference consumers load | ||
| /// <c>libspark_frost</c> from <c>runtimes/{rid}/native/</c> without a deps.json entry. | ||
| /// </summary> | ||
| public class NativeLibraryResolverTests | ||
| { | ||
| [Test] | ||
| public void Candidates_LookUnderTheRuntimesFolderNextToTheApp() | ||
| { | ||
| var candidates = NativeLibraryResolver.Candidates().ToList(); | ||
|
|
||
| candidates.Should().NotBeEmpty(); | ||
| candidates.Should().OnlyContain(path => | ||
| path.StartsWith(Path.Combine(AppContext.BaseDirectory, "runtimes"), StringComparison.Ordinal) && | ||
| Path.GetFileName(Path.GetDirectoryName(path)) == "native"); | ||
| candidates.Should().Contain(path => path.Contains(RuntimeInformation.RuntimeIdentifier, StringComparison.Ordinal)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Candidates_UseThePlatformLibraryFileName() | ||
| { | ||
| var expected = OperatingSystem.IsWindows() ? "spark_frost.dll" | ||
| : OperatingSystem.IsMacOS() ? "libspark_frost.dylib" | ||
| : "libspark_frost.so"; | ||
|
|
||
| NativeLibraryResolver.Candidates().Should().OnlyContain(path => Path.GetFileName(path) == expected); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.