CMakeSetup.cxx 9.9 KB

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