CMakeSetup.cxx 9.4 KB

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