cmakewizard.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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. printf("Variable Name: %s\n", key);
  23. const char* helpstring = iter.GetProperty("HELPSTRING");
  24. printf("Description: %s\n", (helpstring?helpstring:"(none)"));
  25. printf("Current Value: %s\n", iter.GetValue());
  26. printf("New Value (Enter to keep current value): ");
  27. char buffer[4096];
  28. buffer[0] = 0;
  29. fgets(buffer, sizeof(buffer)-1, stdin);
  30. if(strlen(buffer) > 0)
  31. {
  32. std::string sbuffer = buffer;
  33. std::string::size_type pos = sbuffer.find_last_not_of(" \n\r\t");
  34. std::string value = "";
  35. if ( pos != std::string::npos )
  36. {
  37. value = sbuffer.substr(0, pos+1);
  38. }
  39. if ( value.size() > 0 )
  40. {
  41. if(iter.GetType() == cmCacheManager::PATH ||
  42. iter.GetType() == cmCacheManager::FILEPATH)
  43. {
  44. cmSystemTools::ConvertToUnixSlashes(value);
  45. }
  46. if(iter.GetType() == cmCacheManager::BOOL)
  47. {
  48. if(!cmSystemTools::IsOn(value.c_str()))
  49. {
  50. value = "OFF";
  51. }
  52. }
  53. iter.SetValue(value.c_str());
  54. }
  55. }
  56. printf("\n");
  57. }
  58. bool cmakewizard::AskAdvanced()
  59. {
  60. printf("Would you like to see advanced options? [No]:");
  61. char buffer[4096];
  62. buffer[0] = 0;
  63. fgets(buffer, sizeof(buffer)-1, stdin);
  64. if(buffer[0])
  65. {
  66. if(buffer[0] == 'y' || buffer[0] == 'Y')
  67. {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. void cmakewizard::ShowMessage(const char* m)
  74. {
  75. printf("%s\n", m);
  76. }
  77. void cmakewizard::RunWizard(std::vector<std::string> const& args)
  78. {
  79. m_ShowAdvanced = this->AskAdvanced();
  80. cmSystemTools::DisableRunCommandOutput();
  81. cmake make;
  82. make.SetArgs(args);
  83. make.SetCMakeCommand(args[0].c_str());
  84. make.LoadCache();
  85. make.SetCacheArgs(args);
  86. std::map<std::string,std::string> askedCache;
  87. bool asked = false;
  88. // continue asking questions until no new questions are asked
  89. do
  90. {
  91. asked = false;
  92. // run cmake
  93. this->ShowMessage("Please wait while cmake processes CMakeLists.txt files....\n");
  94. make.Configure();
  95. this->ShowMessage("\n");
  96. // load the cache from disk
  97. cmCacheManager *cachem = make.GetCacheManager();
  98. cachem->LoadCache(make.GetHomeOutputDirectory());
  99. cmCacheManager::CacheIterator i = cachem->NewIterator();
  100. // iterate over all entries in the cache
  101. for(;!i.IsAtEnd(); i.Next())
  102. {
  103. std::string key = i.GetName();
  104. if( i.GetType() == cmCacheManager::INTERNAL ||
  105. i.GetType() == cmCacheManager::STATIC ||
  106. i.GetType() == cmCacheManager::UNINITIALIZED )
  107. {
  108. continue;
  109. }
  110. if(askedCache.count(key))
  111. {
  112. std::string& e = askedCache.find(key)->second;
  113. if(e != i.GetValue())
  114. {
  115. if(m_ShowAdvanced || !i.GetPropertyAsBool("ADVANCED"))
  116. {
  117. this->AskUser(key.c_str(), i);
  118. asked = true;
  119. }
  120. }
  121. }
  122. else
  123. {
  124. if(m_ShowAdvanced || !i.GetPropertyAsBool("ADVANCED"))
  125. {
  126. this->AskUser(key.c_str(), i);
  127. asked = true;
  128. }
  129. }
  130. askedCache[key] = i.GetValue();
  131. }
  132. cachem->SaveCache(make.GetHomeOutputDirectory());
  133. }
  134. while(asked);
  135. make.Generate();
  136. this->ShowMessage("CMake complete, run make to build project.\n");
  137. }