Imported Upstream version 2.8.9
[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, 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, sizeof(buffer)-1, stdin))
71     {
72     buffer[0] = 0;
73     }
74   if(buffer[0])
75     {
76     if(buffer[0] == 'y' || buffer[0] == 'Y')
77       {
78       return true;
79       }
80     }
81   return false;
82 }
83
84
85 void cmakewizard::ShowMessage(const char* m)
86 {
87   printf("%s\n", m);
88 }
89
90
91
92 int cmakewizard::RunWizard(std::vector<std::string> const& args)
93 {
94   this->ShowAdvanced = this->AskAdvanced();
95   cmSystemTools::DisableRunCommandOutput();
96   cmake make;
97   make.SetArgs(args);
98   make.SetCMakeCommand(args[0].c_str());
99   make.LoadCache();
100   make.SetCacheArgs(args);
101   std::map<cmStdString, cmStdString> askedCache;
102   bool asked = false;
103   // continue asking questions until no new questions are asked
104   do
105     {
106     asked = false;
107     // run cmake
108     this->ShowMessage(
109       "Please wait while cmake processes CMakeLists.txt files....\n");
110
111     make.Configure();
112     this->ShowMessage("\n");
113     // load the cache from disk
114     cmCacheManager *cachem = make.GetCacheManager();
115     cachem->LoadCache(make.GetHomeOutputDirectory());
116     cmCacheManager::CacheIterator i = cachem->NewIterator();
117     // iterate over all entries in the cache
118     for(;!i.IsAtEnd(); i.Next())
119       {
120       std::string key = i.GetName();
121       if( i.GetType() == cmCacheManager::INTERNAL ||
122           i.GetType() == cmCacheManager::STATIC ||
123           i.GetType() == cmCacheManager::UNINITIALIZED )
124         {
125         continue;
126         }
127       if(askedCache.count(key))
128         {
129         std::string& e = askedCache.find(key)->second;
130         if(e != i.GetValue())
131           {
132           if(this->ShowAdvanced || !i.GetPropertyAsBool("ADVANCED"))
133             {
134             this->AskUser(key.c_str(), i);
135             asked = true;
136             }
137           }
138         }
139       else
140         {    
141         if(this->ShowAdvanced || !i.GetPropertyAsBool("ADVANCED"))
142           {
143           this->AskUser(key.c_str(), i);
144           asked = true;
145           }
146         }
147       askedCache[key] = i.GetValue();
148       }
149     cachem->SaveCache(make.GetHomeOutputDirectory());
150     }
151   while(asked);
152   if(make.Generate() == 0)
153     {
154     this->ShowMessage("CMake complete, run make to build project.\n");
155     return 0;
156     }
157   return 1;
158 }