CMakeSetup.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst 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 "cmStdIoInit.h"
  19. #include "cmStringAlgorithms.h"
  20. #include "cmSystemTools.h" // IWYU pragma: keep
  21. #include "cmake.h"
  22. namespace {
  23. cmDocumentationEntry const cmDocumentationName = {
  24. {},
  25. " cmake-gui - CMake GUI."
  26. };
  27. cmDocumentationEntry const cmDocumentationUsage = {
  28. {},
  29. " cmake-gui [options]\n"
  30. " cmake-gui [options] <path-to-source>\n"
  31. " cmake-gui [options] <path-to-existing-build>\n"
  32. " cmake-gui [options] -S <path-to-source> -B <path-to-build>\n"
  33. " cmake-gui [options] --browse-manual [<filename>]"
  34. };
  35. cmDocumentationEntry const cmDocumentationOptions[3] = {
  36. { "-S <path-to-source>", "Explicitly specify a source directory." },
  37. { "-B <path-to-build>", "Explicitly specify a build directory." },
  38. { "--preset=<preset>", "Specify a configure preset." }
  39. };
  40. } // anonymous namespace
  41. #if defined(Q_OS_MAC)
  42. static int cmOSXInstall(std::string dir);
  43. static void cmAddPluginPath();
  44. #endif
  45. #if defined(USE_QXcbIntegrationPlugin)
  46. Q_IMPORT_PLUGIN(QXcbIntegrationPlugin);
  47. #endif
  48. #if defined(USE_QWindowsIntegrationPlugin)
  49. Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
  50. # if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
  51. Q_IMPORT_PLUGIN(QWindowsVistaStylePlugin);
  52. # endif
  53. #endif
  54. int CMakeGUIExec(CMakeSetupDialog* window);
  55. void SetupDefaultQSettings();
  56. void OpenReferenceManual(QString const& filename);
  57. int main(int argc, char** argv)
  58. {
  59. cm::StdIo::Init();
  60. cmsys::Encoding::CommandLineArguments encoding_args =
  61. cmsys::Encoding::CommandLineArguments::Main(argc, argv);
  62. int argc2 = encoding_args.argc();
  63. char const* const* argv2 = encoding_args.argv();
  64. cmSystemTools::InitializeLibUV();
  65. cmSystemTools::FindCMakeResources(argv2[0]);
  66. // check docs first so that X is not need to get docs
  67. // do docs, if args were given
  68. cmDocumentation doc;
  69. doc.addCMakeStandardDocSections();
  70. if (argc2 > 1 && doc.CheckOptions(argc2, argv2)) {
  71. // Construct and print requested documentation.
  72. cmake hcm(cmState::Role::Help);
  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. QString const& 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 =
  150. cmSystemTools::ToNormalizedPathOnDisk(path.toStdString());
  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 =
  166. cmSystemTools::ToNormalizedPathOnDisk(path.toStdString());
  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. ++i;
  176. if (i >= args.size()) {
  177. OpenReferenceManual("index.html");
  178. } else {
  179. OpenReferenceManual(args[i]);
  180. }
  181. return 0;
  182. }
  183. }
  184. if (!sourceDirectory.empty() &&
  185. (!binaryDirectory.empty() || !presetName.empty())) {
  186. dialog.setSourceDirectory(QString::fromStdString(sourceDirectory));
  187. if (!binaryDirectory.empty()) {
  188. dialog.setBinaryDirectory(QString::fromStdString(binaryDirectory));
  189. if (!presetName.empty()) {
  190. dialog.setStartupBinaryDirectory(true);
  191. }
  192. }
  193. if (!presetName.empty()) {
  194. dialog.setDeferredPreset(QString::fromStdString(presetName));
  195. }
  196. } else {
  197. if (args.count() == 2) {
  198. std::string filePath =
  199. cmSystemTools::ToNormalizedPathOnDisk(args[1].toStdString());
  200. // check if argument is a directory containing CMakeCache.txt
  201. std::string buildFilePath = cmStrCat(filePath, "/CMakeCache.txt");
  202. // check if argument is a CMakeCache.txt file
  203. if (cmSystemTools::GetFilenameName(filePath) == "CMakeCache.txt" &&
  204. cmSystemTools::FileExists(filePath.c_str())) {
  205. buildFilePath = filePath;
  206. }
  207. // check if argument is a directory containing CMakeLists.txt
  208. std::string srcFilePath = cmStrCat(filePath, "/CMakeLists.txt");
  209. if (cmSystemTools::FileExists(buildFilePath.c_str())) {
  210. dialog.setBinaryDirectory(QString::fromStdString(
  211. cmSystemTools::GetFilenamePath(buildFilePath)));
  212. } else if (cmSystemTools::FileExists(srcFilePath.c_str())) {
  213. dialog.setSourceDirectory(QString::fromStdString(filePath));
  214. dialog.setBinaryDirectory(
  215. QString::fromStdString(cmSystemTools::GetLogicalWorkingDirectory()));
  216. }
  217. }
  218. }
  219. return CMakeGUIExec(&dialog);
  220. }
  221. #if defined(Q_OS_MAC)
  222. # include <cerrno>
  223. # include <cstring>
  224. # include <unistd.h>
  225. # include "cm_sys_stat.h"
  226. static bool cmOSXInstall(std::string const& dir, std::string const& tool)
  227. {
  228. if (tool.empty()) {
  229. return true;
  230. }
  231. std::string link = dir + cmSystemTools::GetFilenameName(tool);
  232. struct stat st;
  233. if (lstat(link.c_str(), &st) == 0 && S_ISLNK(st.st_mode)) {
  234. char buf[4096];
  235. ssize_t s = readlink(link.c_str(), buf, sizeof(buf) - 1);
  236. if (s >= 0 && std::string(buf, s) == tool) {
  237. std::cerr << "Exists: '" << link << "' -> '" << tool << "'\n";
  238. return true;
  239. }
  240. }
  241. cmSystemTools::MakeDirectory(dir);
  242. if (symlink(tool.c_str(), link.c_str()) == 0) {
  243. std::cerr << "Linked: '" << link << "' -> '" << tool << "'\n";
  244. return true;
  245. }
  246. int err = errno;
  247. std::cerr << "Failed: '" << link << "' -> '" << tool
  248. << "': " << strerror(err) << "\n";
  249. return false;
  250. }
  251. static int cmOSXInstall(std::string dir)
  252. {
  253. if (!cmHasSuffix(dir, '/')) {
  254. dir += "/";
  255. }
  256. return (cmOSXInstall(dir, cmSystemTools::GetCMakeCommand()) &&
  257. cmOSXInstall(dir, cmSystemTools::GetCTestCommand()) &&
  258. cmOSXInstall(dir, cmSystemTools::GetCPackCommand()) &&
  259. cmOSXInstall(dir, cmSystemTools::GetCMakeGUICommand()) &&
  260. cmOSXInstall(dir, cmSystemTools::GetCMakeCursesCommand()))
  261. ? 0
  262. : 1;
  263. }
  264. // Locate the PlugIns directory and add it to the QApplication library paths.
  265. // We need to resolve all symlinks so we have a known relative path between
  266. // MacOS/CMake and the PlugIns directory.
  267. //
  268. // Note we are using cmSystemTools since Qt can't provide the path to the
  269. // executable before the QApplication is created, and that is when plugin
  270. // searching occurs.
  271. static void cmAddPluginPath()
  272. {
  273. std::string const& path = cmSystemTools::GetCMakeGUICommand();
  274. if (path.empty()) {
  275. return;
  276. }
  277. std::string const& realPath = cmSystemTools::GetRealPath(path);
  278. QFileInfo appPath(QString::fromLocal8Bit(realPath.c_str()));
  279. QDir pluginDir = appPath.dir();
  280. bool const foundPluginDir = pluginDir.cd("../PlugIns");
  281. if (foundPluginDir) {
  282. QApplication::addLibraryPath(pluginDir.path());
  283. }
  284. }
  285. #endif