Skip to content

Commit 3fc5780

Browse files
committed
Strip AOT shared libraries when linking
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`
1 parent 7d9845e commit 3fc5780

File tree

7 files changed

+23
-24
lines changed

7 files changed

+23
-24
lines changed

Documentation/guides/building-apps/build-properties.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ Added in Xamarin.Android 10.1.
7575

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

78+
## AndroidAotStripLibraries
79+
80+
If set to `True` (the default), strip shared libraries produced by the
81+
AOT compiler. This can reduce the library size for up to 30%.
82+
7883
## AndroidApkDigestAlgorithm
7984

8085
A string value which specifies

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Aot.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ They run in a context of an inner build with a single $(RuntimeIdentifier).
5858
AotOutputDirectory="$(_AndroidAotBinDirectory)"
5959
RuntimeIdentifier="$(RuntimeIdentifier)"
6060
EnableLLVM="$(EnableLLVM)"
61-
Profiles="@(AndroidAotProfile)">
61+
Profiles="@(AndroidAotProfile)"
62+
StripLibraries="$(AndroidAotStripLibraries)">
6263
<Output PropertyName="_Triple" TaskParameter="Triple" />
6364
<Output PropertyName="_ToolPrefix" TaskParameter="ToolPrefix" />
6465
<Output PropertyName="_MsymPath" TaskParameter="MsymPath" />

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -204,26 +204,6 @@ _ResolveAssemblies MSBuild target.
204204
IncludeDebugSymbols="$(AndroidIncludeDebugSymbols)">
205205
<Output TaskParameter="OutputLibraries" ItemName="FrameworkNativeLibrary" />
206206
</ProcessNativeLibraries>
207-
<ItemGroup>
208-
<_StrippedFrameworkNativeLibrary Include="@(FrameworkNativeLibrary->'$(IntermediateOutputPath)native\%(RuntimeIdentifier)\%(Filename)%(Extension)')" />
209-
</ItemGroup>
210-
</Target>
211-
212-
<Target Name="_StripFrameworkNativeLibraries"
213-
Condition=" '$(AndroidIncludeDebugSymbols)' != 'true' "
214-
DependsOnTargets="_IncludeNativeSystemLibraries"
215-
Inputs="@(FrameworkNativeLibrary)"
216-
Outputs="@(_StrippedFrameworkNativeLibrary)">
217-
<StripNativeLibraries
218-
SourceFiles="@(FrameworkNativeLibrary)"
219-
DestinationFiles="@(_StrippedFrameworkNativeLibrary)"
220-
ToolPath="$(AndroidBinUtilsDirectory)"
221-
/>
222-
<ItemGroup Condition=" '$(AndroidIncludeDebugSymbols)' != 'true' ">
223-
<FrameworkNativeLibrary Remove="@(FrameworkNativeLibrary)"/>
224-
<FrameworkNativeLibrary Include="@(_StrippedFrameworkNativeLibrary)"/>
225-
<FileWrites Include="@(_StrippedFrameworkNativeLibrary)" />
226-
</ItemGroup>
227207
</Target>
228208

229209
</Project>

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.BuildOrder.targets

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ projects, these properties are set in Xamarin.Android.Legacy.targets.
7474
_CheckApkPerAbiFlag;
7575
_LintChecks;
7676
_IncludeNativeSystemLibraries;
77-
_StripFrameworkNativeLibraries;
7877
_CheckGoogleSdkRequirements;
7978
</_PrepareBuildApkDependsOnTargets>
8079
</PropertyGroup>

src/Xamarin.Android.Build.Tasks/Tasks/GetAotArguments.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public abstract class GetAotArguments : AndroidAsyncTask
4747

4848
public bool EnableLLVM { get; set; }
4949

50+
public bool StripLibraries { get; set; }
51+
5052
public string AndroidSequencePointsMode { get; set; } = "";
5153

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

292294
ldFlags = $"\\\"{string.Join ("\\\";\\\"", libs)}\\\"";
293295
}
294-
return ldFlags;
296+
297+
if (!StripLibraries) {
298+
return ldFlags;
299+
}
300+
301+
const string StripFlag = "-s";
302+
if (ldFlags.Length == 0) {
303+
return StripFlag;
304+
}
305+
306+
return $"{ldFlags} {StripFlag}";
295307
}
296308

297309
static string GetNdkToolchainLibraryDir (NdkTools ndk, string binDir, string archDir = null)

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
193193
<AndroidAotMode Condition=" '$(AndroidAotMode)' == '' ">None</AndroidAotMode>
194194
<AotAssemblies Condition=" '$(AndroidAotMode)' != '' And '$(AndroidAotMode)' != 'None' And '$(AndroidAotMode)' != 'Interpreter' ">True</AotAssemblies>
195195
<AotAssemblies Condition=" '$(AotAssemblies)' == '' ">False</AotAssemblies>
196+
<AndroidAotStripLibraries Condition=" '$(AndroidAotStripLibraries)' == '' ">True</AndroidAotStripLibraries>
196197

197198
<AndroidUseDebugRuntime
198199
Condition="'$(AndroidUseDebugRuntime)' == '' And '$(EmbedAssembliesIntoApk)' == 'True' And '$(Optimize)' == 'True' "

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Legacy.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,8 @@ projects. .NET 5 projects will not import this file.
708708
AdditionalNativeLibraryReferences="@(_AdditionalNativeLibraryReferences)"
709709
YieldDuringToolExecution="$(YieldDuringToolExecution)"
710710
EnableLLVM="$(EnableLLVM)"
711-
Profiles="@(_AotProfiles)">
711+
Profiles="@(_AotProfiles)"
712+
StripLibraries="$(AndroidAotStripLibraries)">
712713
<Output TaskParameter="NativeLibrariesReferences" ItemName="_AdditionalNativeLibraryReferences" />
713714
</Aot>
714715

0 commit comments

Comments
 (0)