ccmake.cxx 5.4 KB

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