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

Small fixes for the dotnet tool #408

Merged
merged 2 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 19 additions & 8 deletions sources/ClangSharp.Interop/Extensions/clang.ResolveLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ static @clang()

private static IntPtr OnDllImport(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
return TryResolveLibrary(libraryName, assembly, searchPath, out var nativeLibrary)
? nativeLibrary
: libraryName.Equals("libclang") && TryResolveClang(assembly, searchPath, out nativeLibrary)
? nativeLibrary
: libraryName.Equals("libClangSharp") && TryResolveClangSharp(assembly, searchPath, out nativeLibrary)
? nativeLibrary
: IntPtr.Zero;
var result = TryResolveLibrary(libraryName, assembly, searchPath, out var nativeLibrary) ? nativeLibrary
: libraryName.Equals("libclang") && TryResolveClang(assembly, searchPath, out nativeLibrary) ? nativeLibrary
: libraryName.Equals("libClangSharp") && TryResolveClangSharp(assembly, searchPath, out nativeLibrary) ? nativeLibrary
: IntPtr.Zero;

if (result == IntPtr.Zero)
{
Console.WriteLine("Failed to resolve libClang or libClangSharp.");
Console.WriteLine("If you are running as a dotnet tool, you may need to manually copy the appropriate DLLs from NuGet due to limitations in the dotnet tool support.");
}
return result;
}

private static bool TryResolveClang(Assembly assembly, DllImportSearchPath? searchPath, out IntPtr nativeLibrary)
Expand All @@ -47,7 +51,14 @@ private static bool TryResolveLibrary(string libraryName, Assembly assembly, Dll

foreach (DllImportResolver resolver in resolvers.Cast<DllImportResolver>())
{
nativeLibrary = resolver(libraryName, assembly, searchPath);
try
{
nativeLibrary = resolver(libraryName, assembly, searchPath);
}
catch
{
nativeLibrary = IntPtr.Zero;
}

if (nativeLibrary != IntPtr.Zero)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<PackAsTool>true</PackAsTool>
<PackAsToolShimRuntimeIdentifiers>win-x64</PackAsToolShimRuntimeIdentifiers>
<RuntimeIdentifier></RuntimeIdentifier>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
</PropertyGroup>
Expand Down