Skip to content

Commit 32162d8

Browse files
committed
Fixes after 'main' branch changes
1 parent f94ed48 commit 32162d8

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

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

+2-1
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

+21-1
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

+2
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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,7 @@ because xbuild doesn't support framework reference assemblies.
16451645
Debug="$(AndroidIncludeDebugSymbols)"
16461646
EmbedAssemblies="$(EmbedAssembliesIntoApk)"
16471647
EnableMarshalMethods="$(_AndroidUseMarshalMethods)"
1648+
EnableNativeRuntimeLinking="$(_AndroidEnableNativeRuntimeLinking)"
16481649
IntermediateOutputDirectory="$(IntermediateOutputPath)"
16491650
ManifestPlaceholders="$(AndroidManifestPlaceholders)"
16501651
ManifestTemplate="$(_AndroidManifestAbs)"
@@ -1902,12 +1903,14 @@ because xbuild doesn't support framework reference assemblies.
19021903
CustomBundleConfigFile="$(AndroidBundleConfigurationFile)"
19031904
TargetsCLR="$(_AndroidUseCLR)"
19041905
ProjectRuntimeConfigFilePath="$(ProjectRuntimeConfigFilePath)"
1905-
EnableNativeRuntimeLinking="$(_AndroidEnableNativeRuntimeLinking)">
1906+
>
19061907
</GenerateNativeApplicationConfigSources>
19071908

19081909
<GenerateNativeMarshalMethodSources
19091910
EnableMarshalMethods="$(_AndroidUseMarshalMethods)"
19101911
EnableManagedMarshalMethodsLookup="$(_AndroidUseManagedMarshalMethodsLookup)"
1912+
EnableNativeRuntimeLinking="$(_AndroidEnableNativeRuntimeLinking)"
1913+
MonoComponents="@(_MonoComponent)"
19111914
EnvironmentOutputDirectory="$(IntermediateOutputPath)android"
19121915
IntermediateOutputDirectory="$(IntermediateOutputPath)"
19131916
ResolvedAssemblies="@(_ResolvedAssemblies)"
@@ -2033,7 +2036,7 @@ because xbuild doesn't support framework reference assemblies.
20332036
<_CompileToDalvikDependsOnTargets>
20342037
_CompileJava;
20352038
_CreateApplicationSharedLibraries;
2036-
_LinkNativeRuntime;
2039+
_LinkNativeRuntime;
20372040
_GetMonoPlatformJarPath;
20382041
_GetLibraryImports;
20392042
_SetProguardMappingFileProperty;

0 commit comments

Comments
 (0)