|
| 1 | + <Project> |
| 2 | + <!-- |
| 3 | + ILRepack.targets provides MSBuild integration for ILRepack, allowing merging of assemblies into a single output. |
| 4 | +
|
| 5 | + Extension Points: |
| 6 | + - Property 'ILRepack': Set to 'true' to enable ILRepack (default: true in Release, false otherwise). |
| 7 | + - Items: |
| 8 | + * ILRepackInclude: assemblies to include |
| 9 | + * ILRepackExclude: assemblies to exclude |
| 10 | + * ILRepackPreserve: assemblies to preserve after merging (otherwise, they are deleted from output) |
| 11 | + |
| 12 | + Items can have metadata 'StartsWith' set to 'true' to indicate prefix matching rather than |
| 13 | + exact matching. |
| 14 | + --> |
| 15 | + |
| 16 | + <PropertyGroup> |
| 17 | + <ILRepack Condition="'$(ILRepack)' == '' and '$(Configuration)' == 'Release'">true</ILRepack> |
| 18 | + <ILRepack Condition="'$(ILRepack)' == ''">false</ILRepack> |
| 19 | + <!-- We need to turn on copy-local for ILRepack to find deps --> |
| 20 | + <CopyLocalLockFileAssemblies Condition="'$(CopyLocalLockFileAssemblies)' == '' and '$(ILRepack)' == 'true'">true</CopyLocalLockFileAssemblies> |
| 21 | + </PropertyGroup> |
| 22 | + |
| 23 | + <Target Name="EnsureILRepack" BeforeTargets="ILRepack" Condition="'$(ILRepack)' == 'true'"> |
| 24 | + <Exec Command="ilrepack --version" StandardErrorImportance="high" StandardOutputImportance="low" ConsoleToMSBuild="true" IgnoreExitCode="true" ContinueOnError="true"> |
| 25 | + <Output TaskParameter="ConsoleOutput" PropertyName="ILRepackOutput" /> |
| 26 | + <Output TaskParameter="ExitCode" PropertyName="ExitCode" /> |
| 27 | + </Exec> |
| 28 | + <Message Importance="high" Text="Using installed dotnet-ilrepack v$(ILRepackOutput)" Condition="$(ExitCode) == '0'" /> |
| 29 | + <Exec Command="dotnet tool install -g dotnet-ilrepack" |
| 30 | + Condition="$(ExitCode) != '0'" /> |
| 31 | + <Exec Command="ilrepack --version" Condition="$(ExitCode) != '0'" ConsoleToMSBuild="true"> |
| 32 | + <Output TaskParameter="ConsoleOutput" PropertyName="ILRepackInstalledOutput" /> |
| 33 | + </Exec> |
| 34 | + <Message Importance="high" Text="Installed dotnet-ilrepack v$(ILRepackInstalledOutput)" Condition="$(ExitCode) != '0'" /> |
| 35 | + </Target> |
| 36 | + |
| 37 | + <Target Name="ILRepackPrepare"> |
| 38 | + <ItemGroup> |
| 39 | + <ILRepackIncludeWildcard Include="@(ILRepackInclude -> WithMetadataValue('StartsWith', 'true'))"/> |
| 40 | + <ILRepackIncludeExact Include="@(ILRepackInclude)" Exclude="@(ILRepackIncludeWildcard)" /> |
| 41 | + <ILRepackExcludeWildcard Include="@(ILRepackExclude -> WithMetadataValue('StartsWith', 'true'))" /> |
| 42 | + <ILRepackExcludeExact Include="@(ILRepackExclude)" Exclude="@(ILRepackExcludeWildcard)" /> |
| 43 | + <ILRepackPreserveWildcard Include="@(ILRepackPreserve -> WithMetadataValue('StartsWith', 'true'))"/> |
| 44 | + <ILRepackPreserveExact Include="@(ILRepackPreserve)" Exclude="@(ILRepackPreserveWildcard)" /> |
| 45 | + </ItemGroup> |
| 46 | + </Target> |
| 47 | + |
| 48 | + <Target Name="ILRepackFilterIncludeStartsWith" DependsOnTargets="ILRepackPrepare;CoreCompile" |
| 49 | + Inputs="@(ILRepackIncludeWildcard)" Outputs="|%(ILRepackIncludeWildcard.Identity)|"> |
| 50 | + <PropertyGroup> |
| 51 | + <LocalFilter>%(ILRepackIncludeWildcard.Identity)</LocalFilter> |
| 52 | + </PropertyGroup> |
| 53 | + <ItemGroup> |
| 54 | + <ILRepackCandidate Include="@(ReferenceCopyLocalPaths)" Condition=" |
| 55 | + '%(Extension)' == '.dll' And |
| 56 | + !$([MSBuild]::ValueOrDefault('%(FileName)', '').EndsWith('.resources', StringComparison.OrdinalIgnoreCase)) And |
| 57 | + $([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('$(LocalFilter)'))" /> |
| 58 | + </ItemGroup> |
| 59 | + </Target> |
| 60 | + |
| 61 | + <Target Name="ILRepackFilterExcludeStartsWith" DependsOnTargets="ILRepackFilterIncludeStartsWith" |
| 62 | + Inputs="@(ILRepackExcludeWildcard)" Outputs="|%(ILRepackExcludeWildcard.Identity)|"> |
| 63 | + <PropertyGroup> |
| 64 | + <LocalFilter>%(ILRepackExcludeWildcard.Identity)</LocalFilter> |
| 65 | + </PropertyGroup> |
| 66 | + <ItemGroup> |
| 67 | + <ILRepackCandidate Remove="@(ILRepackCandidate)" Condition=" |
| 68 | + $([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('$(LocalFilter)'))" /> |
| 69 | + </ItemGroup> |
| 70 | + </Target> |
| 71 | + |
| 72 | + <Target Name="ILRepackFilterInclude" BeforeTargets="ILRepack" DependsOnTargets="ILRepackFilterExcludeStartsWith"> |
| 73 | + <PropertyGroup> |
| 74 | + <ILRepackInclude>;@(ILRepackIncludeExact, ';');</ILRepackInclude> |
| 75 | + </PropertyGroup> |
| 76 | + <ItemGroup> |
| 77 | + <ILRepackCandidate Include="@(ReferenceCopyLocalPaths)" Condition=" |
| 78 | + '%(Extension)' == '.dll' And |
| 79 | + !$([MSBuild]::ValueOrDefault('%(FileName)', '').EndsWith('.resources', StringComparison.OrdinalIgnoreCase)) And |
| 80 | + $(ILRepackInclude.Contains(';%(FileName);'))" /> |
| 81 | + </ItemGroup> |
| 82 | + </Target> |
| 83 | + |
| 84 | + <Target Name="ILRepackFilterExclude" BeforeTargets="ILRepack" DependsOnTargets="CoreCompile"> |
| 85 | + <PropertyGroup> |
| 86 | + <ILRepackExclude>;@(ILRepackExcludeExact, ';');</ILRepackExclude> |
| 87 | + </PropertyGroup> |
| 88 | + <ItemGroup> |
| 89 | + <ILRepackCandidate Remove="@(ILRepackCandidate)" Condition="$(ILRepackExclude.Contains(';%(FileName);'))" /> |
| 90 | + </ItemGroup> |
| 91 | + </Target> |
| 92 | + |
| 93 | + <Target Name="ILRepack" AfterTargets="CoreCompile" BeforeTargets="CopyFilesToOutputDirectory" |
| 94 | + Inputs="@(IntermediateAssembly -> '%(FullPath)')" |
| 95 | + Outputs="$(IntermediateOutputPath)ilrepack.txt" |
| 96 | + Returns="@(MergedAssemblies)" |
| 97 | + Condition="Exists(@(IntermediateAssembly -> '%(FullPath)')) And '$(ILRepack)' == 'true'"> |
| 98 | + |
| 99 | + <ItemGroup> |
| 100 | + <MergedAssemblies Include="@(ILRepackCandidate -> Distinct())" /> |
| 101 | + </ItemGroup> |
| 102 | + <ItemGroup> |
| 103 | + <ReferenceCopyLocalDirs Include="@(ReferenceCopyLocalPaths -> '%(RootDir)%(Directory)')" /> |
| 104 | + <ReferenceCopyLocalPaths Remove="@(MergedAssemblies)" /> |
| 105 | + <LibDir Include="@(ReferenceCopyLocalDirs -> Distinct())" /> |
| 106 | + </ItemGroup> |
| 107 | + <PropertyGroup> |
| 108 | + <AbsoluteAssemblyOriginatorKeyFile Condition="'$(SignAssembly)' == 'true' and '$(AssemblyOriginatorKeyFile)' != ''">$([System.IO.Path]::GetFullPath($([System.IO.Path]::Combine('$(MSBuildProjectDirectory)','$(AssemblyOriginatorKeyFile)'))))</AbsoluteAssemblyOriginatorKeyFile> |
| 109 | + <ILRepackArgs Condition="'$(AbsoluteAssemblyOriginatorKeyFile)' != ''">/keyfile:"$(AbsoluteAssemblyOriginatorKeyFile)" /delaysign</ILRepackArgs> |
| 110 | + <ILRepackArgs>$(ILRepackArgs) /internalize</ILRepackArgs> |
| 111 | + <ILRepackArgs>$(ILRepackArgs) /union</ILRepackArgs> |
| 112 | + <!-- This is needed to merge types with identical names into one, wich happens with IFluentInterface in Merq and Merq.Core (Xamarin.Messaging dependencies) --> |
| 113 | + <ILRepackArgs>$(ILRepackArgs) @(LibDir -> '/lib:"%(Identity)."', ' ')</ILRepackArgs> |
| 114 | + <ILRepackArgs>$(ILRepackArgs) /out:"@(IntermediateAssembly -> '%(FullPath)')"</ILRepackArgs> |
| 115 | + <ILRepackArgs>$(ILRepackArgs) "@(IntermediateAssembly -> '%(FullPath)')"</ILRepackArgs> |
| 116 | + <ILRepackArgs>$(ILRepackArgs) @(MergedAssemblies -> '"%(FullPath)"', ' ')</ILRepackArgs> |
| 117 | + <!--<ILRepackArgs>$(ILRepackArgs) "/lib:$(NetstandardDirectory)"</ILRepackArgs> --> |
| 118 | + <!-- This is needed for ilrepack to find netstandard.dll, which is referenced by the System.Text.Json assembly --> |
| 119 | + </PropertyGroup> |
| 120 | + <Exec Command='ilrepack $(ILRepackArgs)' WorkingDirectory="$(MSBuildProjectDirectory)\$(OutputPath)" StandardErrorImportance="high" IgnoreStandardErrorWarningFormat="true" StandardOutputImportance="low" ConsoleToMSBuild="true" ContinueOnError="true"> |
| 121 | + <Output TaskParameter="ConsoleOutput" PropertyName="ILRepackOutput" /> |
| 122 | + <Output TaskParameter="ExitCode" PropertyName="ExitCode" /> |
| 123 | + </Exec> |
| 124 | + <Message Importance="high" Text="$(ILRepackOutput)" Condition="'$(ExitCode)' != '0'" /> |
| 125 | + <Delete Files="$(IntermediateOutputPath)ilrepack.txt" Condition="'$(ExitCode)' != '0'" /> |
| 126 | + <Touch AlwaysCreate="true" Files="$(IntermediateOutputPath)ilrepack.txt" Condition="'$(ExitCode)' == '0'" /> |
| 127 | + <Error Text="$(ILRepackOutput)" Condition="'$(ExitCode)' != '0' And '$(ContinueOnError)' != 'true'" /> |
| 128 | + <ItemGroup> |
| 129 | + <ILRepackCleanup Include="@(MergedAssemblies)" /> |
| 130 | + </ItemGroup> |
| 131 | + </Target> |
| 132 | + |
| 133 | + <Target Name="ILRepackPreserveStartsWith" DependsOnTargets="ILRepack" |
| 134 | + Inputs="@(ILRepackPreserveWildcard)" Outputs="|%(ILRepackPreserveWildcard.Identity)|"> |
| 135 | + <PropertyGroup> |
| 136 | + <LocalFilter>%(ILRepackPreserveWildcard.Identity)</LocalFilter> |
| 137 | + </PropertyGroup> |
| 138 | + <ItemGroup> |
| 139 | + <ILRepackCleanup Remove="@(ILRepackCleanup)" Condition="$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('$(LocalFilter)'))" /> |
| 140 | + </ItemGroup> |
| 141 | + </Target> |
| 142 | + |
| 143 | + <Target Name="ILRepackPreserve" DependsOnTargets="ILRepack"> |
| 144 | + <PropertyGroup> |
| 145 | + <ILRepackPreserve>;@(ILRepackPreserveExact, ';');</ILRepackPreserve> |
| 146 | + </PropertyGroup> |
| 147 | + <ItemGroup> |
| 148 | + <ILRepackCleanup Remove="@(ILRepackCleanup)" Condition="$(ILRepackPreserve.Contains(';%(FileName);'))" /> |
| 149 | + </ItemGroup> |
| 150 | + </Target> |
| 151 | + |
| 152 | + <Target Name="ILRepackCleanup" AfterTargets="ILRepack" DependsOnTargets="ILRepackPreserveStartsWith;ILRepackPreserve" Returns="@(ILRepackCleanup)"> |
| 153 | + <Delete Files="@(ILRepackCleanup -> '$(MSBuildProjectDirectory)\$(OutputPath)%(Filename)%(Extension)')" |
| 154 | + Condition="Exists('$(MSBuildProjectDirectory)\$(OutputPath)%(Filename)%(Extension)')" /> |
| 155 | + </Target> |
| 156 | + |
| 157 | +</Project> |
0 commit comments