CMakeSetup.cxx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "QCMake.h" // include to disable MS warnings
  4. #include "CMakeSetupDialog.h"
  5. #include "cmAlgorithms.h"
  6. #include "cmDocumentation.h"
  7. #include "cmDocumentationEntry.h"
  8. #include "cmVersion.h"
  9. #include "cmake.h"
  10. #include "cmsys/CommandLineArguments.hxx"
  11. #include "cmsys/Encoding.hxx"
  12. #include "cmsys/SystemTools.hxx"
  13. #include <QApplication>
  14. #include <QDir>
  15. #include <QLocale>
  16. #include <QString>
  17. #include <QTextCodec>
  18. #include <QTranslator>
  19. #include <QtPlugin>
  20. #include <iostream>
  21. #include "cmSystemTools.h" // IWYU pragma: keep
  22. static const char* cmDocumentationName[][2] = { { nullptr,
  23. " cmake-gui - CMake GUI." },
  24. { nullptr, nullptr } };
  25. static const char* cmDocumentationUsage[][2] = {
  26. { nullptr, " cmake-gui [options]\n"
  27. " cmake-gui [options] <path-to-source>\n"
  28. " cmake-gui [options] <path-to-existing-build>" },
  29. { nullptr, nullptr }
  30. };
  31. static const char* cmDocumentationOptions[][2] = { { nullptr, nullptr } };
  32. #if defined(Q_OS_MAC)
  33. static int cmOSXInstall(std::string dir);
  34. static void cmAddPluginPath();
  35. #endif
  36. #if defined(USE_QXcbIntegrationPlugin)
  37. Q_IMPORT_PLUGIN(QXcbIntegrationPlugin);
  38. #endif
  39. #if defined(USE_QWindowsIntegrationPlugin)
  40. Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
  41. #endif
  42. int main(int argc, char** argv)
  43. {
  44. cmsys::Encoding::CommandLineArguments encoding_args =
  45. cmsys::Encoding::CommandLineArguments::Main(argc, argv);
  46. int argc2 = encoding_args.argc();
  47. char const* const* argv2 = encoding_args.argv();
  48. cmSystemTools::InitializeLibUV();
  49. cmSystemTools::FindCMakeResources(argv2[0]);
  50. // check docs first so that X is not need to get docs
  51. // do docs, if args were given
  52. cmDocumentation doc;
  53. doc.addCMakeStandardDocSections();
  54. if (argc2 > 1 && doc.CheckOptions(argc2, argv2)) {
  55. // Construct and print requested documentation.
  56. cmake hcm(cmake::RoleInternal);
  57. hcm.SetHomeDirectory("");
  58. hcm.SetHomeOutputDirectory("");
  59. hcm.AddCMakePaths();
  60. std::vector<cmDocumentationEntry> generators;
  61. hcm.GetGeneratorDocumentation(generators);
  62. doc.SetName("cmake");
  63. doc.SetSection("Name", cmDocumentationName);
  64. doc.SetSection("Usage", cmDocumentationUsage);
  65. doc.AppendSection("Generators", generators);
  66. doc.PrependSection("Options", cmDocumentationOptions);
  67. return (doc.PrintRequestedDocumentation(std::cout) ? 0 : 1);
  68. }
  69. #if defined(Q_OS_MAC)
  70. if (argc2 == 2 && strcmp(argv2[1], "--install") == 0) {
  71. return cmOSXInstall("/usr/local/bin");
  72. }
  73. if (argc2 == 2 && cmHasLiteralPrefix(argv2[1], "--install=")) {
  74. return cmOSXInstall(argv2[1] + 10);
  75. }
  76. #endif
  77. // When we are on OSX and we are launching cmake-gui from a symlink, the
  78. // application will fail to launch as it can't find the qt.conf file which
  79. // tells it what the name of the plugin folder is. We need to add this path
  80. // BEFORE the application is constructed as that is what triggers the
  81. // searching for the platform plugins
  82. #if defined(Q_OS_MAC)
  83. cmAddPluginPath();
  84. #endif
  85. #if QT_VERSION >= 0x050600
  86. QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  87. #endif
  88. QApplication app(argc, argv);
  89. setlocale(LC_NUMERIC, "C");
  90. QTextCodec* utf8_codec = QTextCodec::codecForName("UTF-8");
  91. QTextCodec::setCodecForLocale(utf8_codec);
  92. #if QT_VERSION < 0x050000
  93. // clean out standard Qt paths for plugins, which we don't use anyway
  94. // when creating Mac bundles, it potentially causes problems
  95. foreach (QString p, QApplication::libraryPaths()) {
  96. QApplication::removeLibraryPath(p);
  97. }
  98. #endif
  99. // tell the cmake library where cmake is
  100. QDir cmExecDir(QApplication::applicationDirPath());
  101. #if defined(Q_OS_MAC)
  102. cmExecDir.cd("../../../");
  103. #endif
  104. // pick up translation files if they exists in the data directory
  105. QDir translationsDir = cmExecDir;
  106. translationsDir.cd(QString::fromLocal8Bit(".." CMAKE_DATA_DIR));
  107. translationsDir.cd("i18n");
  108. QTranslator translator;
  109. QString transfile = QString("cmake_%1").arg(QLocale::system().name());
  110. translator.load(transfile, translationsDir.path());
  111. app.installTranslator(&translator);
  112. // app setup
  113. app.setApplicationName("CMakeSetup");
  114. app.setOrganizationName("Kitware");
  115. QIcon appIcon;
  116. appIcon.addFile(":/Icons/CMakeSetup32.png");
  117. appIcon.addFile(":/Icons/CMakeSetup128.png");
  118. app.setWindowIcon(appIcon);
  119. CMakeSetupDialog dialog;
  120. dialog.show();
  121. cmsys::CommandLineArguments arg;
  122. arg.Initialize(argc2, argv2);
  123. std::string binaryDirectory;
  124. std::string sourceDirectory;
  125. typedef cmsys::CommandLineArguments argT;
  126. arg.AddArgument("-B", argT::CONCAT_ARGUMENT, &binaryDirectory,
  127. "Binary Directory");
  128. arg.AddArgument("-H", argT::CONCAT_ARGUMENT, &sourceDirectory,
  129. "Source Directory");
  130. // do not complain about unknown options
  131. arg.StoreUnusedArguments(true);
  132. arg.Parse();
  133. if (!sourceDirectory.empty() && !binaryDirectory.empty()) {
  134. dialog.setSourceDirectory(QString::fromLocal8Bit(sourceDirectory.c_str()));
  135. dialog.setBinaryDirectory(QString::fromLocal8Bit(binaryDirectory.c_str()));
  136. } else {
  137. QStringList args = app.arguments();
  138. if (args.count() == 2) {
  139. std::string filePath =
  140. cmSystemTools::CollapseFullPath(args[1].toLocal8Bit().data());
  141. // check if argument is a directory containing CMakeCache.txt
  142. std::string buildFilePath =
  143. cmSystemTools::CollapseFullPath("CMakeCache.txt", filePath.c_str());
  144. // check if argument is a CMakeCache.txt file
  145. if (cmSystemTools::GetFilenameName(filePath) == "CMakeCache.txt" &&
  146. cmSystemTools::FileExists(filePath.c_str())) {
  147. buildFilePath = filePath;
  148. }
  149. // check if argument is a directory containing CMakeLists.txt
  150. std::string srcFilePath =
  151. cmSystemTools::CollapseFullPath("CMakeLists.txt", filePath.c_str());
  152. if (cmSystemTools::FileExists(buildFilePath.c_str())) {
  153. dialog.setBinaryDirectory(QString::fromLocal8Bit(
  154. cmSystemTools::GetFilenamePath(buildFilePath).c_str()));
  155. } else if (cmSystemTools::FileExists(srcFilePath.c_str())) {
  156. dialog.setSourceDirectory(QString::fromLocal8Bit(filePath.c_str()));
  157. dialog.setBinaryDirectory(QString::fromLocal8Bit(
  158. cmSystemTools::CollapseFullPath(".").c_str()));
  159. }
  160. }
  161. }
  162. return app.exec();
  163. }
  164. #if defined(Q_OS_MAC)
  165. #include "cm_sys_stat.h"
  166. #include <errno.h>
  167. #include <string.h>
  168. #include <unistd.h>
  169. static bool cmOSXInstall(std::string const& dir, std::string const& tool)
  170. {
  171. if (tool.empty()) {
  172. return true;
  173. }
  174. std::string link = dir + cmSystemTools::GetFilenameName(tool);
  175. struct stat st;
  176. if (lstat(link.c_str(), &st) == 0 && S_ISLNK(st.st_mode)) {
  177. char buf[4096];
  178. ssize_t s = readlink(link.c_str(), buf, sizeof(buf) - 1);
  179. if (s >= 0 && std::string(buf, s) == tool) {
  180. std::cerr << "Exists: '" << link << "' -> '" << tool << "'\n";
  181. return true;
  182. }
  183. }
  184. cmSystemTools::MakeDirectory(dir);
  185. if (symlink(tool.c_str(), link.c_str()) == 0) {
  186. std::cerr << "Linked: '" << link << "' -> '" << tool << "'\n";
  187. return true;
  188. } else {
  189. int err = errno;
  190. std::cerr << "Failed: '" << link << "' -> '" << tool
  191. << "': " << strerror(err) << "\n";
  192. return false;
  193. }
  194. }
  195. static int cmOSXInstall(std::string dir)
  196. {
  197. if (!cmHasLiteralSuffix(dir, "/")) {
  198. dir += "/";
  199. }
  200. return (cmOSXInstall(dir, cmSystemTools::GetCMakeCommand()) &&
  201. cmOSXInstall(dir, cmSystemTools::GetCTestCommand()) &&
  202. cmOSXInstall(dir, cmSystemTools::GetCPackCommand()) &&
  203. cmOSXInstall(dir, cmSystemTools::GetCMakeGUICommand()) &&
  204. cmOSXInstall(dir, cmSystemTools::GetCMakeCursesCommand()))
  205. ? 0
  206. : 1;
  207. }
  208. // Locate the PlugIns directory and add it to the QApplication library paths.
  209. // We need to resolve all symlinks so we have a known relative path between
  210. // MacOS/CMake and the PlugIns directory.
  211. //
  212. // Note we are using cmSystemTools since Qt can't provide the path to the
  213. // executable before the QApplication is created, and that is when plugin
  214. // searching occurs.
  215. static void cmAddPluginPath()
  216. {
  217. std::string const& path = cmSystemTools::GetCMakeGUICommand();
  218. if (path.empty()) {
  219. return;
  220. }
  221. std::string const& realPath = cmSystemTools::GetRealPath(path);
  222. QFileInfo appPath(QString::fromLocal8Bit(realPath.c_str()));
  223. QDir pluginDir = appPath.dir();
  224. bool const foundPluginDir = pluginDir.cd("../PlugIns");
  225. if (foundPluginDir) {
  226. QApplication::addLibraryPath(pluginDir.path());
  227. }
  228. }
  229. #endif