CMakeSetup.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <QTranslator>
  10. #include <QtPlugin>
  11. #include "cmsys/CommandLineArguments.hxx"
  12. #include "cmsys/Encoding.hxx"
  13. #include "cmsys/SystemTools.hxx"
  14. #include "CMakeSetupDialog.h"
  15. #include "cmAlgorithms.h"
  16. #include "cmDocumentation.h"
  17. #include "cmDocumentationEntry.h"
  18. #include "cmStringAlgorithms.h"
  19. #include "cmSystemTools.h" // IWYU pragma: keep
  20. #include "cmake.h"
  21. static const char* cmDocumentationName[][2] = { { nullptr,
  22. " cmake-gui - CMake GUI." },
  23. { nullptr, nullptr } };
  24. static const char* cmDocumentationUsage[][2] = {
  25. { nullptr,
  26. " cmake-gui [options]\n"
  27. " cmake-gui [options] <path-to-source>\n"
  28. " cmake-gui [options] <path-to-existing-build>\n"
  29. " cmake-gui [options] -S <path-to-source> -B <path-to-build>\n"
  30. " cmake-gui [options] --browse-manual\n" },
  31. { nullptr, nullptr }
  32. };
  33. static const char* cmDocumentationOptions[][2] = {
  34. { "-S <path-to-source>", "Explicitly specify a source directory." },
  35. { "-B <path-to-build>", "Explicitly specify a build directory." },
  36. { "--preset=<preset>", "Specify a configure preset." },
  37. { nullptr, nullptr }
  38. };
  39. #if defined(Q_OS_MAC)
  40. static int cmOSXInstall(std::string dir);
  41. static void cmAddPluginPath();
  42. #endif
  43. #if defined(USE_QXcbIntegrationPlugin)
  44. Q_IMPORT_PLUGIN(QXcbIntegrationPlugin);
  45. #endif
  46. #if defined(USE_QWindowsIntegrationPlugin)
  47. Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
  48. # if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
  49. Q_IMPORT_PLUGIN(QWindowsVistaStylePlugin);
  50. # endif
  51. #endif
  52. int CMakeGUIExec(CMakeSetupDialog* window);
  53. void SetupDefaultQSettings();
  54. void OpenReferenceManual();
  55. int main(int argc, char** argv)
  56. {
  57. cmSystemTools::EnsureStdPipes();
  58. cmsys::Encoding::CommandLineArguments encoding_args =
  59. cmsys::Encoding::CommandLineArguments::Main(argc, argv);
  60. int argc2 = encoding_args.argc();
  61. char const* const* argv2 = encoding_args.argv();
  62. cmSystemTools::InitializeLibUV();
  63. cmSystemTools::FindCMakeResources(argv2[0]);
  64. // check docs first so that X is not need to get docs
  65. // do docs, if args were given
  66. cmDocumentation doc;
  67. doc.addCMakeStandardDocSections();
  68. if (argc2 > 1 && doc.CheckOptions(argc2, argv2)) {
  69. // Construct and print requested documentation.
  70. cmake hcm(cmake::RoleInternal, cmState::Unknown);
  71. hcm.SetHomeDirectory("");
  72. hcm.SetHomeOutputDirectory("");
  73. hcm.AddCMakePaths();
  74. auto generators = hcm.GetGeneratorsDocumentation();
  75. doc.SetName("cmake");
  76. doc.SetSection("Name", cmDocumentationName);
  77. doc.SetSection("Usage", cmDocumentationUsage);
  78. doc.AppendSection("Generators", generators);
  79. doc.PrependSection("Options", cmDocumentationOptions);
  80. return (doc.PrintRequestedDocumentation(std::cout) ? 0 : 1);
  81. }
  82. #if defined(Q_OS_MAC)
  83. if (argc2 == 2 && strcmp(argv2[1], "--install") == 0) {
  84. return cmOSXInstall("/usr/local/bin");
  85. }
  86. if (argc2 == 2 && cmHasLiteralPrefix(argv2[1], "--install=")) {
  87. return cmOSXInstall(argv2[1] + 10);
  88. }
  89. #endif
  90. // When we are on OSX and we are launching cmake-gui from a symlink, the
  91. // application will fail to launch as it can't find the qt.conf file which
  92. // tells it what the name of the plugin folder is. We need to add this path
  93. // BEFORE the application is constructed as that is what triggers the
  94. // searching for the platform plugins
  95. #if defined(Q_OS_MAC)
  96. cmAddPluginPath();
  97. #endif
  98. #if QT_VERSION >= 0x050600
  99. QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  100. #endif
  101. SetupDefaultQSettings();
  102. QApplication app(argc, argv);
  103. setlocale(LC_NUMERIC, "C");
  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(QIcon::fromTheme("cmake-gui", appIcon));
  124. CMakeSetupDialog dialog;
  125. dialog.show();
  126. QStringList args = QApplication::arguments();
  127. std::string binaryDirectory;
  128. std::string sourceDirectory;
  129. std::string presetName;
  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. } else if (arg.startsWith("--preset=")) {
  167. QString preset = arg.mid(cmStrLen("--preset="));
  168. if (preset.isEmpty()) {
  169. std::cerr << "No preset specified for --preset" << std::endl;
  170. return 1;
  171. }
  172. presetName = preset.toLocal8Bit().data();
  173. } else if (arg == "--browse-manual") {
  174. OpenReferenceManual();
  175. return 0;
  176. }
  177. }
  178. if (!sourceDirectory.empty() &&
  179. (!binaryDirectory.empty() || !presetName.empty())) {
  180. dialog.setSourceDirectory(QString::fromLocal8Bit(sourceDirectory.c_str()));
  181. if (!binaryDirectory.empty()) {
  182. dialog.setBinaryDirectory(
  183. QString::fromLocal8Bit(binaryDirectory.c_str()));
  184. if (!presetName.empty()) {
  185. dialog.setStartupBinaryDirectory(true);
  186. }
  187. }
  188. if (!presetName.empty()) {
  189. dialog.setDeferredPreset(QString::fromLocal8Bit(presetName.c_str()));
  190. }
  191. } else {
  192. if (args.count() == 2) {
  193. std::string filePath =
  194. cmSystemTools::CollapseFullPath(args[1].toLocal8Bit().data());
  195. // check if argument is a directory containing CMakeCache.txt
  196. std::string buildFilePath = cmStrCat(filePath, "/CMakeCache.txt");
  197. // check if argument is a CMakeCache.txt file
  198. if (cmSystemTools::GetFilenameName(filePath) == "CMakeCache.txt" &&
  199. cmSystemTools::FileExists(filePath.c_str())) {
  200. buildFilePath = filePath;
  201. }
  202. // check if argument is a directory containing CMakeLists.txt
  203. std::string srcFilePath = cmStrCat(filePath, "/CMakeLists.txt");
  204. if (cmSystemTools::FileExists(buildFilePath.c_str())) {
  205. dialog.setBinaryDirectory(QString::fromLocal8Bit(
  206. cmSystemTools::GetFilenamePath(buildFilePath).c_str()));
  207. } else if (cmSystemTools::FileExists(srcFilePath.c_str())) {
  208. dialog.setSourceDirectory(QString::fromLocal8Bit(filePath.c_str()));
  209. dialog.setBinaryDirectory(QString::fromLocal8Bit(
  210. cmSystemTools::CollapseFullPath(".").c_str()));
  211. }
  212. }
  213. }
  214. return CMakeGUIExec(&dialog);
  215. }
  216. #if defined(Q_OS_MAC)
  217. # include <cerrno>
  218. # include <cstring>
  219. # include <unistd.h>
  220. # include "cm_sys_stat.h"
  221. static bool cmOSXInstall(std::string const& dir, std::string const& tool)
  222. {
  223. if (tool.empty()) {
  224. return true;
  225. }
  226. std::string link = dir + cmSystemTools::GetFilenameName(tool);
  227. struct stat st;
  228. if (lstat(link.c_str(), &st) == 0 && S_ISLNK(st.st_mode)) {
  229. char buf[4096];
  230. ssize_t s = readlink(link.c_str(), buf, sizeof(buf) - 1);
  231. if (s >= 0 && std::string(buf, s) == tool) {
  232. std::cerr << "Exists: '" << link << "' -> '" << tool << "'\n";
  233. return true;
  234. }
  235. }
  236. cmSystemTools::MakeDirectory(dir);
  237. if (symlink(tool.c_str(), link.c_str()) == 0) {
  238. std::cerr << "Linked: '" << link << "' -> '" << tool << "'\n";
  239. return true;
  240. }
  241. int err = errno;
  242. std::cerr << "Failed: '" << link << "' -> '" << tool
  243. << "': " << strerror(err) << "\n";
  244. return false;
  245. }
  246. static int cmOSXInstall(std::string dir)
  247. {
  248. if (!cmHasLiteralSuffix(dir, "/")) {
  249. dir += "/";
  250. }
  251. return (cmOSXInstall(dir, cmSystemTools::GetCMakeCommand()) &&
  252. cmOSXInstall(dir, cmSystemTools::GetCTestCommand()) &&
  253. cmOSXInstall(dir, cmSystemTools::GetCPackCommand()) &&
  254. cmOSXInstall(dir, cmSystemTools::GetCMakeGUICommand()) &&
  255. cmOSXInstall(dir, cmSystemTools::GetCMakeCursesCommand()))
  256. ? 0
  257. : 1;
  258. }
  259. // Locate the PlugIns directory and add it to the QApplication library paths.
  260. // We need to resolve all symlinks so we have a known relative path between
  261. // MacOS/CMake and the PlugIns directory.
  262. //
  263. // Note we are using cmSystemTools since Qt can't provide the path to the
  264. // executable before the QApplication is created, and that is when plugin
  265. // searching occurs.
  266. static void cmAddPluginPath()
  267. {
  268. std::string const& path = cmSystemTools::GetCMakeGUICommand();
  269. if (path.empty()) {
  270. return;
  271. }
  272. std::string const& realPath = cmSystemTools::GetRealPath(path);
  273. QFileInfo appPath(QString::fromLocal8Bit(realPath.c_str()));
  274. QDir pluginDir = appPath.dir();
  275. bool const foundPluginDir = pluginDir.cd("../PlugIns");
  276. if (foundPluginDir) {
  277. QApplication::addLibraryPath(pluginDir.path());
  278. }
  279. }
  280. #endif