CMakeSetup.cxx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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] = { { 0,
  28. " cmake-gui - CMake GUI." },
  29. { 0, 0 } };
  30. static const char* cmDocumentationUsage[][2] = {
  31. { 0, " cmake-gui [options]\n"
  32. " cmake-gui [options] <path-to-source>\n"
  33. " cmake-gui [options] <path-to-existing-build>" },
  34. { 0, 0 }
  35. };
  36. static const char* cmDocumentationOptions[][2] = { { 0, 0 } };
  37. #if defined(Q_OS_MAC)
  38. static int cmOSXInstall(std::string dir);
  39. static void cmAddPluginPath();
  40. #endif
  41. #if defined(USE_QXcbIntegrationPlugin)
  42. Q_IMPORT_PLUGIN(QXcbIntegrationPlugin);
  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::FindCMakeResources(argv2[0]);
  51. // check docs first so that X is not need to get docs
  52. // do docs, if args were given
  53. cmDocumentation doc;
  54. doc.addCMakeStandardDocSections();
  55. if (argc2 > 1 && doc.CheckOptions(argc2, argv2)) {
  56. // Construct and print requested documentation.
  57. cmake hcm;
  58. hcm.SetHomeDirectory("");
  59. hcm.SetHomeOutputDirectory("");
  60. hcm.AddCMakePaths();
  61. std::vector<cmDocumentationEntry> generators;
  62. hcm.GetGeneratorDocumentation(generators);
  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. QApplication app(argc, argv);
  87. setlocale(LC_NUMERIC, "C");
  88. #if defined(CMAKE_ENCODING_UTF8)
  89. QTextCodec* utf8_codec = QTextCodec::codecForName("UTF-8");
  90. QTextCodec::setCodecForLocale(utf8_codec);
  91. #endif
  92. // clean out standard Qt paths for plugins, which we don't use anyway
  93. // when creating Mac bundles, it potentially causes problems
  94. foreach (QString p, QApplication::libraryPaths()) {
  95. QApplication::removeLibraryPath(p);
  96. }
  97. // tell the cmake library where cmake is
  98. QDir cmExecDir(QApplication::applicationDirPath());
  99. #if defined(Q_OS_MAC)
  100. cmExecDir.cd("../../../");
  101. #endif
  102. // pick up translation files if they exists in the data directory
  103. QDir translationsDir = cmExecDir;
  104. translationsDir.cd(QString::fromLocal8Bit(".." CMAKE_DATA_DIR));
  105. translationsDir.cd("i18n");
  106. QTranslator translator;
  107. QString transfile = QString("cmake_%1").arg(QLocale::system().name());
  108. translator.load(transfile, translationsDir.path());
  109. app.installTranslator(&translator);
  110. // app setup
  111. app.setApplicationName("CMakeSetup");
  112. app.setOrganizationName("Kitware");
  113. QIcon appIcon;
  114. appIcon.addFile(":/Icons/CMakeSetup32.png");
  115. appIcon.addFile(":/Icons/CMakeSetup128.png");
  116. app.setWindowIcon(appIcon);
  117. CMakeSetupDialog dialog;
  118. dialog.show();
  119. cmsys::CommandLineArguments arg;
  120. arg.Initialize(argc2, argv2);
  121. std::string binaryDirectory;
  122. std::string sourceDirectory;
  123. typedef cmsys::CommandLineArguments argT;
  124. arg.AddArgument("-B", argT::CONCAT_ARGUMENT, &binaryDirectory,
  125. "Binary Directory");
  126. arg.AddArgument("-H", argT::CONCAT_ARGUMENT, &sourceDirectory,
  127. "Source Directory");
  128. // do not complain about unknown options
  129. arg.StoreUnusedArguments(true);
  130. arg.Parse();
  131. if (!sourceDirectory.empty() && !binaryDirectory.empty()) {
  132. dialog.setSourceDirectory(QString::fromLocal8Bit(sourceDirectory.c_str()));
  133. dialog.setBinaryDirectory(QString::fromLocal8Bit(binaryDirectory.c_str()));
  134. } else {
  135. QStringList args = app.arguments();
  136. if (args.count() == 2) {
  137. std::string filePath =
  138. cmSystemTools::CollapseFullPath(args[1].toLocal8Bit().data());
  139. // check if argument is a directory containing CMakeCache.txt
  140. std::string buildFilePath =
  141. cmSystemTools::CollapseFullPath("CMakeCache.txt", filePath.c_str());
  142. // check if argument is a CMakeCache.txt file
  143. if (cmSystemTools::GetFilenameName(filePath) == "CMakeCache.txt" &&
  144. cmSystemTools::FileExists(filePath.c_str())) {
  145. buildFilePath = filePath;
  146. }
  147. // check if argument is a directory containing CMakeLists.txt
  148. std::string srcFilePath =
  149. cmSystemTools::CollapseFullPath("CMakeLists.txt", filePath.c_str());
  150. if (cmSystemTools::FileExists(buildFilePath.c_str())) {
  151. dialog.setBinaryDirectory(QString::fromLocal8Bit(
  152. cmSystemTools::GetFilenamePath(buildFilePath).c_str()));
  153. } else if (cmSystemTools::FileExists(srcFilePath.c_str())) {
  154. dialog.setSourceDirectory(QString::fromLocal8Bit(filePath.c_str()));
  155. dialog.setBinaryDirectory(QString::fromLocal8Bit(
  156. cmSystemTools::CollapseFullPath(".").c_str()));
  157. }
  158. }
  159. }
  160. return app.exec();
  161. }
  162. #if defined(Q_OS_MAC)
  163. #include <errno.h>
  164. #include <string.h>
  165. #include <sys/stat.h>
  166. #include <unistd.h>
  167. static bool cmOSXInstall(std::string const& dir, std::string const& tool)
  168. {
  169. if (tool.empty()) {
  170. return true;
  171. }
  172. std::string link = dir + cmSystemTools::GetFilenameName(tool);
  173. struct stat st;
  174. if (lstat(link.c_str(), &st) == 0 && S_ISLNK(st.st_mode)) {
  175. char buf[4096];
  176. ssize_t s = readlink(link.c_str(), buf, sizeof(buf) - 1);
  177. if (s >= 0 && std::string(buf, s) == tool) {
  178. std::cerr << "Exists: '" << link << "' -> '" << tool << "'\n";
  179. return true;
  180. }
  181. }
  182. cmSystemTools::MakeDirectory(dir);
  183. if (symlink(tool.c_str(), link.c_str()) == 0) {
  184. std::cerr << "Linked: '" << link << "' -> '" << tool << "'\n";
  185. return true;
  186. } else {
  187. int err = errno;
  188. std::cerr << "Failed: '" << link << "' -> '" << tool
  189. << "': " << strerror(err) << "\n";
  190. return false;
  191. }
  192. }
  193. static int cmOSXInstall(std::string dir)
  194. {
  195. if (!cmHasLiteralSuffix(dir, "/")) {
  196. dir += "/";
  197. }
  198. return (cmOSXInstall(dir, cmSystemTools::GetCMakeCommand()) &&
  199. cmOSXInstall(dir, cmSystemTools::GetCTestCommand()) &&
  200. cmOSXInstall(dir, cmSystemTools::GetCPackCommand()) &&
  201. cmOSXInstall(dir, cmSystemTools::GetCMakeGUICommand()) &&
  202. cmOSXInstall(dir, cmSystemTools::GetCMakeCursesCommand()))
  203. ? 0
  204. : 1;
  205. }
  206. // Locate the PlugIns directory and add it to the QApplication library paths.
  207. // We need to resolve all symlinks so we have a known relative path between
  208. // MacOS/CMake and the PlugIns directory.
  209. //
  210. // Note we are using cmSystemTools since Qt can't provide the path to the
  211. // executable before the QApplication is created, and that is when plugin
  212. // searching occurs.
  213. static void cmAddPluginPath()
  214. {
  215. std::string const& path = cmSystemTools::GetCMakeGUICommand();
  216. if (path.empty()) {
  217. return;
  218. }
  219. std::string const& realPath = cmSystemTools::GetRealPath(path);
  220. QFileInfo appPath(QString::fromLocal8Bit(realPath.c_str()));
  221. QDir pluginDir = appPath.dir();
  222. bool const foundPluginDir = pluginDir.cd("../PlugIns");
  223. if (foundPluginDir) {
  224. QApplication::addLibraryPath(pluginDir.path());
  225. }
  226. }
  227. #endif