public static string GenerateLinkXmlToPreserveDerivedTypes(string librariesFolder, RuntimeClassRegistry usedClasses) { string tempFileName = Path.GetTempFileName(); using (TextWriter textWriter = new StreamWriter(tempFileName)) { textWriter.WriteLine("<linker>"); foreach (AssemblyDefinition current in MonoAssemblyStripping.CollectAllAssemblies(librariesFolder, usedClasses)) { if (!AssemblyHelper.IsUnityEngineModule(current)) { HashSet <TypeDefinition> hashSet = new HashSet <TypeDefinition>(); MonoAssemblyStripping.CollectBlackListTypes(hashSet, current.MainModule.Types, usedClasses.GetAllManagedBaseClassesAsString()); if (hashSet.Count != 0) { textWriter.WriteLine("<assembly fullname=\"{0}\">", current.Name.Name); foreach (TypeDefinition current2 in hashSet) { textWriter.WriteLine("<type fullname=\"{0}\" preserve=\"all\"/>", current2.FullName); } textWriter.WriteLine("</assembly>"); } } } textWriter.WriteLine("</linker>"); } return(tempFileName); }
public static string GenerateLinkXmlToPreserveDerivedTypes(string librariesFolder, RuntimeClassRegistry usedClasses) { string path = Path.GetTempFileName(); using (TextWriter w = new StreamWriter(path)) { w.WriteLine("<linker>"); foreach (var assembly in CollectAllAssemblies(librariesFolder, usedClasses)) { if (AssemblyHelper.IsUnityEngineModule(assembly)) { continue; } var typesToPreserve = new HashSet <TypeDefinition>(); CollectBlackListTypes(typesToPreserve, assembly.MainModule.Types, usedClasses.GetAllManagedBaseClassesAsString()); // don't write out xml file for assemblies with no types since link.xml files on disk have special meaning to IL2CPP stripping if (typesToPreserve.Count == 0) { continue; } w.WriteLine("<assembly fullname=\"{0}\">", assembly.Name.Name); foreach (var typeToPreserve in typesToPreserve) { w.WriteLine("<type fullname=\"{0}\" preserve=\"all\"/>", typeToPreserve.FullName); } w.WriteLine("</assembly>"); } w.WriteLine("</linker>"); } return(path); }
public static bool IsIgnoredSystemDll(AssemblyDefinition assembly) { if (AssemblyHelper.IsUnityEngineModule(assembly)) { return(true); } var name = assembly.Name.Name; return(name.StartsWith("System") || name.Equals("UnityEngine") || name.Equals("UnityEngine.Networking") || name.Equals("Mono.Posix") || name.Equals("Moq")); }
public static bool IsIgnoredSystemDll(AssemblyDefinition assembly) { bool result; if (AssemblyHelper.IsUnityEngineModule(assembly)) { result = true; } else { string name = assembly.Name.Name; result = (name.StartsWith("System") || name.Equals("UnityEngine") || name.Equals("UnityEngine.Networking") || name.Equals("Mono.Posix") || name.Equals("Moq")); } return(result); }
public static string GenerateLinkXmlToPreserveDerivedTypes(string librariesFolder, RuntimeClassRegistry usedClasses) { var sb = new StringBuilder(); bool wrotePreservations = false; sb.AppendLine("<linker>"); foreach (var assembly in CollectAllAssemblies(librariesFolder, usedClasses)) { if (AssemblyHelper.IsUnityEngineModule(assembly)) { continue; } var typesToPreserve = new HashSet <TypeDefinition>(); CollectBlackListTypes(typesToPreserve, assembly.MainModule.Types, usedClasses.GetAllManagedBaseClassesAsString()); // don't write out xml file for assemblies with no types since link.xml files on disk have special meaning to IL2CPP stripping if (typesToPreserve.Count == 0) { continue; } wrotePreservations = true; sb.AppendLine(string.Format("<assembly fullname=\"{0}\">", assembly.Name.Name)); foreach (var typeToPreserve in typesToPreserve) { sb.AppendLine(string.Format("<type fullname=\"{0}\" preserve=\"all\"/>", typeToPreserve.FullName)); } sb.AppendLine("</assembly>"); } sb.AppendLine("</linker>"); // Don't generate and pass xml files that are empty. While they are harmless, they are noisey and just cause useless clutter if (!wrotePreservations) { return(null); } string path = Path.GetTempFileName(); File.WriteAllText(path, sb.ToString()); return(path); }
static public void WriteCPlusPlusFileForStaticAOTModuleRegistration(BuildTarget buildTarget, string file, CrossCompileOptions crossCompileOptions, bool advancedLic, string targetDevice, bool stripping, RuntimeClassRegistry usedClassRegistry, AssemblyReferenceChecker checker, string stagingAreaDataManaged, IIl2CppPlatformProvider platformProvider) { // generate the Interal Call Summary file var icallSummaryPath = Path.Combine(stagingAreaDataManaged, "ICallSummary.txt"); var dlls = Directory.GetFiles(stagingAreaDataManaged, "UnityEngine.*Module.dll").Concat(new[] { Path.Combine(stagingAreaDataManaged, "UnityEngine.dll") }); var exe = Path.Combine(MonoInstallationFinder.GetFrameWorksFolder(), "Tools/InternalCallRegistrationWriter/InternalCallRegistrationWriter.exe"); var args = string.Format("-assembly=\"{0}\" -summary=\"{1}\"", dlls.Aggregate((dllArg, next) => dllArg + ";" + next), icallSummaryPath ); Runner.RunManagedProgram(exe, args); HashSet <UnityType> nativeClasses; HashSet <string> nativeModules; CodeStrippingUtils.GenerateDependencies(Path.GetDirectoryName(stagingAreaDataManaged), icallSummaryPath, usedClassRegistry, stripping, out nativeClasses, out nativeModules, platformProvider); using (TextWriter w = new StreamWriter(file)) { string[] fileNames = checker.GetAssemblyFileNames(); AssemblyDefinition[] assemblies = checker.GetAssemblyDefinitions(); bool fastICall = (crossCompileOptions & CrossCompileOptions.FastICall) != 0; ArrayList nativeMethods = BuildNativeMethodList(assemblies); if (buildTarget == BuildTarget.iOS) { w.WriteLine("#include \"RegisterMonoModules.h\""); w.WriteLine("#include <stdio.h>"); } w.WriteLine(""); w.WriteLine("#if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR"); w.WriteLine(" #define DECL_USER_FUNC(f) void f() __attribute__((weak_import))"); w.WriteLine(" #define REGISTER_USER_FUNC(f)\\"); w.WriteLine(" do {\\"); w.WriteLine(" if(f != NULL)\\"); w.WriteLine(" mono_dl_register_symbol(#f, (void*)f);\\"); w.WriteLine(" else\\"); w.WriteLine(" ::printf_console(\"Symbol \'%s\' not found. Maybe missing implementation for Simulator?\\n\", #f);\\"); w.WriteLine(" }while(0)"); w.WriteLine("#else"); w.WriteLine(" #define DECL_USER_FUNC(f) void f() "); w.WriteLine(" #if !defined(__arm64__)"); w.WriteLine(" #define REGISTER_USER_FUNC(f) mono_dl_register_symbol(#f, (void*)&f)"); w.WriteLine(" #else"); w.WriteLine(" #define REGISTER_USER_FUNC(f)"); w.WriteLine(" #endif"); w.WriteLine("#endif"); w.WriteLine("extern \"C\"\n{"); w.WriteLine(" typedef void* gpointer;"); w.WriteLine(" typedef int gboolean;"); if (buildTarget == BuildTarget.iOS) { w.WriteLine(" const char* UnityIPhoneRuntimeVersion = \"{0}\";", Application.unityVersion); w.WriteLine(" void mono_dl_register_symbol (const char* name, void *addr);"); w.WriteLine("#if !defined(__arm64__)"); w.WriteLine(" extern int mono_ficall_flag;"); w.WriteLine("#endif"); } w.WriteLine(" void mono_aot_register_module(gpointer *aot_info);"); w.WriteLine("#if __ORBIS__ || SN_TARGET_PSP2"); w.WriteLine("#define DLL_EXPORT __declspec(dllexport)"); // ps4 and psp2 need dllexport. w.WriteLine("#else"); w.WriteLine("#define DLL_EXPORT"); w.WriteLine("#endif"); w.WriteLine("#if !(TARGET_IPHONE_SIMULATOR)"); w.WriteLine(" extern gboolean mono_aot_only;"); for (int q = 0; q < fileNames.Length; ++q) { string fileName = fileNames[q]; string assemblyName = assemblies[q].Name.Name; assemblyName = assemblyName.Replace(".", "_"); assemblyName = assemblyName.Replace("-", "_"); assemblyName = assemblyName.Replace(" ", "_"); w.WriteLine(" extern gpointer* mono_aot_module_{0}_info; // {1}", assemblyName, fileName); } w.WriteLine("#endif // !(TARGET_IPHONE_SIMULATOR)"); foreach (string nmethod in nativeMethods) { w.WriteLine(" DECL_USER_FUNC({0});", nmethod); } w.WriteLine("}"); w.WriteLine("DLL_EXPORT void RegisterMonoModules()"); w.WriteLine("{"); w.WriteLine("#if !(TARGET_IPHONE_SIMULATOR) && !defined(__arm64__)"); w.WriteLine(" mono_aot_only = true;"); if (buildTarget == BuildTarget.iOS) { w.WriteLine(" mono_ficall_flag = {0};", fastICall ? "true" : "false"); } foreach (AssemblyDefinition definition in assemblies) { string assemblyName = definition.Name.Name; assemblyName = assemblyName.Replace(".", "_"); assemblyName = assemblyName.Replace("-", "_"); assemblyName = assemblyName.Replace(" ", "_"); w.WriteLine(" mono_aot_register_module(mono_aot_module_{0}_info);", assemblyName); } w.WriteLine("#endif // !(TARGET_IPHONE_SIMULATOR) && !defined(__arm64__)"); w.WriteLine(""); if (buildTarget == BuildTarget.iOS) { foreach (string nmethod in nativeMethods) { w.WriteLine(" REGISTER_USER_FUNC({0});", nmethod); } } w.WriteLine("}"); w.WriteLine(""); if (buildTarget == BuildTarget.iOS) { var inputAssemblies = new List <AssemblyDefinition>(); for (int i = 0; i < assemblies.Length; i++) { if (AssemblyHelper.IsUnityEngineModule(assemblies[i])) { inputAssemblies.Add(assemblies[i]); } } GenerateRegisterInternalCalls(inputAssemblies.ToArray(), w); GenerateRegisterModules(nativeClasses, nativeModules, w, stripping); if (stripping && usedClassRegistry != null) { GenerateRegisterClassesForStripping(nativeClasses, w); } else { GenerateRegisterClasses(nativeClasses, w); } } w.Close(); } }
public static void WriteCPlusPlusFileForStaticAOTModuleRegistration(BuildTarget buildTarget, string file, CrossCompileOptions crossCompileOptions, bool advancedLic, string targetDevice, bool stripping, RuntimeClassRegistry usedClassRegistry, AssemblyReferenceChecker checker, string stagingAreaDataManaged, IIl2CppPlatformProvider platformProvider) { string text = Path.Combine(stagingAreaDataManaged, "ICallSummary.txt"); IEnumerable <string> source = Directory.GetFiles(stagingAreaDataManaged, "UnityEngine.*Module.dll").Concat(new string[] { Path.Combine(stagingAreaDataManaged, "UnityEngine.dll") }); string exe = Path.Combine(MonoInstallationFinder.GetFrameWorksFolder(), "Tools/InternalCallRegistrationWriter/InternalCallRegistrationWriter.exe"); string args = string.Format("-assembly=\"{0}\" -summary=\"{1}\"", source.Aggregate((string dllArg, string next) => dllArg + ";" + next), text); Runner.RunManagedProgram(exe, args); HashSet <UnityType> hashSet; HashSet <string> nativeModules; CodeStrippingUtils.GenerateDependencies(Path.GetDirectoryName(stagingAreaDataManaged), text, usedClassRegistry, stripping, out hashSet, out nativeModules, platformProvider); using (TextWriter textWriter = new StreamWriter(file)) { string[] assemblyFileNames = checker.GetAssemblyFileNames(); AssemblyDefinition[] assemblyDefinitions = checker.GetAssemblyDefinitions(); bool flag = (crossCompileOptions & CrossCompileOptions.FastICall) != CrossCompileOptions.Dynamic; ArrayList arrayList = MonoAOTRegistration.BuildNativeMethodList(assemblyDefinitions); if (buildTarget == BuildTarget.iOS) { textWriter.WriteLine("#include \"RegisterMonoModules.h\""); textWriter.WriteLine("#include <stdio.h>"); } textWriter.WriteLine(""); textWriter.WriteLine("#if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR"); textWriter.WriteLine(" #define DECL_USER_FUNC(f) void f() __attribute__((weak_import))"); textWriter.WriteLine(" #define REGISTER_USER_FUNC(f)\\"); textWriter.WriteLine(" do {\\"); textWriter.WriteLine(" if(f != NULL)\\"); textWriter.WriteLine(" mono_dl_register_symbol(#f, (void*)f);\\"); textWriter.WriteLine(" else\\"); textWriter.WriteLine(" ::printf_console(\"Symbol '%s' not found. Maybe missing implementation for Simulator?\\n\", #f);\\"); textWriter.WriteLine(" }while(0)"); textWriter.WriteLine("#else"); textWriter.WriteLine(" #define DECL_USER_FUNC(f) void f() "); textWriter.WriteLine(" #if !defined(__arm64__)"); textWriter.WriteLine(" #define REGISTER_USER_FUNC(f) mono_dl_register_symbol(#f, (void*)&f)"); textWriter.WriteLine(" #else"); textWriter.WriteLine(" #define REGISTER_USER_FUNC(f)"); textWriter.WriteLine(" #endif"); textWriter.WriteLine("#endif"); textWriter.WriteLine("extern \"C\"\n{"); textWriter.WriteLine(" typedef void* gpointer;"); textWriter.WriteLine(" typedef int gboolean;"); if (buildTarget == BuildTarget.iOS) { textWriter.WriteLine(" const char* UnityIPhoneRuntimeVersion = \"{0}\";", Application.unityVersion); textWriter.WriteLine(" void mono_dl_register_symbol (const char* name, void *addr);"); textWriter.WriteLine("#if !defined(__arm64__)"); textWriter.WriteLine(" extern int mono_ficall_flag;"); textWriter.WriteLine("#endif"); } textWriter.WriteLine(" void mono_aot_register_module(gpointer *aot_info);"); textWriter.WriteLine("#if __ORBIS__ || SN_TARGET_PSP2"); textWriter.WriteLine("#define DLL_EXPORT __declspec(dllexport)"); textWriter.WriteLine("#else"); textWriter.WriteLine("#define DLL_EXPORT"); textWriter.WriteLine("#endif"); textWriter.WriteLine("#if !(TARGET_IPHONE_SIMULATOR)"); textWriter.WriteLine(" extern gboolean mono_aot_only;"); for (int i = 0; i < assemblyFileNames.Length; i++) { string arg = assemblyFileNames[i]; string text2 = assemblyDefinitions[i].Name.Name; text2 = text2.Replace(".", "_"); text2 = text2.Replace("-", "_"); text2 = text2.Replace(" ", "_"); textWriter.WriteLine(" extern gpointer* mono_aot_module_{0}_info; // {1}", text2, arg); } textWriter.WriteLine("#endif // !(TARGET_IPHONE_SIMULATOR)"); IEnumerator enumerator = arrayList.GetEnumerator(); try { while (enumerator.MoveNext()) { string arg2 = (string)enumerator.Current; textWriter.WriteLine(" DECL_USER_FUNC({0});", arg2); } } finally { IDisposable disposable; if ((disposable = (enumerator as IDisposable)) != null) { disposable.Dispose(); } } textWriter.WriteLine("}"); textWriter.WriteLine("DLL_EXPORT void RegisterMonoModules()"); textWriter.WriteLine("{"); textWriter.WriteLine("#if !(TARGET_IPHONE_SIMULATOR) && !defined(__arm64__)"); textWriter.WriteLine(" mono_aot_only = true;"); if (buildTarget == BuildTarget.iOS) { textWriter.WriteLine(" mono_ficall_flag = {0};", (!flag) ? "false" : "true"); } AssemblyDefinition[] array = assemblyDefinitions; for (int j = 0; j < array.Length; j++) { AssemblyDefinition assemblyDefinition = array[j]; string text3 = assemblyDefinition.Name.Name; text3 = text3.Replace(".", "_"); text3 = text3.Replace("-", "_"); text3 = text3.Replace(" ", "_"); textWriter.WriteLine(" mono_aot_register_module(mono_aot_module_{0}_info);", text3); } textWriter.WriteLine("#endif // !(TARGET_IPHONE_SIMULATOR) && !defined(__arm64__)"); textWriter.WriteLine(""); if (buildTarget == BuildTarget.iOS) { IEnumerator enumerator2 = arrayList.GetEnumerator(); try { while (enumerator2.MoveNext()) { string arg3 = (string)enumerator2.Current; textWriter.WriteLine(" REGISTER_USER_FUNC({0});", arg3); } } finally { IDisposable disposable2; if ((disposable2 = (enumerator2 as IDisposable)) != null) { disposable2.Dispose(); } } } textWriter.WriteLine("}"); textWriter.WriteLine(""); if (buildTarget == BuildTarget.iOS) { List <AssemblyDefinition> list = new List <AssemblyDefinition>(); for (int k = 0; k < assemblyDefinitions.Length; k++) { if (AssemblyHelper.IsUnityEngineModule(assemblyDefinitions[k])) { list.Add(assemblyDefinitions[k]); } } MonoAOTRegistration.GenerateRegisterInternalCalls(list.ToArray(), textWriter); MonoAOTRegistration.GenerateRegisterModules(hashSet, nativeModules, textWriter, stripping); if (stripping && usedClassRegistry != null) { MonoAOTRegistration.GenerateRegisterClassesForStripping(hashSet, textWriter); } else { MonoAOTRegistration.GenerateRegisterClasses(hashSet, textWriter); } } textWriter.Close(); } }