Skip to content

Commit

Permalink
Merge pull request #408 from tannergooding/main
Browse files Browse the repository at this point in the history
Small fixes for the dotnet tool
  • Loading branch information
tannergooding authored Nov 7, 2022
2 parents bbf343f + f93ceba commit 9d084eb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
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

0 comments on commit 9d084eb

Please sign in to comment.