ccmake.cxx 5.0 KB

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