CMakeSetup.cxx 8.3 KB

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