CMakeSetup.cxx 9.5 KB

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