public override bool RunTask() { try { var path = Path.GetFullPath(LockFile); var key = new Tuple <string, string> (nameof(WriteLockFile), path); // Use the full path as part of the key // Check if already registered, for sanity var existing = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal <DeleteFileAfterBuild> (key, RegisteredTaskObjectLifetime.Build); if (existing == null) { if (File.Exists(path)) { Log.LogCodedWarning("XA5302", Properties.Resources.XA5302, path); } else { Directory.CreateDirectory(Path.GetDirectoryName(path)); File.WriteAllText(path, ""); } BuildEngine4.RegisterTaskObjectAssemblyLocal(key, new DeleteFileAfterBuild(path), RegisteredTaskObjectLifetime.Build); } else { Log.LogDebugMessage("Lock file was created earlier in the build."); } } catch (Exception ex) { Log.LogDebugMessage($"Exception in {nameof (WriteLockFile)}: {ex}"); } // We want to always continue return(true); }
string FindMono() { string mono = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal <string> (MonoKey, Lifetime); if (!string.IsNullOrEmpty(mono)) { Log.LogDebugMessage($"Found cached mono via {nameof (BuildEngine4.RegisterTaskObject)}"); return(mono); } var env = Environment.GetEnvironmentVariable("PATH"); if (string.IsNullOrEmpty(env)) { foreach (var path in env.Split(Path.PathSeparator)) { if (File.Exists(mono = Path.Combine(path, "mono"))) { Log.LogDebugMessage("Found mono in $PATH"); BuildEngine4.RegisterTaskObjectAssemblyLocal(MonoKey, mono, Lifetime); return(mono); } } } foreach (var path in KnownMonoPaths) { if (File.Exists(mono = path)) { Log.LogDebugMessage($"Found mono in {nameof (KnownMonoPaths)}"); BuildEngine4.RegisterTaskObjectAssemblyLocal(MonoKey, mono, Lifetime); return(mono); } } // Last resort BuildEngine4.RegisterTaskObjectAssemblyLocal(MonoKey, mono = "mono", Lifetime); return(mono); }
public override bool RunTask() { if (InputAssemblies == null) { return(true); } var output = new List <ITaskItem> (InputAssemblies.Length); foreach (var assemblyItem in InputAssemblies) { // Skip .NET 6.0 <FrameworkReference/> assemblies var frameworkReferenceName = assemblyItem.GetMetadata("FrameworkReferenceName") ?? ""; if (frameworkReferenceName == "Microsoft.Android") { continue; // No need to process Mono.Android.dll or Java.Interop.dll } if (frameworkReferenceName.StartsWith("Microsoft.NETCore.", StringComparison.OrdinalIgnoreCase)) { continue; // No need to process BCL assemblies } if (!File.Exists(assemblyItem.ItemSpec)) { Log.LogDebugMessage($"Skipping non-existent dependency '{assemblyItem.ItemSpec}'."); continue; } if (string.Equals(assemblyItem.GetMetadata("TargetPlatformIdentifier"), "android", StringComparison.OrdinalIgnoreCase)) { output.Add(assemblyItem); continue; } using (var pe = new PEReader(File.OpenRead(assemblyItem.ItemSpec))) { var reader = pe.GetMetadataReader(); // Check in-memory cache var module = reader.GetModuleDefinition(); var key = (nameof(FilterAssemblies), reader.GetGuid(module.Mvid)); var value = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal(key, Lifetime); if (value is bool isMonoAndroidAssembly) { if (isMonoAndroidAssembly) { Log.LogDebugMessage($"Cached: {assemblyItem.ItemSpec}"); output.Add(assemblyItem); } continue; } // Check assembly definition var assemblyDefinition = reader.GetAssemblyDefinition(); if (IsAndroidAssembly(assemblyDefinition, reader)) { output.Add(assemblyItem); BuildEngine4.RegisterTaskObjectAssemblyLocal(key, value: true, Lifetime); continue; } // Fallback to looking for a Mono.Android reference if (MonoAndroidHelper.HasMonoAndroidReference(reader)) { Log.LogDebugMessage($"Mono.Android reference found: {assemblyItem.ItemSpec}"); output.Add(assemblyItem); BuildEngine4.RegisterTaskObjectAssemblyLocal(key, value: true, Lifetime); continue; } // Fallback to looking for *.jar or __Android EmbeddedResource files if (HasEmbeddedResource(reader)) { Log.LogDebugMessage($"EmbeddedResource found: {assemblyItem.ItemSpec}"); output.Add(assemblyItem); BuildEngine4.RegisterTaskObjectAssemblyLocal(key, value: true, Lifetime); continue; } // Not a MonoAndroid assembly, store false BuildEngine4.RegisterTaskObjectAssemblyLocal(key, value: false, Lifetime); } } OutputAssemblies = output.ToArray(); return(!Log.HasLoggedErrors); }
void AddEnvironment() { bool usesMonoAOT = false; bool usesAssemblyPreload = EnablePreloadAssembliesDefault; bool brokenExceptionTransitions = false; var environmentVariables = new Dictionary <string, string> (StringComparer.Ordinal); var systemProperties = new Dictionary <string, string> (StringComparer.Ordinal); if (!Enum.TryParse(PackageNamingPolicy, out PackageNamingPolicy pnp)) { pnp = PackageNamingPolicyEnum.LowercaseCrc64; } AotMode aotMode = AotMode.None; if (!string.IsNullOrEmpty(AndroidAotMode) && Aot.GetAndroidAotMode(AndroidAotMode, out aotMode) && aotMode != AotMode.None) { usesMonoAOT = true; } bool haveLogLevel = false; bool haveMonoDebug = false; bool havebuildId = false; bool haveHttpMessageHandler = false; bool haveTlsProvider = false; bool haveMonoGCParams = false; SequencePointsMode sequencePointsMode; if (!Aot.TryGetSequencePointsMode(AndroidSequencePointsMode, out sequencePointsMode)) { sequencePointsMode = SequencePointsMode.None; } foreach (ITaskItem env in Environments ?? new TaskItem[0]) { foreach (string line in File.ReadLines(env.ItemSpec)) { var lineToWrite = line; if (lineToWrite.StartsWith("MONO_LOG_LEVEL=", StringComparison.Ordinal)) { haveLogLevel = true; } if (lineToWrite.StartsWith("MONO_GC_PARAMS=", StringComparison.Ordinal)) { haveMonoGCParams = true; } if (lineToWrite.StartsWith("XAMARIN_BUILD_ID=", StringComparison.Ordinal)) { havebuildId = true; } if (lineToWrite.StartsWith("MONO_DEBUG=", StringComparison.Ordinal)) { haveMonoDebug = true; if (sequencePointsMode != SequencePointsMode.None && !lineToWrite.Contains("gen-compact-seq-points")) { lineToWrite = line + ",gen-compact-seq-points"; } } if (lineToWrite.StartsWith("XA_HTTP_CLIENT_HANDLER_TYPE=", StringComparison.Ordinal)) { haveHttpMessageHandler = true; } if (lineToWrite.StartsWith("XA_TLS_PROVIDER=", StringComparison.Ordinal)) { haveTlsProvider = true; } if (lineToWrite.StartsWith("mono.enable_assembly_preload=", StringComparison.Ordinal)) { int idx = lineToWrite.IndexOf('='); uint val; if (idx < lineToWrite.Length - 1 && UInt32.TryParse(lineToWrite.Substring(idx + 1), out val)) { usesAssemblyPreload = idx == 1; } continue; } if (lineToWrite.StartsWith("XA_BROKEN_EXCEPTION_TRANSITIONS=")) { brokenExceptionTransitions = true; continue; } AddEnvironmentVariableLine(lineToWrite); } } if (_Debug && !haveLogLevel) { AddEnvironmentVariable(defaultLogLevel[0], defaultLogLevel[1]); } if (sequencePointsMode != SequencePointsMode.None && !haveMonoDebug) { AddEnvironmentVariable(defaultMonoDebug[0], defaultMonoDebug[1]); } if (!havebuildId) { AddEnvironmentVariable("XAMARIN_BUILD_ID", BuildId); } if (!haveHttpMessageHandler) { if (HttpClientHandlerType == null) { AddEnvironmentVariable(defaultHttpMessageHandler[0], defaultHttpMessageHandler[1]); } else { AddEnvironmentVariable("XA_HTTP_CLIENT_HANDLER_TYPE", HttpClientHandlerType.Trim()); } } if (!haveTlsProvider) { if (TlsProvider == null) { AddEnvironmentVariable(defaultTlsProvider[0], defaultTlsProvider[1]); } else { AddEnvironmentVariable("XA_TLS_PROVIDER", TlsProvider.Trim()); } } if (!haveMonoGCParams) { if (EnableSGenConcurrent) { AddEnvironmentVariable("MONO_GC_PARAMS", "major=marksweep-conc"); } else { AddEnvironmentVariable("MONO_GC_PARAMS", "major=marksweep"); } } global::Android.Runtime.BoundExceptionType boundExceptionType; if (String.IsNullOrEmpty(BoundExceptionType) || String.Compare(BoundExceptionType, "System", StringComparison.OrdinalIgnoreCase) == 0) { boundExceptionType = global::Android.Runtime.BoundExceptionType.System; } else if (String.Compare(BoundExceptionType, "Java", StringComparison.OrdinalIgnoreCase) == 0) { boundExceptionType = global::Android.Runtime.BoundExceptionType.Java; } else { throw new InvalidOperationException($"Unsupported BoundExceptionType value '{BoundExceptionType}'"); } var appConfState = BuildEngine4.GetRegisteredTaskObjectAssemblyLocal <ApplicationConfigTaskState> (ApplicationConfigTaskState.RegisterTaskObjectKey, RegisteredTaskObjectLifetime.Build); foreach (string abi in SupportedAbis) { NativeAssemblerTargetProvider asmTargetProvider = GetAssemblyTargetProvider(abi); string baseAsmFilePath = Path.Combine(EnvironmentOutputDirectory, $"environment.{abi.ToLowerInvariant ()}"); string asmFilePath = $"{baseAsmFilePath}.s"; var asmgen = new ApplicationConfigNativeAssemblyGenerator(asmTargetProvider, baseAsmFilePath, environmentVariables, systemProperties) { IsBundledApp = IsBundledApplication, UsesMonoAOT = usesMonoAOT, UsesMonoLLVM = EnableLLVM, UsesAssemblyPreload = usesAssemblyPreload, MonoAOTMode = aotMode.ToString().ToLowerInvariant(), AndroidPackageName = AndroidPackageName, BrokenExceptionTransitions = brokenExceptionTransitions, PackageNamingPolicy = pnp, BoundExceptionType = boundExceptionType, InstantRunEnabled = InstantRunEnabled, JniAddNativeMethodRegistrationAttributePresent = appConfState != null ? appConfState.JniAddNativeMethodRegistrationAttributePresent : false, }; using (var sw = MemoryStreamPool.Shared.CreateStreamWriter()) { asmgen.Write(sw); sw.Flush(); Files.CopyIfStreamChanged(sw.BaseStream, asmFilePath); } } void AddEnvironmentVariable(string name, string value) { if (Char.IsUpper(name [0]) || !Char.IsLetter(name [0])) { environmentVariables [ValidAssemblerString(name)] = ValidAssemblerString(value); } else { systemProperties [ValidAssemblerString(name)] = ValidAssemblerString(value); } } void AddEnvironmentVariableLine(string l) { string line = l?.Trim(); if (String.IsNullOrEmpty(line) || line [0] == '#') { return; } string[] nv = line.Split(new char[] { '=' }, 2); AddEnvironmentVariable(nv[0].Trim(), nv.Length < 2 ? String.Empty : nv[1].Trim()); } string ValidAssemblerString(string s) { return(s.Replace("\"", "\\\"")); } }