CMakeSetup.cxx 8.5 KB

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