cmakewizard.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. }
  42. else
  43. {
  44. std::cerr << "strange error, should be in cache but is not... " << key << "\n";
  45. }
  46. }
  47. std::cout << "\n";
  48. }
  49. bool cmakewizard::AskAdvanced()
  50. {
  51. std::cout << "Would you like to see advanced options? [No]:";
  52. char buffer[4096];
  53. buffer[0] = 0;
  54. std::cin.getline(buffer, sizeof(buffer));
  55. if(buffer[0])
  56. {
  57. if(buffer[0] == 'y' || buffer[0] == 'Y')
  58. {
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. void cmakewizard::ShowMessage(const char* m)
  65. {
  66. std::cout << m << "\n";
  67. }
  68. void cmakewizard::RunWizard(std::vector<std::string> const& args)
  69. {
  70. m_ShowAdvanced = this->AskAdvanced();
  71. cmSystemTools::DisableRunCommandOutput();
  72. cmake make;
  73. cmCacheManager::CacheEntryMap askedCache;
  74. bool asked = false;
  75. // continue asking questions until no new questions are asked
  76. do
  77. {
  78. asked = false;
  79. // run cmake
  80. this->ShowMessage("Please wait while cmake processes CMakeLists.txt files....\n");
  81. make.Generate(args);
  82. this->ShowMessage("\n");
  83. // load the cache from disk
  84. cmCacheManager::GetInstance()->
  85. LoadCache(cmSystemTools::GetCurrentWorkingDirectory().c_str());
  86. cmCacheManager::CacheEntryMap const& currentCache =
  87. cmCacheManager::GetInstance()->GetCacheMap();
  88. // iterate over all entries in the cache
  89. for(cmCacheManager::CacheEntryMap::const_iterator i = currentCache.begin();
  90. i != currentCache.end(); ++i)
  91. {
  92. std::string key = i->first;
  93. cmCacheManager::CacheEntry ce = i->second;
  94. if(ce.m_Type == cmCacheManager::INTERNAL
  95. || ce.m_Type == cmCacheManager::STATIC)
  96. {
  97. continue;
  98. }
  99. if(askedCache.count(key))
  100. {
  101. cmCacheManager::CacheEntry& e = askedCache.find(key)->second;
  102. if(e.m_Value != ce.m_Value)
  103. {
  104. if(m_ShowAdvanced || !cmCacheManager::GetInstance()->IsAdvanced(key.c_str()))
  105. {
  106. this->AskUser(key.c_str(), ce);
  107. asked = true;
  108. }
  109. }
  110. }
  111. else
  112. {
  113. if(m_ShowAdvanced || !cmCacheManager::GetInstance()->IsAdvanced(key.c_str()))
  114. {
  115. this->AskUser(key.c_str(), ce);
  116. asked = true;
  117. }
  118. }
  119. askedCache[key] = i->second;
  120. }
  121. cmCacheManager::GetInstance()->
  122. SaveCache(cmSystemTools::GetCurrentWorkingDirectory().c_str());
  123. }
  124. while(asked);
  125. this->ShowMessage("CMake complete, run make to build project.\n");
  126. }