ProjectUtils_linux.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <ProjectUtils.h>
  8. #include <PythonBindingsInterface.h>
  9. #include <QProcessEnvironment>
  10. #include <QDir>
  11. #include <AzCore/Settings/SettingsRegistryImpl.h>
  12. #include <AzCore/Settings/SettingsRegistryMergeUtils.h>
  13. #include <AzCore/Utils/Utils.h>
  14. namespace O3DE::ProjectManager
  15. {
  16. namespace ProjectUtils
  17. {
  18. // The list of clang C/C++ compiler command lines to validate on the host Linux system
  19. // Only Ubuntu has clang++-<version> symlinks, other linux distros do not,
  20. // so a empty string entry is added to the end
  21. const QStringList SupportedClangVersions = {"-13", "-12", "-11", "-10", "-9", "-8", "-7", "-6.0", ""};
  22. AZ::Outcome<void, QString> SetupCommandLineProcessEnvironment()
  23. {
  24. return AZ::Success();
  25. }
  26. AZ::Outcome<QString, QString> FindSupportedCMake()
  27. {
  28. // Validate that cmake is installed and is in the command line
  29. auto whichCMakeResult = ProjectUtils::ExecuteCommandResult("which", QStringList{ ProjectCMakeCommand });
  30. if (!whichCMakeResult.IsSuccess())
  31. {
  32. return AZ::Failure(
  33. QObject::tr("CMake not found. <br><br>"
  34. "Make sure that the minimum version of CMake is installed and available from the command prompt. "
  35. "Refer to the <a href='https://o3de.org/docs/welcome-guide/setup/requirements/#cmake'>O3DE "
  36. "requirements</a> page for more information."));
  37. }
  38. QString cmakeInstalledPath = whichCMakeResult.GetValue().split("\n")[0];
  39. // Query the version of the installed cmake
  40. auto queryCmakeVersionQuery = ExecuteCommandResult(cmakeInstalledPath, QStringList{ "--version" });
  41. if (queryCmakeVersionQuery.IsSuccess())
  42. {
  43. AZ_TracePrintf(
  44. "Project Manager", "\"%s\" detected.", queryCmakeVersionQuery.GetValue().split("\n")[0].toUtf8().constData());
  45. }
  46. return AZ::Success(QString{ cmakeInstalledPath });
  47. }
  48. AZ::Outcome<QString, QString> FindSupportedCompilerForPlatform([[maybe_unused]] const ProjectInfo& projectInfo)
  49. {
  50. // Query the version of cmake that is installed
  51. if (auto queryCmakeVersionQuery = FindSupportedCMake(); !queryCmakeVersionQuery.IsSuccess())
  52. {
  53. return queryCmakeVersionQuery;
  54. }
  55. // Look for the first compatible version of clang. The list below will contain the known clang compilers that have been tested
  56. // for O3DE.
  57. for (const QString& supportClangVersion : SupportedClangVersions)
  58. {
  59. auto whichClangResult =
  60. ProjectUtils::ExecuteCommandResult("which", QStringList{ QString("clang%1").arg(supportClangVersion) });
  61. auto whichClangPPResult =
  62. ProjectUtils::ExecuteCommandResult("which", QStringList{ QString("clang++%1").arg(supportClangVersion) });
  63. if (whichClangResult.IsSuccess() && whichClangPPResult.IsSuccess())
  64. {
  65. return AZ::Success(QString("clang%1").arg(supportClangVersion));
  66. }
  67. }
  68. // Finally fallback to trying to detect gcc executables without a compiler version
  69. auto whichGccNoVersionResult = ProjectUtils::ExecuteCommandResult("which", QStringList{ QString("gcc") });
  70. auto whichGPlusPlusNoVersionResult = ProjectUtils::ExecuteCommandResult("which", QStringList{ QString("g++") });
  71. if (whichGccNoVersionResult.IsSuccess() && whichGPlusPlusNoVersionResult.IsSuccess())
  72. {
  73. return AZ::Success(QString("gcc"));
  74. }
  75. return AZ::Failure(QObject::tr("Neither clang nor gcc not found. <br><br>"
  76. "Make sure that the clang or gcc is installed and available from the command prompt. "
  77. "Refer to the <a href='https://o3de.org/docs/welcome-guide/setup/requirements/#cmake'>O3DE "
  78. "requirements</a> page for more information."));
  79. }
  80. AZ::Outcome<void, QString> OpenCMakeGUI(const QString& projectPath)
  81. {
  82. AZ::Outcome processEnvResult = SetupCommandLineProcessEnvironment();
  83. if (!processEnvResult.IsSuccess())
  84. {
  85. return AZ::Failure(processEnvResult.GetError());
  86. }
  87. QString projectBuildPath = QDir(projectPath).filePath(ProjectBuildPathPostfix);
  88. AZ::Outcome projectBuildPathResult = GetProjectBuildPath(projectPath);
  89. if (projectBuildPathResult.IsSuccess())
  90. {
  91. projectBuildPath = projectBuildPathResult.GetValue();
  92. }
  93. QProcess process;
  94. // if the project build path is relative, it should be relative to the project path
  95. process.setWorkingDirectory(projectPath);
  96. process.setProgram("cmake-gui");
  97. process.setArguments({ "-S", projectPath, "-B", projectBuildPath });
  98. if(!process.startDetached())
  99. {
  100. return AZ::Failure(QObject::tr("Failed to start CMake GUI"));
  101. }
  102. return AZ::Success();
  103. }
  104. AZ::Outcome<QString, QString> RunGetPythonScript(const QString& engineRoot)
  105. {
  106. return ExecuteCommandResultModalDialog(
  107. QString("%1/python/get_python.sh").arg(engineRoot),
  108. {},
  109. QObject::tr("Running get_python script..."));
  110. }
  111. AZ::IO::FixedMaxPath GetEditorExecutablePath(const AZ::IO::PathView& projectPath)
  112. {
  113. AZ::IO::FixedMaxPath editorPath;
  114. AZ::IO::FixedMaxPath fixedProjectPath{ projectPath };
  115. // First attempt to launch the Editor.exe within the project build directory if it exists
  116. AZ::IO::FixedMaxPath buildPathSetregPath = fixedProjectPath
  117. / AZ::SettingsRegistryConstants::DevUserRegistryFolder
  118. / "Platform" / AZ_TRAIT_OS_PLATFORM_CODENAME / "build_path.setreg";
  119. if (AZ::IO::SystemFile::Exists(buildPathSetregPath.c_str()))
  120. {
  121. AZ::SettingsRegistryImpl settingsRegistry;
  122. // Merge the build_path.setreg into the local SettingsRegistry instance
  123. if (AZ::IO::FixedMaxPath projectBuildPath;
  124. settingsRegistry.MergeSettingsFile(buildPathSetregPath.Native(),
  125. AZ::SettingsRegistryInterface::Format::JsonMergePatch)
  126. && settingsRegistry.Get(projectBuildPath.Native(), AZ::SettingsRegistryMergeUtils::ProjectBuildPath))
  127. {
  128. // local Settings Registry will be used to merge the build_path.setreg for the supplied projectPath
  129. AZ::IO::FixedMaxPath buildConfigurationPath = (fixedProjectPath / projectBuildPath).LexicallyNormal();
  130. // First try <project-build-path>/bin/$<CONFIG> and if that path doesn't exist
  131. // try <project-build-path>/bin/$<PLATFORM>/$<CONFIG>
  132. buildConfigurationPath /= "bin";
  133. AZStd::fixed_vector<AZ::IO::FixedMaxPath, 4> paths = {
  134. buildConfigurationPath / AZ_BUILD_CONFIGURATION_TYPE / "Editor",
  135. buildConfigurationPath / AZ_TRAIT_OS_PLATFORM_CODENAME / AZ_BUILD_CONFIGURATION_TYPE / "Editor"
  136. };
  137. // always try profile config because that is the default
  138. if (strcmp(AZ_BUILD_CONFIGURATION_TYPE, "profile") != 0)
  139. {
  140. paths.emplace_back(buildConfigurationPath / "profile" / "Editor");
  141. paths.emplace_back(buildConfigurationPath / AZ_TRAIT_OS_PLATFORM_CODENAME / "profile" / "Editor");
  142. }
  143. for (auto& path : paths)
  144. {
  145. if(AZ::IO::SystemFile::Exists(path.ReplaceExtension(AZ_TRAIT_OS_EXECUTABLE_EXTENSION).c_str()))
  146. {
  147. return path;
  148. }
  149. }
  150. }
  151. }
  152. // No Editor executable was found in the project build folder so if this project uses a
  153. // different engine we must find the Editor executable for that engine
  154. if(auto engineResult = PythonBindingsInterface::Get()->GetProjectEngine(projectPath.Native().data()); engineResult)
  155. {
  156. auto engineInfo = engineResult.GetValue<EngineInfo>();
  157. if (!engineInfo.m_thisEngine)
  158. {
  159. AZ::IO::FixedMaxPath fixedEnginePath{ engineInfo.m_path.toUtf8().constData() };
  160. // try the default sdk path
  161. // in the future we may be able to use additional .setreg entries to locate an alternate binary path
  162. if (editorPath = (fixedEnginePath / "bin" / AZ_TRAIT_OS_PLATFORM_CODENAME / "profile" / "Default" / "Editor").
  163. ReplaceExtension(AZ_TRAIT_OS_EXECUTABLE_EXTENSION);
  164. AZ::IO::SystemFile::Exists(editorPath.c_str()))
  165. {
  166. return editorPath;
  167. }
  168. return {};
  169. }
  170. }
  171. // Fall back to checking if an Editor exists in O3DE executable directory
  172. editorPath = AZ::IO::FixedMaxPath(AZ::Utils::GetExecutableDirectory()) / "Editor";
  173. editorPath.ReplaceExtension(AZ_TRAIT_OS_EXECUTABLE_EXTENSION);
  174. if (AZ::IO::SystemFile::Exists(editorPath.c_str()))
  175. {
  176. return editorPath;
  177. }
  178. return {};
  179. }
  180. AZ::Outcome<QString, QString> CreateDesktopShortcut([[maybe_unused]] const QString& filename, [[maybe_unused]] const QString& targetPath, [[maybe_unused]] const QStringList& arguments)
  181. {
  182. return AZ::Failure(QObject::tr("Creating desktop shortcuts functionality not implemented for this platform yet."));
  183. }
  184. } // namespace ProjectUtils
  185. } // namespace O3DE::ProjectManager