BuildAndroid.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include <Atomic/Core/StringUtils.h>
  5. #include <Atomic/IO/FileSystem.h>
  6. #include <Atomic/IO/File.h>
  7. #include "../ToolSystem.h"
  8. #include "../ToolEnvironment.h"
  9. #include "../Subprocess/SubprocessSystem.h"
  10. #include "../Project/Project.h"
  11. #include "AndroidProjectGenerator.h"
  12. #include "BuildSystem.h"
  13. #include "BuildEvents.h"
  14. #include "BuildAndroid.h"
  15. namespace ToolCore
  16. {
  17. BuildAndroid::BuildAndroid(Context* context, Project* project) : BuildBase(context, project)
  18. {
  19. }
  20. BuildAndroid::~BuildAndroid()
  21. {
  22. }
  23. void BuildAndroid::SendBuildFailure(const String& message)
  24. {
  25. VariantMap buildError;
  26. buildError[BuildFailed::P_PLATFORMID] = PLATFORMID_ANDROID;
  27. buildError[BuildFailed::P_MESSAGE] = message;
  28. SendEvent(E_BUILDFAILED, buildError);
  29. }
  30. void BuildAndroid::RunAntDebug()
  31. {
  32. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  33. SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
  34. ToolPrefs* tprefs = tenv->GetToolPrefs();
  35. Poco::Process::Env env;
  36. #ifdef ATOMIC_PLATFORM_OSX
  37. String antCommand = tprefs->GetAntPath();
  38. Vector<String> args;
  39. args.Push("debug");
  40. #else
  41. // C:\ProgramData\Oracle\Java\javapath;
  42. Vector<String> args;
  43. String antCommand = "cmd";
  44. String antPath = prefs->GetAntPath() + "/ant.bat";
  45. env["JAVA_HOME"] = prefs->GetJDKRootPath().CString();
  46. // ant is a batch file on windows, so have to run with cmd /c
  47. args.Push("/c");
  48. args.Push("\"" + antPath + "\"");
  49. args.Push("debug");
  50. #endif
  51. currentBuildPhase_ = AntBuildDebug;
  52. Subprocess* subprocess = subs->Launch(antCommand, args, buildPath_, env);
  53. if (!subprocess)
  54. {
  55. SendBuildFailure("BuildFailed::RunAntDebug");
  56. return;
  57. }
  58. VariantMap buildOutput;
  59. buildOutput[BuildOutput::P_TEXT] = "<color #D4FB79>Starting Android Deployment</color>\n\n";
  60. SendEvent(E_BUILDOUTPUT, buildOutput);
  61. //SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleEvent));
  62. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
  63. }
  64. void BuildAndroid::Initialize()
  65. {
  66. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  67. Project* project = tsystem->GetProject();
  68. Vector<String> defaultResourcePaths;
  69. GetDefaultResourcePaths(defaultResourcePaths);
  70. String projectResources = project->GetResourcePath();
  71. for (unsigned i = 0; i < defaultResourcePaths.Size(); i++)
  72. {
  73. AddResourceDir(defaultResourcePaths[i]);
  74. }
  75. // TODO: smart filtering of cache
  76. AddResourceDir(project->GetProjectPath() + "Cache/");
  77. AddResourceDir(projectResources);
  78. BuildResourceEntries();
  79. }
  80. void BuildAndroid::Build(const String& buildPath)
  81. {
  82. ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
  83. ToolSystem* tsystem = GetSubsystem<ToolSystem>();
  84. Project* project = tsystem->GetProject();
  85. buildPath_ = AddTrailingSlash(buildPath) + GetBuildSubfolder();
  86. Initialize();
  87. //generate manifest file
  88. String manifest;
  89. for (unsigned i = 0; i < resourceEntries_.Size(); i++)
  90. {
  91. BuildResourceEntry* entry = resourceEntries_[i];
  92. manifest += entry->packagePath_;
  93. if ( i != resourceEntries_.Size() - 1)
  94. manifest += ";";
  95. }
  96. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  97. if (fileSystem->DirExists(buildPath_))
  98. fileSystem->RemoveDir(buildPath_, true);
  99. String buildSourceDir = tenv->GetToolDataDir();
  100. String androidProject = buildSourceDir + "Deployment/Android";
  101. // Copy the base android project
  102. fileSystem->CopyDir(androidProject, buildPath_);
  103. Vector<String> defaultResourcePaths;
  104. GetDefaultResourcePaths(defaultResourcePaths);
  105. String projectResources = project->GetResourcePath();
  106. for (unsigned i = 0; i < defaultResourcePaths.Size(); i++)
  107. {
  108. fileSystem->CopyDir(defaultResourcePaths[i], buildPath_ + "/assets/" + GetFileName(RemoveTrailingSlash(defaultResourcePaths[i])));
  109. }
  110. fileSystem->CopyDir(project->GetProjectPath() + "Cache/", buildPath_ + "/assets/Cache");
  111. fileSystem->CopyDir(projectResources, buildPath_ + "/assets/AtomicResources");
  112. // write the manifest
  113. SharedPtr<File> mfile(new File(context_, buildPath_ + "/assets/AtomicManifest", FILE_WRITE));
  114. mfile->WriteString(manifest);
  115. mfile->Close();
  116. AndroidProjectGenerator gen(context_);
  117. gen.SetBuildPath(buildPath_);
  118. if (!gen.Generate())
  119. {
  120. SendBuildFailure(gen.GetErrorText());
  121. return;
  122. }
  123. RunAntDebug();
  124. //BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  125. //buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_);
  126. //fileSystem->SystemCommandAsync("/Applications/Firefox.app/Contents/MacOS/firefox");
  127. }
  128. }