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