cmakewizard.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. if(!fgets(buffer, sizeof(buffer)-1, stdin))
  27. {
  28. buffer[0] = 0;
  29. }
  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. if(!fgets(buffer, sizeof(buffer)-1, stdin))
  63. {
  64. buffer[0] = 0;
  65. }
  66. if(buffer[0])
  67. {
  68. if(buffer[0] == 'y' || buffer[0] == 'Y')
  69. {
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. void cmakewizard::ShowMessage(const char* m)
  76. {
  77. printf("%s\n", m);
  78. }
  79. int cmakewizard::RunWizard(std::vector<std::string> const& args)
  80. {
  81. this->ShowAdvanced = this->AskAdvanced();
  82. cmSystemTools::DisableRunCommandOutput();
  83. cmake make;
  84. make.SetArgs(args);
  85. make.SetCMakeCommand(args[0].c_str());
  86. make.LoadCache();
  87. make.SetCacheArgs(args);
  88. std::map<cmStdString, cmStdString> askedCache;
  89. bool asked = false;
  90. // continue asking questions until no new questions are asked
  91. do
  92. {
  93. asked = false;
  94. // run cmake
  95. this->ShowMessage(
  96. "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->LoadCache(make.GetHomeOutputDirectory());
  102. cmCacheManager::CacheIterator i = cachem->NewIterator();
  103. // iterate over all entries in the cache
  104. for(;!i.IsAtEnd(); i.Next())
  105. {
  106. std::string key = i.GetName();
  107. if( i.GetType() == cmCacheManager::INTERNAL ||
  108. i.GetType() == cmCacheManager::STATIC ||
  109. i.GetType() == cmCacheManager::UNINITIALIZED )
  110. {
  111. continue;
  112. }
  113. if(askedCache.count(key))
  114. {
  115. std::string& e = askedCache.find(key)->second;
  116. if(e != i.GetValue())
  117. {
  118. if(this->ShowAdvanced || !i.GetPropertyAsBool("ADVANCED"))
  119. {
  120. this->AskUser(key.c_str(), i);
  121. asked = true;
  122. }
  123. }
  124. }
  125. else
  126. {
  127. if(this->ShowAdvanced || !i.GetPropertyAsBool("ADVANCED"))
  128. {
  129. this->AskUser(key.c_str(), i);
  130. asked = true;
  131. }
  132. }
  133. askedCache[key] = i.GetValue();
  134. }
  135. cachem->SaveCache(make.GetHomeOutputDirectory());
  136. }
  137. while(asked);
  138. if(make.Generate() == 0)
  139. {
  140. this->ShowMessage("CMake complete, run make to build project.\n");
  141. return 0;
  142. }
  143. return 1;
  144. }