BuildAndroid.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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/Core/StringUtils.h>
  23. #include <Atomic/IO/FileSystem.h>
  24. #include <Atomic/IO/File.h>
  25. #include <Atomic/IO/MemoryBuffer.h>
  26. #include "../ToolSystem.h"
  27. #include "../ToolEnvironment.h"
  28. #include "../Subprocess/SubprocessSystem.h"
  29. #include "../Project/Project.h"
  30. #include "../Project/ProjectBuildSettings.h"
  31. #include "../Platform/PlatformAndroid.h"
  32. #include "AndroidProjectGenerator.h"
  33. #include "BuildSystem.h"
  34. #include "BuildEvents.h"
  35. #include "BuildAndroid.h"
  36. namespace ToolCore
  37. {
  38. BuildAndroid::BuildAndroid(Context* context, Project* project) : BuildBase(context, project, PLATFORMID_ANDROID)
  39. {
  40. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  41. // this cast isn't great
  42. platformAndroid_ = (PlatformAndroid*) toolSystem->GetPlatformByID(PLATFORMID_ANDROID);
  43. }
  44. BuildAndroid::~BuildAndroid()
  45. {
  46. }
  47. void BuildAndroid::HandleADBStartActivityComplete(StringHash eventType, VariantMap& eventData)
  48. {
  49. }
  50. // adb shell am start -n com.package.name/com.package.name.ActivityName
  51. void BuildAndroid::RunADBStartActivity()
  52. {
  53. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  54. String adbCommand = platformAndroid_->GetADBCommand();
  55. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  56. Project* project = toolSystem->GetProject();
  57. AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
  58. String stringArgs;
  59. const char* cpackage = settings->GetPackageName().CString();
  60. stringArgs.AppendWithFormat("shell am start -n %s/%s.AtomicGameEngine",cpackage, cpackage);
  61. Vector<String> args = stringArgs.Split(' ');
  62. currentBuildPhase_ = ADBStartActivity;
  63. Subprocess* subprocess = subs->Launch(adbCommand, args, buildPath_);
  64. if (!subprocess)
  65. {
  66. FailBuild("BuildFailed::RunStartActivity");
  67. return;
  68. }
  69. VariantMap buildOutput;
  70. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Starting Android Activity</color>\n\n";
  71. SendEvent(E_BUILDOUTPUT, buildOutput);
  72. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleADBStartActivityComplete));
  73. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  74. }
  75. void BuildAndroid::HandleRunADBInstallComplete(StringHash eventType, VariantMap& eventData)
  76. {
  77. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  78. if (!code)
  79. {
  80. RunADBStartActivity();
  81. }
  82. else
  83. {
  84. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  85. buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_, false);
  86. }
  87. }
  88. void BuildAndroid::RunADBInstall()
  89. {
  90. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  91. String adbCommand = platformAndroid_->GetADBCommand();
  92. Vector<String> args = String("install -r ./bin/Atomic-debug-unaligned.apk").Split(' ');
  93. currentBuildPhase_ = ADBInstall;
  94. Subprocess* subprocess = subs->Launch(adbCommand, args, buildPath_);
  95. if (!subprocess)
  96. {
  97. FailBuild("BuildFailed::RunADBInstall");
  98. return;
  99. }
  100. VariantMap buildOutput;
  101. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Installing on Android Device</color>\n\n";
  102. SendEvent(E_BUILDOUTPUT, buildOutput);
  103. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleRunADBInstallComplete));
  104. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  105. }
  106. void BuildAndroid::HandleADBListDevicesComplete(StringHash eventType, VariantMap& eventData)
  107. {
  108. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  109. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  110. if (!code)
  111. {
  112. // check if we have any devices attached, otherwise adb install
  113. // will hang looking for devices
  114. bool noDevices = true;
  115. if (deviceListText_.Length())
  116. {
  117. MemoryBuffer reader(deviceListText_.CString(), deviceListText_.Length() + 1);
  118. while (!reader.IsEof())
  119. {
  120. String line = reader.ReadLine();
  121. if (line.Length() && line[0] >= '0' && line[0] <= '9')
  122. {
  123. noDevices = false;
  124. break;
  125. }
  126. }
  127. }
  128. if (!noDevices)
  129. RunADBInstall();
  130. else
  131. {
  132. // can't proceed, though success
  133. buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_);
  134. }
  135. }
  136. else
  137. {
  138. buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_, false);
  139. }
  140. }
  141. void BuildAndroid::HandleADBListDevicesOutputEvent(StringHash eventType, VariantMap& eventData)
  142. {
  143. // E_SUBPROCESSOUTPUT
  144. const String& text = eventData[SubprocessOutput::P_TEXT].GetString();
  145. deviceListText_ += text;
  146. // convert to a build output event and forward to subscribers
  147. VariantMap buildOutputData;
  148. buildOutputData[BuildOutput::P_TEXT] = text;
  149. SendEvent(E_BUILDOUTPUT, buildOutputData);
  150. }
  151. void BuildAndroid::RunADBListDevices()
  152. {
  153. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  154. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  155. String adbCommand = platformAndroid_->GetADBCommand();
  156. deviceListText_.Clear();
  157. Vector<String> args = String("devices").Split(' ');
  158. currentBuildPhase_ = ADBListDevices;
  159. Subprocess* subprocess = subs->Launch(adbCommand, args, "");
  160. if (!subprocess)
  161. {
  162. FailBuild("BuildFailed::RunADBListDevices");
  163. return;
  164. }
  165. VariantMap buildOutput;
  166. buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Listing Android Devices</color>\n\n";
  167. SendEvent(E_BUILDOUTPUT, buildOutput);
  168. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleADBListDevicesComplete));
  169. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildAndroid, HandleADBListDevicesOutputEvent));
  170. }
  171. void BuildAndroid::HandleAntDebugComplete(StringHash eventType, VariantMap& eventData)
  172. {
  173. int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
  174. if (!code)
  175. {
  176. RunADBListDevices();
  177. }
  178. else
  179. {
  180. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  181. buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_, false);
  182. }
  183. }
  184. void BuildAndroid::RunAntDebug()
  185. {
  186. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  187. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  188. ToolPrefs* tprefs = tenv->GetToolPrefs();
  189. Poco::Process::Env env;
  190. #ifdef ATOMIC_PLATFORM_OSX
  191. String antCommand = tprefs->GetAntPath();
  192. Vector<String> args;
  193. args.Push("debug");
  194. #endif
  195. #ifdef ATOMIC_PLATFORM_WINDOWS
  196. // C:\ProgramData\Oracle\Java\javapath;
  197. Vector<String> args;
  198. String antCommand = "cmd";
  199. String antPath = tprefs->GetAntPath() + "/ant.bat";
  200. env["JAVA_HOME"] = tprefs->GetJDKRootPath().CString();
  201. // ant is a batch file on windows, so have to run with cmd /c
  202. args.Push("/c");
  203. args.Push("\"" + antPath + "\"");
  204. args.Push("debug");
  205. #endif
  206. #ifdef ATOMIC_PLATFORM_LINUX
  207. String antCommand = tprefs->GetAntPath();
  208. if ( antCommand.Empty() ) // user didnt fill it out, use installed one
  209. {
  210. antCommand = "/usr/bin/ant"; // system default if installed
  211. }
  212. else
  213. {
  214. antCommand.Append("/ant");
  215. }
  216. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  217. if ( !fileSystem->FileExists ( antCommand) )
  218. {
  219. FailBuild("BuildFailed ant program not installed");
  220. }
  221. Vector<String> args;
  222. args.Push("debug");
  223. #endif
  224. currentBuildPhase_ = AntBuildDebug;
  225. Subprocess* subprocess = subs->Launch(antCommand, args, buildPath_, env);
  226. if (!subprocess)
  227. {
  228. FailBuild("BuildFailed::RunAntDebug");
  229. return;
  230. }
  231. VariantMap buildOutput;
  232. buildOutput[BuildOutput::P_TEXT] = "<color #D4FB79>Starting Android Deployment</color>\n\n";
  233. SendEvent(E_BUILDOUTPUT, buildOutput);
  234. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleAntDebugComplete));
  235. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  236. }
  237. void BuildAndroid::Initialize()
  238. {
  239. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  240. Project* project = tsystem->GetProject();
  241. Vector<String> defaultResourcePaths;
  242. GetDefaultResourcePaths(defaultResourcePaths);
  243. String projectResources = project->GetResourcePath();
  244. for (unsigned i = 0; i < defaultResourcePaths.Size(); i++)
  245. {
  246. AddResourceDir(defaultResourcePaths[i]);
  247. }
  248. // TODO: smart filtering of cache
  249. AddResourceDir(project->GetProjectPath() + "Cache/");
  250. AddResourceDir(projectResources);
  251. BuildResourceEntries();
  252. }
  253. void BuildAndroid::Build(const String& buildPath)
  254. {
  255. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  256. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  257. Project* project = tsystem->GetProject();
  258. buildPath_ = AddTrailingSlash(buildPath) + GetBuildSubfolder();
  259. Initialize();
  260. //generate manifest file
  261. String manifest;
  262. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  263. {
  264. BuildResourceEntry* entry = resourceEntries_[i];
  265. manifest += entry->packagePath_;
  266. if ( i != resourceEntries_.Size() - 1)
  267. manifest += ";";
  268. }
  269. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  270. if (fileSystem->DirExists(buildPath_))
  271. fileSystem->RemoveDir(buildPath_, true);
  272. String buildSourceDir = tenv->GetToolDataDir();
  273. String androidProject = buildSourceDir + "Deployment/Android";
  274. // Copy the base android project
  275. fileSystem->CopyDir(androidProject, buildPath_);
  276. Vector<String> defaultResourcePaths;
  277. GetDefaultResourcePaths(defaultResourcePaths);
  278. String projectResources = project->GetResourcePath();
  279. for (unsigned i = 0; i < defaultResourcePaths.Size(); i++)
  280. {
  281. fileSystem->CopyDir(defaultResourcePaths[i], buildPath_ + "/assets/" + GetFileName(RemoveTrailingSlash(defaultResourcePaths[i])));
  282. }
  283. fileSystem->CopyDir(project->GetProjectPath() + "Cache/", buildPath_ + "/assets/Cache");
  284. fileSystem->CopyDir(projectResources, buildPath_ + "/assets/AtomicResources");
  285. // write the manifest
  286. SharedPtr<File> mfile(new File(context_, buildPath_ + "/assets/AtomicManifest", FILE_WRITE));
  287. mfile->WriteString(manifest);
  288. mfile->Close();
  289. AndroidProjectGenerator gen(context_);
  290. gen.SetBuildPath(buildPath_);
  291. if (!gen.Generate())
  292. {
  293. FailBuild(gen.GetErrorText());
  294. return;
  295. }
  296. RunAntDebug();
  297. }
  298. }