cmakewizard.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // On Mac OSX getline looks like it is broken, so we have to use
  17. // fgets. This is why we are including stdio.h.
  18. #include <stdio.h>
  19. cmakewizard::cmakewizard()
  20. {
  21. m_ShowAdvanced = false;
  22. }
  23. void cmakewizard::AskUser(const char* key, cmCacheManager::CacheIterator& iter)
  24. {
  25. std::cout << "Variable Name: " << key << "\n";
  26. const char* helpstring = iter.GetProperty("HELPSTRING");
  27. std::cout << "Description: " << (helpstring?helpstring:"(none)") << "\n";
  28. std::cout << "Current Value: " << iter.GetValue() << "\n";
  29. std::cout << "New Value (Enter to keep current value): ";
  30. char buffer[4096];
  31. buffer[0] = 0;
  32. fgets(buffer, sizeof(buffer)-1, stdin);
  33. if(buffer[0])
  34. {
  35. std::string value = buffer;
  36. if(iter.GetType() == cmCacheManager::PATH ||
  37. iter.GetType() == cmCacheManager::FILEPATH)
  38. {
  39. cmSystemTools::ConvertToUnixSlashes(value);
  40. }
  41. if(iter.GetType() == cmCacheManager::BOOL)
  42. {
  43. if(!cmSystemTools::IsOn(value.c_str()))
  44. {
  45. value = "OFF";
  46. }
  47. }
  48. iter.SetValue(value.c_str());
  49. }
  50. std::cout << "\n";
  51. }
  52. bool cmakewizard::AskAdvanced()
  53. {
  54. std::cout << "Would you like to see advanced options? [No]:";
  55. char buffer[4096];
  56. buffer[0] = 0;
  57. fgets(buffer, sizeof(buffer)-1, stdin);
  58. if(buffer[0])
  59. {
  60. if(buffer[0] == 'y' || buffer[0] == 'Y')
  61. {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. void cmakewizard::ShowMessage(const char* m)
  68. {
  69. std::cout << m << "\n";
  70. }
  71. void cmakewizard::RunWizard(std::vector<std::string> const& args)
  72. {
  73. m_ShowAdvanced = this->AskAdvanced();
  74. cmSystemTools::DisableRunCommandOutput();
  75. cmake make;
  76. make.SetArgs(args);
  77. make.SetCMakeCommand(args[0].c_str());
  78. make.LoadCache();
  79. make.SetCacheArgs(args);
  80. std::map<std::string,std::string> 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.Configure();
  89. this->ShowMessage("\n");
  90. // load the cache from disk
  91. cmCacheManager *cachem = make.GetCacheManager();
  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. if( i.GetType() == cmCacheManager::INTERNAL ||
  100. i.GetType() == cmCacheManager::STATIC ||
  101. i.GetType() == cmCacheManager::UNINITIALIZED )
  102. {
  103. continue;
  104. }
  105. if(askedCache.count(key))
  106. {
  107. std::string& e = askedCache.find(key)->second;
  108. if(e != i.GetValue())
  109. {
  110. if(m_ShowAdvanced || !i.GetPropertyAsBool("ADVANCED"))
  111. {
  112. this->AskUser(key.c_str(), i);
  113. asked = true;
  114. }
  115. }
  116. }
  117. else
  118. {
  119. if(m_ShowAdvanced || !i.GetPropertyAsBool("ADVANCED"))
  120. {
  121. this->AskUser(key.c_str(), i);
  122. asked = true;
  123. }
  124. }
  125. askedCache[key] = i.GetValue();
  126. }
  127. cachem->SaveCache(cmSystemTools::GetCurrentWorkingDirectory().c_str());
  128. }
  129. while(asked);
  130. make.Generate();
  131. this->ShowMessage("CMake complete, run make to build project.\n");
  132. }