-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathMicrosoft.Android.Sdk.NativeAOT.targets
More file actions
287 lines (248 loc) · 16 KB
/
Microsoft.Android.Sdk.NativeAOT.targets
File metadata and controls
287 lines (248 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<!--
***********************************************************************************************
Microsoft.Android.Sdk.NativeAOT.targets
This file contains the NativeAOT-specific MSBuild logic for .NET for Android.
***********************************************************************************************
-->
<Project>
<UsingTask TaskName="Xamarin.Android.Tasks.SetNdkPathForIlc" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.GenerateNativeAotLibraryLoadAssemblerSources" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.GenerateNativeAotEnvironmentAssemblerSources" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<!-- Default property values for NativeAOT -->
<PropertyGroup>
<_AndroidRuntimePackRuntime>NativeAOT</_AndroidRuntimePackRuntime>
<_AndroidJcwCodegenTarget Condition=" '$(_AndroidJcwCodegenTarget)' == '' ">JavaInterop1</_AndroidJcwCodegenTarget>
<_AndroidTypeMapImplementation Condition=" '$(_AndroidTypeMapImplementation)' == '' ">managed</_AndroidTypeMapImplementation>
<!-- .NET SDK gives: error NETSDK1191: A runtime identifier for the property 'PublishAot' couldn't be inferred. Specify a rid explicitly. -->
<AllowPublishAotWithoutRuntimeIdentifier Condition=" '$(AllowPublishAotWithoutRuntimeIdentifier)' == '' ">true</AllowPublishAotWithoutRuntimeIdentifier>
<!-- NativeAOT's targets currently gives an error about cross-compilation -->
<DisableUnsupportedError Condition=" $([MSBuild]::IsOSPlatform('windows')) and '$(DisableUnsupportedError)' == '' ">true</DisableUnsupportedError>
<!-- HACK: make dotnet restore include Microsoft.NETCore.App.Runtime.NativeAOT.linux-bionic-arm64 -->
<_IsPublishing Condition=" '$(_IsPublishing)' == '' ">true</_IsPublishing>
<!-- ResolvedFileToPublish is not populated until _AndroidComputeIlcCompileInputs, so
proguard generation must run after that target instead of after ILLink. -->
<_GenerateProguardAfterTargets>_AndroidComputeIlcCompileInputs</_GenerateProguardAfterTargets>
</PropertyGroup>
<!-- Default property values for NativeAOT Debug configuration -->
<PropertyGroup Condition="'$(DebugSymbols)' == 'true'">
<NativeDebugSymbols>true</NativeDebugSymbols>
<StripSymbols>false</StripSymbols>
<CompressSymbols>false</CompressSymbols>
</PropertyGroup>
<!-- Default feature switches -->
<ItemGroup>
<RuntimeHostConfigurationOption Include="Microsoft.Android.Runtime.RuntimeFeature.IsMonoRuntime"
Value="false"
Trim="true"
/>
<RuntimeHostConfigurationOption Include="Microsoft.Android.Runtime.RuntimeFeature.IsCoreClrRuntime"
Value="false"
Trim="true"
/>
</ItemGroup>
<!-- Make IlcCompile depend on the trimmer -->
<PropertyGroup>
<IlcCompileDependsOn>
_AndroidBeforeIlcCompile;
SetupOSSpecificProps;
PrepareForILLink;
ILLink;
ComputeIlcCompileInputs;
_AndroidComputeIlcCompileInputs;
$(IlcCompileDependsOn)
</IlcCompileDependsOn>
</PropertyGroup>
<Target Name="_AndroidBeforeIlcCompile"
DependsOnTargets="_PrepareLinking"
BeforeTargets="SetupProperties">
<!-- Example settings from: https://github.com/jonathanpeppers/Android-NativeAOT/blob/ea69d122cdc7de67aa6a5db14b7e560763c63cdd/DotNet/libdotnet.targets -->
<PropertyGroup>
<_NdkAbi Condition=" '$(RuntimeIdentifier)' == 'android-arm64' ">aarch64</_NdkAbi>
<_NdkAbi Condition=" '$(RuntimeIdentifier)' == 'android-x64' ">x86_64</_NdkAbi>
<_NDKApiLevel Condition=" '$(RuntimeIdentifier)' == 'android-arm64' ">$(AndroidNdkApiLevel_Arm64)</_NDKApiLevel>
<_NDKApiLevel Condition=" '$(RuntimeIdentifier)' == 'android-x64' ">$(AndroidNdkAPiLevel_X64)</_NDKApiLevel>
<_NdkSysrootAbi>$(_NdkAbi)-linux-android</_NdkSysrootAbi>
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('windows')) ">windows-x86_64</_NdkPrebuiltAbi>
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('osx')) ">darwin-x86_64</_NdkPrebuiltAbi>
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('linux')) ">linux-x86_64</_NdkPrebuiltAbi>
<!-- We need to explicitly specify the extension on Windows, since some Android NDK versions
for Windows contain bash shell scripts without the extension, in addition to the
Windows-specific .cmd ones. We must use the latter since end users might not have bash
installed on Windows -->
<_NdkWrapperScriptExt Condition=" $([MSBuild]::IsOSPlatform('windows')) ">.cmd</_NdkWrapperScriptExt>
<_NdkSysrootDir>$(_AndroidNdkDirectory)toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/sysroot/usr/lib/$(_NdkSysrootAbi)/</_NdkSysrootDir>
<_NdkBinDir>$(_AndroidNdkDirectory)toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/bin/</_NdkBinDir>
<!--
Don't call `clang` directly, use the wrapper scripts instead since they properly pass
arguments required to link the application using the right API level libraries.
Using the script also makes it certain that we're not invoking a random `clang` in
from the system but rather the NDK one.
Also, we cannot use $(_NdkBinDir) here because, on Windows, the `Exec` task will get
confused by the forward slash in the path (even though it's a valid path separator
character on Windows) and signal the following error:
SetupOSSpecificProps:
where /Q "C:\Android\android-sdk\ndk\29.0.14206865\toolchains/llvm/prebuilt/windows-x86_64/bin/clang++"
1>EXEC : error : Invalid pattern is specified in "path:pattern".
The command "where /Q "C:\Android\android-sdk\ndk\29.0.14206865\toolchains/llvm/prebuilt/windows-x86_64/bin/clang++"" exited with code 2.
This could be fixed by making sure $(_NdkBinDir) path is fixed-up to use the canonical
form of path separation for the host OS, but it's simpler to just use the NDK-specific
compiler/linker wrapper script name.
Also, do NOT use `clang++` here because it will link the dynamic libc++ library into the
application. If, for some reason, `clang++` has to be used, `<LinkerArgs>` need to be
added to pass `-nostdlib` to it.
-->
<CppCompilerAndLinker>$(_NdkAbi)-linux-android$(_NDKApiLevel)-clang$(_NdkWrapperScriptExt)</CppCompilerAndLinker>
<CppLinker>$(_NdkAbi)-linux-android$(_NDKApiLevel)-clang$(_NdkWrapperScriptExt)</CppLinker>
<ObjCopyName>llvm-objcopy</ObjCopyName>
<!-- We must ensure this is `false`, as it would interfere with statically linking libc++ -->
<LinkStandardCPlusPlusLibrary>false</LinkStandardCPlusPlusLibrary>
<!-- Example settings from: https://github.com/xamarin/xamarin-macios/blob/c43d4ea40bc777969e3b158cf46446df292d8449/dotnet/targets/Xamarin.Shared.Sdk.targets#L541-L550 -->
<RunILLink>true</RunILLink>
<!--
We want to suppress warnings from trimmer and only show warnings from ILC.
Otherwise, you would get 2x for every warning.
-->
<_OriginalSuppressTrimAnalysisWarnings>$(SuppressTrimAnalysisWarnings)</_OriginalSuppressTrimAnalysisWarnings>
<SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
<!-- Ensure ILLink respects the value of SuppressTrimAnalysisWarnings -->
<_ExtraTrimmerArgs>$(_ExtraTrimmerArgs) --notrimwarn</_ExtraTrimmerArgs>
</PropertyGroup>
<SetNdkPathForIlc NdkBinDirectory="$(_NdkBinDir)" />
</Target>
<!--
The standard NativeAOT targets (_ComputeManagedAssemblyForILLink) replace @(ManagedAssemblyToLink)
with framework-only assemblies, removing the user assembly. This is fine in the standard NativeAOT
flow where RunILLink=false (ILC does its own trimming). But Android re-enables ILLink (RunILLink=true
above), so the user assembly must be in @(ManagedAssemblyToLink) for two reasons:
1. ILLink needs it in AssemblyPaths to process it (not just as a root name)
2. _RunILLink's Inputs include @(ManagedAssemblyToLink), so changes to the user assembly
correctly trigger ILLink to re-run on incremental builds
-->
<Target Name="_AndroidFixManagedAssemblyToLinkForILLink"
AfterTargets="_ComputeManagedAssemblyForILLink"
Condition="'$(NativeCompilationDuringPublish)' == 'true'">
<ItemGroup>
<ManagedAssemblyToLink Include="@(IntermediateAssembly)" />
</ItemGroup>
</Target>
<Target Name="_AndroidComputeIlcCompileInputs">
<PropertyGroup>
<!-- Turn trimmer warnings back to original value -->
<SuppressTrimAnalysisWarnings>$(_OriginalSuppressTrimAnalysisWarnings)</SuppressTrimAnalysisWarnings>
</PropertyGroup>
<ItemGroup>
<!-- Android needs a proper soname property or it will refuse to load the library -->
<LinkerArg Include=""-Wl,-soname,lib$(TargetName)$(NativeBinaryExt)"" />
<LinkerArg Include="-Wl,--error-unresolved-symbols" />
<LinkerArg Include="-Wl,--no-undefined" />
<!-- Prevent ComputeLinkedFilesToPublish from removing trimmed assemblies from
ResolvedFileToPublish. We run ILLink before ILC (to support custom trimmer
steps), so the assemblies are already trimmed and must remain in
ResolvedFileToPublish for the outer build's _ResolveAssemblies target.
Context: https://github.com/dotnet/runtime/pull/124801 -->
<_IlcManagedInputAssemblies Remove="@(_IlcManagedInputAssemblies)" />
<!-- Required for [UnmanagedCallersOnly] to work inside this assembly -->
<UnmanagedEntryPointsAssembly Include="Microsoft.Android.Runtime.NativeAOT" />
<!-- Give ILLink's output to ILC -->
<IlcCompileInput Remove="@(IlcCompileInput)" />
<IlcCompileInput Include="$(IntermediateLinkDir)$(TargetName)$(TargetExt)" />
<_AndroidILLinkAssemblies Include="@(ManagedAssemblyToLink->'$(IntermediateLinkDir)%(Filename)%(Extension)')" Condition="Exists('$(IntermediateLinkDir)%(Filename)%(Extension)')" />
<!-- Exclude the user assembly: it's already in IlcCompileInput above and must not also appear in IlcReference -->
<_AndroidILLinkAssemblies Remove="$(IntermediateLinkDir)$(TargetName)$(TargetExt)" />
<IlcReference Remove="@(IlcReference)" />
<IlcReference Include="@(PrivateSdkAssemblies)" />
<IlcReference Include="@(_AndroidILLinkAssemblies)" />
<!-- Root *everything* for ILC, as it's already trimmed -->
<TrimmerRootAssembly Include="@(_AndroidILLinkAssemblies->'%(Filename)')" Exclude="System.Private.CoreLib" TrimMode="All" />
<!-- Passes linked assemblies to outer MSBuild tasks/targets -->
<ResolvedFileToPublish Include="@(IlcCompileInput);@(_AndroidILLinkAssemblies)" RuntimeIdentifier="$(RuntimeIdentifier)" />
<!-- Satellite assemblies -->
<IlcSatelliteAssembly Include="$(_OuterIntermediateSatelliteAssembliesWithTargetPath)" />
<_NdkLibs Include="@(RuntimePackAsset->WithMetadataValue('Filename', 'libnaot-android.$(Configuration.ToLower())-static-$(Configuration.ToLower())'))" />
<!-- Include libc++ -->
<_NdkLibs Include="$(_NdkSysrootDir)libc++_static.a" />
<_NdkLibs Include="$(_NdkSysrootDir)libc++abi.a" />
<LinkerArg Include=""%(_NdkLibs.Identity)"" />
<!-- This library conflicts with static libc++ -->
<NativeLibrary Remove="$(IlcSdkPath)libstdc++compat.a" />
<LinkerArg Remove="$(IlcSdkPath)libstdc++compat.a" />
<!-- Every p/invoke using the `xa-internal-api` "library" will be called directly -->
<DirectPInvoke Include="xa-internal-api" />
</ItemGroup>
</Target>
<Target Name="_PrepareNativeAotAndroidAppInputs">
<ItemGroup>
<_PrivateBuildTargetAbi Condition=" '$(RuntimeIdentifier)' == 'android-arm64' " Include="arm64-v8a" />
<_PrivateBuildTargetAbi Condition=" '$(RuntimeIdentifier)' == 'android-x64' " Include="x86_64" />
</ItemGroup>
<PrepareAbiItems
BuildTargetAbis="@(_PrivateBuildTargetAbi)"
NativeSourcesDir="$(_NativeAssemblySourceDir)"
Debug="$(AndroidIncludeDebugSymbols)"
Mode="jni_init">
<Output TaskParameter="AssemblySources" ItemName="_PrivateJniInitFuncsAssemblySource" />
</PrepareAbiItems>
<PrepareAbiItems
BuildTargetAbis="@(_PrivateBuildTargetAbi)"
NativeSourcesDir="$(_NativeAssemblySourceDir)"
Debug="$(AndroidIncludeDebugSymbols)"
Mode="environment">
<Output TaskParameter="AssemblySources" ItemName="_PrivateEnvironmentAssemblySource" />
</PrepareAbiItems>
<ItemGroup>
<_PrivateJniInitFuncsNativeObjectFile Include="@(_PrivateJniInitFuncsAssemblySource->'$([System.IO.Path]::ChangeExtension('%(Identity)', '.o'))')">
<abi>%(_PrivateJniInitFuncsAssemblySource.abi)</abi>
</_PrivateJniInitFuncsNativeObjectFile>
<_PrivateEnvironmentNativeObjectFile Include="@(_PrivateEnvironmentAssemblySource->'$([System.IO.Path]::ChangeExtension('%(Identity)', '.o'))')">
<abi>%(_PrivateEnvironmentAssemblySource.abi)</abi>
</_PrivateEnvironmentNativeObjectFile>
<_PrivateAndroidNaotResolvedAssemblyFiles Include="@(ResolvedFileToPublish->Distinct())" Condition=" '%(ResolvedFileToPublish.Extension)' == '.dll' " />
<!-- In .NET 11+, the SDK's ILLink target populates ResolvedFileToPublish with linked assemblies
(via _LinkedResolvedFileToPublish) WITHOUT RuntimeIdentifier metadata. _AndroidComputeIlcCompileInputs
adds the same items WITH RuntimeIdentifier, but ->Distinct() keeps the first occurrence (without metadata).
Stamp RuntimeIdentifier on any items that lack it, since we're in the inner build with a single RID. -->
<_PrivateAndroidNaotResolvedAssemblyFiles
Update="@(_PrivateAndroidNaotResolvedAssemblyFiles)"
Condition=" '%(_PrivateAndroidNaotResolvedAssemblyFiles.RuntimeIdentifier)' == '' "
RuntimeIdentifier="$(RuntimeIdentifier)"
/>
</ItemGroup>
</Target>
<Target
Name="_GenerateNativeAotAndroidAppAssemblerSources"
DependsOnTargets="_GenerateEnvironmentFiles;_PrepareNativeAotAndroidAppInputs"
Inputs="@(_PrivateAndroidNaotResolvedAssemblyFiles);@(AndroidEnvironment);@(LibraryEnvironments)"
Outputs="@(_PrivateJniInitFuncsAssemblySource);@(_PrivateJniInitFuncsNativeObjectFile);@(_PrivateEnvironmentAssemblySource);@(_PrivateEnvironmentNativeObjectFile)">
<GenerateNativeAotLibraryLoadAssemblerSources
ResolvedAssemblies="@(_PrivateAndroidNaotResolvedAssemblyFiles)"
CustomJniInitFunctions="@(AndroidStaticJniInitFunction)"
OutputSources="@(_PrivateJniInitFuncsAssemblySource)" />
<GenerateNativeAotEnvironmentAssemblerSources
Environments="@(AndroidEnvironment);@(LibraryEnvironments)"
HttpClientHandlerType="$(AndroidHttpClientHandlerType)"
OutputSources="@(_PrivateEnvironmentAssemblySource)"
RID="$(RuntimeIdentifier)" />
<CompileNativeAssembly
Sources="@(_PrivateJniInitFuncsAssemblySource);@(_PrivateEnvironmentAssemblySource)"
DebugBuild="$(AndroidIncludeDebugSymbols)"
WorkingDirectory="$(_NativeAssemblySourceDir)"
AndroidBinUtilsDirectory="$(AndroidBinUtilsDirectory)" />
<ItemGroup>
<LinkerArg Include="@(_PrivateJniInitFuncsNativeObjectFile)" />
<LinkerArg Include="@(_PrivateEnvironmentNativeObjectFile)" />
</ItemGroup>
</Target>
<!-- Make sure we have a chance to generate our application-specific static library before linking
takes place. We must run after `IlcCompile`, which is a dependency of `LinkNative` -->
<Target
Name="_GenerateNativeAotAndroidAppLibrary"
AfterTargets="IlcCompile"
DependsOnTargets="_GenerateNativeAotAndroidAppAssemblerSources">
</Target>
<Target Name="_AndroidFixNativeLibraryFileName" AfterTargets="ComputeFilesToPublish">
<ItemGroup>
<!-- Fix paths to contain lib-prefix -->
<ResolvedFileToPublish Update="@(ResolvedFileToPublish)" ArchiveFileName="lib%(FileName)%(Extension)" Condition=" '%(Filename)%(Extension)' == '$(TargetName)$(NativeBinaryExt)' " />
</ItemGroup>
</Target>
</Project>