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