CMakeSetup.cxx 10 KB

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