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 "../Subprocess/SubprocessSystem.h"
  30. #include "BuildIOS.h"
  31. #include "BuildEvents.h"
  32. #include "BuildSystem.h"
  33. namespace ToolCore
  34. {
  35. BuildIOS::BuildIOS(Context * context, Project *project) : BuildBase(context, project, PLATFORMID_IOS)
  36. {
  37. }
  38. BuildIOS::~BuildIOS()
  39. {
  40. }
  41. String BuildIOS::GenerateInfoPlist()
  42. {
  43. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  44. Project* project = tsystem->GetProject();
  45. ProjectBuildSettings* buildSettings = project->GetBuildSettings();
  46. IOSBuildSettings* iosSettings = buildSettings->GetIOSBuildSettings();
  47. String plist = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  48. plist += "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
  49. plist += "<plist version=\"1.0\">\n";
  50. plist += "<dict>\n";
  51. plist += "<key>CFBundleDevelopmentRegion</key>\n";
  52. plist += "<string>English</string>\n";
  53. plist += "<key>CFBundleExecutable</key>\n";
  54. plist += "<string>AtomicPlayer</string>\n";
  55. plist += "<key>CFBundleGetInfoString</key>\n";
  56. plist += "<string>\"Atomic Player\"</string>\n";
  57. plist += "<key>CFBundleIconFile</key>\n";
  58. plist += "<string></string>\n";
  59. plist += "<key>CFBundleIdentifier</key>\n";
  60. plist.AppendWithFormat("<string>%s</string>\n", iosSettings->GetPackageName().CString());
  61. plist += "<key>CFBundleInfoDictionaryVersion</key>\n";
  62. plist += "<string>6.0</string>\n";
  63. plist += "<key>CFBundleLongVersionString</key>\n";
  64. plist += "<string></string>\n";
  65. plist += "<key>CFBundleName</key>\n";
  66. plist += "<string></string>\n";
  67. plist += "<key>CFBundlePackageType</key>\n";
  68. plist += "<string>APPL</string>\n";
  69. plist += "<key>CFBundleShortVersionString</key>\n";
  70. plist += "<string></string>\n";
  71. plist += "<key>CFBundleSignature</key>\n";
  72. plist += "<string>????</string>\n";
  73. plist += "<key>CFBundleVersion</key>\n";
  74. plist += "<string></string>\n";
  75. plist += "<key>CSResourcesFileMapped</key>\n";
  76. plist += "<true/>\n";
  77. plist += "<key>LSRequiresCarbon</key>\n";
  78. plist += "<true/>\n";
  79. plist += "<key>NSHumanReadableCopyright</key>\n";
  80. plist += "<string>\"(c) Your Company\"</string>\n";
  81. plist += "<key>CFBundleIconFiles</key>\n";
  82. plist += "<array>\n";
  83. plist += "<string></string>\n";
  84. plist += "</array>\n";
  85. plist += "</dict>\n";
  86. plist += "</plist>\n";
  87. return plist;
  88. }
  89. String BuildIOS::GenerateEntitlements()
  90. {
  91. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  92. Project* project = tsystem->GetProject();
  93. ProjectBuildSettings* buildSettings = project->GetBuildSettings();
  94. IOSBuildSettings* iosSettings = buildSettings->GetIOSBuildSettings();
  95. String app_identifer = iosSettings->GetAppIDPrefix() + ".";
  96. app_identifer += iosSettings->GetPackageName();
  97. String team_identifier = iosSettings->GetAppIDPrefix();
  98. String keychain_access_group = app_identifer;
  99. String entitlements = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  100. entitlements += "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
  101. entitlements += "<plist version=\"1.0\">\n";
  102. entitlements += "<dict>\n";
  103. entitlements += "<key>application-identifier</key>\n";
  104. entitlements.AppendWithFormat("<string>%s</string>\n", app_identifer.CString());
  105. entitlements += "<key>com.apple.developer.team-identifier</key>\n";
  106. entitlements.AppendWithFormat("<string>%s</string>\n", team_identifier.CString());
  107. entitlements += "<key>get-task-allow</key>\n";
  108. entitlements += "<true/>\n";
  109. entitlements += "<key>keychain-access-groups</key>\n";
  110. entitlements += "<array>\n";
  111. entitlements.AppendWithFormat("<string>%s</string>\n", keychain_access_group.CString());
  112. entitlements += "</array>\n";
  113. entitlements += "</dict>\n";
  114. entitlements += "</plist>\n";
  115. return entitlements;
  116. }
  117. void BuildIOS::Initialize()
  118. {
  119. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  120. Project* project = tsystem->GetProject();
  121. Vector<String> defaultResourcePaths;
  122. GetDefaultResourcePaths(defaultResourcePaths);
  123. String projectResources = project->GetResourcePath();
  124. for (unsigned i = 0; i < defaultResourcePaths.Size(); i++)
  125. {
  126. AddResourceDir(defaultResourcePaths[i]);
  127. }
  128. // TODO: smart filtering of cache
  129. AddResourceDir(project->GetProjectPath() + "Cache/");
  130. AddResourceDir(projectResources);
  131. BuildResourceEntries();
  132. }
  133. void BuildIOS::RunConvertPList()
  134. {
  135. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  136. Vector<String> args = String("-convert binary1 ./AtomicPlayer.app/Info.plist").Split(' ');
  137. currentBuildPhase_ = ConvertPList;
  138. Subprocess* subprocess = subs->Launch("/usr/bin/plutil", args, buildPath_);
  139. if (!subprocess)
  140. {
  141. FailBuild("BuildFailed::RunConvertPList");
  142. return;
  143. }
  144. VariantMap buildOutput;
  145. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Starting iOS Deployment</color>\n\n";
  146. SendEvent(E_BUILDOUTPUT, buildOutput);
  147. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildIOS, HandleConvertPListComplete));
  148. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  149. }
  150. void BuildIOS::HandleConvertPListComplete(StringHash eventType, VariantMap& eventData)
  151. {
  152. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  153. if (!code)
  154. {
  155. RunCodeSign();
  156. }
  157. else
  158. {
  159. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  160. buildSystem->BuildComplete(PLATFORMID_IOS, buildPath_, false);
  161. }
  162. }
  163. void BuildIOS::RunCodeSign()
  164. {
  165. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  166. Vector<String> args;
  167. args.Push("--force");
  168. args.Push("--sign");
  169. args.Push("iPhone Developer");
  170. args.Push("--entitlements");
  171. args.Push("./AtomicPlayer.app.xcent");
  172. args.Push("./AtomicPlayer.app");
  173. Poco::Process::Env env;
  174. env["CODESIGN_ALLOCATE"] = "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate";
  175. currentBuildPhase_ = CodeSign;
  176. Subprocess* subprocess = subs->Launch("/usr/bin/codesign", args, buildPath_, env);
  177. if (!subprocess)
  178. {
  179. FailBuild("BuildFailed::RunCodeSign");
  180. return;
  181. }
  182. VariantMap buildOutput;
  183. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Code Signing iOS Deployment</color>\n\n";
  184. SendEvent(E_BUILDOUTPUT, buildOutput);
  185. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildIOS, HandleCodeSignComplete));
  186. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  187. }
  188. void BuildIOS::HandleCodeSignComplete(StringHash eventType, VariantMap& eventData)
  189. {
  190. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  191. if (!code)
  192. {
  193. RunDeploy();
  194. }
  195. else
  196. {
  197. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  198. buildSystem->BuildComplete(PLATFORMID_IOS, buildPath_, false);
  199. }
  200. }
  201. void BuildIOS::RunDeploy()
  202. {
  203. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  204. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  205. String iosDeploy = tenv->GetIOSDeployBinary();
  206. Vector<String> args;
  207. // https://github.com/phonegap/ios-deploy/issues/152
  208. // args.Push("--uninstall");
  209. args.Push("--bundle");
  210. args.Push("./AtomicPlayer.app");
  211. currentBuildPhase_ = Deploy;
  212. Subprocess* subprocess = subs->Launch(iosDeploy.CString(), args, buildPath_);
  213. if (!subprocess)
  214. {
  215. FailBuild("BuildFailed::RunDeploy");
  216. return;
  217. }
  218. VariantMap buildOutput;
  219. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Deploying to iOS Device</color>\n\n";
  220. SendEvent(E_BUILDOUTPUT, buildOutput);
  221. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildIOS, HandleDeployComplete));
  222. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  223. }
  224. void BuildIOS::HandleDeployComplete(StringHash eventType, VariantMap& eventData)
  225. {
  226. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  227. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  228. buildSystem->BuildComplete(PLATFORMID_IOS, buildPath_, true);
  229. }
  230. void BuildIOS::Build(const String& buildPath)
  231. {
  232. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  233. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  234. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  235. Project* project = tsystem->GetProject();
  236. ProjectBuildSettings* buildSettings = project->GetBuildSettings();
  237. IOSBuildSettings* iosSettings = buildSettings->GetIOSBuildSettings();
  238. buildPath_ = AddTrailingSlash(buildPath) + GetBuildSubfolder();
  239. Initialize();
  240. if (!BuildClean(buildPath_))
  241. return;
  242. String buildSourceDir = tenv->GetToolDataDir();
  243. String buildAppSourceDir = buildSourceDir + "Deployment/IOS/AtomicPlayer.app";
  244. if (!BuildCreateDirectory(buildPath_))
  245. return;
  246. String buildDestDist = buildPath_ + "/AtomicPlayer.app";
  247. if (!BuildCreateDirectory(buildDestDist))
  248. return;
  249. String resourcePackagePath = buildDestDist + "/AtomicResources" + PAK_EXTENSION;
  250. GenerateResourcePackage(resourcePackagePath);
  251. if (!BuildCopyFile(buildAppSourceDir + "/AtomicPlayer", buildDestDist + "/AtomicPlayer"))
  252. return;
  253. if (!BuildCopyFile(buildAppSourceDir + "/PkgInfo", buildDestDist + "/PkgInfo"))
  254. return;
  255. if (!BuildCopyFile(iosSettings->GetProvisionFile(), buildDestDist + "/embedded.mobileprovision"))
  256. return;
  257. String entitlements = GenerateEntitlements();
  258. String plist = GenerateInfoPlist();
  259. File file(context_, buildPath_ + "/AtomicPlayer.app.xcent", FILE_WRITE);
  260. if (file.IsOpen())
  261. {
  262. file.Write(entitlements.CString(), entitlements.Length());
  263. file.Close();
  264. }
  265. File pfile(context_, buildDestDist + "/Info.plist", FILE_WRITE);
  266. if (pfile.IsOpen())
  267. {
  268. pfile.Write(plist.CString(), plist.Length());
  269. pfile.Close();
  270. }
  271. RunConvertPList();
  272. }
  273. }