ccmake.cxx 5.7 KB

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