CMakeSetup.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include <iostream>
  4. #include "QCMake.h" // include to disable MS warnings
  5. #include <QApplication>
  6. #include <QDir>
  7. #include <QLocale>
  8. #include <QString>
  9. #include <QTranslator>
  10. #include <QtPlugin>
  11. #include "cmsys/CommandLineArguments.hxx"
  12. #include "cmsys/Encoding.hxx"
  13. #include "cmsys/SystemTools.hxx"
  14. #include "CMakeSetupDialog.h"
  15. #include "cmAlgorithms.h"
  16. #include "cmDocumentation.h"
  17. #include "cmDocumentationEntry.h"
  18. #include "cmStringAlgorithms.h"
  19. #include "cmSystemTools.h" // IWYU pragma: keep
  20. #include "cmake.h"
  21. static const char* cmDocumentationName[][2] = { { nullptr,
  22. " cmake-gui - CMake GUI." },
  23. { nullptr, nullptr } };
  24. static const char* cmDocumentationUsage[][2] = {
  25. { nullptr,
  26. " cmake-gui [options]\n"
  27. " cmake-gui [options] <path-to-source>\n"
  28. " cmake-gui [options] <path-to-existing-build>\n"
  29. " cmake-gui [options] -S <path-to-source> -B <path-to-build>\n"
  30. " cmake-gui [options] --browse-manual\n" },
  31. { nullptr, nullptr }
  32. };
  33. static const char* cmDocumentationOptions[][2] = {
  34. { "-S <path-to-source>", "Explicitly specify a source directory." },
  35. { "-B <path-to-build>", "Explicitly specify a build directory." },
  36. { "--preset=<preset>", "Specify a configure preset." },
  37. { nullptr, nullptr }
  38. };
  39. #if defined(Q_OS_MAC)
  40. static int cmOSXInstall(std::string dir);
  41. static void cmAddPluginPath();
  42. #endif
  43. #if defined(USE_QXcbIntegrationPlugin)
  44. Q_IMPORT_PLUGIN(QXcbIntegrationPlugin);
  45. #endif
  46. #if defined(USE_QWindowsIntegrationPlugin)
  47. Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
  48. # if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
  49. Q_IMPORT_PLUGIN(QWindowsVistaStylePlugin);
  50. # endif
  51. #endif
  52. int CMakeGUIExec(CMakeSetupDialog* window);
  53. void SetupDefaultQSettings();
  54. void OpenReferenceManual();
  55. int main(int argc, char** argv)
  56. {
  57. cmSystemTools::EnsureStdPipes();
  58. cmsys::Encoding::CommandLineArguments encoding_args =
  59. cmsys::Encoding::CommandLineArguments::Main(argc, argv);
  60. int argc2 = encoding_args.argc();
  61. char const* const* argv2 = encoding_args.argv();
  62. cmSystemTools::InitializeLibUV();
  63. cmSystemTools::FindCMakeResources(argv2[0]);
  64. // check docs first so that X is not need to get docs
  65. // do docs, if args were given
  66. cmDocumentation doc;
  67. doc.addCMakeStandardDocSections();
  68. if (argc2 > 1 && doc.CheckOptions(argc2, argv2)) {
  69. // Construct and print requested documentation.
  70. cmake hcm(cmake::RoleInternal, cmState::Help);
  71. hcm.SetHomeDirectory("");
  72. hcm.SetHomeOutputDirectory("");
  73. hcm.AddCMakePaths();
  74. auto generators = hcm.GetGeneratorsDocumentation();
  75. doc.SetName("cmake");
  76. doc.SetSection("Name", cmDocumentationName);
  77. doc.SetSection("Usage", cmDocumentationUsage);
  78. doc.AppendSection("Generators", generators);
  79. doc.PrependSection("Options", cmDocumentationOptions);
  80. return !doc.PrintRequestedDocumentation(std::cout);
  81. }
  82. #if defined(Q_OS_MAC)
  83. if (argc2 == 2 && strcmp(argv2[1], "--install") == 0) {
  84. return cmOSXInstall("/usr/local/bin");
  85. }
  86. if (argc2 == 2 && cmHasLiteralPrefix(argv2[1], "--install=")) {
  87. return cmOSXInstall(argv2[1] + 10);
  88. }
  89. #endif
  90. // When we are on OSX and we are launching cmake-gui from a symlink, the
  91. // application will fail to launch as it can't find the qt.conf file which
  92. // tells it what the name of the plugin folder is. We need to add this path
  93. // BEFORE the application is constructed as that is what triggers the
  94. // searching for the platform plugins
  95. #if defined(Q_OS_MAC)
  96. cmAddPluginPath();
  97. #endif
  98. // HighDpiScaling is always enabled starting with Qt6, but will still issue a
  99. // deprecation warning if you try to set the attribute for it
  100. #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) && \
  101. QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
  102. QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  103. #endif
  104. SetupDefaultQSettings();
  105. QApplication app(argc, argv);
  106. setlocale(LC_NUMERIC, "C");
  107. // tell the cmake library where cmake is
  108. QDir cmExecDir(QApplication::applicationDirPath());
  109. #if defined(Q_OS_MAC)
  110. cmExecDir.cd("../../../");
  111. #endif
  112. // pick up translation files if they exists in the data directory
  113. QDir translationsDir = cmExecDir;
  114. translationsDir.cd(".." CMAKE_DATA_DIR);
  115. translationsDir.cd("i18n");
  116. QTranslator translator;
  117. if (translator.load(QLocale(), "cmake", "_", translationsDir.path())) {
  118. QApplication::installTranslator(&translator);
  119. }
  120. // app setup
  121. QApplication::setApplicationName("CMakeSetup");
  122. QApplication::setOrganizationName("Kitware");
  123. QIcon appIcon;
  124. appIcon.addFile(":/Icons/CMakeSetup32.png");
  125. appIcon.addFile(":/Icons/CMakeSetup128.png");
  126. QApplication::setWindowIcon(QIcon::fromTheme("cmake-gui", appIcon));
  127. CMakeSetupDialog dialog;
  128. dialog.show();
  129. QStringList args = QApplication::arguments();
  130. std::string binaryDirectory;
  131. std::string sourceDirectory;
  132. std::string presetName;
  133. for (int i = 1; i < args.size(); ++i) {
  134. const QString& arg = args[i];
  135. if (arg.startsWith("-S")) {
  136. QString path = arg.mid(2);
  137. if (path.isEmpty()) {
  138. ++i;
  139. if (i >= args.size()) {
  140. std::cerr << "No source directory specified for -S" << std::endl;
  141. return 1;
  142. }
  143. path = args[i];
  144. if (path[0] == '-') {
  145. std::cerr << "No source directory specified for -S" << std::endl;
  146. return 1;
  147. }
  148. }
  149. sourceDirectory = cmSystemTools::CollapseFullPath(path.toStdString());
  150. cmSystemTools::ConvertToUnixSlashes(sourceDirectory);
  151. } else if (arg.startsWith("-B")) {
  152. QString path = arg.mid(2);
  153. if (path.isEmpty()) {
  154. ++i;
  155. if (i >= args.size()) {
  156. std::cerr << "No build directory specified for -B" << std::endl;
  157. return 1;
  158. }
  159. path = args[i];
  160. if (path[0] == '-') {
  161. std::cerr << "No build directory specified for -B" << std::endl;
  162. return 1;
  163. }
  164. }
  165. binaryDirectory = cmSystemTools::CollapseFullPath(path.toStdString());
  166. cmSystemTools::ConvertToUnixSlashes(binaryDirectory);
  167. } else if (arg.startsWith("--preset=")) {
  168. QString preset = arg.mid(cmStrLen("--preset="));
  169. if (preset.isEmpty()) {
  170. std::cerr << "No preset specified for --preset" << std::endl;
  171. return 1;
  172. }
  173. presetName = preset.toStdString();
  174. } else if (arg == "--browse-manual") {
  175. OpenReferenceManual();
  176. return 0;
  177. }
  178. }
  179. if (!sourceDirectory.empty() &&
  180. (!binaryDirectory.empty() || !presetName.empty())) {
  181. dialog.setSourceDirectory(QString::fromStdString(sourceDirectory));
  182. if (!binaryDirectory.empty()) {
  183. dialog.setBinaryDirectory(QString::fromStdString(binaryDirectory));
  184. if (!presetName.empty()) {
  185. dialog.setStartupBinaryDirectory(true);
  186. }
  187. }
  188. if (!presetName.empty()) {
  189. dialog.setDeferredPreset(QString::fromStdString(presetName));
  190. }
  191. } else {
  192. if (args.count() == 2) {
  193. std::string filePath =
  194. cmSystemTools::CollapseFullPath(args[1].toStdString());
  195. // check if argument is a directory containing CMakeCache.txt
  196. std::string buildFilePath = cmStrCat(filePath, "/CMakeCache.txt");
  197. // check if argument is a CMakeCache.txt file
  198. if (cmSystemTools::GetFilenameName(filePath) == "CMakeCache.txt" &&
  199. cmSystemTools::FileExists(filePath.c_str())) {
  200. buildFilePath = filePath;
  201. }
  202. // check if argument is a directory containing CMakeLists.txt
  203. std::string srcFilePath = cmStrCat(filePath, "/CMakeLists.txt");
  204. if (cmSystemTools::FileExists(buildFilePath.c_str())) {
  205. dialog.setBinaryDirectory(QString::fromStdString(
  206. cmSystemTools::GetFilenamePath(buildFilePath)));
  207. } else if (cmSystemTools::FileExists(srcFilePath.c_str())) {
  208. dialog.setSourceDirectory(QString::fromStdString(filePath));
  209. dialog.setBinaryDirectory(
  210. QString::fromStdString(cmSystemTools::CollapseFullPath(".")));
  211. }
  212. }
  213. }
  214. return CMakeGUIExec(&dialog);
  215. }
  216. #if defined(Q_OS_MAC)
  217. # include <cerrno>
  218. # include <cstring>
  219. # include <unistd.h>
  220. # include "cm_sys_stat.h"
  221. static bool cmOSXInstall(std::string const& dir, std::string const& tool)
  222. {
  223. if (tool.empty()) {
  224. return true;
  225. }
  226. std::string link = dir + cmSystemTools::GetFilenameName(tool);
  227. struct stat st;
  228. if (lstat(link.c_str(), &st) == 0 && S_ISLNK(st.st_mode)) {
  229. char buf[4096];
  230. ssize_t s = readlink(link.c_str(), buf, sizeof(buf) - 1);
  231. if (s >= 0 && std::string(buf, s) == tool) {
  232. std::cerr << "Exists: '" << link << "' -> '" << tool << "'\n";
  233. return true;
  234. }
  235. }
  236. cmSystemTools::MakeDirectory(dir);
  237. if (symlink(tool.c_str(), link.c_str()) == 0) {
  238. std::cerr << "Linked: '" << link << "' -> '" << tool << "'\n";
  239. return true;
  240. }
  241. int err = errno;
  242. std::cerr << "Failed: '" << link << "' -> '" << tool
  243. << "': " << strerror(err) << "\n";
  244. return false;
  245. }
  246. static int cmOSXInstall(std::string dir)
  247. {
  248. if (!cmHasLiteralSuffix(dir, "/")) {
  249. dir += "/";
  250. }
  251. return (cmOSXInstall(dir, cmSystemTools::GetCMakeCommand()) &&
  252. cmOSXInstall(dir, cmSystemTools::GetCTestCommand()) &&
  253. cmOSXInstall(dir, cmSystemTools::GetCPackCommand()) &&
  254. cmOSXInstall(dir, cmSystemTools::GetCMakeGUICommand()) &&
  255. cmOSXInstall(dir, cmSystemTools::GetCMakeCursesCommand()))
  256. ? 0
  257. : 1;
  258. }
  259. // Locate the PlugIns directory and add it to the QApplication library paths.
  260. // We need to resolve all symlinks so we have a known relative path between
  261. // MacOS/CMake and the PlugIns directory.
  262. //
  263. // Note we are using cmSystemTools since Qt can't provide the path to the
  264. // executable before the QApplication is created, and that is when plugin
  265. // searching occurs.
  266. static void cmAddPluginPath()
  267. {
  268. std::string const& path = cmSystemTools::GetCMakeGUICommand();
  269. if (path.empty()) {
  270. return;
  271. }
  272. std::string const& realPath = cmSystemTools::GetRealPath(path);
  273. QFileInfo appPath(QString::fromLocal8Bit(realPath.c_str()));
  274. QDir pluginDir = appPath.dir();
  275. bool const foundPluginDir = pluginDir.cd("../PlugIns");
  276. if (foundPluginDir) {
  277. QApplication::addLibraryPath(pluginDir.path());
  278. }
  279. }
  280. #endif