Skip to content

Commit

Permalink
Remove IO from source generator, load dll-s directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Aug 26, 2022
1 parent e79477a commit 3ed7a22
Showing 1 changed file with 3 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,29 +226,6 @@ private static string FixupExceptionMessage(string message)
/// <exception cref="Win32Exception">Thrown if a library fails to load.</exception>
private static void LoadNativeDxcLibraries()
{
// Extracts a specified native library for a given runtime identifier
static string ExtractLibrary(string folder, string rid, string name)
{
string sourceFilename = $"ComputeSharp.SourceGenerators.ComputeSharp.Libraries.{rid}.{name}.dll";
string targetFilename = Path.Combine(folder, rid, $"{name}.dll");

Directory.CreateDirectory(Path.GetDirectoryName(targetFilename));

using Stream sourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(sourceFilename);

try
{
using Stream destinationStream = File.Open(targetFilename, FileMode.CreateNew, FileAccess.Write);

sourceStream.CopyTo(destinationStream);
}
catch (IOException)
{
}

return targetFilename;
}

// Loads a target native library
static unsafe void LoadLibrary(string filename)
{
Expand All @@ -275,10 +252,10 @@ static unsafe void LoadLibrary(string filename)
_ => throw new NotSupportedException("Invalid process architecture")
};

string folder = Path.Combine(Path.GetTempPath(), "ComputeSharp.SourceGenerators", Path.GetRandomFileName());
string folder = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Libraries", rid);

LoadLibrary(ExtractLibrary(folder, rid, "dxil"));
LoadLibrary(ExtractLibrary(folder, rid, "dxcompiler"));
LoadLibrary(Path.Combine(folder, "dxil.dll"));
LoadLibrary(Path.Combine(folder, "dxcompiler.dll"));

areDxcLibrariesLoaded = true;
}
Expand Down

0 comments on commit 3ed7a22

Please sign in to comment.