cmakewizard.cxx 4.1 KB

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