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+ - Item 'ILRepackInclude': list of assembly filenames to explicitly include in merging.
8+ - Item 'ILRepackExclude': list of assembly filenames to exclude from merging.
9+ - Item 'ILRepackPreserve': Assemblies that should not be deleted after merging, even if merged.
10+ -->
11+
12+ <PropertyGroup >
13+ <ILRepack Condition =" '$(ILRepack)' == '' and '$(Configuration)' == 'Release'" >true</ILRepack >
14+ <ILRepack Condition =" '$(ILRepack)' == ''" >false</ILRepack >
15+ <!-- We need to turn on copy-local for ILRepack to find deps -->
16+ <CopyLocalLockFileAssemblies Condition =" '$(CopyLocalLockFileAssemblies)' == '' and '$(ILRepack)' == 'true'" >true</CopyLocalLockFileAssemblies >
17+ </PropertyGroup >
18+
19+ <Target Name =" EnsureILRepack" BeforeTargets =" ILRepack" Condition =" '$(ILRepack)' == 'true'" >
20+ <Exec Command =" ilrepack --version" StandardErrorImportance =" high" StandardOutputImportance =" low" ConsoleToMSBuild =" true" IgnoreExitCode =" true" ContinueOnError =" true" >
21+ <Output TaskParameter =" ConsoleOutput" PropertyName =" ILRepackOutput" />
22+ <Output TaskParameter =" ExitCode" PropertyName =" ExitCode" />
23+ </Exec >
24+ <Message Importance =" high" Text =" Using installed dotnet-ilrepack v$(ILRepackOutput)" Condition =" $(ExitCode) == '0'" />
25+ <Exec Command =" dotnet tool install -g dotnet-ilrepack"
26+ Condition =" $(ExitCode) != '0'" />
27+ <Exec Command =" ilrepack --version" Condition =" $(ExitCode) != '0'" ConsoleToMSBuild =" true" >
28+ <Output TaskParameter =" ConsoleOutput" PropertyName =" ILRepackInstalledOutput" />
29+ </Exec >
30+ <Message Importance =" high" Text =" Installed dotnet-ilrepack v$(ILRepackInstalledOutput)" Condition =" $(ExitCode) != '0'" />
31+ </Target >
32+
33+ <Target Name =" ILRepack" AfterTargets =" CoreCompile" BeforeTargets =" CopyFilesToOutputDirectory"
34+ Inputs =" @(IntermediateAssembly -> '%(FullPath)')"
35+ Outputs =" $(IntermediateOutputPath)ilrepack.txt"
36+ Returns =" @(MergedAssemblies)"
37+ Condition =" Exists(@(IntermediateAssembly -> '%(FullPath)')) And '$(ILRepack)' == 'true'" >
38+ <PropertyGroup >
39+ <ILRepackInclude >;@(ILRepackInclude, ';');</ILRepackInclude >
40+ <ILRepackExclude >;@(ILRepackExclude, ';');</ILRepackExclude >
41+ </PropertyGroup >
42+ <ItemGroup >
43+ <ReferenceCopyLocalAssemblies Include =" @(ReferenceCopyLocalPaths)" Condition =" '%(Extension)' == '.dll'
44+ And !$([MSBuild]::ValueOrDefault('%(FileName)', '').EndsWith('.resources', StringComparison.OrdinalIgnoreCase))" />
45+
46+ <!-- Default to never mergingmerging analyzer, C# or system assemblies -->
47+ <MergedAssemblies Include =" @(ReferenceCopyLocalAssemblies)" Condition ="
48+ !$(ILRepackExclude.Contains(';%(FileName);')) And
49+ !$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('Microsoft.CodeAnalysis', StringComparison.OrdinalIgnoreCase)) And
50+ !$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('Microsoft.CSharp', StringComparison.OrdinalIgnoreCase)) And
51+ !$([MSBuild]::ValueOrDefault('%(FileName)', '').StartsWith('System.', StringComparison.OrdinalIgnoreCase))"
52+ />
53+ <!-- Brings in explicitly opted-in assemblies -->
54+ <MergedAssemblies Include =" @(ReferenceCopyLocalAssemblies)" Condition =" $(ILRepackInclude.Contains(';%(FileName);'))" />
55+ </ItemGroup >
56+ <ItemGroup >
57+ <ReferenceCopyLocalDirs Include =" @(ReferenceCopyLocalPaths -> '%(RootDir)%(Directory)')" />
58+ <ReferenceCopyLocalPaths Remove =" @(MergedAssemblies)" />
59+ <LibDir Include =" @(ReferenceCopyLocalDirs -> Distinct())" />
60+ </ItemGroup >
61+ <PropertyGroup >
62+ <AbsoluteAssemblyOriginatorKeyFile Condition =" '$(SignAssembly)' == 'true' and '$(AssemblyOriginatorKeyFile)' != ''" >$([System.IO.Path]::GetFullPath($([System.IO.Path]::Combine('$(MSBuildProjectDirectory)','$(AssemblyOriginatorKeyFile)'))))</AbsoluteAssemblyOriginatorKeyFile >
63+ <ILRepackArgs Condition =" '$(AbsoluteAssemblyOriginatorKeyFile)' != ''" >/keyfile:"$(AbsoluteAssemblyOriginatorKeyFile)" /delaysign</ILRepackArgs >
64+ <ILRepackArgs >$(ILRepackArgs) /internalize</ILRepackArgs >
65+ <ILRepackArgs >$(ILRepackArgs) /union</ILRepackArgs >
66+ <!-- This is needed to merge types with identical names into one, wich happens with IFluentInterface in Merq and Merq.Core (Xamarin.Messaging dependencies) -->
67+ <ILRepackArgs >$(ILRepackArgs) @(LibDir -> '/lib:"%(Identity)."', ' ')</ILRepackArgs >
68+ <ILRepackArgs >$(ILRepackArgs) /out:"@(IntermediateAssembly -> '%(FullPath)')"</ILRepackArgs >
69+ <ILRepackArgs >$(ILRepackArgs) "@(IntermediateAssembly -> '%(FullPath)')"</ILRepackArgs >
70+ <ILRepackArgs >$(ILRepackArgs) @(MergedAssemblies -> '"%(FullPath)"', ' ')</ILRepackArgs >
71+ <!-- <ILRepackArgs>$(ILRepackArgs) "/lib:$(NetstandardDirectory)"</ILRepackArgs> -->
72+ <!-- This is needed for ilrepack to find netstandard.dll, which is referenced by the System.Text.Json assembly -->
73+ </PropertyGroup >
74+ <Exec Command =' ilrepack $(ILRepackArgs)' WorkingDirectory =" $(MSBuildProjectDirectory)\$(OutputPath)" StandardErrorImportance =" high" IgnoreStandardErrorWarningFormat =" true" StandardOutputImportance =" low" ConsoleToMSBuild =" true" ContinueOnError =" true" >
75+ <Output TaskParameter =" ConsoleOutput" PropertyName =" ILRepackOutput" />
76+ <Output TaskParameter =" ExitCode" PropertyName =" ExitCode" />
77+ </Exec >
78+ <Message Importance =" high" Text =" $(ILRepackOutput)" Condition =" '$(ExitCode)' != '0'" />
79+ <Delete Files =" $(IntermediateOutputPath)ilrepack.txt" Condition =" '$(ExitCode)' != '0'" />
80+ <Touch AlwaysCreate =" true" Files =" $(IntermediateOutputPath)ilrepack.txt" Condition =" '$(ExitCode)' == '0'" />
81+ <Error Text =" $(ILRepackOutput)" Condition =" '$(ExitCode)' != '0' And '$(ContinueOnError)' != 'true'" />
82+ <ItemGroup >
83+ <MergedAssembliesToRemove Include =" @(MergedAssemblies)" />
84+ <MergedAssembliesToRemove Remove =" @(ILRepackPreserve)" />
85+ </ItemGroup >
86+ <Delete Files =" @(MergedAssembliesToRemove -> '$(MSBuildProjectDirectory)\$(OutputPath)%(Filename)%(Extension)')" Condition =" Exists('$(MSBuildProjectDirectory)\$(OutputPath)%(Filename)%(Extension)')" />
87+ </Target >
88+
89+ </Project >
0 commit comments