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"
10 #include <cmext/algorithm>
12 #include "cmsys/String.h"
14 #include "cmIDEFlagTable.h"
15 #include "cmStringAlgorithms.h"
17 cmIDEOptions::cmIDEOptions()
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;
29 cmIDEOptions::~cmIDEOptions()
33 void cmIDEOptions::HandleFlag(std::string const& flag)
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);
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);
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;
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') {
62 // The next argument will have the definition.
63 this->DoingDefine = true;
65 // Store this definition.
66 this->Defines.push_back(flag.substr(2));
70 // Look for include directory.
71 if (this->AllowInclude && len > 1 && flag[1] == 'I') {
73 // The next argument will have the include directory.
74 this->DoingInclude = true;
76 // Store this include directory.
77 this->Includes.push_back(flag.substr(2));
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)) {
90 // If any map entry handled the flag we are done.
96 // This option is not known. Store it in the output flags.
97 this->StoreUnknownFlag(flag);
100 bool cmIDEOptions::CheckFlagTable(cmIDEFlagTable const* table,
101 std::string const& flag, bool& flag_handled)
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));
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;
127 // This flag table entry provides a fixed value.
128 this->FlagMap[entry->IDEName] = entry->value;
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)) {
139 // If the entry was found the flag has been handled.
140 flag_handled = flag_handled || entry_found;
146 void cmIDEOptions::FlagMapUpdate(cmIDEFlagTable const* entry,
147 std::string const& new_value)
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);
159 // Use the user-specified value.
160 this->FlagMap[entry->IDEName] = new_value;
164 void cmIDEOptions::AddDefine(const std::string& def)
166 this->Defines.push_back(def);
169 void cmIDEOptions::AddDefines(std::string const& defines)
171 if (!defines.empty()) {
172 // Expand the list of definitions.
173 cmExpandList(defines, this->Defines);
176 void cmIDEOptions::AddDefines(const std::vector<std::string>& defines)
178 cm::append(this->Defines, defines);
181 std::vector<std::string> const& cmIDEOptions::GetDefines() const
183 return this->Defines;
186 void cmIDEOptions::AddInclude(const std::string& include)
188 this->Includes.push_back(include);
191 void cmIDEOptions::AddIncludes(std::string const& includes)
193 if (!includes.empty()) {
194 // Expand the list of includes.
195 cmExpandList(includes, this->Includes);
198 void cmIDEOptions::AddIncludes(const std::vector<std::string>& includes)
200 cm::append(this->Includes, includes);
203 std::vector<std::string> const& cmIDEOptions::GetIncludes() const
205 return this->Includes;
208 void cmIDEOptions::AddFlag(std::string const& flag, std::string const& value)
210 this->FlagMap[flag] = value;
213 void cmIDEOptions::AddFlag(std::string const& flag,
214 std::vector<std::string> const& value)
216 this->FlagMap[flag] = value;
219 void cmIDEOptions::AppendFlag(std::string const& flag,
220 std::string const& value)
222 this->FlagMap[flag].push_back(value);
225 void cmIDEOptions::AppendFlag(std::string const& flag,
226 std::vector<std::string> const& value)
228 FlagValue& fv = this->FlagMap[flag];
229 std::copy(value.begin(), value.end(), std::back_inserter(fv));
232 void cmIDEOptions::AppendFlagString(std::string const& flag,
233 std::string const& value)
235 this->FlagMap[flag].append_with_space(value);
238 void cmIDEOptions::RemoveFlag(std::string const& flag)
240 this->FlagMap.erase(flag);
243 bool cmIDEOptions::HasFlag(std::string const& flag) const
245 return this->FlagMap.find(flag) != this->FlagMap.end();
248 const char* cmIDEOptions::GetFlag(std::string const& flag) const
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();