cmakewizard.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmakewizard.h"
  14. #include "cmake.h"
  15. #include "cmCacheManager.h"
  16. cmakewizard::cmakewizard()
  17. {
  18. m_ShowAdvanced = false;
  19. }
  20. void cmakewizard::AskUser(const char* key, cmCacheManager::CacheEntry & entry)
  21. {
  22. std::cout << "Variable Name: " << key << "\n";
  23. std::cout << "Description: " << entry.m_HelpString << "\n";
  24. std::cout << "Current Value: " << entry.m_Value.c_str() << "\n";
  25. std::cout << "New Value (Enter to keep current value): ";
  26. char buffer[4096];
  27. buffer[0] = 0;
  28. std::cin.getline(buffer, sizeof(buffer));
  29. if(buffer[0])
  30. {
  31. cmCacheManager::CacheEntry *entry =
  32. cmCacheManager::GetInstance()->GetCacheEntry(key);
  33. if(entry)
  34. {
  35. entry->m_Value = buffer;
  36. if(entry->m_Type == cmCacheManager::PATH ||
  37. entry->m_Type == cmCacheManager::FILEPATH)
  38. {
  39. cmSystemTools::ConvertToUnixSlashes(entry->m_Value);
  40. }
  41. if(entry->m_Type == cmCacheManager::BOOL)
  42. {
  43. if(!cmSystemTools::IsOn(buffer))
  44. {
  45. entry->m_Value = "OFF";
  46. }
  47. }
  48. }
  49. else
  50. {
  51. std::cerr << "strange error, should be in cache but is not... " << key << "\n";
  52. }
  53. }
  54. std::cout << "\n";
  55. }
  56. bool cmakewizard::AskAdvanced()
  57. {
  58. std::cout << "Would you like to see advanced options? [No]:";
  59. char buffer[4096];
  60. buffer[0] = 0;
  61. std::cin.getline(buffer, sizeof(buffer));
  62. if(buffer[0])
  63. {
  64. if(buffer[0] == 'y' || buffer[0] == 'Y')
  65. {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. void cmakewizard::ShowMessage(const char* m)
  72. {
  73. std::cout << m << "\n";
  74. }
  75. void cmakewizard::RunWizard(std::vector<std::string> const& args)
  76. {
  77. m_ShowAdvanced = this->AskAdvanced();
  78. cmSystemTools::DisableRunCommandOutput();
  79. cmake make;
  80. cmCacheManager::CacheEntryMap askedCache;
  81. bool asked = false;
  82. // continue asking questions until no new questions are asked
  83. do
  84. {
  85. asked = false;
  86. // run cmake
  87. this->ShowMessage("Please wait while cmake processes CMakeLists.txt files....\n");
  88. make.Generate(args);
  89. this->ShowMessage("\n");
  90. // load the cache from disk
  91. cmCacheManager *cachem = cmCacheManager::GetInstance();
  92. cachem->
  93. LoadCache(cmSystemTools::GetCurrentWorkingDirectory().c_str());
  94. cmCacheManager::CacheIterator i = cachem->NewIterator();
  95. // iterate over all entries in the cache
  96. for(;!i.IsAtEnd(); i.Next())
  97. {
  98. std::string key = i.GetName();
  99. cmCacheManager::CacheEntry ce = i.GetEntry();
  100. if(ce.m_Type == cmCacheManager::INTERNAL
  101. || ce.m_Type == cmCacheManager::STATIC)
  102. {
  103. continue;
  104. }
  105. if(askedCache.count(key))
  106. {
  107. cmCacheManager::CacheEntry& e = askedCache.find(key)->second;
  108. if(e.m_Value != ce.m_Value)
  109. {
  110. if(m_ShowAdvanced || !cmCacheManager::GetInstance()->IsAdvanced(key.c_str()))
  111. {
  112. this->AskUser(key.c_str(), ce);
  113. asked = true;
  114. }
  115. }
  116. }
  117. else
  118. {
  119. if(m_ShowAdvanced || !cmCacheManager::GetInstance()->IsAdvanced(key.c_str()))
  120. {
  121. this->AskUser(key.c_str(), ce);
  122. asked = true;
  123. }
  124. }
  125. askedCache[key] = i.GetEntry();
  126. }
  127. cmCacheManager::GetInstance()->
  128. SaveCache(cmSystemTools::GetCurrentWorkingDirectory().c_str());
  129. }
  130. while(asked);
  131. this->ShowMessage("CMake complete, run make to build project.\n");
  132. }