BuildIOS.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/File.h>
  23. #include <Atomic/IO/FileSystem.h>
  24. #include <Atomic/Resource/ResourceCache.h>
  25. #include "../ToolSystem.h"
  26. #include "../ToolEnvironment.h"
  27. #include "../Project/Project.h"
  28. #include "../Project/ProjectBuildSettings.h"
  29. #include "../Assets/AssetDatabase.h"
  30. #include "../Subprocess/SubprocessSystem.h"
  31. #include "BuildIOS.h"
  32. #include "BuildEvents.h"
  33. #include "BuildSystem.h"
  34. namespace ToolCore
  35. {
  36. BuildIOS::BuildIOS(Context * context, Project *project) : BuildBase(context, project, PLATFORMID_IOS)
  37. {
  38. }
  39. BuildIOS::~BuildIOS()
  40. {
  41. }
  42. String BuildIOS::GenerateInfoPlist()
  43. {
  44. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  45. Project* project = tsystem->GetProject();
  46. ProjectBuildSettings* buildSettings = project->GetBuildSettings();
  47. IOSBuildSettings* iosSettings = buildSettings->GetIOSBuildSettings();
  48. String plist = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  49. plist += "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
  50. plist += "<plist version=\"1.0\">\n";
  51. plist += "<dict>\n";
  52. plist += "<key>CFBundleDevelopmentRegion</key>\n";
  53. plist += "<string>English</string>\n";
  54. plist += "<key>CFBundleExecutable</key>\n";
  55. plist += "<string>AtomicPlayer</string>\n";
  56. plist += "<key>CFBundleGetInfoString</key>\n";
  57. plist += "<string>\"Atomic Player\"</string>\n";
  58. plist += "<key>CFBundleIconFile</key>\n";
  59. plist += "<string></string>\n";
  60. plist += "<key>CFBundleIdentifier</key>\n";
  61. plist.AppendWithFormat("<string>%s</string>\n", iosSettings->GetPackageName().CString());
  62. plist += "<key>CFBundleInfoDictionaryVersion</key>\n";
  63. plist += "<string>6.0</string>\n";
  64. plist += "<key>CFBundleLongVersionString</key>\n";
  65. plist += "<string></string>\n";
  66. plist += "<key>CFBundleName</key>\n";
  67. plist += "<string></string>\n";
  68. plist += "<key>CFBundlePackageType</key>\n";
  69. plist += "<string>APPL</string>\n";
  70. plist += "<key>CFBundleShortVersionString</key>\n";
  71. plist += "<string></string>\n";
  72. plist += "<key>CFBundleSignature</key>\n";
  73. plist += "<string>????</string>\n";
  74. plist += "<key>CFBundleVersion</key>\n";
  75. plist += "<string></string>\n";
  76. plist += "<key>CSResourcesFileMapped</key>\n";
  77. plist += "<true/>\n";
  78. plist += "<key>LSRequiresCarbon</key>\n";
  79. plist += "<true/>\n";
  80. plist += "<key>NSHumanReadableCopyright</key>\n";
  81. plist += "<string>\"(c) Your Company\"</string>\n";
  82. plist += "<key>CFBundleIconFiles</key>\n";
  83. plist += "<array>\n";
  84. plist += "<string></string>\n";
  85. plist += "</array>\n";
  86. plist += "</dict>\n";
  87. plist += "</plist>\n";
  88. return plist;
  89. }
  90. String BuildIOS::GenerateEntitlements()
  91. {
  92. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  93. Project* project = tsystem->GetProject();
  94. ProjectBuildSettings* buildSettings = project->GetBuildSettings();
  95. IOSBuildSettings* iosSettings = buildSettings->GetIOSBuildSettings();
  96. String app_identifer = iosSettings->GetAppIDPrefix() + ".";
  97. app_identifer += iosSettings->GetPackageName();
  98. String team_identifier = iosSettings->GetAppIDPrefix();
  99. String keychain_access_group = app_identifer;
  100. String entitlements = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  101. entitlements += "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
  102. entitlements += "<plist version=\"1.0\">\n";
  103. entitlements += "<dict>\n";
  104. entitlements += "<key>application-identifier</key>\n";
  105. entitlements.AppendWithFormat("<string>%s</string>\n", app_identifer.CString());
  106. entitlements += "<key>com.apple.developer.team-identifier</key>\n";
  107. entitlements.AppendWithFormat("<string>%s</string>\n", team_identifier.CString());
  108. entitlements += "<key>get-task-allow</key>\n";
  109. entitlements += "<true/>\n";
  110. entitlements += "<key>keychain-access-groups</key>\n";
  111. entitlements += "<array>\n";
  112. entitlements.AppendWithFormat("<string>%s</string>\n", keychain_access_group.CString());
  113. entitlements += "</array>\n";
  114. entitlements += "</dict>\n";
  115. entitlements += "</plist>\n";
  116. return entitlements;
  117. }
  118. void BuildIOS::Initialize()
  119. {
  120. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  121. Project* project = tsystem->GetProject();
  122. Vector<String> defaultResourcePaths;
  123. GetDefaultResourcePaths(defaultResourcePaths);
  124. for (unsigned i = 0; i < defaultResourcePaths.Size(); i++)
  125. {
  126. AddResourceDir(defaultResourcePaths[i]);
  127. }
  128. BuildDefaultResourceEntries();
  129. // TODO: smart filtering of cache
  130. String projectResources = project->GetResourcePath();
  131. AddProjectResourceDir(projectResources);
  132. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  133. String cachePath = db->GetCachePath();
  134. AddProjectResourceDir(cachePath);
  135. BuildProjectResourceEntries();
  136. }
  137. void BuildIOS::RunConvertPList()
  138. {
  139. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  140. Vector<String> args = String("-convert binary1 ./AtomicPlayer.app/Info.plist").Split(' ');
  141. currentBuildPhase_ = ConvertPList;
  142. Subprocess* subprocess = subs->Launch("/usr/bin/plutil", args, buildPath_);
  143. if (!subprocess)
  144. {
  145. FailBuild("BuildFailed::RunConvertPList");
  146. return;
  147. }
  148. VariantMap buildOutput;
  149. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Starting iOS Deployment</color>\n\n";
  150. SendEvent(E_BUILDOUTPUT, buildOutput);
  151. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildIOS, HandleConvertPListComplete));
  152. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  153. }
  154. void BuildIOS::HandleConvertPListComplete(StringHash eventType, VariantMap& eventData)
  155. {
  156. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  157. if (!code)
  158. {
  159. RunCodeSign();
  160. }
  161. else
  162. {
  163. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  164. buildSystem->BuildComplete(PLATFORMID_IOS, buildPath_, false);
  165. }
  166. }
  167. void BuildIOS::RunCodeSign()
  168. {
  169. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  170. Vector<String> args;
  171. args.Push("--force");
  172. args.Push("--sign");
  173. args.Push("iPhone Developer");
  174. args.Push("--entitlements");
  175. args.Push("./AtomicPlayer.app.xcent");
  176. args.Push("./AtomicPlayer.app");
  177. Poco::Process::Env env;
  178. env["CODESIGN_ALLOCATE"] = "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate";
  179. currentBuildPhase_ = CodeSign;
  180. Subprocess* subprocess = subs->Launch("/usr/bin/codesign", args, buildPath_, env);
  181. if (!subprocess)
  182. {
  183. FailBuild("BuildFailed::RunCodeSign");
  184. return;
  185. }
  186. VariantMap buildOutput;
  187. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Code Signing iOS Deployment</color>\n\n";
  188. SendEvent(E_BUILDOUTPUT, buildOutput);
  189. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildIOS, HandleCodeSignComplete));
  190. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  191. }
  192. void BuildIOS::HandleCodeSignComplete(StringHash eventType, VariantMap& eventData)
  193. {
  194. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  195. if (!code)
  196. {
  197. RunDeploy();
  198. }
  199. else
  200. {
  201. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  202. buildSystem->BuildComplete(PLATFORMID_IOS, buildPath_, false);
  203. }
  204. }
  205. void BuildIOS::RunDeploy()
  206. {
  207. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  208. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  209. String iosDeploy = tenv->GetIOSDeployBinary();
  210. Vector<String> args;
  211. // https://github.com/phonegap/ios-deploy/issues/152
  212. // args.Push("--uninstall");
  213. args.Push("--bundle");
  214. args.Push("./AtomicPlayer.app");
  215. currentBuildPhase_ = Deploy;
  216. Subprocess* subprocess = subs->Launch(iosDeploy.CString(), args, buildPath_);
  217. if (!subprocess)
  218. {
  219. FailBuild("BuildFailed::RunDeploy");
  220. return;
  221. }
  222. VariantMap buildOutput;
  223. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Deploying to iOS Device</color>\n\n";
  224. SendEvent(E_BUILDOUTPUT, buildOutput);
  225. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildIOS, HandleDeployComplete));
  226. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  227. }
  228. void BuildIOS::HandleDeployComplete(StringHash eventType, VariantMap& eventData)
  229. {
  230. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  231. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  232. buildSystem->BuildComplete(PLATFORMID_IOS, buildPath_, true);
  233. }
  234. void BuildIOS::Build(const String& buildPath)
  235. {
  236. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  237. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  238. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  239. Project* project = tsystem->GetProject();
  240. ProjectBuildSettings* buildSettings = project->GetBuildSettings();
  241. IOSBuildSettings* iosSettings = buildSettings->GetIOSBuildSettings();
  242. buildPath_ = AddTrailingSlash(buildPath) + GetBuildSubfolder();
  243. Initialize();
  244. if (fileSystem->DirExists(buildPath_))
  245. fileSystem->RemoveDir(buildPath_, true);
  246. String buildSourceDir = tenv->GetToolDataDir();
  247. String buildAppSourceDir = buildSourceDir + "Deployment/IOS/AtomicPlayer.app";
  248. fileSystem->CreateDir(buildPath_);
  249. String buildDestDist = buildPath_ + "/AtomicPlayer.app";
  250. fileSystem->CreateDir(buildDestDist);
  251. String resourcePackagePath = buildDestDist + "/AtomicResources" + PAK_EXTENSION;
  252. GenerateResourcePackage(resourcePackagePath);
  253. fileSystem->Copy(buildAppSourceDir + "/AtomicPlayer", buildDestDist + "/AtomicPlayer");
  254. fileSystem->Copy(buildAppSourceDir + "/PkgInfo", buildDestDist + "/PkgInfo");
  255. fileSystem->Copy(iosSettings->GetProvisionFile(), buildDestDist + "/embedded.mobileprovision");
  256. String entitlements = GenerateEntitlements();
  257. String plist = GenerateInfoPlist();
  258. File file(context_, buildPath_ + "/AtomicPlayer.app.xcent", FILE_WRITE);
  259. if (file.IsOpen())
  260. {
  261. file.Write(entitlements.CString(), entitlements.Length());
  262. file.Close();
  263. }
  264. File pfile(context_, buildDestDist + "/Info.plist", FILE_WRITE);
  265. if (pfile.IsOpen())
  266. {
  267. pfile.Write(plist.CString(), plist.Length());
  268. pfile.Close();
  269. }
  270. RunConvertPList();
  271. }
  272. }