CMakeSetup.cxx 10 KB

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