cmakewizard.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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(strlen(buffer) > 0)
  34. {
  35. std::string sbuffer = buffer;
  36. std::string::size_type pos = sbuffer.find_last_not_of(" \n\r\t");
  37. std::string value = "";
  38. if ( pos != std::string::npos )
  39. {
  40. value = sbuffer.substr(0, pos+1);
  41. }
  42. if ( value.size() > 0 )
  43. {
  44. if(iter.GetType() == cmCacheManager::PATH ||
  45. iter.GetType() == cmCacheManager::FILEPATH)
  46. {
  47. cmSystemTools::ConvertToUnixSlashes(value);
  48. }
  49. if(iter.GetType() == cmCacheManager::BOOL)
  50. {
  51. if(!cmSystemTools::IsOn(value.c_str()))
  52. {
  53. value = "OFF";
  54. }
  55. }
  56. iter.SetValue(value.c_str());
  57. }
  58. }
  59. std::cout << "\n";
  60. }
  61. bool cmakewizard::AskAdvanced()
  62. {
  63. std::cout << "Would you like to see advanced options? [No]:";
  64. char buffer[4096];
  65. buffer[0] = 0;
  66. fgets(buffer, sizeof(buffer)-1, stdin);
  67. if(buffer[0])
  68. {
  69. if(buffer[0] == 'y' || buffer[0] == 'Y')
  70. {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. void cmakewizard::ShowMessage(const char* m)
  77. {
  78. std::cout << m << "\n";
  79. }
  80. void cmakewizard::RunWizard(std::vector<std::string> const& args)
  81. {
  82. m_ShowAdvanced = this->AskAdvanced();
  83. cmSystemTools::DisableRunCommandOutput();
  84. cmake make;
  85. make.SetArgs(args);
  86. make.SetCMakeCommand(args[0].c_str());
  87. make.LoadCache();
  88. make.SetCacheArgs(args);
  89. std::map<std::string,std::string> askedCache;
  90. bool asked = false;
  91. // continue asking questions until no new questions are asked
  92. do
  93. {
  94. asked = false;
  95. // run cmake
  96. this->ShowMessage("Please wait while cmake processes CMakeLists.txt files....\n");
  97. make.Configure();
  98. this->ShowMessage("\n");
  99. // load the cache from disk
  100. cmCacheManager *cachem = make.GetCacheManager();
  101. cachem->
  102. LoadCache(cmSystemTools::GetCurrentWorkingDirectory().c_str());
  103. cmCacheManager::CacheIterator i = cachem->NewIterator();
  104. // iterate over all entries in the cache
  105. for(;!i.IsAtEnd(); i.Next())
  106. {
  107. std::string key = i.GetName();
  108. if( i.GetType() == cmCacheManager::INTERNAL ||
  109. i.GetType() == cmCacheManager::STATIC ||
  110. i.GetType() == cmCacheManager::UNINITIALIZED )
  111. {
  112. continue;
  113. }
  114. if(askedCache.count(key))
  115. {
  116. std::string& e = askedCache.find(key)->second;
  117. if(e != i.GetValue())
  118. {
  119. if(m_ShowAdvanced || !i.GetPropertyAsBool("ADVANCED"))
  120. {
  121. this->AskUser(key.c_str(), i);
  122. asked = true;
  123. }
  124. }
  125. }
  126. else
  127. {
  128. if(m_ShowAdvanced || !i.GetPropertyAsBool("ADVANCED"))
  129. {
  130. this->AskUser(key.c_str(), i);
  131. asked = true;
  132. }
  133. }
  134. askedCache[key] = i.GetValue();
  135. }
  136. cachem->SaveCache(cmSystemTools::GetCurrentWorkingDirectory().c_str());
  137. }
  138. while(asked);
  139. make.Generate();
  140. this->ShowMessage("CMake complete, run make to build project.\n");
  141. }