Skip to content

Instantly share code, notes, and snippets.

@0xced
Last active January 19, 2023 20:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 0xced/3c38e6d7c5dfd16536ba9412cb79ba64 to your computer and use it in GitHub Desktop.
Save 0xced/3c38e6d7c5dfd16536ba9412cb79ba64 to your computer and use it in GitHub Desktop.
Embed native e_sqlite3.dll or SQLite.Interop.dll with Costura without having to manually copy any dll
<!-- Useful when bundling an app using Microsoft.EntityFrameworkCore.Sqlite, which depends on SQLitePCLRaw.bundle_green, which depends on SQLitePCLRaw.lib.e_sqlite3.v110_xp (having native Windows dlls) -->
<ItemGroup>
<EmbeddedResource Include="$(NugetPackageRoot)\sqlitepclraw.lib.e_sqlite3.v110_xp\1.1.13\runtimes\win-x86\native\e_sqlite3.dll">
<Link>costura32\e_sqlite3.dll</Link>
</EmbeddedResource>
<EmbeddedResource Include="$(NugetPackageRoot)\sqlitepclraw.lib.e_sqlite3.v110_xp\1.1.13\runtimes\win-x64\native\e_sqlite3.dll">
<Link>costura64\e_sqlite3.dll</Link>
</EmbeddedResource>
</ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net472</TargetFramework>
<!-- We don't need to copy SQLite.Interop.dll since they are embedded with Costura (used by %USERPROFILE%\.nuget\packages\system.data.sqlite.core\{version}\build\net46\System.Data.SQLite.Core.targets) -->
<CopySQLiteInteropFiles>false</CopySQLiteInteropFiles>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Costura.Fody" Version="3.3.3" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.111" />
</ItemGroup>
<Target Name="EmbedSQLiteInteropFiles" BeforeTargets="ResolveReferences">
<ItemGroup>
<!-- Condition on item metadata trick adapted from https://stackoverflow.com/questions/5103026/in-msbuild-can-i-use-the-string-replace-function-on-a-metadata-item/8904902#8904902 -->
<EmbeddedResource Include="@(SQLiteInteropFiles)">
<!-- Syntax could be simplified in the future in MSBuild, see [Metadata should support instance methods](https://github.com/microsoft/msbuild/issues/1155) -->
<Link Condition="$([MSBuild]::ValueOrDefault('%(Identity)', '').Contains('x86'))">costura32\%(Filename)%(Extension)</Link>
<Link Condition="$([MSBuild]::ValueOrDefault('%(Identity)', '').Contains('x64'))">costura64\%(Filename)%(Extension)</Link>
</EmbeddedResource>
</ItemGroup>
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment