CMakeSetup.cxx 10 KB

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