CMakeSetup.cxx 8.3 KB

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