ProjectUtils_mac.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <QProcess>
  9. #include <QStandardPaths>
  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. AZ::Outcome<void, QString> SetupCommandLineProcessEnvironment()
  19. {
  20. // For CMake on Mac, if its installed through home-brew, then it will be installed
  21. // under /usr/local/bin, which may not be in the system PATH environment.
  22. // Add that path for the command line process so that it will be able to locate
  23. // a home-brew installed version of CMake
  24. QString pathEnv = qEnvironmentVariable("PATH");
  25. QStringList pathEnvList = pathEnv.split(":");
  26. if (!pathEnvList.contains("/usr/local/bin"))
  27. {
  28. pathEnv += ":/usr/local/bin";
  29. if (!qputenv("PATH", pathEnv.toStdString().c_str()))
  30. {
  31. return AZ::Failure(QObject::tr("Failed to set PATH environment variable"));
  32. }
  33. }
  34. return AZ::Success();
  35. }
  36. AZ::Outcome<QString, QString> FindSupportedCMake()
  37. {
  38. // Validate that cmake is installed and is in the command line
  39. auto whichCMakeResult = ProjectUtils::ExecuteCommandResult("which", QStringList{ ProjectCMakeCommand });
  40. if (!whichCMakeResult.IsSuccess())
  41. {
  42. return AZ::Failure(
  43. QObject::tr("CMake not found. <br><br>"
  44. "Make sure that the minimum version of CMake is installed and available from the command prompt. "
  45. "Refer to the <a href='https://o3de.org/docs/welcome-guide/setup/requirements/#cmake'>O3DE "
  46. "requirements</a> page for more information."));
  47. }
  48. QString cmakeInstalledPath = whichCMakeResult.GetValue().split("\n")[0];
  49. // Query the version of the installed cmake
  50. auto queryCmakeVersionQuery = ExecuteCommandResult(cmakeInstalledPath, QStringList{ "--version" });
  51. if (queryCmakeVersionQuery.IsSuccess())
  52. {
  53. AZ_TracePrintf(
  54. "Project Manager", "\"%s\" detected.", queryCmakeVersionQuery.GetValue().split("\n")[0].toUtf8().constData());
  55. }
  56. return AZ::Success(QString{ cmakeInstalledPath });
  57. }
  58. AZ::Outcome<QString, QString> FindSupportedCompilerForPlatform([[maybe_unused]] const ProjectInfo& projectInfo)
  59. {
  60. AZ::Outcome processEnvResult = SetupCommandLineProcessEnvironment();
  61. if (!processEnvResult.IsSuccess())
  62. {
  63. return AZ::Failure(processEnvResult.GetError());
  64. }
  65. // Query the version of the installed cmake
  66. if (auto queryCmakeVersionQuery = FindSupportedCMake(); !queryCmakeVersionQuery.IsSuccess())
  67. {
  68. return queryCmakeVersionQuery;
  69. }
  70. // Query for the version of xcodebuild (if installed)
  71. auto queryXcodeBuildVersion = ExecuteCommandResult("xcodebuild", QStringList{ "-version" });
  72. if (!queryXcodeBuildVersion.IsSuccess())
  73. {
  74. return AZ::Failure(QObject::tr("Unable to detect XCodeBuilder on this host."));
  75. }
  76. QString xcodeBuilderVersionNumber = queryXcodeBuildVersion.GetValue().split("\n")[0];
  77. AZ_TracePrintf("Project Manager", "XcodeBuilder version %s detected.", xcodeBuilderVersionNumber.toUtf8().constData());
  78. return AZ::Success(xcodeBuilderVersionNumber);
  79. }
  80. AZ::Outcome<void, QString> OpenCMakeGUI(const QString& projectPath)
  81. {
  82. const QString cmakeHelp = QObject::tr("Please verify you've installed CMake.app from "
  83. "<a href=\"https://cmake.org\">cmake.org</a> or, if using HomeBrew, "
  84. "have installed it with <pre>brew install --cask cmake</pre>");
  85. QString cmakeAppPath = QStandardPaths::locate(QStandardPaths::ApplicationsLocation, "CMake.app", QStandardPaths::LocateDirectory);
  86. if (cmakeAppPath.isEmpty())
  87. {
  88. return AZ::Failure(QObject::tr("CMake.app not found.") + cmakeHelp);
  89. }
  90. QString projectBuildPath = QDir(projectPath).filePath(ProjectBuildPathPostfix);
  91. AZ::Outcome result = GetProjectBuildPath(projectPath);
  92. if (result.IsSuccess())
  93. {
  94. projectBuildPath = result.GetValue();
  95. }
  96. QProcess process;
  97. // if the project build path is relative, it should be relative to the project path
  98. process.setWorkingDirectory(projectPath);
  99. process.setProgram("open");
  100. process.setArguments({"-a", "CMake", "--args", "-S", projectPath, "-B", projectBuildPath});
  101. if(!process.startDetached())
  102. {
  103. return AZ::Failure(QObject::tr("CMake.app failed to open.") + cmakeHelp);
  104. }
  105. return AZ::Success();
  106. }
  107. AZ::Outcome<QString, QString> RunGetPythonScript(const QString& engineRoot)
  108. {
  109. return ExecuteCommandResultModalDialog(
  110. QString("%1/python/get_python.sh").arg(engineRoot),
  111. {},
  112. QObject::tr("Running get_python script..."));
  113. }
  114. AZ::IO::FixedMaxPath GetEditorExecutablePath(const AZ::IO::PathView& projectPath)
  115. {
  116. AZ::IO::FixedMaxPath editorPath;
  117. AZ::IO::FixedMaxPath fixedProjectPath{ projectPath };
  118. // First attempt to launch the Editor.exe within the project build directory if it exists
  119. AZ::IO::FixedMaxPath buildPathSetregPath = fixedProjectPath
  120. / AZ::SettingsRegistryInterface::DevUserRegistryFolder
  121. / "Platform" / AZ_TRAIT_OS_PLATFORM_CODENAME / "build_path.setreg";
  122. if (AZ::IO::SystemFile::Exists(buildPathSetregPath.c_str()))
  123. {
  124. AZ::SettingsRegistryImpl localRegistry;
  125. // Merge the build_path.setreg into the local SettingsRegistry instance
  126. if (AZ::IO::FixedMaxPath projectBuildPath;
  127. localRegistry.MergeSettingsFile(buildPathSetregPath.Native(),
  128. AZ::SettingsRegistryInterface::Format::JsonMergePatch)
  129. && localRegistry.Get(projectBuildPath.Native(), AZ::SettingsRegistryMergeUtils::ProjectBuildPath))
  130. {
  131. // local Settings Registry will be used to merge the build_path.setreg for the supplied projectPath
  132. AZ::IO::FixedMaxPath buildConfigurationPath = (fixedProjectPath / projectBuildPath).LexicallyNormal();
  133. // First try "<project-build-path>/bin/$<CONFIG>/Editor.app/Contents/MacOS"
  134. // Followed by "<project-build-path>/bin/$<PLATFORM>/$<CONFIG>/Editor.app/Contents/MacOS"
  135. // Directory existence is checked in this case
  136. buildConfigurationPath /= "bin";
  137. if (editorPath = (buildConfigurationPath
  138. / AZ_BUILD_CONFIGURATION_TYPE / "Editor.app/Contents/MacOS/Editor");
  139. AZ::IO::SystemFile::Exists(editorPath.c_str()))
  140. {
  141. return editorPath;
  142. }
  143. else if (editorPath = (buildConfigurationPath / AZ_TRAIT_OS_PLATFORM_CODENAME
  144. / AZ_BUILD_CONFIGURATION_TYPE / "Editor.app/Contents/MacOS/Editor");
  145. AZ::IO::SystemFile::Exists(editorPath.c_str()))
  146. {
  147. return editorPath;
  148. }
  149. }
  150. }
  151. // Fall back to locating the Editor.app bundle which should exists
  152. // outside of the current O3DE.app bundle
  153. editorPath = (AZ::IO::FixedMaxPath(AZ::Utils::GetExecutableDirectory()) /
  154. "../../../Editor.app/Contents/MacOS/Editor").LexicallyNormal();
  155. if (!AZ::IO::SystemFile::Exists(editorPath.c_str()))
  156. {
  157. // Attempt to search the O3DE.app global settings registry for an InstalledBinaryFolder
  158. // key which indicates the relative path to an SDK binary directory on MacOS
  159. if (auto settingsRegistry = AZ::SettingsRegistry::Get(); settingsRegistry != nullptr)
  160. {
  161. if (AZ::IO::FixedMaxPath installedBinariesPath;
  162. settingsRegistry->Get(installedBinariesPath.Native(),
  163. AZ::SettingsRegistryMergeUtils::FilePathKey_InstalledBinaryFolder))
  164. {
  165. if (AZ::IO::FixedMaxPath engineRootFolder;
  166. settingsRegistry->Get(engineRootFolder.Native(),
  167. AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder))
  168. {
  169. editorPath = engineRootFolder / installedBinariesPath / "Editor.app/Contents/MacOS/Editor";
  170. }
  171. }
  172. }
  173. if (!AZ::IO::SystemFile::Exists(editorPath.c_str()))
  174. {
  175. AZ_Error("ProjectManager", false, "Unable to find the Editor app bundle!");
  176. return {};
  177. }
  178. }
  179. return editorPath;
  180. }
  181. AZ::Outcome<QString, QString> CreateDesktopShortcut([[maybe_unused]] const QString& filename, [[maybe_unused]] const QString& targetPath, [[maybe_unused]] const QStringList& arguments)
  182. {
  183. return AZ::Failure(QObject::tr("Creating desktop shortcuts functionality not implemented for this platform yet."));
  184. }
  185. } // namespace ProjectUtils
  186. } // namespace O3DE::ProjectManager