Skip to content

Commit

Permalink
[dotnet] Implement support for our different link modes. (xamarin#9460)
Browse files Browse the repository at this point in the history
* [mmp] Rename LinkMode.All to LinkMode.Full.

So that we can continue to use Enum.Parse<LinkMode> to parse 'Full' as the link mode.

* [dotnet] Implement support for our different link modes.

Tell the managed linker what to do with each input assembly depending the selected
link mode (link all, link sdk, don't link).
  • Loading branch information
rolfbjarne authored Aug 24, 2020
1 parent ccdd577 commit d7ab847
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
22 changes: 21 additions & 1 deletion dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@
<_AdditionalTaskAssemblyDirectory>$(_XamarinSdkRootDirectory)tools/dotnet-linker/</_AdditionalTaskAssemblyDirectory>
<_AdditionalTaskAssembly>$(_AdditionalTaskAssemblyDirectory)dotnet-linker.dll</_AdditionalTaskAssembly>
</PropertyGroup>
<Target Name="_ComputeLinkerArguments" DependsOnTargets="_ComputeLinkMode">
<Target Name="_ComputeLinkerArguments" DependsOnTargets="_ComputeLinkMode;ComputeResolvedFilesToPublishList">
<!-- Validate the linker mode -->
<Error Text="Invalid link mode: '$(_LinkMode)'. Valid link modes are: 'None', 'SdkOnly' and 'Full'" Condition="'$(_LinkMode)' != 'None' And '$(_LinkMode)' != 'SdkOnly' And '$(_LinkMode)' != 'Full'" />

<PropertyGroup>
<!-- Pass the custom options to our custom steps -->
<_CustomLinkerOptionsFile>$([System.IO.Path]::GetFullPath('$(IntermediateOutputPath)custom-linker-options.txt'))</_CustomLinkerOptionsFile>
Expand Down Expand Up @@ -146,6 +149,11 @@
<!-- System.Runtime.dll isn't always copied to the .app -->
<_ExtraTrimmerArgs>$(_ExtraTrimmerArgs) -p copy "System.Runtime"</_ExtraTrimmerArgs>

<!-- TrimMode specifies what the linker will do with framework assemblies -->
<TrimMode Condition="'$(_LinkMode)' == 'None'">copy</TrimMode> <!-- Don't use 'copyused', because that might still end up saving some assemblies, and if that's the platform assembly, it may break the partial static registrar -->
<TrimMode Condition="'$(_LinkMode)' == 'SdkOnly' Or '$(_LinkMode)' == 'Full'">link</TrimMode>
<!-- For Full link mode we also need to set TrimMode for all non-framework assemblies. This is done further below -->

<!-- Verbose output, so that we get something to stdout when something goes wrong -->
<_ExtraTrimmerArgs>$(_ExtraTrimmerArgs) --verbose</_ExtraTrimmerArgs>

Expand All @@ -161,7 +169,19 @@
-->
<_ExtraTrimmerArgs>$(_ExtraTrimmerArgs) -b</_ExtraTrimmerArgs>
</PropertyGroup>

<ItemGroup>
<!-- Mark all assemblies to be linked if we're linking all assemblies -->
<ResolvedFileToPublish
Update="@(ResolvedFileToPublish)"
Condition="'$(_LinkMode)' == 'Full' And '%(ResolvedFileToPublish.Extension)' == '.dll' And '%(ResolvedFileToPublish.AssetType)' != 'native'"
>
<TrimMode>link</TrimMode>
</ResolvedFileToPublish>

<!-- Mark our entry assembly as a root assembly. -->
<TrimmerRootAssembly Include="$(AssemblyName)" />

<!-- add a custom step which inserts any other steps we need -->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)">
<BeforeStep>LoadReferencesStep</BeforeStep>
Expand Down
2 changes: 1 addition & 1 deletion tools/common/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public partial class Application
public ApplePlatform Platform { get { return Driver.TargetFramework.Platform; } }

// Linker config
public LinkMode LinkMode = LinkMode.All;
public LinkMode LinkMode = LinkMode.Full;
public List<string> LinkSkipped = new List<string> ();
public List<string> Definitions = new List<string> ();
public I18nAssemblies I18n;
Expand Down
2 changes: 1 addition & 1 deletion tools/common/LinkMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Xamarin.Bundler {
public enum LinkMode {
None,
SDKOnly,
All,
Full,
Platform,
}
}
2 changes: 1 addition & 1 deletion tools/mmp/driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ static void Pack (IList<string> unprocessed)
if (generate_plist)
GeneratePList ();

if (App.LinkMode != LinkMode.All && App.RuntimeOptions != null)
if (App.LinkMode != LinkMode.Full && App.RuntimeOptions != null)
App.RuntimeOptions.Write (resources_dir);

if (App.AOTOptions != null && App.AOTOptions.IsAOT) {
Expand Down

0 comments on commit d7ab847

Please sign in to comment.