CMakeSetup.cxx 8.4 KB

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