示例#1
0
 public TypeMappingReleaseNativeAssemblyGenerator(NativeAssemblerTargetProvider targetProvider, NativeTypeMappingData mappingData, string baseFileName, bool sharedBitsWritten, bool sharedIncludeUsesAbiPrefix = false)
     : base(targetProvider, baseFileName, sharedIncludeUsesAbiPrefix)
 {
     this.mappingData       = mappingData ?? throw new ArgumentNullException(nameof(mappingData));
     this.baseFileName      = baseFileName;
     this.sharedBitsWritten = sharedIncludeUsesAbiPrefix ? false : sharedBitsWritten;
 }
示例#2
0
 protected NativeAssemblyGenerator(NativeAssemblerTargetProvider targetProvider)
 {
     if (targetProvider == null)
     {
         throw new ArgumentNullException(nameof(targetProvider));
     }
     TargetProvider = targetProvider;
 }
示例#3
0
 public ApplicationConfigNativeAssemblyGenerator(NativeAssemblerTargetProvider targetProvider, string baseFileName, IDictionary <string, string> environmentVariables, IDictionary <string, string> systemProperties)
     : base(targetProvider, baseFileName)
 {
     if (environmentVariables != null)
     {
         this.environmentVariables = new SortedDictionary <string, string> (environmentVariables, StringComparer.Ordinal);
     }
     if (systemProperties != null)
     {
         this.systemProperties = new SortedDictionary <string, string> (systemProperties, StringComparer.Ordinal);
     }
 }
        public TypeMappingDebugNativeAssemblyGenerator(NativeAssemblerTargetProvider targetProvider, TypeMapGenerator.ModuleDebugData data, string baseFileName, bool sharedBitsWritten, bool sharedIncludeUsesAbiPrefix = false)
            : base(targetProvider, baseFileName, sharedIncludeUsesAbiPrefix)
        {
            if (String.IsNullOrEmpty(baseFileName))
            {
                throw new ArgumentException("must not be null or empty", nameof(baseFileName));
            }
            this.data = data ?? throw new ArgumentNullException(nameof(data));

            this.baseFileName      = baseFileName;
            this.sharedBitsWritten = sharedBitsWritten;
        }
示例#5
0
        public TypeMappingNativeAssemblyGenerator(NativeAssemblerTargetProvider targetProvider, NativeAssemblyDataStream dataStream, string dataFileName, uint dataSize, string mappingFieldName)
            : base(targetProvider)
        {
            if (dataStream == null)
            {
                throw new ArgumentNullException(nameof(dataStream));
            }
            if (String.IsNullOrEmpty(dataFileName))
            {
                throw new ArgumentException("must not be null or empty", nameof(dataFileName));
            }
            if (String.IsNullOrEmpty(mappingFieldName))
            {
                throw new ArgumentException("must not be null or empty", nameof(mappingFieldName));
            }

            this.dataStream       = dataStream;
            this.dataFileName     = dataFileName;
            this.dataSize         = dataSize;
            this.mappingFieldName = mappingFieldName;
        }
示例#6
0
        protected NativeAssemblyGenerator(NativeAssemblerTargetProvider targetProvider, string baseFilePath, bool sharedIncludeUsesAbiPrefix = false)
        {
            if (targetProvider == null)
            {
                throw new ArgumentNullException(nameof(targetProvider));
            }
            if (String.IsNullOrEmpty(baseFilePath))
            {
                throw new ArgumentException("must not be null or empty", nameof(baseFilePath));
            }

            TargetProvider = targetProvider;
            if (sharedIncludeUsesAbiPrefix)
            {
                SharedIncludeFile = $"{baseFilePath}.{targetProvider.AbiName}-shared.inc";
            }
            else
            {
                SharedIncludeFile = $"{baseFilePath}.shared.inc";
            }
            TypemapsIncludeFile = $"{baseFilePath}.{targetProvider.AbiName}-managed.inc";
            MainSourceFile      = $"{baseFilePath}.{targetProvider.AbiName}.s";
        }
示例#7
0
 public CompressedAssembliesNativeAssemblyGenerator(IDictionary <string, CompressedAssemblyInfo> assemblies, NativeAssemblerTargetProvider targetProvider, string baseFilePath)
     : base(targetProvider, baseFilePath)
 {
     this.assemblies = assemblies;
     dataIncludeFile = $"{baseFilePath}-data.inc";
 }
        void GenerateCompressedAssemblySources()
        {
            if (Debug || !EnableCompression)
            {
                Generate(null);
                return;
            }

            var assemblies = new SortedDictionary <string, CompressedAssemblyInfo> (StringComparer.Ordinal);

            foreach (ITaskItem assembly in ResolvedAssemblies)
            {
                if (bool.TryParse(assembly.GetMetadata("AndroidSkipAddToPackage"), out bool value) && value)
                {
                    continue;
                }

                if (assemblies.ContainsKey(assembly.ItemSpec))
                {
                    continue;
                }

                var fi = new FileInfo(assembly.ItemSpec);
                if (!fi.Exists)
                {
                    Log.LogError($"Assembly {assembly.ItemSpec} does not exist");
                    continue;
                }

                assemblies.Add(CompressedAssemblyInfo.GetDictionaryKey(assembly),
                               new CompressedAssemblyInfo(checked ((uint)fi.Length)));
            }

            uint index = 0;

            foreach (var kvp in assemblies)
            {
                kvp.Value.DescriptorIndex = index++;
            }

            string key = CompressedAssemblyInfo.GetKey(ProjectFullPath);

            Log.LogDebugMessage($"Storing compression assemblies info with key '{key}'");
            BuildEngine4.RegisterTaskObjectAssemblyLocal(key, assemblies, RegisteredTaskObjectLifetime.Build);
            Generate(assemblies);

            void Generate(IDictionary <string, CompressedAssemblyInfo> dict)
            {
                foreach (string abi in SupportedAbis)
                {
                    NativeAssemblerTargetProvider asmTargetProvider = GeneratePackageManagerJava.GetAssemblyTargetProvider(abi);
                    string baseAsmFilePath = Path.Combine(EnvironmentOutputDirectory, $"compressed_assemblies.{abi.ToLowerInvariant ()}");
                    string asmFilePath     = $"{baseAsmFilePath}.s";
                    var    asmgen          = new CompressedAssembliesNativeAssemblyGenerator(dict, asmTargetProvider, baseAsmFilePath);

                    using (var sw = MemoryStreamPool.Shared.CreateStreamWriter()) {
                        asmgen.Write(sw);
                        sw.Flush();
                        if (MonoAndroidHelper.CopyIfStreamChanged(sw.BaseStream, asmFilePath))
                        {
                            Log.LogDebugMessage($"File {asmFilePath} was regenerated");
                        }
                    }
                }
            }
        }
示例#9
0
        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("\"", "\\\""));
            }
        }