CMakeSetup.cxx 9.5 KB

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