CMakeSetup.cxx 8.5 KB

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