ProjectBuilderWorker.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <ProjectBuilderWorker.h>
  9. #include <ProjectManagerDefs.h>
  10. #include <PythonBindingsInterface.h>
  11. #include <ProjectUtils.h>
  12. #include <QDir>
  13. #include <QFile>
  14. #include <QProcess>
  15. #include <QProcessEnvironment>
  16. #include <QTextStream>
  17. #include <QThread>
  18. namespace O3DE::ProjectManager
  19. {
  20. ProjectBuilderWorker::ProjectBuilderWorker(const ProjectInfo& projectInfo)
  21. : QObject()
  22. , m_projectInfo(projectInfo)
  23. {
  24. }
  25. void ProjectBuilderWorker::BuildProject()
  26. {
  27. auto result = BuildProjectForPlatform();
  28. if (result.IsSuccess())
  29. {
  30. emit Done();
  31. }
  32. else
  33. {
  34. emit Done(result.GetError());
  35. }
  36. }
  37. QString ProjectBuilderWorker::GetLogFilePath() const
  38. {
  39. QDir logFilePath(m_projectInfo.m_path);
  40. // Make directories if they aren't on disk
  41. if (!logFilePath.cd(ProjectBuildPathPostfix))
  42. {
  43. logFilePath.mkpath(ProjectBuildPathPostfix);
  44. logFilePath.cd(ProjectBuildPathPostfix);
  45. }
  46. if (!logFilePath.cd(ProjectBuildPathCmakeFiles))
  47. {
  48. logFilePath.mkpath(ProjectBuildPathCmakeFiles);
  49. logFilePath.cd(ProjectBuildPathCmakeFiles);
  50. }
  51. return logFilePath.filePath(ProjectBuildErrorLogName);
  52. }
  53. void ProjectBuilderWorker::QStringToAZTracePrint([[maybe_unused]] const QString& error)
  54. {
  55. AZ_TracePrintf("Project Manager", error.toStdString().c_str());
  56. }
  57. AZ::Outcome<void, QString> ProjectBuilderWorker::BuildProjectForPlatform()
  58. {
  59. // Check if we are trying to cancel task
  60. if (QThread::currentThread()->isInterruptionRequested())
  61. {
  62. QStringToAZTracePrint(BuildCancelled);
  63. return AZ::Failure(BuildCancelled);
  64. }
  65. QFile logFile(GetLogFilePath());
  66. if (!logFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
  67. {
  68. QString error = tr("Failed to open log file.");
  69. QStringToAZTracePrint(error);
  70. return AZ::Failure(error);
  71. }
  72. EngineInfo engineInfo;
  73. AZ::Outcome<EngineInfo> engineInfoResult = PythonBindingsInterface::Get()->GetEngineInfo();
  74. if (engineInfoResult.IsSuccess())
  75. {
  76. engineInfo = engineInfoResult.GetValue();
  77. }
  78. else
  79. {
  80. QString error = tr("Failed to get engine info.");
  81. QStringToAZTracePrint(error);
  82. return AZ::Failure(error);
  83. }
  84. QTextStream logStream(&logFile);
  85. if (QThread::currentThread()->isInterruptionRequested())
  86. {
  87. logFile.close();
  88. QStringToAZTracePrint(BuildCancelled);
  89. return AZ::Failure(BuildCancelled);
  90. }
  91. UpdateProgress(tr("Setting Up Environment"));
  92. auto currentEnvironmentRequest = ProjectUtils::SetupCommandLineProcessEnvironment();
  93. if (!currentEnvironmentRequest.IsSuccess())
  94. {
  95. QStringToAZTracePrint(currentEnvironmentRequest.GetError());
  96. return AZ::Failure(currentEnvironmentRequest.GetError());
  97. }
  98. m_configProjectProcess = new QProcess(this);
  99. m_configProjectProcess->setProcessChannelMode(QProcess::MergedChannels);
  100. m_configProjectProcess->setWorkingDirectory(m_projectInfo.m_path);
  101. auto cmakeGenerateArgumentsResult = ConstructCmakeGenerateProjectArguments(engineInfo.m_thirdPartyPath);
  102. if (!cmakeGenerateArgumentsResult.IsSuccess())
  103. {
  104. QStringToAZTracePrint(cmakeGenerateArgumentsResult.GetError());
  105. return AZ::Failure(cmakeGenerateArgumentsResult.GetError());
  106. }
  107. auto cmakeGenerateArguments = cmakeGenerateArgumentsResult.GetValue();
  108. logStream << cmakeGenerateArguments.join(' ') << '\n';
  109. m_configProjectProcess->start(cmakeGenerateArguments.front(), cmakeGenerateArguments.mid(1));
  110. if (!m_configProjectProcess->waitForStarted())
  111. {
  112. QString error = tr("Configuring project failed to start.");
  113. QStringToAZTracePrint(error);
  114. return AZ::Failure(error);
  115. }
  116. bool containsGeneratingDone = false;
  117. while (m_configProjectProcess->waitForReadyRead(MaxBuildTimeMSecs))
  118. {
  119. QString configOutput = m_configProjectProcess->readAllStandardOutput();
  120. if (configOutput.contains("Generating done"))
  121. {
  122. containsGeneratingDone = true;
  123. }
  124. logStream << configOutput;
  125. logStream.flush();
  126. // Show last line of output if any
  127. auto configOutputLines = configOutput.split('\n', Qt::SkipEmptyParts);
  128. if (configOutputLines.length() > 0)
  129. {
  130. UpdateProgress(configOutputLines.last());
  131. }
  132. if (QThread::currentThread()->isInterruptionRequested())
  133. {
  134. logFile.close();
  135. m_configProjectProcess->close();
  136. QStringToAZTracePrint(BuildCancelled);
  137. return AZ::Failure(BuildCancelled);
  138. }
  139. }
  140. if (m_configProjectProcess->exitStatus() != QProcess::ExitStatus::NormalExit || m_configProjectProcess->exitCode() != 0 ||
  141. !containsGeneratingDone)
  142. {
  143. QString error = tr("Configuring project failed. See log for details.");
  144. QStringToAZTracePrint(error);
  145. return AZ::Failure(error);
  146. }
  147. m_buildProjectProcess = new QProcess(this);
  148. m_buildProjectProcess->setProcessChannelMode(QProcess::MergedChannels);
  149. m_buildProjectProcess->setWorkingDirectory(m_projectInfo.m_path);
  150. auto cmakeBuildArgumentsResult = ConstructCmakeBuildCommandArguments();
  151. if (!cmakeBuildArgumentsResult.IsSuccess())
  152. {
  153. QStringToAZTracePrint(cmakeBuildArgumentsResult.GetError());
  154. return AZ::Failure(cmakeBuildArgumentsResult.GetError());
  155. }
  156. auto cmakeBuildArguments = cmakeBuildArgumentsResult.GetValue();
  157. logStream << cmakeBuildArguments.join(' ') << '\n';
  158. m_buildProjectProcess->start(cmakeBuildArguments.front(), cmakeBuildArguments.mid(1));
  159. if (!m_buildProjectProcess->waitForStarted())
  160. {
  161. QString error = tr("Building project failed to start.");
  162. QStringToAZTracePrint(error);
  163. return AZ::Failure(error);
  164. }
  165. while (m_buildProjectProcess->waitForReadyRead(MaxBuildTimeMSecs))
  166. {
  167. QString buildOutput = m_buildProjectProcess->readAllStandardOutput();
  168. logStream << buildOutput;
  169. logStream.flush();
  170. // Show last line of output
  171. if (QStringList strs = buildOutput.split('\n', Qt::SkipEmptyParts);
  172. !strs.empty())
  173. {
  174. UpdateProgress(strs.last());
  175. }
  176. if (QThread::currentThread()->isInterruptionRequested())
  177. {
  178. // QProcess is unable to kill its child processes so we need to ask the operating system to do that for us
  179. auto killProcessArgumentsResult = ConstructKillProcessCommandArguments(QString::number(m_buildProjectProcess->processId()));
  180. if (!killProcessArgumentsResult.IsSuccess())
  181. {
  182. return AZ::Failure(killProcessArgumentsResult.GetError());
  183. }
  184. auto killProcessArguments = killProcessArgumentsResult.GetValue();
  185. QProcess killBuildProcess;
  186. killBuildProcess.setProcessChannelMode(QProcess::MergedChannels);
  187. killBuildProcess.start(killProcessArguments.front(), killProcessArguments.mid(1));
  188. killBuildProcess.waitForFinished();
  189. logStream << "Killing Project Build.";
  190. logStream << killBuildProcess.readAllStandardOutput();
  191. m_buildProjectProcess->kill();
  192. logFile.close();
  193. QStringToAZTracePrint(BuildCancelled);
  194. return AZ::Failure(BuildCancelled);
  195. }
  196. }
  197. if (m_buildProjectProcess->exitStatus() != QProcess::ExitStatus::NormalExit || m_buildProjectProcess->exitCode() != 0)
  198. {
  199. QString error = tr("Building project failed. See log for details.");
  200. QStringToAZTracePrint(error);
  201. return AZ::Failure(error);
  202. }
  203. return AZ::Success();
  204. }
  205. } // namespace O3DE::ProjectManager