Skip to content

Commit 3687479

Browse files
committed
Fixes after 'main' branch changes
1 parent 3b2b675 commit 3687479

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class GenerateMainAndroidManifest : AndroidTask
2929
public bool Debug { get; set; }
3030
public bool EmbedAssemblies { get; set; }
3131
public bool EnableMarshalMethods { get; set; }
32+
public bool EnableNativeRuntimeLinking { get; set; }
3233
[Required]
3334
public string IntermediateOutputDirectory { get; set; } = "";
3435
public string []? ManifestPlaceholders { get; set; }
@@ -77,7 +78,7 @@ public override bool RunTask ()
7778

7879
// If we still need the NativeCodeGenState in the <GenerateNativeMarshalMethodSources> task because we're using marshal methods,
7980
// we're going to transfer it to a new object that doesn't require holding open Cecil AssemblyDefinitions.
80-
if (UseMarshalMethods) {
81+
if (UseMarshalMethods || EnableNativeRuntimeLinking) {
8182
var nativeCodeGenStateObject = MarshalMethodCecilAdapter.GetNativeCodeGenStateCollection (Log, nativeCodeGenStates);
8283

8384
Log.LogDebugMessage ($"Saving {nameof (NativeCodeGenStateObject)} to {nameof (GenerateJavaStubs.NativeCodeGenStateObjectRegisterTaskKey)}");

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public class GenerateNativeMarshalMethodSources : AndroidTask
2121

2222
public bool EnableMarshalMethods { get; set; }
2323

24+
public bool EnableNativeRuntimeLinking { get; set; }
25+
26+
public ITaskItem[] MonoComponents { get; set; } = [];
27+
2428
[Required]
2529
public string EnvironmentOutputDirectory { get; set; } = "";
2630

@@ -39,7 +43,7 @@ public override bool RunTask ()
3943
{
4044
NativeCodeGenStateCollection? nativeCodeGenStates = null;
4145

42-
if (EnableMarshalMethods) {
46+
if (EnableMarshalMethods || EnableNativeRuntimeLinking) {
4347
// Retrieve the stored NativeCodeGenStateCollection (and remove it from the cache)
4448
nativeCodeGenStates = BuildEngine4.UnregisterTaskObjectAssemblyLocal<NativeCodeGenStateCollection> (
4549
MonoAndroidHelper.GetProjectBuildSpecificTaskObjectKey (GenerateJavaStubs.NativeCodeGenStateObjectRegisterTaskKey, WorkingDirectory, IntermediateOutputDirectory),
@@ -58,7 +62,9 @@ void Generate (NativeCodeGenStateCollection? nativeCodeGenStates, string abi)
5862
var targetAbi = abi.ToLowerInvariant ();
5963
var targetArch = MonoAndroidHelper.AbiToTargetArch (abi);
6064
var marshalMethodsBaseAsmFilePath = Path.Combine (EnvironmentOutputDirectory, $"marshal_methods.{targetAbi}");
65+
var pinvokePreserveBaseAsmFilePath = EnableNativeRuntimeLinking ? Path.Combine (EnvironmentOutputDirectory, $"pinvoke_preserve.{targetAbi}") : null;
6166
var marshalMethodsLlFilePath = $"{marshalMethodsBaseAsmFilePath}.ll";
67+
var pinvokePreserveLlFilePath = pinvokePreserveBaseAsmFilePath != null ? $"{pinvokePreserveBaseAsmFilePath}.ll" : null;
6268
var (assemblyCount, uniqueAssemblyNames) = GetAssemblyCountAndUniqueNames ();
6369

6470
MarshalMethodsNativeAssemblyGenerator marshalMethodsAsmGen;
@@ -80,6 +86,20 @@ void Generate (NativeCodeGenStateCollection? nativeCodeGenStates, string abi)
8086
);
8187
}
8288

89+
if (EnableNativeRuntimeLinking) {
90+
var pinvokePreserveGen = new PreservePinvokesNativeAssemblyGenerator (Log, EnsureCodeGenState (nativeCodeGenStates, targetArch), MonoComponents);
91+
LLVMIR.LlvmIrModule pinvokePreserveModule = pinvokePreserveGen.Construct ();
92+
using var pinvokePreserveWriter = MemoryStreamPool.Shared.CreateStreamWriter ();
93+
try {
94+
pinvokePreserveGen.Generate (pinvokePreserveModule, targetArch, pinvokePreserveWriter, pinvokePreserveLlFilePath!);
95+
} catch {
96+
throw;
97+
} finally {
98+
pinvokePreserveWriter.Flush ();
99+
Files.CopyIfStreamChanged (pinvokePreserveWriter.BaseStream, pinvokePreserveLlFilePath!);
100+
}
101+
}
102+
83103
var marshalMethodsModule = marshalMethodsAsmGen.Construct ();
84104
using var marshalMethodsWriter = MemoryStreamPool.Shared.CreateStreamWriter ();
85105

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public override bool RunTask ()
3030
// Write all the user assemblies
3131
pkgmgr.WriteLine ("public class MonoPackageManager_Resources {");
3232
pkgmgr.WriteLine ("\tpublic static String[] Assemblies = new String[]{");
33+
3334
pkgmgr.WriteLine ("\t\t/* We need to ensure that \"{0}\" comes first in this list. */", mainFileName);
3435
pkgmgr.WriteLine ("\t\t\"" + mainFileName + "\",");
3536
foreach (var assembly in ResolvedUserAssemblies) {
@@ -54,6 +55,7 @@ public override bool RunTask ()
5455

5556
// Only copy to the real location if the contents actually changed
5657
var dest = Path.GetFullPath (Path.Combine (OutputDirectory, "MonoPackageManager_Resources.java"));
58+
5759
Files.CopyIfStreamChanged (pkgmgr.BaseStream, dest);
5860
}
5961

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,7 @@ because xbuild doesn't support framework reference assemblies.
16491649
Debug="$(AndroidIncludeDebugSymbols)"
16501650
EmbedAssemblies="$(EmbedAssembliesIntoApk)"
16511651
EnableMarshalMethods="$(_AndroidUseMarshalMethods)"
1652+
EnableNativeRuntimeLinking="$(_AndroidEnableNativeRuntimeLinking)"
16521653
IntermediateOutputDirectory="$(IntermediateOutputPath)"
16531654
ManifestPlaceholders="$(AndroidManifestPlaceholders)"
16541655
ManifestTemplate="$(_AndroidManifestAbs)"
@@ -1906,12 +1907,14 @@ because xbuild doesn't support framework reference assemblies.
19061907
CustomBundleConfigFile="$(AndroidBundleConfigurationFile)"
19071908
TargetsCLR="$(_AndroidUseCLR)"
19081909
ProjectRuntimeConfigFilePath="$(ProjectRuntimeConfigFilePath)"
1909-
EnableNativeRuntimeLinking="$(_AndroidEnableNativeRuntimeLinking)">
1910+
>
19101911
</GenerateNativeApplicationConfigSources>
19111912

19121913
<GenerateNativeMarshalMethodSources
19131914
EnableMarshalMethods="$(_AndroidUseMarshalMethods)"
19141915
EnableManagedMarshalMethodsLookup="$(_AndroidUseManagedMarshalMethodsLookup)"
1916+
EnableNativeRuntimeLinking="$(_AndroidEnableNativeRuntimeLinking)"
1917+
MonoComponents="@(_MonoComponent)"
19151918
EnvironmentOutputDirectory="$(IntermediateOutputPath)android"
19161919
IntermediateOutputDirectory="$(IntermediateOutputPath)"
19171920
ResolvedAssemblies="@(_ResolvedAssemblies)"
@@ -2037,7 +2040,7 @@ because xbuild doesn't support framework reference assemblies.
20372040
<_CompileToDalvikDependsOnTargets>
20382041
_CompileJava;
20392042
_CreateApplicationSharedLibraries;
2040-
_LinkNativeRuntime;
2043+
_LinkNativeRuntime;
20412044
_GetMonoPlatformJarPath;
20422045
_GetLibraryImports;
20432046
_SetProguardMappingFileProperty;

0 commit comments

Comments
 (0)