-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathMicrosoft.Android.Sdk.NativeAOT.targets
More file actions
460 lines (402 loc) · 23.5 KB
/
Microsoft.Android.Sdk.NativeAOT.targets
File metadata and controls
460 lines (402 loc) · 23.5 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<!--
***********************************************************************************************
Microsoft.Android.Sdk.NativeAOT.targets
This file contains the NativeAOT-specific MSBuild logic for .NET for Android.
We follow an approach similar to xamarin-macios:
1. Set NativeCompilationDuringPublish=false to skip ILC SDK's native linking
2. Set NativeLib=Static so ILC produces only object files
3. Use our own LinkNativeAotLibrary task with android-native-tools (ld.lld)
This eliminates the NDK dependency for NativeAOT builds.
***********************************************************************************************
-->
<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)" />
<UsingTask TaskName="Xamarin.Android.Tasks.LinkNativeAotLibrary" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.ProcessRuntimePackLibraryDirectories" 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>
<!--
Disable ILC SDK's native compilation pipeline - we use our own linker (android-native-tools)
This prevents Microsoft.NETCore.Native.Publish.targets from being imported
-->
<NativeCompilationDuringPublish>false</NativeCompilationDuringPublish>
<!-- We must find the BCL libraries using the runtime pack instead of using the built-in NativeAOT BCL -->
<PublishAotUsingRuntimePack>true</PublishAotUsingRuntimePack>
<!--
Set to 'true' to fall back to NDK-based linking (legacy behavior).
When false (default), we use android-native-tools which ships with the Android workload.
-->
<AndroidNativeAotUseNdk Condition=" '$(AndroidNativeAotUseNdk)' == '' ">false</AndroidNativeAotUseNdk>
<!--
Hook into the _ComputeFilesToPublishForRuntimeIdentifiers dependency chain.
This ensures our NativeAOT compilation runs in the inner build, similar to how
MonoVM AOT uses _AndroidAot.
-->
<_RunAotMaybe Condition=" '$(_AndroidUseMarshalMethods)' != 'True' ">_AndroidNativeAotCompileAndLink</_RunAotMaybe>
</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>
<!--
Define our own IlcCompileDependsOn chain.
Since NativeCompilationDuringPublish=false, ILC SDK won't set this up automatically.
-->
<PropertyGroup>
<IlcCompileDependsOn>
_AndroidBeforeIlcCompile;
Compile;
SetupOSSpecificProps;
_AndroidAfterSetupOSSpecificProps;
PrepareForILLink;
ILLink;
ComputeIlcCompileInputs;
_AndroidComputeIlcCompileInputs;
</IlcCompileDependsOn>
</PropertyGroup>
<Target Name="_AndroidBeforeIlcCompile"
DependsOnTargets="_PrepareLinking"
BeforeTargets="SetupProperties">
<PropertyGroup>
<!--
Tell ILC to produce only object files, not link them.
We'll do the linking ourselves with our custom linker.
-->
<NativeLib>Static</NativeLib>
<!-- We must ensure this is `false`, as it would interfere with statically linking libc++ -->
<LinkStandardCPlusPlusLibrary>false</LinkStandardCPlusPlusLibrary>
<!--
Temporarily disable ILC's symbol stripping probe in SetupOSSpecificProps.
The ILC SDK's ObjCopyName probe uses "where /Q" on Windows, which fails on absolute
paths (it interprets C:\ as a path:pattern separator). Since we handle our own native
linking/stripping in LinkNativeAotLibrary (which uses ld.lld -s), we don't need ILC's
ObjCopyName probe at all. Save the effective StripSymbols value, suppress the probe,
and restore it in _AndroidAfterSetupOSSpecificProps.
-->
<_AndroidNativeAotStripSymbols Condition=" '$(_AndroidNativeAotStripSymbols)' == '' ">$(StripSymbols)</_AndroidNativeAotStripSymbols>
<StripSymbols>false</StripSymbols>
<!-- We want to run ILLink ourselves before ILC -->
<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>
</Target>
<!--
Restore StripSymbols after SetupOSSpecificProps has finished probing.
See _AndroidBeforeIlcCompile for why this is necessary.
-->
<Target Name="_AndroidAfterSetupOSSpecificProps">
<PropertyGroup>
<StripSymbols>$(_AndroidNativeAotStripSymbols)</StripSymbols>
</PropertyGroup>
</Target>
<Target Name="_AndroidComputeIlcCompileInputs">
<PropertyGroup>
<!-- Turn trimmer warnings back to original value -->
<SuppressTrimAnalysisWarnings>$(_OriginalSuppressTrimAnalysisWarnings)</SuppressTrimAnalysisWarnings>
</PropertyGroup>
<ItemGroup>
<!-- 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)')" />
<!-- Remove the main assembly from _AndroidILLinkAssemblies since it's already in IlcCompileInput -->
<_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.
Remove any existing items first to avoid duplicates - ComputeFilesToPublish may have already added them.
-->
<ResolvedFileToPublish Remove="@(IlcCompileInput)" />
<ResolvedFileToPublish Remove="@(_AndroidILLinkAssemblies)" />
<ResolvedFileToPublish Include="@(IlcCompileInput);@(_AndroidILLinkAssemblies)" RuntimeIdentifier="$(RuntimeIdentifier)" />
<!--
Remove NativeAOT runtime pack shared libraries (.so) from the publish output.
These PAL libraries (System.Native, System.IO.Compression.Native, etc.) are already
statically linked into the application binary via their .a counterparts in @(NativeLibrary).
Without this removal, both the static and shared versions end up in the APK.
-->
<_NativeAotSharedLibsToRemove Include="@(ResolvedFileToPublish)"
Condition=" '%(ResolvedFileToPublish.Extension)' == '.so' and $([System.String]::new('%(ResolvedFileToPublish.NuGetPackageId)').StartsWith('Microsoft.NETCore.App.Runtime.NativeAOT.')) " />
<ResolvedFileToPublish Remove="@(_NativeAotSharedLibsToRemove)" />
<!-- Satellite assemblies -->
<IlcSatelliteAssembly Include="$(_OuterIntermediateSatelliteAssembliesWithTargetPath)" />
<!-- This library conflicts with static libc++ -->
<NativeLibrary 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)" />
</Target>
<!--
Main entry point for NativeAOT compilation and linking.
This target is called via $(_RunAotMaybe) from _ComputeFilesToPublishForRuntimeIdentifiers.
It explicitly calls IlcCompile (which ILC SDK defines) and then performs custom linking.
-->
<PropertyGroup>
<_AndroidNativeAotLinkTarget Condition=" '$(AndroidNativeAotUseNdk)' != 'true' ">_LinkNativeAotLibrary</_AndroidNativeAotLinkTarget>
<_AndroidNativeAotLinkTarget Condition=" '$(AndroidNativeAotUseNdk)' == 'true' ">_GenerateNativeAotAndroidAppLibrary</_AndroidNativeAotLinkTarget>
</PropertyGroup>
<Target Name="_AndroidNativeAotCompileAndLink"
Condition=" '$(PublishAot)' == 'true' and '$(RuntimeIdentifier)' != '' "
DependsOnTargets="IlcCompile;$(_AndroidNativeAotLinkTarget)">
<!-- IlcCompile produces $(NativeObject), then linking target links it into a .so -->
</Target>
<!--
Collect all native libraries that ILC SDK would have linked.
We need these for our custom linker.
-->
<Target Name="_CollectNativeAotLinkInputs"
DependsOnTargets="_GenerateNativeAotAndroidAppAssemblerSources">
<ItemGroup>
<!-- Our generated assembler object files (NativeObject is passed separately to LinkNativeAotLibrary) -->
<_NativeAotObjectFiles Include="@(_PrivateJniInitFuncsNativeObjectFile)" />
<_NativeAotObjectFiles Include="@(_PrivateEnvironmentNativeObjectFile)" />
<!--
Collect static archives from ILC SDK.
These are populated by SetupOSSpecificProps in Microsoft.NETCore.Native.Unix.targets
-->
<_NativeAotArchives Include="@(NativeLibrary)" />
<!-- Add our Android-specific runtime library -->
<_NativeAotArchives Include="@(RuntimePackAsset->WithMetadataValue('Filename', 'libnaot-android.$(Configuration.ToLower())-static-$(Configuration.ToLower())'))" />
</ItemGroup>
</Target>
<!--
Custom native linking using android-native-tools (ld.lld).
This runs after IlcCompile (via _AndroidNativeAotCompileAndLink) and replaces the ILC SDK's LinkNative target.
-->
<Target
Name="_LinkNativeAotLibrary"
DependsOnTargets="_CollectNativeAotLinkInputs"
Condition=" '$(AndroidNativeAotUseNdk)' != 'true' ">
<PropertyGroup>
<_NativeAotOutputLibrary>$(NativeOutputPath)lib$(TargetName).so</_NativeAotOutputLibrary>
</PropertyGroup>
<!--
We need to run ProcessRuntimePackLibraryDirectories here because this code runs in an inner build
and `_RuntimePackLibraryDirectory` items aren't carried over from the outer build.
This discovers sysroot library directories (containing libc.so, liblog.so, etc.) from the runtime pack.
Note: Removal of sysroot libraries from ResolvedFileToPublish happens in the outer build's
_ResolveAssemblies target, not here.
-->
<ProcessRuntimePackLibraryDirectories
ResolvedFilesToPublish="@(ResolvedFileToPublish)">
<Output TaskParameter="RuntimePackLibraryDirectories" ItemName="_RuntimePackLibraryDirectory" />
</ProcessRuntimePackLibraryDirectories>
<MakeDir Directories="$(NativeOutputPath)" />
<LinkNativeAotLibrary
AndroidBinUtilsDirectory="$(AndroidBinUtilsDirectory)"
IntermediateOutputPath="$(NativeIntermediateOutputPath)"
NativeObject="$(NativeObject)"
NativeObjectFiles="@(_NativeAotObjectFiles)"
NativeArchives="@(_NativeAotArchives)"
OutputLibrary="$(_NativeAotOutputLibrary)"
RuntimeIdentifier="$(RuntimeIdentifier)"
RuntimePackLibraryDirectories="@(_RuntimePackLibraryDirectory)"
StripDebugSymbols="$(StripSymbols)"
SaveDebugSymbols="$(NativeDebugSymbols)"
/>
<ItemGroup>
<!-- Register the output library for publishing -->
<ResolvedFileToPublish Include="$(_NativeAotOutputLibrary)">
<RelativePath>lib$(TargetName).so</RelativePath>
<ArchiveFileName>lib$(TargetName).so</ArchiveFileName>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
<!--
Legacy NDK-based linking (for compatibility).
Only used when AndroidNativeAotUseNdk=true.
Unlike the default path (which uses android-native-tools ld.lld), this invokes
the NDK's clang++ compiler driver to link the NativeAOT output into a shared library.
Since NativeCompilationDuringPublish=false and NativeLib=Static are set unconditionally,
ILC always produces only .o files. This target collects those .o files and all static
archives, then performs the linking via clang++ directly.
-->
<Target
Name="_GenerateNativeAotAndroidAppLibrary"
DependsOnTargets="_CollectNativeAotLinkInputs"
Condition=" '$(AndroidNativeAotUseNdk)' == 'true' ">
<PropertyGroup>
<_NdkAbi Condition=" '$(RuntimeIdentifier)' == 'android-arm64' ">aarch64</_NdkAbi>
<_NdkAbi Condition=" '$(RuntimeIdentifier)' == 'android-x64' ">x86_64</_NdkAbi>
<_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>
<_NdkApiLevel Condition=" '$(RuntimeIdentifier)' == 'android-arm64' ">$(AndroidNdkApiLevel_Arm64)</_NdkApiLevel>
<_NdkApiLevel Condition=" '$(RuntimeIdentifier)' == 'android-x64' ">$(AndroidNdkApiLevel_X64)</_NdkApiLevel>
<_NdkSysrootDir>$(_AndroidNdkDirectory)toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/sysroot/usr/lib/$(_NdkSysrootAbi)/</_NdkSysrootDir>
<_NdkBinDir>$(_AndroidNdkDirectory)toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/bin/</_NdkBinDir>
<!-- On Windows, NDK wrapper scripts require the .cmd extension -->
<_NdkWrapperScriptExt Condition=" $([MSBuild]::IsOSPlatform('windows')) ">.cmd</_NdkWrapperScriptExt>
<!-- Use ABI-prefixed clang++ wrapper which sets target triple, sysroot, and API level automatically -->
<_NdkClang>$(_NdkBinDir)$(_NdkSysrootAbi)$(_NdkApiLevel)-clang++$(_NdkWrapperScriptExt)</_NdkClang>
<_NativeAotOutputLibrary>$(NativeOutputPath)lib$(TargetName).so</_NativeAotOutputLibrary>
<_NdkLinkRspFile>$(NativeIntermediateOutputPath)ndk-link.rsp</_NdkLinkRspFile>
</PropertyGroup>
<ItemGroup>
<!-- NDK-specific static C++ libraries -->
<_NdkLibs Include="$(_NdkSysrootDir)libc++_static.a" />
<_NdkLibs Include="$(_NdkSysrootDir)libc++abi.a" />
</ItemGroup>
<ItemGroup>
<!--
Build the linker argument list for clang++.
The ABI-prefixed NDK wrapper handles target triple, sysroot, CRT files
(crtbegin_so.o/crtend_so.o), libunwind, and compiler-rt builtins automatically.
-->
<_NdkLinkerArgs Include="-shared" />
<!-- Prevent clang++ from auto-linking dynamic libc++; we link static libc++ explicitly -->
<_NdkLinkerArgs Include="-nostdlib++" />
<_NdkLinkerArgs Include="-o "$(_NativeAotOutputLibrary)"" />
<_NdkLinkerArgs Include="-Wl,-soname,$([System.IO.Path]::GetFileName('$(_NativeAotOutputLibrary)'))" />
<_NdkLinkerArgs Include="-Wl,--build-id=sha1" />
<_NdkLinkerArgs Include="-Wl,--eh-frame-hdr" />
<_NdkLinkerArgs Include="-Wl,-z,relro" />
<_NdkLinkerArgs Include="-Wl,-z,now" />
<_NdkLinkerArgs Include="-Wl,-z,max-page-size=16384" />
<_NdkLinkerArgs Include="-Wl,-z,nostart-stop-gc" />
<_NdkLinkerArgs Include="-Wl,--gc-sections" />
<_NdkLinkerArgs Include="-Wl,--as-needed" />
<_NdkLinkerArgs Include="-Wl,--no-undefined" />
<_NdkLinkerArgs Include="-Wl,-Bsymbolic" />
<_NdkLinkerArgs Condition=" '$(StripSymbols)' == 'true' " Include="-Wl,-s" />
<_NdkLinkerArgs Condition=" '$(NativeDebugSymbols)' == 'true' and '$(StripSymbols)' != 'true' " Include="-g" />
<!-- ILC-compiled native object -->
<_NdkLinkerArgs Include=""$(NativeObject)"" />
<!-- Generated assembler objects (JNI init, environment) -->
<_NdkLinkerArgs Include="@(_NativeAotObjectFiles->'"%(Identity)"')" />
<!-- ILC SDK static archives (PAL libraries, runtime, etc.) -->
<_NdkLinkerArgs Include="@(_NativeAotArchives->'"%(Identity)"')" />
<!-- NDK C++ libraries -->
<_NdkLinkerArgs Include="@(_NdkLibs->'"%(Identity)"')" />
<!-- System libraries required by PAL libraries on Android -->
<_NdkLinkerArgs Include="-llog" />
<_NdkLinkerArgs Include="-lz" />
<_NdkLinkerArgs Include="-lm" />
<_NdkLinkerArgs Include="-ldl" />
</ItemGroup>
<MakeDir Directories="$(NativeOutputPath)" />
<WriteLinesToFile File="$(_NdkLinkRspFile)" Lines="@(_NdkLinkerArgs)" Overwrite="true" Encoding="utf-8" />
<Message Text="Linking NativeAOT library with NDK: $(_NdkClang)" Importance="high" />
<Exec Command=""$(_NdkClang)" @"$(_NdkLinkRspFile)"" />
<ItemGroup>
<!-- Register the output library for publishing -->
<ResolvedFileToPublish Include="$(_NativeAotOutputLibrary)">
<RelativePath>lib$(TargetName).so</RelativePath>
<ArchiveFileName>lib$(TargetName).so</ArchiveFileName>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</ResolvedFileToPublish>
</ItemGroup>
</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>