CMakeSetup.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "QCMake.h" // include to disable MS warnings
  11. #include "CMakeSetupDialog.h"
  12. #include "cmAlgorithms.h"
  13. #include "cmDocumentation.h"
  14. #include "cmVersion.h"
  15. #include "cmake.h"
  16. #include <QApplication>
  17. #include <QDir>
  18. #include <QLocale>
  19. #include <QString>
  20. #include <QTextCodec>
  21. #include <QTranslator>
  22. #include <QtPlugin>
  23. #include <cmsys/CommandLineArguments.hxx>
  24. #include <cmsys/Encoding.hxx>
  25. #include <cmsys/SystemTools.hxx>
  26. #include "cmSystemTools.h" // IWYU pragma: keep
  27. static const char* cmDocumentationName[][2] = { { CM_NULLPTR,
  28. " cmake-gui - CMake GUI." },
  29. { CM_NULLPTR, CM_NULLPTR } };
  30. static const char* cmDocumentationUsage[][2] = {
  31. { CM_NULLPTR, " cmake-gui [options]\n"
  32. " cmake-gui [options] <path-to-source>\n"
  33. " cmake-gui [options] <path-to-existing-build>" },
  34. { CM_NULLPTR, CM_NULLPTR }
  35. };
  36. static const char* cmDocumentationOptions[]
  37. [2] = { { CM_NULLPTR, CM_NULLPTR } };
  38. #if defined(Q_OS_MAC)
  39. static int cmOSXInstall(std::string dir);
  40. static void cmAddPluginPath();
  41. #endif
  42. #if defined(USE_QXcbIntegrationPlugin)
  43. Q_IMPORT_PLUGIN(QXcbIntegrationPlugin);
  44. #endif
  45. int main(int argc, char** argv)
  46. {
  47. cmsys::Encoding::CommandLineArguments encoding_args =
  48. cmsys::Encoding::CommandLineArguments::Main(argc, argv);
  49. int argc2 = encoding_args.argc();
  50. char const* const* argv2 = encoding_args.argv();
  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;
  59. hcm.SetHomeDirectory("");
  60. hcm.SetHomeOutputDirectory("");
  61. hcm.AddCMakePaths();
  62. std::vector<cmDocumentationEntry> generators;
  63. hcm.GetGeneratorDocumentation(generators);
  64. doc.SetName("cmake");
  65. doc.SetSection("Name", cmDocumentationName);
  66. doc.SetSection("Usage", cmDocumentationUsage);
  67. doc.AppendSection("Generators", generators);
  68. doc.PrependSection("Options", cmDocumentationOptions);
  69. return (doc.PrintRequestedDocumentation(std::cout) ? 0 : 1);
  70. }
  71. #if defined(Q_OS_MAC)
  72. if (argc2 == 2 && strcmp(argv2[1], "--install") == 0) {
  73. return cmOSXInstall("/usr/local/bin");
  74. }
  75. if (argc2 == 2 && cmHasLiteralPrefix(argv2[1], "--install=")) {
  76. return cmOSXInstall(argv2[1] + 10);
  77. }
  78. #endif
  79. // When we are on OSX and we are launching cmake-gui from a symlink, the
  80. // application will fail to launch as it can't find the qt.conf file which
  81. // tells it what the name of the plugin folder is. We need to add this path
  82. // BEFORE the application is constructed as that is what triggers the
  83. // searching for the platform plugins
  84. #if defined(Q_OS_MAC)
  85. cmAddPluginPath();
  86. #endif
  87. QApplication app(argc, argv);
  88. setlocale(LC_NUMERIC, "C");
  89. #if defined(CMAKE_ENCODING_UTF8)
  90. QTextCodec* utf8_codec = QTextCodec::codecForName("UTF-8");
  91. QTextCodec::setCodecForLocale(utf8_codec);
  92. #endif
  93. // clean out standard Qt paths for plugins, which we don't use anyway
  94. // when creating Mac bundles, it potentially causes problems
  95. foreach (QString p, QApplication::libraryPaths()) {
  96. QApplication::removeLibraryPath(p);
  97. }
  98. // tell the cmake library where cmake is
  99. QDir cmExecDir(QApplication::applicationDirPath());
  100. #if defined(Q_OS_MAC)
  101. cmExecDir.cd("../../../");
  102. #endif
  103. // pick up translation files if they exists in the data directory
  104. QDir translationsDir = cmExecDir;
  105. translationsDir.cd(QString::fromLocal8Bit(".." CMAKE_DATA_DIR));
  106. translationsDir.cd("i18n");
  107. QTranslator translator;
  108. QString transfile = QString("cmake_%1").arg(QLocale::system().name());
  109. translator.load(transfile, translationsDir.path());
  110. app.installTranslator(&translator);
  111. // app setup
  112. app.setApplicationName("CMakeSetup");
  113. app.setOrganizationName("Kitware");
  114. QIcon appIcon;
  115. appIcon.addFile(":/Icons/CMakeSetup32.png");
  116. appIcon.addFile(":/Icons/CMakeSetup128.png");
  117. app.setWindowIcon(appIcon);
  118. CMakeSetupDialog dialog;
  119. dialog.show();
  120. cmsys::CommandLineArguments arg;
  121. arg.Initialize(argc2, argv2);
  122. std::string binaryDirectory;
  123. std::string sourceDirectory;
  124. typedef cmsys::CommandLineArguments argT;
  125. arg.AddArgument("-B", argT::CONCAT_ARGUMENT, &binaryDirectory,
  126. "Binary Directory");
  127. arg.AddArgument("-H", argT::CONCAT_ARGUMENT, &sourceDirectory,
  128. "Source Directory");
  129. // do not complain about unknown options
  130. arg.StoreUnusedArguments(true);
  131. arg.Parse();
  132. if (!sourceDirectory.empty() && !binaryDirectory.empty()) {
  133. dialog.setSourceDirectory(QString::fromLocal8Bit(sourceDirectory.c_str()));
  134. dialog.setBinaryDirectory(QString::fromLocal8Bit(binaryDirectory.c_str()));
  135. } else {
  136. QStringList args = app.arguments();
  137. if (args.count() == 2) {
  138. std::string filePath =
  139. cmSystemTools::CollapseFullPath(args[1].toLocal8Bit().data());
  140. // check if argument is a directory containing CMakeCache.txt
  141. std::string buildFilePath =
  142. cmSystemTools::CollapseFullPath("CMakeCache.txt", filePath.c_str());
  143. // check if argument is a CMakeCache.txt file
  144. if (cmSystemTools::GetFilenameName(filePath) == "CMakeCache.txt" &&
  145. cmSystemTools::FileExists(filePath.c_str())) {
  146. buildFilePath = filePath;
  147. }
  148. // check if argument is a directory containing CMakeLists.txt
  149. std::string srcFilePath =
  150. cmSystemTools::CollapseFullPath("CMakeLists.txt", filePath.c_str());
  151. if (cmSystemTools::FileExists(buildFilePath.c_str())) {
  152. dialog.setBinaryDirectory(QString::fromLocal8Bit(
  153. cmSystemTools::GetFilenamePath(buildFilePath).c_str()));
  154. } else if (cmSystemTools::FileExists(srcFilePath.c_str())) {
  155. dialog.setSourceDirectory(QString::fromLocal8Bit(filePath.c_str()));
  156. dialog.setBinaryDirectory(QString::fromLocal8Bit(
  157. cmSystemTools::CollapseFullPath(".").c_str()));
  158. }
  159. }
  160. }
  161. return app.exec();
  162. }
  163. #if defined(Q_OS_MAC)
  164. #include <errno.h>
  165. #include <string.h>
  166. #include <sys/stat.h>
  167. #include <unistd.h>
  168. static bool cmOSXInstall(std::string const& dir, std::string const& tool)
  169. {
  170. if (tool.empty()) {
  171. return true;
  172. }
  173. std::string link = dir + cmSystemTools::GetFilenameName(tool);
  174. struct stat st;
  175. if (lstat(link.c_str(), &st) == 0 && S_ISLNK(st.st_mode)) {
  176. char buf[4096];
  177. ssize_t s = readlink(link.c_str(), buf, sizeof(buf) - 1);
  178. if (s >= 0 && std::string(buf, s) == tool) {
  179. std::cerr << "Exists: '" << link << "' -> '" << tool << "'\n";
  180. return true;
  181. }
  182. }
  183. cmSystemTools::MakeDirectory(dir);
  184. if (symlink(tool.c_str(), link.c_str()) == 0) {
  185. std::cerr << "Linked: '" << link << "' -> '" << tool << "'\n";
  186. return true;
  187. } else {
  188. int err = errno;
  189. std::cerr << "Failed: '" << link << "' -> '" << tool
  190. << "': " << strerror(err) << "\n";
  191. return false;
  192. }
  193. }
  194. static int cmOSXInstall(std::string dir)
  195. {
  196. if (!cmHasLiteralSuffix(dir, "/")) {
  197. dir += "/";
  198. }
  199. return (cmOSXInstall(dir, cmSystemTools::GetCMakeCommand()) &&
  200. cmOSXInstall(dir, cmSystemTools::GetCTestCommand()) &&
  201. cmOSXInstall(dir, cmSystemTools::GetCPackCommand()) &&
  202. cmOSXInstall(dir, cmSystemTools::GetCMakeGUICommand()) &&
  203. cmOSXInstall(dir, cmSystemTools::GetCMakeCursesCommand()))
  204. ? 0
  205. : 1;
  206. }
  207. // Locate the PlugIns directory and add it to the QApplication library paths.
  208. // We need to resolve all symlinks so we have a known relative path between
  209. // MacOS/CMake and the PlugIns directory.
  210. //
  211. // Note we are using cmSystemTools since Qt can't provide the path to the
  212. // executable before the QApplication is created, and that is when plugin
  213. // searching occurs.
  214. static void cmAddPluginPath()
  215. {
  216. std::string const& path = cmSystemTools::GetCMakeGUICommand();
  217. if (path.empty()) {
  218. return;
  219. }
  220. std::string const& realPath = cmSystemTools::GetRealPath(path);
  221. QFileInfo appPath(QString::fromLocal8Bit(realPath.c_str()));
  222. QDir pluginDir = appPath.dir();
  223. bool const foundPluginDir = pluginDir.cd("../PlugIns");
  224. if (foundPluginDir) {
  225. QApplication::addLibraryPath(pluginDir.path());
  226. }
  227. }
  228. #endif