cmakewizard.cxx 4.0 KB

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