ccmake.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.SetHomeDirectory("");
  68. hcm.SetHomeOutputDirectory("");
  69. hcm.AddCMakePaths();
  70. auto generators = hcm.GetGeneratorsDocumentation();
  71. doc.SetName("ccmake");
  72. doc.SetSection("Name", cmDocumentationName);
  73. doc.SetSection("Usage", cmDocumentationUsage);
  74. if (argc == 1) {
  75. doc.AppendSection("Usage", cmDocumentationUsageNote);
  76. }
  77. doc.AppendSection("Generators", generators);
  78. doc.PrependSection("Options", cmake::CMAKE_STANDARD_OPTIONS_TABLE);
  79. return !doc.PrintRequestedDocumentation(std::cout);
  80. }
  81. bool debug = false;
  82. unsigned int i;
  83. int j;
  84. std::vector<std::string> args;
  85. for (j = 0; j < argc; ++j) {
  86. if (strcmp(argv[j], "-debug") == 0) {
  87. debug = true;
  88. } else {
  89. args.emplace_back(argv[j]);
  90. }
  91. }
  92. std::string cacheDir = cmSystemTools::GetLogicalWorkingDirectory();
  93. for (i = 1; i < args.size(); ++i) {
  94. std::string const& arg = args[i];
  95. if (cmHasPrefix(arg, "-B")) {
  96. cacheDir = arg.substr(2);
  97. }
  98. }
  99. cmSystemTools::DisableRunCommandOutput();
  100. if (debug) {
  101. cmCursesForm::DebugStart();
  102. }
  103. if (!initscr()) {
  104. fprintf(stderr, "Error: ncurses initialization failed\n");
  105. exit(1);
  106. }
  107. noecho(); /* Echo off */
  108. cbreak(); /* nl- or cr not needed */
  109. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  110. cmCursesColor::InitColors();
  111. #ifndef _WIN32
  112. signal(SIGWINCH, onsig);
  113. #endif // _WIN32
  114. int x;
  115. int y;
  116. getmaxyx(stdscr, y, x);
  117. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  118. endwin();
  119. std::cerr << "Window is too small. A size of at least "
  120. << cmCursesMainForm::MIN_WIDTH << " x "
  121. << cmCursesMainForm::MIN_HEIGHT << " is required to run ccmake."
  122. << std::endl;
  123. return 1;
  124. }
  125. cmCursesMainForm* myform;
  126. myform = new cmCursesMainForm(args, x);
  127. if (myform->LoadCache(cacheDir.c_str())) {
  128. curses_clear();
  129. touchwin(stdscr);
  130. endwin();
  131. delete myform;
  132. std::cerr << "Error running cmake::LoadCache(). Aborting.\n";
  133. return 1;
  134. }
  135. /*
  136. * The message is stored in a list by the form which will be
  137. * joined by '\n' before display.
  138. * Removing any trailing '\n' avoid extra empty lines in the final results
  139. */
  140. auto cleanMessage = [](std::string const& message) -> std::string {
  141. auto msg = message;
  142. if (!msg.empty() && msg.back() == '\n') {
  143. msg.pop_back();
  144. }
  145. return msg;
  146. };
  147. cmSystemTools::SetMessageCallback(
  148. [&](std::string const& message, cmMessageMetadata const& md) {
  149. myform->AddError(cleanMessage(message), md.title);
  150. });
  151. cmSystemTools::SetStderrCallback([&](std::string const& message) {
  152. myform->AddError(cleanMessage(message), "");
  153. });
  154. cmSystemTools::SetStdoutCallback([&](std::string const& message) {
  155. myform->UpdateProgress(cleanMessage(message), -1);
  156. });
  157. cmCursesForm::CurrentForm = myform;
  158. myform->InitializeUI();
  159. if (myform->Configure(1) == 0) {
  160. myform->Render(1, 1, x, y);
  161. myform->HandleInput();
  162. }
  163. // Need to clean-up better
  164. curses_clear();
  165. touchwin(stdscr);
  166. endwin();
  167. delete cmCursesForm::CurrentForm;
  168. cmCursesForm::CurrentForm = nullptr;
  169. std::cout << std::endl << std::endl;
  170. return 0;
  171. }