LLVMSharp icon indicating copy to clipboard operation
LLVMSharp copied to clipboard

`libLLVM` isn't working as expected

Open NickStrupat opened this issue 4 years ago • 4 comments

It's my understanding (could be wrong) that if I don't expect LLVM to be installed on the machine running my program, I should install the libLLVM package so it will output the LLVM library alongside my program.

Assuming that's correct so far, when my csproj is like so:

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net5.0</TargetFramework>
	</PropertyGroup>
	
	<ItemGroup>
		<PackageReference Include="LLVMSharp" Version="11.0.0-beta" />
		<PackageReference Include="libLLVM" Version="11.0.0" />
	</ItemGroup>
</Project>

...and my Program.cs is like so:

using LLVMSharp.Interop;
using System;

using var module = LLVMModuleRef.CreateWithName("NativeBinary");

I get this exception, indicating that it can't find the runtime library.

System.DllNotFoundException
  HResult=0x80131524
  Message=Unable to load DLL 'libLLVM' or one of its dependencies: The specified module could not be found. (0x8007007E)
  Source=LLVMSharp
  StackTrace:
   at LLVMSharp.Interop.LLVM.ModuleCreateWithName(SByte* ModuleID)
   at LLVMSharp.Interop.LLVMModuleRef.CreateWithName(ReadOnlySpan`1 ModuleID) in /_/sources/LLVMSharp/Interop.Extensions/LLVMModuleRef.cs:line 94
   at LLVMSharp.Interop.LLVMModuleRef.CreateWithName(String ModuleID) in /_/sources/LLVMSharp/Interop.Extensions/LLVMModuleRef.cs:line 89
   at <Program>$.<Main>$(String[] args) in C:\Dev\LLVM-Dotnet-Testing\Program.cs:line 4

The workaround I used is to specify my specific runtime by referencing package libLLVM.runtime.win-x64. While this works, I'd like to be able to build and have it output all the runtimes. Or, build specifically for a runtime with an RID and have it output the necessary libLLVM.

NickStrupat avatar Feb 16 '21 05:02 NickStrupat

This is an issue with NuGet/the runtime as dependencies aren't normally copied to the output folder for non-published builds and so, particularly with the RID specific setup libLLVM has to use, there is no way for the runtime to know where to look for the native dependency.

The easiest workaround today is to add the following to the project since that resolves to a RID and allows the correct native bits for "current machine" to be copied by default:

<PropertyGroup>
    <RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == ''">$(NETCoreSdkRuntimeIdentifier)</RuntimeIdentifier>
</PropertyGroup>

tannergooding avatar Feb 16 '21 16:02 tannergooding

Oh interesting... so if I specify a RID, I'll get the right libLLVM runtime along with my "published" output? That makes sense.

I guess I assumed NuGet would bring all of them along for the ride until a RID can be determined (by JIT?).

Thanks for you help.

NickStrupat avatar Feb 16 '21 16:02 NickStrupat

Just noting that I updated the above property to be conditional, since you want to do that so an explicit RID specified on the command line still works.

tannergooding avatar Feb 16 '21 18:02 tannergooding

I didn't see this, so I had to build LLVMSharp myself and then copied LLVMSharp\artifacts\bin\tests\LLVMSharp.UnitTests\Debug\net5.0\libLLVM.dll to make it work.

donaldnevermore avatar Mar 17 '21 13:03 donaldnevermore