dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.36k stars 967 forks source link

Run ILLinker on Winforms codebase and diff to identify deadcode #186

Open ericstj opened 5 years ago

Tanya-Solyanik commented 5 years ago

https://github.com/dotnet/core/blob/master/samples/linker-instructions.md

ericstj commented 5 years ago

More info is in this issue in CoreFx https://github.com/dotnet/corefx/issues/17905

zsd4yr commented 5 years ago

@hughbe this sounds sort of like the work you have been doing?

RussKie commented 4 years ago

I got a guidance from @eerhardt, putting it here for the future reference.

. . .

The way the linker is run in the dotnet/runtime repo is different than how an application developer runs the linker.

See how the linker is run to generate the report referred above to here: https://github.com/dotnet/runtime/blob/8a2820e35c5ad841d4c3aae3af8b1ace37d22660/eng/illink.targets#L292-L336

It can either be run manually on your assembly, or be automated with MSBuild, like done in dotnet/runtime.

To run it, get the linker from the following NuGet package: https://dev.azure.com/dnceng/public/_packaging?_a=package&feed=dotnet5&package=Microsoft.NET.ILLink.Tasks&protocolType=NuGet&version=5.0.0-preview.3.20412.1

And can run it like:

dotnet C:\Users\eerhardt\.nuget\packages\microsoft.net.illink.tasks\5.0.0-preview.3.20410.1\tools\netcoreapp3.0\illink.dll

passing in arguments like:

-reference "<full path to your assembly>"
-out "<full output path>"
-r <assembly name>
-c skip -u skip 
-p link <assembly name>
-t -b true --strip-substitutions false --strip-link-attributes false
--ignore-link-attributes true
--disable-opt unusedinterfaces 
--keep-dep-attributes true 
-d "<full path to a directory that contains references, such as System.Private.CoreLib>"

So a full example:

-reference "F:\git\runtime\artifacts\obj\Microsoft.Extensions.Caching.Abstractions\netstandard2.0-Debug\Microsoft.Extensions.Caching.Abstractions.dll"
-out "C:\temp\junk"
-r Microsoft.Extensions.Caching.Abstractions
-c skip -u skip 
-p link Microsoft.Extensions.Caching.Abstractions
-t -b true --strip-substitutions false --strip-link-attributes false
--ignore-link-attributes true
--disable-opt unusedinterfaces 
--keep-dep-attributes true 
-d "F:\git\runtime\artifacts\bin\microsoft.netcore.app.runtime.win-x64\Debug\runtimes\win-x64\lib\net5.0"

The above arguments can be put in a .rsp file, and then pass that file on the command line with an @ sign before the file name: dotnet <path-to-linker> @commandArgs.rsp.

After the assembly is linked, it can be diff'ed it using asmdiff.exe using the same args in that illink.targets file above.