ccmake.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <csignal>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10. #include "cmsys/Encoding.hxx"
  11. #include "cmCursesColor.h"
  12. #include "cmCursesForm.h"
  13. #include "cmCursesMainForm.h"
  14. #include "cmCursesStandardIncludes.h"
  15. #include "cmDocumentation.h"
  16. #include "cmDocumentationEntry.h"
  17. #include "cmMessageMetadata.h"
  18. #include "cmState.h"
  19. #include "cmStringAlgorithms.h"
  20. #include "cmSystemTools.h"
  21. #include "cmake.h"
  22. namespace {
  23. const cmDocumentationEntry cmDocumentationName = {
  24. {},
  25. " ccmake - Curses Interface for CMake."
  26. };
  27. const cmDocumentationEntry cmDocumentationUsage[2] = {
  28. { {},
  29. " ccmake <path-to-source>\n"
  30. " ccmake <path-to-existing-build>" },
  31. { {},
  32. "Specify a source directory to (re-)generate a build system for "
  33. "it in the current working directory. Specify an existing build "
  34. "directory to re-generate its build system." },
  35. };
  36. const cmDocumentationEntry cmDocumentationUsageNote = {
  37. {},
  38. "Run 'ccmake --help' for more information."
  39. };
  40. #ifndef _WIN32
  41. extern "C" {
  42. void onsig(int /*unused*/)
  43. {
  44. if (cmCursesForm::CurrentForm) {
  45. cmCursesForm::CurrentForm->HandleResize();
  46. }
  47. signal(SIGWINCH, onsig);
  48. }
  49. }
  50. #endif // _WIN32
  51. } // anonymous namespace
  52. cmCursesForm* cmCursesForm::CurrentForm = nullptr;
  53. int main(int argc, char const* const* argv)
  54. {
  55. cmSystemTools::EnsureStdPipes();
  56. cmsys::Encoding::CommandLineArguments encoding_args =
  57. cmsys::Encoding::CommandLineArguments::Main(argc, argv);
  58. argc = encoding_args.argc();
  59. argv = encoding_args.argv();
  60. cmSystemTools::InitializeLibUV();
  61. cmSystemTools::FindCMakeResources(argv[0]);
  62. cmDocumentation doc;
  63. doc.addCMakeStandardDocSections();
  64. if (doc.CheckOptions(argc, argv)) {
  65. cmake hcm(cmake::RoleInternal, cmState::Help);
  66. hcm.SetHomeDirectory("");
  67. hcm.SetHomeOutputDirectory("");
  68. hcm.AddCMakePaths();
  69. auto generators = hcm.GetGeneratorsDocumentation();
  70. doc.SetName("ccmake");
  71. doc.SetSection("Name", cmDocumentationName);
  72. doc.SetSection("Usage", cmDocumentationUsage);
  73. if (argc == 1) {
  74. doc.AppendSection("Usage", cmDocumentationUsageNote);
  75. }
  76. doc.AppendSection("Generators", generators);
  77. doc.PrependSection("Options", cmake::CMAKE_STANDARD_OPTIONS_TABLE);
  78. return !doc.PrintRequestedDocumentation(std::cout);
  79. }
  80. bool debug = false;
  81. unsigned int i;
  82. int j;
  83. std::vector<std::string> args;
  84. for (j = 0; j < argc; ++j) {
  85. if (strcmp(argv[j], "-debug") == 0) {
  86. debug = true;
  87. } else {
  88. args.emplace_back(argv[j]);
  89. }
  90. }
  91. std::string cacheDir = cmSystemTools::GetCurrentWorkingDirectory();
  92. for (i = 1; i < args.size(); ++i) {
  93. std::string const& arg = args[i];
  94. if (cmHasPrefix(arg, "-B")) {
  95. cacheDir = arg.substr(2);
  96. }
  97. }
  98. cmSystemTools::DisableRunCommandOutput();
  99. if (debug) {
  100. cmCursesForm::DebugStart();
  101. }
  102. if (!initscr()) {
  103. fprintf(stderr, "Error: ncurses initialization failed\n");
  104. exit(1);
  105. }
  106. noecho(); /* Echo off */
  107. cbreak(); /* nl- or cr not needed */
  108. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  109. cmCursesColor::InitColors();
  110. #ifndef _WIN32
  111. signal(SIGWINCH, onsig);
  112. #endif // _WIN32
  113. int x;
  114. int y;
  115. getmaxyx(stdscr, y, x);
  116. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  117. endwin();
  118. std::cerr << "Window is too small. A size of at least "
  119. << cmCursesMainForm::MIN_WIDTH << " x "
  120. << cmCursesMainForm::MIN_HEIGHT << " is required to run ccmake."
  121. << std::endl;
  122. return 1;
  123. }
  124. cmCursesMainForm* myform;
  125. myform = new cmCursesMainForm(args, x);
  126. if (myform->LoadCache(cacheDir.c_str())) {
  127. curses_clear();
  128. touchwin(stdscr);
  129. endwin();
  130. delete myform;
  131. std::cerr << "Error running cmake::LoadCache(). Aborting.\n";
  132. return 1;
  133. }
  134. /*
  135. * The message is stored in a list by the form which will be
  136. * joined by '\n' before display.
  137. * Removing any trailing '\n' avoid extra empty lines in the final results
  138. */
  139. auto cleanMessage = [](const std::string& message) -> std::string {
  140. auto msg = message;
  141. if (!msg.empty() && msg.back() == '\n') {
  142. msg.pop_back();
  143. }
  144. return msg;
  145. };
  146. cmSystemTools::SetMessageCallback(
  147. [&](const std::string& message, const cmMessageMetadata& md) {
  148. myform->AddError(cleanMessage(message), md.title);
  149. });
  150. cmSystemTools::SetStderrCallback([&](const std::string& message) {
  151. myform->AddError(cleanMessage(message), "");
  152. });
  153. cmSystemTools::SetStdoutCallback([&](const std::string& message) {
  154. myform->UpdateProgress(cleanMessage(message), -1);
  155. });
  156. cmCursesForm::CurrentForm = myform;
  157. myform->InitializeUI();
  158. if (myform->Configure(1) == 0) {
  159. myform->Render(1, 1, x, y);
  160. myform->HandleInput();
  161. }
  162. // Need to clean-up better
  163. curses_clear();
  164. touchwin(stdscr);
  165. endwin();
  166. delete cmCursesForm::CurrentForm;
  167. cmCursesForm::CurrentForm = nullptr;
  168. std::cout << std::endl << std::endl;
  169. return 0;
  170. }