ccmake.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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"
  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, y;
  51. getmaxyx(stdscr, y, x);
  52. cmCursesForm::CurrentForm->Render(1, 1, x, y);
  53. cmCursesForm::CurrentForm->UpdateStatusBar();
  54. }
  55. signal(SIGWINCH, onsig);
  56. }
  57. }
  58. void CMakeMessageHandler(const char* message, const char* title,
  59. bool& /*unused*/, void* clientData)
  60. {
  61. cmCursesForm* self = static_cast<cmCursesForm*>(clientData);
  62. self->AddError(message, title);
  63. }
  64. int main(int argc, char const* const* argv)
  65. {
  66. cmsys::Encoding::CommandLineArguments encoding_args =
  67. cmsys::Encoding::CommandLineArguments::Main(argc, argv);
  68. argc = encoding_args.argc();
  69. argv = encoding_args.argv();
  70. cmSystemTools::InitializeLibUV();
  71. cmSystemTools::FindCMakeResources(argv[0]);
  72. cmDocumentation doc;
  73. doc.addCMakeStandardDocSections();
  74. if (doc.CheckOptions(argc, argv)) {
  75. cmake hcm(cmake::RoleInternal, cmState::Unknown);
  76. hcm.SetHomeDirectory("");
  77. hcm.SetHomeOutputDirectory("");
  78. hcm.AddCMakePaths();
  79. std::vector<cmDocumentationEntry> generators;
  80. hcm.GetGeneratorDocumentation(generators);
  81. doc.SetName("ccmake");
  82. doc.SetSection("Name", cmDocumentationName);
  83. doc.SetSection("Usage", cmDocumentationUsage);
  84. if (argc == 1) {
  85. doc.AppendSection("Usage", cmDocumentationUsageNote);
  86. }
  87. doc.SetSection("Generators", generators);
  88. doc.PrependSection("Options", cmDocumentationOptions);
  89. return doc.PrintRequestedDocumentation(std::cout) ? 0 : 1;
  90. }
  91. bool debug = false;
  92. unsigned int i;
  93. int j;
  94. std::vector<std::string> args;
  95. for (j = 0; j < argc; ++j) {
  96. if (strcmp(argv[j], "-debug") == 0) {
  97. debug = true;
  98. } else {
  99. args.emplace_back(argv[j]);
  100. }
  101. }
  102. std::string cacheDir = cmSystemTools::GetCurrentWorkingDirectory();
  103. for (i = 1; i < args.size(); ++i) {
  104. std::string arg = args[i];
  105. if (arg.find("-B", 0) == 0) {
  106. cacheDir = arg.substr(2);
  107. }
  108. }
  109. cmSystemTools::DisableRunCommandOutput();
  110. if (debug) {
  111. cmCursesForm::DebugStart();
  112. }
  113. initscr(); /* Initialization */
  114. noecho(); /* Echo off */
  115. cbreak(); /* nl- or cr not needed */
  116. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  117. signal(SIGWINCH, onsig);
  118. int x, y;
  119. getmaxyx(stdscr, y, x);
  120. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  121. endwin();
  122. std::cerr << "Window is too small. A size of at least "
  123. << cmCursesMainForm::MIN_WIDTH << " x "
  124. << cmCursesMainForm::MIN_HEIGHT << " is required to run ccmake."
  125. << std::endl;
  126. return 1;
  127. }
  128. cmCursesMainForm* myform;
  129. myform = new cmCursesMainForm(args, x);
  130. if (myform->LoadCache(cacheDir.c_str())) {
  131. curses_clear();
  132. touchwin(stdscr);
  133. endwin();
  134. delete myform;
  135. std::cerr << "Error running cmake::LoadCache(). Aborting.\n";
  136. return 1;
  137. }
  138. cmSystemTools::SetMessageCallback(CMakeMessageHandler, myform);
  139. cmCursesForm::CurrentForm = myform;
  140. myform->InitializeUI();
  141. if (myform->Configure(1) == 0) {
  142. myform->Render(1, 1, x, y);
  143. myform->HandleInput();
  144. }
  145. // Need to clean-up better
  146. curses_clear();
  147. touchwin(stdscr);
  148. endwin();
  149. delete cmCursesForm::CurrentForm;
  150. cmCursesForm::CurrentForm = nullptr;
  151. std::cout << std::endl << std::endl;
  152. return 0;
  153. }