CMakeSetup.cxx 10 KB

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