ccmake.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "cmCursesForm.h"
  4. #include "cmCursesMainForm.h"
  5. #include "cmCursesStandardIncludes.h"
  6. #include "cmDocumentation.h"
  7. #include "cmDocumentationEntry.h" // IWYU pragma: keep
  8. #include "cmState.h"
  9. #include "cmSystemTools.h"
  10. #include "cmake.h"
  11. #include "cmsys/Encoding.hxx"
  12. #include <iostream>
  13. #include <signal.h>
  14. #include <string.h>
  15. #include <string>
  16. #include <vector>
  17. static const char* cmDocumentationName[][2] = {
  18. { nullptr, " ccmake - Curses Interface for CMake." },
  19. { nullptr, nullptr }
  20. };
  21. static const char* cmDocumentationUsage[][2] = {
  22. { nullptr,
  23. " ccmake <path-to-source>\n"
  24. " ccmake <path-to-existing-build>" },
  25. { nullptr,
  26. "Specify a source directory to (re-)generate a build system for "
  27. "it in the current working directory. Specify an existing build "
  28. "directory to re-generate its build system." },
  29. { nullptr, nullptr }
  30. };
  31. static const char* cmDocumentationUsageNote[][2] = {
  32. { nullptr, "Run 'ccmake --help' for more information." },
  33. { nullptr, nullptr }
  34. };
  35. static const char* cmDocumentationOptions[][2] = {
  36. CMAKE_STANDARD_OPTIONS_TABLE,
  37. { nullptr, nullptr }
  38. };
  39. cmCursesForm* cmCursesForm::CurrentForm = nullptr;
  40. extern "C" {
  41. void onsig(int /*unused*/)
  42. {
  43. if (cmCursesForm::CurrentForm) {
  44. endwin();
  45. initscr(); /* Initialization */
  46. noecho(); /* Echo off */
  47. cbreak(); /* nl- or cr not needed */
  48. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  49. refresh();
  50. int x;
  51. int y;
  52. getmaxyx(stdscr, y, x);
  53. cmCursesForm::CurrentForm->Render(1, 1, x, y);
  54. cmCursesForm::CurrentForm->UpdateStatusBar();
  55. }
  56. signal(SIGWINCH, onsig);
  57. }
  58. }
  59. int main(int argc, char const* const* argv)
  60. {
  61. cmSystemTools::EnsureStdPipes();
  62. cmsys::Encoding::CommandLineArguments encoding_args =
  63. cmsys::Encoding::CommandLineArguments::Main(argc, argv);
  64. argc = encoding_args.argc();
  65. argv = encoding_args.argv();
  66. cmSystemTools::InitializeLibUV();
  67. cmSystemTools::FindCMakeResources(argv[0]);
  68. cmDocumentation doc;
  69. doc.addCMakeStandardDocSections();
  70. if (doc.CheckOptions(argc, argv)) {
  71. cmake hcm(cmake::RoleInternal, cmState::Unknown);
  72. hcm.SetHomeDirectory("");
  73. hcm.SetHomeOutputDirectory("");
  74. hcm.AddCMakePaths();
  75. auto generators = hcm.GetGeneratorsDocumentation();
  76. doc.SetName("ccmake");
  77. doc.SetSection("Name", cmDocumentationName);
  78. doc.SetSection("Usage", cmDocumentationUsage);
  79. if (argc == 1) {
  80. doc.AppendSection("Usage", cmDocumentationUsageNote);
  81. }
  82. doc.AppendSection("Generators", generators);
  83. doc.PrependSection("Options", cmDocumentationOptions);
  84. return doc.PrintRequestedDocumentation(std::cout) ? 0 : 1;
  85. }
  86. bool debug = false;
  87. unsigned int i;
  88. int j;
  89. std::vector<std::string> args;
  90. for (j = 0; j < argc; ++j) {
  91. if (strcmp(argv[j], "-debug") == 0) {
  92. debug = true;
  93. } else {
  94. args.emplace_back(argv[j]);
  95. }
  96. }
  97. std::string cacheDir = cmSystemTools::GetCurrentWorkingDirectory();
  98. for (i = 1; i < args.size(); ++i) {
  99. std::string arg = args[i];
  100. if (arg.find("-B", 0) == 0) {
  101. cacheDir = arg.substr(2);
  102. }
  103. }
  104. cmSystemTools::DisableRunCommandOutput();
  105. if (debug) {
  106. cmCursesForm::DebugStart();
  107. }
  108. initscr(); /* Initialization */
  109. noecho(); /* Echo off */
  110. cbreak(); /* nl- or cr not needed */
  111. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  112. signal(SIGWINCH, onsig);
  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. cmSystemTools::SetMessageCallback(
  135. [myform](const std::string& message, const char* title) {
  136. myform->AddError(message, title);
  137. });
  138. cmCursesForm::CurrentForm = myform;
  139. myform->InitializeUI();
  140. if (myform->Configure(1) == 0) {
  141. myform->Render(1, 1, x, y);
  142. myform->HandleInput();
  143. }
  144. // Need to clean-up better
  145. curses_clear();
  146. touchwin(stdscr);
  147. endwin();
  148. delete cmCursesForm::CurrentForm;
  149. cmCursesForm::CurrentForm = nullptr;
  150. std::cout << std::endl << std::endl;
  151. return 0;
  152. }