| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- //
- // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include <Atomic/Core/StringUtils.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/IO/File.h>
- #include <Atomic/IO/MemoryBuffer.h>
- #include "../ToolSystem.h"
- #include "../ToolEnvironment.h"
- #include "../Subprocess/SubprocessSystem.h"
- #include "../Project/Project.h"
- #include "../Project/ProjectBuildSettings.h"
- #include "../Platform/PlatformAndroid.h"
- #include "AndroidProjectGenerator.h"
- #include "BuildSystem.h"
- #include "BuildEvents.h"
- #include "BuildAndroid.h"
- namespace ToolCore
- {
- BuildAndroid::BuildAndroid(Context* context, Project* project) : BuildBase(context, project, PLATFORMID_ANDROID)
- {
- ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
- // this cast isn't great
- platformAndroid_ = (PlatformAndroid*) toolSystem->GetPlatformByID(PLATFORMID_ANDROID);
- }
- BuildAndroid::~BuildAndroid()
- {
- }
- void BuildAndroid::HandleADBStartActivityComplete(StringHash eventType, VariantMap& eventData)
- {
- }
- // adb shell am start -n com.package.name/com.package.name.ActivityName
- void BuildAndroid::RunADBStartActivity()
- {
- SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
- String adbCommand = platformAndroid_->GetADBCommand();
- ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
- Project* project = toolSystem->GetProject();
- AndroidBuildSettings* settings = project->GetBuildSettings()->GetAndroidBuildSettings();
- String stringArgs;
- const char* cpackage = settings->GetPackageName().CString();
- stringArgs.AppendWithFormat("shell am start -n %s/%s.AtomicGameEngine",cpackage, cpackage);
- Vector<String> args = stringArgs.Split(' ');
- currentBuildPhase_ = ADBStartActivity;
- Subprocess* subprocess = subs->Launch(adbCommand, args, buildPath_);
- if (!subprocess)
- {
- FailBuild("StartActivity operation did not launch successfully.");
- return;
- }
- VariantMap buildOutput;
- buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Starting Android Activity</color>\n\n";
- SendEvent(E_BUILDOUTPUT, buildOutput);
- SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleADBStartActivityComplete));
- SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
- }
- void BuildAndroid::HandleRunADBInstallComplete(StringHash eventType, VariantMap& eventData)
- {
- int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
- if (!code)
- {
- RunADBStartActivity();
- }
- else
- {
- BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
- buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_, false);
- }
- }
- void BuildAndroid::RunADBInstall()
- {
- ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
- ToolPrefs* prefs = tenv->GetToolPrefs();
- SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
- String adbCommand = platformAndroid_->GetADBCommand();
- Vector<String> args;
- if ( prefs->GetReleaseCheck() > 0 ) // install release apk
- args = String("install -r ./bin/Atomic-release.apk").Split(' ');
- else
- args = String("install -r ./bin/Atomic-debug-unaligned.apk").Split(' ');
- currentBuildPhase_ = ADBInstall;
- Subprocess* subprocess = subs->Launch(adbCommand, args, buildPath_);
- if (!subprocess)
- {
- FailBuild("APK Device Installation operation did not launch successfully.");
- return;
- }
- VariantMap buildOutput;
- buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Installing on Android Device</color>\n\n";
- SendEvent(E_BUILDOUTPUT, buildOutput);
- SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleRunADBInstallComplete));
- SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
- }
- void BuildAndroid::HandleADBListDevicesComplete(StringHash eventType, VariantMap& eventData)
- {
- BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
- int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
- if (!code)
- {
- // check if we have any devices attached, otherwise adb install
- // will hang looking for devices
- bool noDevices = true;
- if (deviceListText_.Length())
- {
- MemoryBuffer reader(deviceListText_.CString(), deviceListText_.Length() + 1);
- while (!reader.IsEof())
- {
- String line = reader.ReadLine();
- if (line.Length() && line[0] >= '0' && line[0] <= '9')
- {
- noDevices = false;
- break;
- }
- }
- }
- if (!noDevices)
- RunADBInstall();
- else
- {
- // can't proceed, though success
- buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_);
- }
- }
- else
- {
- buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_, false);
- }
- }
- void BuildAndroid::HandleADBListDevicesOutputEvent(StringHash eventType, VariantMap& eventData)
- {
- // E_SUBPROCESSOUTPUT
- const String& text = eventData[SubprocessOutput::P_TEXT].GetString();
- deviceListText_ += text;
- // convert to a build output event and forward to subscribers
- VariantMap buildOutputData;
- buildOutputData[BuildOutput::P_TEXT] = text;
- SendEvent(E_BUILDOUTPUT, buildOutputData);
- }
- void BuildAndroid::RunADBListDevices()
- {
- ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
- SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
- String adbCommand = platformAndroid_->GetADBCommand();
- deviceListText_.Clear();
- Vector<String> args = String("devices").Split(' ');
- currentBuildPhase_ = ADBListDevices;
- Subprocess* subprocess = subs->Launch(adbCommand, args, "");
- if (!subprocess)
- {
- FailBuild("Android List Device operation did not launch successfully.");
- return;
- }
- VariantMap buildOutput;
- buildOutput[BuildOutput::P_TEXT] = "\n\n<color #D4FB79>Listing Android Devices</color>\n\n";
- SendEvent(E_BUILDOUTPUT, buildOutput);
- SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleADBListDevicesComplete));
- SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildAndroid, HandleADBListDevicesOutputEvent));
- }
- void BuildAndroid::HandleAntDebugComplete(StringHash eventType, VariantMap& eventData)
- {
- int code = eventData[SubprocessComplete::P_RETCODE].GetInt();
- if (!code)
- {
- RunADBListDevices();
- }
- else
- {
- BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
- buildSystem->BuildComplete(PLATFORMID_ANDROID, buildPath_, false);
- }
- }
- void BuildAndroid::RunAntDebug()
- {
- ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
- SubprocessSystem* subs = GetSubsystem<SubprocessSystem>();
- ToolPrefs* tprefs = tenv->GetToolPrefs();
- Poco::Process::Env env;
- String buildApk = "debug"; // the default
- if ( tprefs->GetReleaseCheck() > 0 ) // create release apk
- buildApk = "release";
- #ifdef ATOMIC_PLATFORM_OSX
- String antCommand = tprefs->GetAntPath();
- Vector<String> args;
- args.Push(buildApk);
- #endif
- #ifdef ATOMIC_PLATFORM_WINDOWS
- // C:\ProgramData\Oracle\Java\javapath;
- Vector<String> args;
- String antCommand = "cmd";
- String antPath = tprefs->GetAntPath() + "/ant.bat";
- env["JAVA_HOME"] = tprefs->GetJDKRootPath().CString();
- // ant is a batch file on windows, so have to run with cmd /c
- args.Push("/c");
- args.Push("\"" + antPath + "\"");
- args.Push(buildApk);
- #endif
- #ifdef ATOMIC_PLATFORM_LINUX
- String antCommand = tprefs->GetAntPath();
- if ( antCommand.Empty() ) // user didnt fill it out, use installed one
- {
- antCommand = "/usr/bin/ant"; // system default if installed
- }
- else
- {
- antCommand.Append("/ant");
- }
- FileSystem* fileSystem = GetSubsystem<FileSystem>();
- if ( !fileSystem->FileExists ( antCommand) )
- {
- FailBuild("The ant program can not be found, check the Ant Path in Build Settings.");
- }
- Vector<String> args;
- args.Push(buildApk);
- #endif
- currentBuildPhase_ = AntBuildDebug;
- Subprocess* subprocess = subs->Launch(antCommand, args, buildPath_, env);
- if (!subprocess)
- {
- FailBuild("The ant build operation did not launch successfully.");
- return;
- }
- VariantMap buildOutput;
- buildOutput[BuildOutput::P_TEXT] = "<color #D4FB79>Starting Android " + buildApk + " Deployment</color>\n\n";
- SendEvent(E_BUILDOUTPUT, buildOutput);
- SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(BuildAndroid, HandleAntDebugComplete));
- SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(BuildBase, HandleSubprocessOutputEvent));
- }
- void BuildAndroid::Initialize()
- {
- ToolSystem* tsystem = GetSubsystem<ToolSystem>();
- Project* project = tsystem->GetProject();
- Vector<String> defaultResourcePaths;
- GetDefaultResourcePaths(defaultResourcePaths);
- String projectResources = project->GetResourcePath();
- for (unsigned i = 0; i < defaultResourcePaths.Size(); i++)
- {
- AddResourceDir(defaultResourcePaths[i]);
- }
- // TODO: smart filtering of cache
- AddResourceDir(project->GetProjectPath() + "Cache/");
- AddResourceDir(projectResources);
- BuildResourceEntries();
- }
- void BuildAndroid::Build(const String& buildPath)
- {
- ToolEnvironment* tenv = GetSubsystem<ToolEnvironment>();
- ToolSystem* tsystem = GetSubsystem<ToolSystem>();
- Project* project = tsystem->GetProject();
- buildPath_ = AddTrailingSlash(buildPath) + GetBuildSubfolder();
- Initialize();
-
- if (!BuildClean(buildPath_))
- return;
- //generate manifest file
- String manifest;
- for (unsigned i = 0; i < resourceEntries_.Size(); i++)
- {
- BuildResourceEntry* entry = resourceEntries_[i];
- manifest += entry->packagePath_;
- if ( i != resourceEntries_.Size() - 1)
- manifest += ";";
- }
- String buildSourceDir = tenv->GetToolDataDir();
- String androidProject = buildSourceDir + "Deployment/Android";
- // Copy the base android project
- FileSystem* fileSystem = GetSubsystem<FileSystem>();
- if( !BuildCopyDir(androidProject, buildPath_))
- return;
- Vector<String> defaultResourcePaths;
- GetDefaultResourcePaths(defaultResourcePaths);
- String projectResources = project->GetResourcePath();
- for (unsigned i = 0; i < defaultResourcePaths.Size(); i++)
- {
- if ( !BuildCopyDir(defaultResourcePaths[i], buildPath_ + "/assets/" + GetFileName(RemoveTrailingSlash(defaultResourcePaths[i]))))
- return;
- }
- if( !BuildCopyDir(project->GetProjectPath() + "Cache/", buildPath_ + "/assets/Cache"))
- return;
- if( !BuildCopyDir(projectResources, buildPath_ + "/assets/AtomicResources"))
- return;
- // write the manifest
- SharedPtr<File> mfile(new File(context_, buildPath_ + "/assets/AtomicManifest", FILE_WRITE));
- mfile->WriteString(manifest);
- mfile->Close();
- //check for Deployment/Android/libs/armeabi-v7a/libAtomicPlayer.so
- if ( !fileSystem->FileExists(androidProject + "/libs/armeabi-v7a/libAtomicPlayer.so") )
- {
- FailBuild( "The file libAtomicPlayer.so is not found. This is required for APK generation." );
- return;
- }
- AndroidProjectGenerator gen(context_, this);
- gen.SetBuildPath(buildPath_);
- if (!gen.Generate())
- {
- FailBuild(gen.GetErrorText());
- return;
- }
- RunAntDebug();
- }
- }
|