AddLil2cppSourceCodeToXcodeproj2021OrOlder.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using HybridCLR.Editor.Installer;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using UnityEditor;
  8. using System.Reflection;
  9. using HybridCLR.Editor.Settings;
  10. #if (UNITY_2020 || UNITY_2021) && UNITY_IOS
  11. using UnityEditor.Build;
  12. using UnityEditor.Callbacks;
  13. using UnityEditor.iOS.Xcode;
  14. using UnityEngine;
  15. namespace HybridCLR.Editor.BuildProcessors
  16. {
  17. public static class AddLil2cppSourceCodeToXcodeproj2021OrOlder
  18. {
  19. //[MenuItem("Test/GenProj")]
  20. //public static void Modify()
  21. //{
  22. // OnPostProcessBuild(BuildTarget.iOS, $"{SettingsUtil.ProjectDir}/Build-iOS");
  23. //}
  24. //[MenuItem("Test/CreateLumps")]
  25. //public static void CreateLumpsCmd()
  26. //{
  27. // CreateLumps($"{SettingsUtil.LocalIl2CppDir}/libil2cpp", $"{SettingsUtil.HybridCLRDataDir}/lumps");
  28. //}
  29. [PostProcessBuild]
  30. public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
  31. {
  32. if (target != BuildTarget.iOS || !HybridCLRSettings.Instance.enable)
  33. return;
  34. /*
  35. * 1. 生成lump,并且添加到工程
  36. 3. 将libil2cpp目录复制到 Library/. 删除旧的. search paths里修改 libil2cpp/include为libil2cpp
  37. 3. Libraries/bdwgc/include -> Libraries/external/bdwgc/include
  38. 4. 将external目录复制到 Library/external。删除旧目录
  39. 5. 将Library/external/baselib/Platforms/OSX改名为 IOS 全大写
  40. 6. 将 external/zlib下c 文件添加到工程
  41. 7. 移除libil2cpp.a
  42. 8. Include path add libil2cpp/os/ClassLibraryPAL/brotli/include
  43. 9. add external/xxHash
  44. */
  45. string pbxprojFile = $"{pathToBuiltProject}/Unity-iPhone.xcodeproj/project.pbxproj";
  46. string srcLibil2cppDir = $"{SettingsUtil.LocalIl2CppDir}/libil2cpp";
  47. string dstLibil2cppDir = $"{pathToBuiltProject}/Libraries/libil2cpp";
  48. string lumpDir = $"{pathToBuiltProject}/Libraries/lumps";
  49. string srcExternalDir = $"{SettingsUtil.LocalIl2CppDir}/external";
  50. string dstExternalDir = $"{pathToBuiltProject}/Libraries/external";
  51. //RemoveExternalLibil2cppOption(srcExternalDir, dstExternalDir);
  52. CopyLibil2cppToXcodeProj(srcLibil2cppDir, dstLibil2cppDir);
  53. CopyExternalToXcodeProj(srcExternalDir, dstExternalDir);
  54. var lumpFiles = CreateLumps(dstLibil2cppDir, lumpDir);
  55. var extraSources = GetExtraSourceFiles(dstExternalDir, dstLibil2cppDir);
  56. var cflags = new List<string>()
  57. {
  58. "-DIL2CPP_MONO_DEBUGGER_DISABLED",
  59. };
  60. ModifyPBXProject(pathToBuiltProject, pbxprojFile, lumpFiles, extraSources, cflags);
  61. }
  62. private static string GetRelativePathFromProj(string path)
  63. {
  64. return path.Substring(path.IndexOf("Libraries", StringComparison.Ordinal)).Replace('\\', '/');
  65. }
  66. private static void ModifyPBXProject(string pathToBuiltProject, string pbxprojFile, List<LumpFile> lumpFiles, List<string> extraFiles, List<string> cflags)
  67. {
  68. var proj = new PBXProject();
  69. proj.ReadFromFile(pbxprojFile);
  70. string targetGUID = proj.GetUnityFrameworkTargetGuid();
  71. // 移除旧的libil2cpp.a
  72. var libil2cppGUID = proj.FindFileGuidByProjectPath("Libraries/libil2cpp.a");
  73. if (!string.IsNullOrEmpty(libil2cppGUID))
  74. {
  75. proj.RemoveFileFromBuild(targetGUID, libil2cppGUID);
  76. proj.RemoveFile(libil2cppGUID);
  77. File.Delete(Path.Combine(pathToBuiltProject, "Libraries", "libil2cpp.a"));
  78. }
  79. //var lumpGroupGuid = proj.AddFile("Lumps", $"Classes/Lumps", PBXSourceTree.Group);
  80. foreach (var lumpFile in lumpFiles)
  81. {
  82. string lumpFileName = Path.GetFileName(lumpFile.lumpFile);
  83. string projPathOfFile = $"Classes/Lumps/{lumpFileName}";
  84. string relativePathOfFile = GetRelativePathFromProj(lumpFile.lumpFile);
  85. string lumpGuid = proj.FindFileGuidByProjectPath(projPathOfFile);
  86. if (!string.IsNullOrEmpty(lumpGuid))
  87. {
  88. proj.RemoveFileFromBuild(targetGUID, lumpGuid);
  89. proj.RemoveFile(lumpGuid);
  90. }
  91. lumpGuid = proj.AddFile(relativePathOfFile, projPathOfFile, PBXSourceTree.Source);
  92. proj.AddFileToBuild(targetGUID, lumpGuid);
  93. }
  94. foreach (var extraFile in extraFiles)
  95. {
  96. string projPathOfFile = $"Classes/Extrals/{Path.GetFileName(extraFile)}";
  97. string extraFileGuid = proj.FindFileGuidByProjectPath(projPathOfFile);
  98. if (!string.IsNullOrEmpty(extraFileGuid))
  99. {
  100. proj.RemoveFileFromBuild(targetGUID, extraFileGuid);
  101. proj.RemoveFile(extraFileGuid);
  102. //Debug.LogWarning($"remove exist extra file:{projPathOfFile} guid:{extraFileGuid}");
  103. }
  104. var lumpGuid = proj.AddFile(GetRelativePathFromProj(extraFile), projPathOfFile, PBXSourceTree.Source);
  105. proj.AddFileToBuild(targetGUID, lumpGuid);
  106. }
  107. foreach(var configName in proj.BuildConfigNames())
  108. {
  109. //Debug.Log($"build config:{bcn}");
  110. string configGuid = proj.BuildConfigByName(targetGUID, configName);
  111. string headerSearchPaths = "HEADER_SEARCH_PATHS";
  112. string hspProp = proj.GetBuildPropertyForConfig(configGuid, headerSearchPaths);
  113. //Debug.Log($"config guid:{configGuid} prop:{hspProp}");
  114. string newPro = hspProp.Replace("libil2cpp/include", "libil2cpp")
  115. .Replace("Libraries/bdwgc", "Libraries/external/bdwgc");
  116. if (!newPro.Contains("Libraries/libil2cpp/os/ClassLibraryPAL/brotli/include"))
  117. {
  118. newPro += " $(SRCROOT)/Libraries/libil2cpp/os/ClassLibraryPAL/brotli/include";
  119. }
  120. if (!newPro.Contains("Libraries/external/xxHash"))
  121. {
  122. newPro += " $(SRCROOT)/Libraries/external/xxHash";
  123. }
  124. //Debug.Log($"config:{bcn} new prop:{newPro}");
  125. proj.SetBuildPropertyForConfig(configGuid, headerSearchPaths, newPro);
  126. string cflagKey = "OTHER_CFLAGS";
  127. string cfProp = proj.GetBuildPropertyForConfig(configGuid, cflagKey);
  128. foreach (var flag in cflags)
  129. {
  130. if (!cfProp.Contains(flag))
  131. {
  132. cfProp += " " + flag;
  133. }
  134. }
  135. if (configName.Contains("Debug") && !cfProp.Contains("-DIL2CPP_DEBUG="))
  136. {
  137. cfProp += " -DIL2CPP_DEBUG=1 -DDEBUG=1";
  138. }
  139. proj.SetBuildPropertyForConfig(configGuid, cflagKey, cfProp);
  140. }
  141. proj.WriteToFile(pbxprojFile);
  142. }
  143. private static void CopyLibil2cppToXcodeProj(string srcLibil2cppDir, string dstLibil2cppDir)
  144. {
  145. BashUtil.RemoveDir(dstLibil2cppDir);
  146. BashUtil.CopyDir(srcLibil2cppDir, dstLibil2cppDir, true);
  147. }
  148. private static void CopyExternalToXcodeProj(string srcExternalDir, string dstExternalDir)
  149. {
  150. BashUtil.RemoveDir(dstExternalDir);
  151. BashUtil.CopyDir(srcExternalDir, dstExternalDir, true);
  152. string baselibPlatfromsDir = $"{dstExternalDir}/baselib/Platforms";
  153. BashUtil.RemoveDir($"{baselibPlatfromsDir}/IOS");
  154. BashUtil.CopyDir($"{baselibPlatfromsDir}/OSX", $"{baselibPlatfromsDir}/IOS", true);
  155. }
  156. class LumpFile
  157. {
  158. public List<string> cppFiles = new List<string>();
  159. public readonly string lumpFile;
  160. public readonly string il2cppConfigFile;
  161. public LumpFile(string lumpFile, string il2cppConfigFile)
  162. {
  163. this.lumpFile = lumpFile;
  164. this.il2cppConfigFile = il2cppConfigFile;
  165. this.cppFiles.Add(il2cppConfigFile);
  166. }
  167. public void SaveFile()
  168. {
  169. var lumpFileContent = new List<string>();
  170. foreach (var file in cppFiles)
  171. {
  172. lumpFileContent.Add($"#include \"{GetRelativePathFromProj(file)}\"");
  173. }
  174. File.WriteAllLines(lumpFile, lumpFileContent, Encoding.UTF8);
  175. Debug.Log($"create lump file:{lumpFile}");
  176. }
  177. }
  178. private static List<LumpFile> CreateLumps(string libil2cppDir, string outputDir)
  179. {
  180. BashUtil.RecreateDir(outputDir);
  181. string il2cppConfigFile = $"{libil2cppDir}/il2cpp-config.h";
  182. var lumpFiles = new List<LumpFile>();
  183. int lumpFileIndex = 0;
  184. foreach (var cppDir in Directory.GetDirectories(libil2cppDir, "*", SearchOption.AllDirectories).Concat(new string[] {libil2cppDir}))
  185. {
  186. var lumpFile = new LumpFile($"{outputDir}/lump_{Path.GetFileName(cppDir)}_{lumpFileIndex}.cpp", il2cppConfigFile);
  187. foreach (var file in Directory.GetFiles(cppDir, "*.cpp", SearchOption.TopDirectoryOnly))
  188. {
  189. lumpFile.cppFiles.Add(file);
  190. }
  191. lumpFile.SaveFile();
  192. lumpFiles.Add(lumpFile);
  193. ++lumpFileIndex;
  194. }
  195. var mmFiles = Directory.GetFiles(libil2cppDir, "*.mm", SearchOption.AllDirectories);
  196. if (mmFiles.Length > 0)
  197. {
  198. var lumpFile = new LumpFile($"{outputDir}/lump_mm.mm", il2cppConfigFile);
  199. foreach (var file in mmFiles)
  200. {
  201. lumpFile.cppFiles.Add(file);
  202. }
  203. lumpFile.SaveFile();
  204. lumpFiles.Add(lumpFile);
  205. }
  206. return lumpFiles;
  207. }
  208. private static List<string> GetExtraSourceFiles(string externalDir, string libil2cppDir)
  209. {
  210. var files = new List<string>();
  211. foreach (string extraDir in new string[]
  212. {
  213. $"{externalDir}/zlib",
  214. $"{externalDir}/xxHash",
  215. $"{libil2cppDir}/os/ClassLibraryPAL/brotli",
  216. })
  217. {
  218. if (!Directory.Exists(extraDir))
  219. {
  220. continue;
  221. }
  222. files.AddRange(Directory.GetFiles(extraDir, "*.c", SearchOption.AllDirectories));
  223. }
  224. return files;
  225. }
  226. }
  227. }
  228. #endif