cmakewizard.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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::CacheIterator& iter)
  21. {
  22. std::cout << "Variable Name: " << key << "\n";
  23. const char* helpstring = iter.GetProperty("HELPSTRING");
  24. std::cout << "Description: " << (helpstring?helpstring:"(none)") << "\n";
  25. std::cout << "Current Value: " << iter.GetValue() << "\n";
  26. std::cout << "New Value (Enter to keep current value): ";
  27. char buffer[4096];
  28. buffer[0] = 0;
  29. std::cin.getline(buffer, sizeof(buffer));
  30. if(buffer[0])
  31. {
  32. std::string value = buffer;
  33. if(iter.GetType() == cmCacheManager::PATH ||
  34. iter.GetType() == cmCacheManager::FILEPATH)
  35. {
  36. cmSystemTools::ConvertToUnixSlashes(value);
  37. }
  38. if(iter.GetType() == cmCacheManager::BOOL)
  39. {
  40. if(!cmSystemTools::IsOn(value.c_str()))
  41. {
  42. value = "OFF";
  43. }
  44. }
  45. iter.SetValue(value.c_str());
  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. make.SetArgs(args);
  74. std::map<std::string,std::string> askedCache;
  75. bool asked = false;
  76. // continue asking questions until no new questions are asked
  77. do
  78. {
  79. asked = false;
  80. // run cmake
  81. this->ShowMessage("Please wait while cmake processes CMakeLists.txt files....\n");
  82. make.Configure(args[0].c_str(),&args);
  83. this->ShowMessage("\n");
  84. // load the cache from disk
  85. cmCacheManager *cachem = make.GetCacheManager();
  86. cachem->
  87. LoadCache(cmSystemTools::GetCurrentWorkingDirectory().c_str());
  88. cmCacheManager::CacheIterator i = cachem->NewIterator();
  89. // iterate over all entries in the cache
  90. for(;!i.IsAtEnd(); i.Next())
  91. {
  92. std::string key = i.GetName();
  93. if( i.GetType() == cmCacheManager::INTERNAL ||
  94. i.GetType() == cmCacheManager::STATIC ||
  95. i.GetType() == cmCacheManager::UNINITIALIZED )
  96. {
  97. continue;
  98. }
  99. if(askedCache.count(key))
  100. {
  101. std::string& e = askedCache.find(key)->second;
  102. if(e != i.GetValue())
  103. {
  104. if(m_ShowAdvanced || !i.GetPropertyAsBool("ADVANCED"))
  105. {
  106. this->AskUser(key.c_str(), i);
  107. asked = true;
  108. }
  109. }
  110. }
  111. else
  112. {
  113. if(m_ShowAdvanced || !i.GetPropertyAsBool("ADVANCED"))
  114. {
  115. this->AskUser(key.c_str(), i);
  116. asked = true;
  117. }
  118. }
  119. askedCache[key] = i.GetValue();
  120. }
  121. cachem->SaveCache(cmSystemTools::GetCurrentWorkingDirectory().c_str());
  122. }
  123. while(asked);
  124. make.Generate();
  125. this->ShowMessage("CMake complete, run make to build project.\n");
  126. }