Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion external/Java.Interop
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ This file contains the NativeAOT-specific MSBuild logic for .NET for Android.
<!-- Include libc++ -->
<_NdkLibs Include="$(_NdkSysrootDir)libc++_static.a" />
<_NdkLibs Include="$(_NdkSysrootDir)libc++abi.a" />

<LinkerArg Include="&quot;%(_NdkLibs.Identity)&quot;" />

<!-- This library conflicts with static libc++ -->
Expand Down Expand Up @@ -181,7 +181,6 @@ This file contains the NativeAOT-specific MSBuild logic for .NET for Android.
OutputSources="@(_PrivateJniInitFuncsAssemblySource)" />

<GenerateNativeAotEnvironmentAssemblerSources
Debug="$(AndroidIncludeDebugSymbols)"
Environments="@(AndroidEnvironment);@(LibraryEnvironments)"
HttpClientHandlerType="$(AndroidHttpClientHandlerType)"
OutputSources="@(_PrivateEnvironmentAssemblySource)"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.dot.jni.nativeaot;

import android.system.ErrnoException;
import android.system.Os;
import android.util.Log;
import java.lang.String;

public class NativeAotEnvironmentVars
{
private static final String TAG = "NativeAotEnvironmentVars";

static String[] envNames = new String[] {
@ENVIRONMENT_VAR_NAMES@
};

static String[] envValues = new String[] {
@ENVIRONMENT_VAR_VALUES@
};

public static void Initialize ()
{
Log.d (TAG, "Initializing environment variables");

if (envNames.length != envValues.length) {
Log.w (TAG, "Unable to initialize environment variables, name and value arrays have different sizes");
return;
}

try {
for (int i = 0; i < envNames.length; i++) {
Log.d (TAG, "Setting env var: '" + envNames[i] + "'='" + envValues[i] + "'");
Os.setenv (envNames[i], envValues[i], true /* overwrite */);
}
} catch (ErrnoException e) {
Log.e (TAG, "Failed to set environment variables");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class NativeAotRuntimeProvider

public NativeAotRuntimeProvider() {
Log.d(TAG, "NativeAotRuntimeProvider()");
NativeAotEnvironmentVars.Initialize ();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Java.Interop.Tools.JavaCallableWrappers;
using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Framework;
Expand All @@ -28,6 +30,13 @@ public class GenerateAdditionalProviderSources : AndroidTask
[Required]
public string TargetName { get; set; } = "";

public ITaskItem[]? Environments { get; set; }

// We need to pass these two to the environment builder, otherwise not used
// by this task. See also GenerateNativeApplicationSources.cs
public string? HttpClientHandlerType { get; set; }
public bool EnableSGenConcurrent { get; set; }

AndroidRuntime androidRuntime;
JavaPeerStyle codeGenerationTarget;

Expand Down Expand Up @@ -71,14 +80,37 @@ void Generate (NativeCodeGenStateObject codeGenState)
Files.CopyIfStringChanged (contents, real_provider);
}

// For NativeAOT, generate JavaInteropRuntime.java
// For NativeAOT, generate JavaInteropRuntime.java and NativeAotEnvironmentVars.java
if (androidRuntime == Xamarin.Android.Tasks.AndroidRuntime.NativeAOT) {
const string fileName = "JavaInteropRuntime.java";
string template = GetResource (fileName);
var contents = template.Replace ("@MAIN_ASSEMBLY_NAME@", TargetName);
var path = Path.Combine (OutputDirectory, "src", "net", "dot", "jni", "nativeaot", fileName);
Log.LogDebugMessage ($"Writing: {path}");
Files.CopyIfStringChanged (contents, path);
GenerateJavaSource (
"JavaInteropRuntime.java",
new Dictionary<string, string> (StringComparer.Ordinal) {
{ "@MAIN_ASSEMBLY_NAME@", TargetName },
}
);

// We care only about environment variables here
var envBuilder = new EnvironmentBuilder (Log);
envBuilder.Read (Environments);
GenerateNativeApplicationConfigSources.AddDefaultEnvironmentVariables (envBuilder, HttpClientHandlerType, EnableSGenConcurrent);

var envVarNames = new StringBuilder ();
var envVarValues = new StringBuilder ();
foreach (var kvp in envBuilder.EnvironmentVariables) {
// All the strings already have double-quotes properly quoted, EnvironmentBuilder took care of that
AppendEnvVarEntry (envVarNames, kvp.Key);
AppendEnvVarEntry (envVarValues, kvp.Value);
}

var envVars = new Dictionary<string, string> (StringComparer.Ordinal) {
{ "@ENVIRONMENT_VAR_NAMES@", envVarNames.ToString () },
{ "@ENVIRONMENT_VAR_VALUES@", envVarValues.ToString () },
};

GenerateJavaSource (
"NativeAotEnvironmentVars.java",
envVars
);
}

// Create additional application java sources.
Expand All @@ -105,6 +137,26 @@ void Generate (NativeCodeGenStateObject codeGenState)
real_app_dir,
template => template.Replace ("// REGISTER_APPLICATION_AND_INSTRUMENTATION_CLASSES_HERE", regCallsWriter.ToString ())
);

void AppendEnvVarEntry (StringBuilder sb, string value)
{
sb.Append ("\t\t\"");
sb.Append (value);
sb.Append ("\",\n");
}

void GenerateJavaSource (string fileName, Dictionary<string, string> replacements)
{
var template = new StringBuilder (GetResource (fileName));

foreach (var kvp in replacements) {
template.Replace (kvp.Key, kvp.Value);
}

var path = Path.Combine (OutputDirectory, "src", "net", "dot", "jni", "nativeaot", fileName);
Log.LogDebugMessage ($"Writing: {path}");
Files.CopyIfStringChanged (template.ToString (), path);
}
}

string GetResource (string resource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public class GenerateNativeAotEnvironmentAssemblerSources : AndroidTask

[Required]
public string RID { get; set; } = "";

public bool Debug { get; set; }
public ITaskItem[]? Environments { get; set; }
public string? HttpClientHandlerType { get; set; }

Expand All @@ -23,10 +21,9 @@ public override bool RunTask ()
var envBuilder = new EnvironmentBuilder (Log);
envBuilder.Read (Environments);

if (Debug) {
envBuilder.AddDefaultDebugBuildLogLevel ();
}
envBuilder.AddHttpClientHandlerType (HttpClientHandlerType);
// Environment variables are set by Java (code generated in the GenerateAdditionalProviderSources task)
// We still want to set system properties, if any
envBuilder.EnvironmentVariables.Clear ();

string abi = MonoAndroidHelper.RidToAbi (RID);
AndroidTargetArch targetArch = MonoAndroidHelper.RidToArch (RID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class GenerateNativeApplicationConfigSources : AndroidTask
[Required]
public bool TargetsCLR { get; set; }

[Required]
public string AndroidRuntime { get; set; } = "";

public bool EnableMarshalMethods { get; set; }
public bool EnableManagedMarshalMethodsLookup { get; set; }
public string? RuntimeConfigBinFilePath { get; set; }
Expand All @@ -76,8 +79,19 @@ bool _Debug {

static internal AndroidTargetArch GetAndroidTargetArchForAbi (string abi) => MonoAndroidHelper.AbiToTargetArch (abi);

AndroidRuntime androidRuntime;

internal static void AddDefaultEnvironmentVariables (EnvironmentBuilder envBuilder, string? httpClientHandlerType, bool enableSGenConcurrent)
{
envBuilder.AddDefaultMonoDebug ();
envBuilder.AddHttpClientHandlerType (httpClientHandlerType);
envBuilder.AddMonoGcParams (enableSGenConcurrent);
}

public override bool RunTask ()
{
androidRuntime = MonoAndroidHelper.ParseAndroidRuntime (AndroidRuntime);

bool usesMonoAOT = false;

if (!Enum.TryParse (PackageNamingPolicy, out PackageNamingPolicy pnp)) {
Expand All @@ -101,9 +115,15 @@ public override bool RunTask ()
if (_Debug) {
envBuilder.AddDefaultDebugBuildLogLevel ();
}
envBuilder.AddDefaultMonoDebug ();
envBuilder.AddHttpClientHandlerType (HttpClientHandlerType);
envBuilder.AddMonoGcParams (EnableSGenConcurrent);

if (androidRuntime != Xamarin.Android.Tasks.AndroidRuntime.NativeAOT) {
AddDefaultEnvironmentVariables (envBuilder, HttpClientHandlerType, EnableSGenConcurrent);
} else {
// NativeAOT sets all the environment variables from Java, we don't want to repeat that
// process in the native code. This is just a precaution, because NativeAOT builds should
// not even use this task.
envBuilder.EnvironmentVariables.Clear ();
}

global::Android.Runtime.BoundExceptionType boundExceptionType;
if (String.IsNullOrEmpty (BoundExceptionType) || MonoAndroidHelper.StringEquals (BoundExceptionType, "System", StringComparison.OrdinalIgnoreCase)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,30 @@ public void CheckHttpClientHandlerType ([Values] AndroidRuntime runtime)
proj.SetProperty ("AndroidHttpClientHandlerType", expectedDefaultValue);
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
var intermediateOutputDir = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath);
List<EnvironmentHelper.EnvironmentFile> envFiles = EnvironmentHelper.GatherEnvironmentFiles (intermediateOutputDir, supportedAbis, true, runtime);
Dictionary<string, string> envvars = EnvironmentHelper.ReadEnvironmentVariables (envFiles, runtime);

List<EnvironmentHelper.EnvironmentFile>? envFiles = null;
Dictionary<string, string> envvars;

if (runtime == AndroidRuntime.NativeAOT) {
envvars = EnvironmentHelper.ReadNativeAotEnvironmentVariables (intermediateOutputDir);
} else {
envFiles = EnvironmentHelper.GatherEnvironmentFiles (intermediateOutputDir, supportedAbis, true, runtime);
envvars = EnvironmentHelper.ReadEnvironmentVariables (envFiles, runtime);
}

Assert.IsTrue (envvars.ContainsKey (httpClientHandlerVarName), $"Environment should contain '{httpClientHandlerVarName}'.");
Assert.AreEqual (expectedDefaultValue, envvars[httpClientHandlerVarName]);

proj.SetProperty ("AndroidHttpClientHandlerType", expectedUpdatedValue);
Assert.IsTrue (b.Build (proj), "Second build should have succeeded.");
envFiles = EnvironmentHelper.GatherEnvironmentFiles (intermediateOutputDir, supportedAbis, true, runtime);
envvars = EnvironmentHelper.ReadEnvironmentVariables (envFiles, runtime);

if (runtime == AndroidRuntime.NativeAOT) {
envvars = EnvironmentHelper.ReadNativeAotEnvironmentVariables (intermediateOutputDir);
} else {
envFiles = EnvironmentHelper.GatherEnvironmentFiles (intermediateOutputDir, supportedAbis, true, runtime);
envvars = EnvironmentHelper.ReadEnvironmentVariables (envFiles, runtime);
}

Assert.IsTrue (envvars.ContainsKey (httpClientHandlerVarName), $"Environment should contain '{httpClientHandlerVarName}'.");
Assert.AreEqual (expectedUpdatedValue, envvars[httpClientHandlerVarName]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,71 @@ public static List<EnvironmentFile> GatherEnvironmentFiles (string outputDirecto
return environmentFiles;
}

public static Dictionary<string, string> ReadNativeAotEnvironmentVariables (string outputDirectoryRoot, bool required = true)
{
var ret = new Dictionary<string, string> (StringComparer.Ordinal);

string javaSourcePath = Path.Combine (outputDirectoryRoot, "android", "src", "net", "dot", "jni", "nativeaot", "NativeAotEnvironmentVars.java");
bool exists = File.Exists (javaSourcePath);
if (required) {
Assert.IsTrue (exists, $"NativeAOT Java source with environment variables does not exist: {javaSourcePath}");
} else if (!exists) {
return ret;
}

var names = new List<string> ();
var values = new List<string> ();
bool collectingNames = false;
bool collectingValues = false;
int lineNum = 0;

foreach (string l in File.ReadAllLines (javaSourcePath)) {
lineNum++;
string line = l.Trim ();
switch (line) {
case "static String[] envNames = new String[] {":
collectingNames = true;
collectingValues = false;
continue;

case "static String[] envValues = new String[] {":
collectingValues = true;
collectingNames = false;
continue;

case "};":
collectingValues = false;
collectingNames = false;
continue;

case "":
continue;
}

if (!collectingValues && !collectingNames) {
continue;
}

Assert.IsTrue (line[0] == '"', $"Line {lineNum} in '{javaSourcePath}' doesn't start with a double quote: '{line}'");
Assert.IsTrue (line[line.Length - 1] == ',', $"Line {lineNum} in '{javaSourcePath}' doesn't end with a comma: '{line}'");
Assert.IsTrue (line[line.Length - 2] == '"', $"Line {lineNum} in '{javaSourcePath}' doesn't close the quoted string properly: '{line}'");

string data = line.Substring (1, line.Length - 3);
if (collectingNames) {
names.Add (data);
} else if (collectingValues) {
values.Add (data);
}
}

Assert.AreEqual (names.Count, values.Count, "Environment variable name and value arrays aren't of the same size in '{javaSourcePath}'");
for (int i = 0; i < names.Count; i++) {
ret.Add (names[i], values[i]);
}

return ret;
}

public static void AssertValidEnvironmentSharedLibrary (string outputDirectoryRoot, string sdkDirectory, string ndkDirectory, string supportedAbis, AndroidRuntime runtime)
{
NdkTools ndk = NdkTools.Create (ndkDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,9 @@ because xbuild doesn't support framework reference assemblies.
<GenerateAdditionalProviderSources
AdditionalProviderSources="@(_AdditionalProviderSources)"
AndroidRuntime="$(_AndroidRuntime)"
Environments="@(_EnvironmentFiles)"
HttpClientHandlerType="$(AndroidHttpClientHandlerType)"
EnableSGenConcurrent="$(AndroidEnableSGenConcurrent)"
CodeGenerationTarget="$(_AndroidJcwCodegenTarget)"
IntermediateOutputDirectory="$(IntermediateOutputPath)"
OutputDirectory="$(IntermediateOutputPath)android"
Expand Down Expand Up @@ -1906,6 +1909,7 @@ because xbuild doesn't support framework reference assemblies.
EnableManagedMarshalMethodsLookup="$(_AndroidUseManagedMarshalMethodsLookup)"
CustomBundleConfigFile="$(AndroidBundleConfigurationFile)"
TargetsCLR="$(_AndroidUseCLR)"
AndroidRuntime="$(_AndroidRuntime)"
ProjectRuntimeConfigFilePath="$(ProjectRuntimeConfigFilePath)"
>
</GenerateNativeApplicationConfigSources>
Expand Down