resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmIDEOptions.cxx
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #include "cmIDEOptions.h"
4
5 #include <algorithm>
6 #include <cstring>
7 #include <iterator>
8 #include <utility>
9
10 #include <cmext/algorithm>
11
12 #include "cmsys/String.h"
13
14 #include "cmIDEFlagTable.h"
15 #include "cmStringAlgorithms.h"
16
17 cmIDEOptions::cmIDEOptions()
18 {
19   this->DoingDefine = false;
20   this->AllowDefine = true;
21   this->DoingInclude = false;
22   this->AllowSlash = false;
23   this->DoingFollowing = 0;
24   for (int i = 0; i < FlagTableCount; ++i) {
25     this->FlagTable[i] = 0;
26   }
27 }
28
29 cmIDEOptions::~cmIDEOptions()
30 {
31 }
32
33 void cmIDEOptions::HandleFlag(std::string const& flag)
34 {
35   // If the last option was -D then this option is the definition.
36   if (this->DoingDefine) {
37     this->DoingDefine = false;
38     this->Defines.push_back(flag);
39     return;
40   }
41
42   // If the last option was -I then this option is the include directory.
43   if (this->DoingInclude) {
44     this->DoingInclude = false;
45     this->Includes.push_back(flag);
46     return;
47   }
48
49   // If the last option expected a following value, this is it.
50   if (this->DoingFollowing) {
51     this->FlagMapUpdate(this->DoingFollowing, flag);
52     this->DoingFollowing = 0;
53     return;
54   }
55
56   // Look for known arguments.
57   size_t len = flag.length();
58   if (len > 0 && (flag[0] == '-' || (this->AllowSlash && flag[0] == '/'))) {
59     // Look for preprocessor definitions.
60     if (this->AllowDefine && len > 1 && flag[1] == 'D') {
61       if (len <= 2) {
62         // The next argument will have the definition.
63         this->DoingDefine = true;
64       } else {
65         // Store this definition.
66         this->Defines.push_back(flag.substr(2));
67       }
68       return;
69     }
70     // Look for include directory.
71     if (this->AllowInclude && len > 1 && flag[1] == 'I') {
72       if (len <= 2) {
73         // The next argument will have the include directory.
74         this->DoingInclude = true;
75       } else {
76         // Store this include directory.
77         this->Includes.push_back(flag.substr(2));
78       }
79       return;
80     }
81
82     // Look through the available flag tables.
83     bool flag_handled = false;
84     for (int i = 0; i < FlagTableCount && this->FlagTable[i]; ++i) {
85       if (this->CheckFlagTable(this->FlagTable[i], flag, flag_handled)) {
86         return;
87       }
88     }
89
90     // If any map entry handled the flag we are done.
91     if (flag_handled) {
92       return;
93     }
94   }
95
96   // This option is not known.  Store it in the output flags.
97   this->StoreUnknownFlag(flag);
98 }
99
100 bool cmIDEOptions::CheckFlagTable(cmIDEFlagTable const* table,
101                                   std::string const& flag, bool& flag_handled)
102 {
103   const char* pf = flag.c_str() + 1;
104   // Look for an entry in the flag table matching this flag.
105   for (cmIDEFlagTable const* entry = table; !entry->IDEName.empty(); ++entry) {
106     bool entry_found = false;
107     if (entry->special & cmIDEFlagTable::UserValue) {
108       // This flag table entry accepts a user-specified value.  If
109       // the entry specifies UserRequired we must match only if a
110       // non-empty value is given.
111       int n = static_cast<int>(entry->commandFlag.length());
112       if ((strncmp(pf, entry->commandFlag.c_str(), n) == 0 ||
113            (entry->special & cmIDEFlagTable::CaseInsensitive &&
114             cmsysString_strncasecmp(pf, entry->commandFlag.c_str(), n))) &&
115           (!(entry->special & cmIDEFlagTable::UserRequired) ||
116            static_cast<int>(strlen(pf)) > n)) {
117         this->FlagMapUpdate(entry, std::string(pf + n));
118         entry_found = true;
119       }
120     } else if (strcmp(pf, entry->commandFlag.c_str()) == 0 ||
121                (entry->special & cmIDEFlagTable::CaseInsensitive &&
122                 cmsysString_strcasecmp(pf, entry->commandFlag.c_str()) == 0)) {
123       if (entry->special & cmIDEFlagTable::UserFollowing) {
124         // This flag expects a value in the following argument.
125         this->DoingFollowing = entry;
126       } else {
127         // This flag table entry provides a fixed value.
128         this->FlagMap[entry->IDEName] = entry->value;
129       }
130       entry_found = true;
131     }
132
133     // If the flag has been handled by an entry not requesting a
134     // search continuation we are done.
135     if (entry_found && !(entry->special & cmIDEFlagTable::Continue)) {
136       return true;
137     }
138
139     // If the entry was found the flag has been handled.
140     flag_handled = flag_handled || entry_found;
141   }
142
143   return false;
144 }
145
146 void cmIDEOptions::FlagMapUpdate(cmIDEFlagTable const* entry,
147                                  std::string const& new_value)
148 {
149   if (entry->special & cmIDEFlagTable::UserIgnored) {
150     // Ignore the user-specified value.
151     this->FlagMap[entry->IDEName] = entry->value;
152   } else if (entry->special & cmIDEFlagTable::SemicolonAppendable) {
153     this->FlagMap[entry->IDEName].push_back(new_value);
154   } else if (entry->special & cmIDEFlagTable::SpaceAppendable) {
155     this->FlagMap[entry->IDEName].append_with_space(new_value);
156   } else if (entry->special & cmIDEFlagTable::CommaAppendable) {
157     this->FlagMap[entry->IDEName].append_with_comma(new_value);
158   } else {
159     // Use the user-specified value.
160     this->FlagMap[entry->IDEName] = new_value;
161   }
162 }
163
164 void cmIDEOptions::AddDefine(const std::string& def)
165 {
166   this->Defines.push_back(def);
167 }
168
169 void cmIDEOptions::AddDefines(std::string const& defines)
170 {
171   if (!defines.empty()) {
172     // Expand the list of definitions.
173     cmExpandList(defines, this->Defines);
174   }
175 }
176 void cmIDEOptions::AddDefines(const std::vector<std::string>& defines)
177 {
178   cm::append(this->Defines, defines);
179 }
180
181 std::vector<std::string> const& cmIDEOptions::GetDefines() const
182 {
183   return this->Defines;
184 }
185
186 void cmIDEOptions::AddInclude(const std::string& include)
187 {
188   this->Includes.push_back(include);
189 }
190
191 void cmIDEOptions::AddIncludes(std::string const& includes)
192 {
193   if (!includes.empty()) {
194     // Expand the list of includes.
195     cmExpandList(includes, this->Includes);
196   }
197 }
198 void cmIDEOptions::AddIncludes(const std::vector<std::string>& includes)
199 {
200   cm::append(this->Includes, includes);
201 }
202
203 std::vector<std::string> const& cmIDEOptions::GetIncludes() const
204 {
205   return this->Includes;
206 }
207
208 void cmIDEOptions::AddFlag(std::string const& flag, std::string const& value)
209 {
210   this->FlagMap[flag] = value;
211 }
212
213 void cmIDEOptions::AddFlag(std::string const& flag,
214                            std::vector<std::string> const& value)
215 {
216   this->FlagMap[flag] = value;
217 }
218
219 void cmIDEOptions::AppendFlag(std::string const& flag,
220                               std::string const& value)
221 {
222   this->FlagMap[flag].push_back(value);
223 }
224
225 void cmIDEOptions::AppendFlag(std::string const& flag,
226                               std::vector<std::string> const& value)
227 {
228   FlagValue& fv = this->FlagMap[flag];
229   std::copy(value.begin(), value.end(), std::back_inserter(fv));
230 }
231
232 void cmIDEOptions::AppendFlagString(std::string const& flag,
233                                     std::string const& value)
234 {
235   this->FlagMap[flag].append_with_space(value);
236 }
237
238 void cmIDEOptions::RemoveFlag(std::string const& flag)
239 {
240   this->FlagMap.erase(flag);
241 }
242
243 bool cmIDEOptions::HasFlag(std::string const& flag) const
244 {
245   return this->FlagMap.find(flag) != this->FlagMap.end();
246 }
247
248 const char* cmIDEOptions::GetFlag(std::string const& flag) const
249 {
250   // This method works only for single-valued flags!
251   std::map<std::string, FlagValue>::const_iterator i =
252     this->FlagMap.find(flag);
253   if (i != this->FlagMap.cend() && i->second.size() == 1) {
254     return i->second[0].c_str();
255   }
256   return nullptr;
257 }