CMakeSetup.cxx 7.4 KB

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