Skip to content

Commit

Permalink
Strip AOT shared libraries when linking
Browse files Browse the repository at this point in the history
Fixes: dotnet#6840
Context: dotnet#6683

Pass the `-s` flag to the native linker in order to produce shared
AOT libraries without debug symbols.  This is controlled by a new
msbuild property `$(AndroidStripAotLibraries)`, which defaults to
`true`
  • Loading branch information
grendello committed Mar 17, 2022
1 parent 7d9845e commit 3fc5780
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 24 deletions.
5 changes: 5 additions & 0 deletions Documentation/guides/building-apps/build-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ Added in Xamarin.Android 10.1.

The port that `aprofutil` should connect to when obtaining profiling data.

## AndroidAotStripLibraries

If set to `True` (the default), strip shared libraries produced by the
AOT compiler. This can reduce the library size for up to 30%.

## AndroidApkDigestAlgorithm

A string value which specifies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ They run in a context of an inner build with a single $(RuntimeIdentifier).
AotOutputDirectory="$(_AndroidAotBinDirectory)"
RuntimeIdentifier="$(RuntimeIdentifier)"
EnableLLVM="$(EnableLLVM)"
Profiles="@(AndroidAotProfile)">
Profiles="@(AndroidAotProfile)"
StripLibraries="$(AndroidAotStripLibraries)">
<Output PropertyName="_Triple" TaskParameter="Triple" />
<Output PropertyName="_ToolPrefix" TaskParameter="ToolPrefix" />
<Output PropertyName="_MsymPath" TaskParameter="MsymPath" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,26 +204,6 @@ _ResolveAssemblies MSBuild target.
IncludeDebugSymbols="$(AndroidIncludeDebugSymbols)">
<Output TaskParameter="OutputLibraries" ItemName="FrameworkNativeLibrary" />
</ProcessNativeLibraries>
<ItemGroup>
<_StrippedFrameworkNativeLibrary Include="@(FrameworkNativeLibrary->'$(IntermediateOutputPath)native\%(RuntimeIdentifier)\%(Filename)%(Extension)')" />
</ItemGroup>
</Target>

<Target Name="_StripFrameworkNativeLibraries"
Condition=" '$(AndroidIncludeDebugSymbols)' != 'true' "
DependsOnTargets="_IncludeNativeSystemLibraries"
Inputs="@(FrameworkNativeLibrary)"
Outputs="@(_StrippedFrameworkNativeLibrary)">
<StripNativeLibraries
SourceFiles="@(FrameworkNativeLibrary)"
DestinationFiles="@(_StrippedFrameworkNativeLibrary)"
ToolPath="$(AndroidBinUtilsDirectory)"
/>
<ItemGroup Condition=" '$(AndroidIncludeDebugSymbols)' != 'true' ">
<FrameworkNativeLibrary Remove="@(FrameworkNativeLibrary)"/>
<FrameworkNativeLibrary Include="@(_StrippedFrameworkNativeLibrary)"/>
<FileWrites Include="@(_StrippedFrameworkNativeLibrary)" />
</ItemGroup>
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
_CheckApkPerAbiFlag;
_LintChecks;
_IncludeNativeSystemLibraries;
_StripFrameworkNativeLibraries;
_CheckGoogleSdkRequirements;
</_PrepareBuildApkDependsOnTargets>
</PropertyGroup>
Expand Down
14 changes: 13 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/GetAotArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public abstract class GetAotArguments : AndroidAsyncTask

public bool EnableLLVM { get; set; }

public bool StripLibraries { get; set; }

public string AndroidSequencePointsMode { get; set; } = "";

public ITaskItem [] Profiles { get; set; } = Array.Empty<ITaskItem> ();
Expand Down Expand Up @@ -291,7 +293,17 @@ string GetLdFlags(NdkTools ndk, AndroidTargetArch arch, int level, string toolPr

ldFlags = $"\\\"{string.Join ("\\\";\\\"", libs)}\\\"";
}
return ldFlags;

if (!StripLibraries) {
return ldFlags;
}

const string StripFlag = "-s";
if (ldFlags.Length == 0) {
return StripFlag;
}

return $"{ldFlags} {StripFlag}";
}

static string GetNdkToolchainLibraryDir (NdkTools ndk, string binDir, string archDir = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<AndroidAotMode Condition=" '$(AndroidAotMode)' == '' ">None</AndroidAotMode>
<AotAssemblies Condition=" '$(AndroidAotMode)' != '' And '$(AndroidAotMode)' != 'None' And '$(AndroidAotMode)' != 'Interpreter' ">True</AotAssemblies>
<AotAssemblies Condition=" '$(AotAssemblies)' == '' ">False</AotAssemblies>
<AndroidAotStripLibraries Condition=" '$(AndroidAotStripLibraries)' == '' ">True</AndroidAotStripLibraries>

<AndroidUseDebugRuntime
Condition="'$(AndroidUseDebugRuntime)' == '' And '$(EmbedAssembliesIntoApk)' == 'True' And '$(Optimize)' == 'True' "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,8 @@ projects. .NET 5 projects will not import this file.
AdditionalNativeLibraryReferences="@(_AdditionalNativeLibraryReferences)"
YieldDuringToolExecution="$(YieldDuringToolExecution)"
EnableLLVM="$(EnableLLVM)"
Profiles="@(_AotProfiles)">
Profiles="@(_AotProfiles)"
StripLibraries="$(AndroidAotStripLibraries)">
<Output TaskParameter="NativeLibrariesReferences" ItemName="_AdditionalNativeLibraryReferences" />
</Aot>

Expand Down

0 comments on commit 3fc5780

Please sign in to comment.