cmake/Source/cmIDEOptions.cxx

258 lines
7.4 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2009-10-04 10:30:41 +03:00
#include "cmIDEOptions.h"
2017-04-14 19:02:05 +02:00
#include <iterator>
2020-02-01 23:06:01 +01:00
2020-08-30 11:54:41 +02:00
#include <cmext/algorithm>
2017-04-14 19:02:05 +02:00
#include <string.h>
2020-02-01 23:06:01 +01:00
#include "cmsys/String.h"
2019-11-11 23:01:05 +01:00
#include "cmAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmIDEFlagTable.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2016-03-13 13:35:51 +01:00
2009-10-04 10:30:41 +03:00
cmIDEOptions::cmIDEOptions()
{
this->DoingDefine = false;
this->AllowDefine = true;
2018-04-23 21:13:27 +02:00
this->DoingInclude = false;
2009-10-04 10:30:41 +03:00
this->AllowSlash = false;
2015-04-27 22:25:09 +02:00
this->DoingFollowing = 0;
2016-07-09 11:21:54 +02:00
for (int i = 0; i < FlagTableCount; ++i) {
2009-10-04 10:30:41 +03:00
this->FlagTable[i] = 0;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}
cmIDEOptions::~cmIDEOptions()
{
}
2018-08-09 18:06:22 +02:00
void cmIDEOptions::HandleFlag(std::string const& flag)
2009-10-04 10:30:41 +03:00
{
// If the last option was -D then this option is the definition.
2016-07-09 11:21:54 +02:00
if (this->DoingDefine) {
2009-10-04 10:30:41 +03:00
this->DoingDefine = false;
this->Defines.push_back(flag);
return;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2018-04-23 21:13:27 +02:00
// If the last option was -I then this option is the include directory.
if (this->DoingInclude) {
this->DoingInclude = false;
this->Includes.push_back(flag);
return;
}
2015-04-27 22:25:09 +02:00
// If the last option expected a following value, this is it.
2016-07-09 11:21:54 +02:00
if (this->DoingFollowing) {
2015-04-27 22:25:09 +02:00
this->FlagMapUpdate(this->DoingFollowing, flag);
this->DoingFollowing = 0;
return;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2009-10-04 10:30:41 +03:00
// Look for known arguments.
2018-08-09 18:06:22 +02:00
size_t len = flag.length();
if (len > 0 && (flag[0] == '-' || (this->AllowSlash && flag[0] == '/'))) {
2009-10-04 10:30:41 +03:00
// Look for preprocessor definitions.
2018-08-09 18:06:22 +02:00
if (this->AllowDefine && len > 1 && flag[1] == 'D') {
if (len <= 2) {
2009-10-04 10:30:41 +03:00
// The next argument will have the definition.
this->DoingDefine = true;
2016-07-09 11:21:54 +02:00
} else {
2009-10-04 10:30:41 +03:00
// Store this definition.
2018-08-09 18:06:22 +02:00
this->Defines.push_back(flag.substr(2));
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
return;
}
2018-04-23 21:13:27 +02:00
// Look for include directory.
2018-08-09 18:06:22 +02:00
if (this->AllowInclude && len > 1 && flag[1] == 'I') {
if (len <= 2) {
2018-04-23 21:13:27 +02:00
// The next argument will have the include directory.
this->DoingInclude = true;
} else {
// Store this include directory.
2018-08-09 18:06:22 +02:00
this->Includes.push_back(flag.substr(2));
2018-04-23 21:13:27 +02:00
}
return;
}
2009-10-04 10:30:41 +03:00
// Look through the available flag tables.
bool flag_handled = false;
2016-07-09 11:21:54 +02:00
for (int i = 0; i < FlagTableCount && this->FlagTable[i]; ++i) {
if (this->CheckFlagTable(this->FlagTable[i], flag, flag_handled)) {
2009-10-04 10:30:41 +03:00
return;
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// If any map entry handled the flag we are done.
2016-07-09 11:21:54 +02:00
if (flag_handled) {
2009-10-04 10:30:41 +03:00
return;
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// This option is not known. Store it in the output flags.
this->StoreUnknownFlag(flag);
}
bool cmIDEOptions::CheckFlagTable(cmIDEFlagTable const* table,
2018-08-09 18:06:22 +02:00
std::string const& flag, bool& flag_handled)
2009-10-04 10:30:41 +03:00
{
2018-08-09 18:06:22 +02:00
const char* pf = flag.c_str() + 1;
2009-10-04 10:30:41 +03:00
// Look for an entry in the flag table matching this flag.
2019-11-11 23:01:05 +01:00
for (cmIDEFlagTable const* entry = table; !entry->IDEName.empty(); ++entry) {
2009-10-04 10:30:41 +03:00
bool entry_found = false;
2016-07-09 11:21:54 +02:00
if (entry->special & cmIDEFlagTable::UserValue) {
2009-10-04 10:30:41 +03:00
// This flag table entry accepts a user-specified value. If
// the entry specifies UserRequired we must match only if a
// non-empty value is given.
2019-11-11 23:01:05 +01:00
int n = static_cast<int>(entry->commandFlag.length());
if ((strncmp(pf, entry->commandFlag.c_str(), n) == 0 ||
2016-07-09 11:21:54 +02:00
(entry->special & cmIDEFlagTable::CaseInsensitive &&
2019-11-11 23:01:05 +01:00
cmsysString_strncasecmp(pf, entry->commandFlag.c_str(), n))) &&
2016-07-09 11:21:54 +02:00
(!(entry->special & cmIDEFlagTable::UserRequired) ||
2018-08-09 18:06:22 +02:00
static_cast<int>(strlen(pf)) > n)) {
this->FlagMapUpdate(entry, std::string(pf + n));
2009-10-04 10:30:41 +03:00
entry_found = true;
}
2019-11-11 23:01:05 +01:00
} else if (strcmp(pf, entry->commandFlag.c_str()) == 0 ||
2016-07-09 11:21:54 +02:00
(entry->special & cmIDEFlagTable::CaseInsensitive &&
2019-11-11 23:01:05 +01:00
cmsysString_strcasecmp(pf, entry->commandFlag.c_str()) == 0)) {
2016-07-09 11:21:54 +02:00
if (entry->special & cmIDEFlagTable::UserFollowing) {
2015-04-27 22:25:09 +02:00
// This flag expects a value in the following argument.
this->DoingFollowing = entry;
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
// This flag table entry provides a fixed value.
this->FlagMap[entry->IDEName] = entry->value;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
entry_found = true;
}
2009-10-04 10:30:41 +03:00
// If the flag has been handled by an entry not requesting a
// search continuation we are done.
2016-07-09 11:21:54 +02:00
if (entry_found && !(entry->special & cmIDEFlagTable::Continue)) {
2009-10-04 10:30:41 +03:00
return true;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// If the entry was found the flag has been handled.
flag_handled = flag_handled || entry_found;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
return false;
}
2015-04-27 22:25:09 +02:00
void cmIDEOptions::FlagMapUpdate(cmIDEFlagTable const* entry,
2018-08-09 18:06:22 +02:00
std::string const& new_value)
2015-04-27 22:25:09 +02:00
{
2016-07-09 11:21:54 +02:00
if (entry->special & cmIDEFlagTable::UserIgnored) {
2015-04-27 22:25:09 +02:00
// Ignore the user-specified value.
this->FlagMap[entry->IDEName] = entry->value;
2016-07-09 11:21:54 +02:00
} else if (entry->special & cmIDEFlagTable::SemicolonAppendable) {
2015-04-27 22:25:09 +02:00
this->FlagMap[entry->IDEName].push_back(new_value);
2017-07-20 19:35:53 +02:00
} else if (entry->special & cmIDEFlagTable::SpaceAppendable) {
this->FlagMap[entry->IDEName].append_with_space(new_value);
2019-11-11 23:01:05 +01:00
} else if (entry->special & cmIDEFlagTable::CommaAppendable) {
this->FlagMap[entry->IDEName].append_with_comma(new_value);
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
// Use the user-specified value.
this->FlagMap[entry->IDEName] = new_value;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
2009-10-04 10:30:41 +03:00
void cmIDEOptions::AddDefine(const std::string& def)
{
this->Defines.push_back(def);
}
2018-08-09 18:06:22 +02:00
void cmIDEOptions::AddDefines(std::string const& defines)
2009-10-04 10:30:41 +03:00
{
2018-08-09 18:06:22 +02:00
if (!defines.empty()) {
2009-10-04 10:30:41 +03:00
// Expand the list of definitions.
2020-02-01 23:06:01 +01:00
cmExpandList(defines, this->Defines);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
void cmIDEOptions::AddDefines(const std::vector<std::string>& defines)
2013-11-03 12:27:13 +02:00
{
2020-08-30 11:54:41 +02:00
cm::append(this->Defines, defines);
2013-11-03 12:27:13 +02:00
}
2009-10-04 10:30:41 +03:00
2017-04-14 19:02:05 +02:00
std::vector<std::string> const& cmIDEOptions::GetDefines() const
{
return this->Defines;
}
2018-04-23 21:13:27 +02:00
void cmIDEOptions::AddInclude(const std::string& include)
{
this->Includes.push_back(include);
}
2018-08-09 18:06:22 +02:00
void cmIDEOptions::AddIncludes(std::string const& includes)
2018-04-23 21:13:27 +02:00
{
2018-08-09 18:06:22 +02:00
if (!includes.empty()) {
2018-04-23 21:13:27 +02:00
// Expand the list of includes.
2020-02-01 23:06:01 +01:00
cmExpandList(includes, this->Includes);
2018-04-23 21:13:27 +02:00
}
}
void cmIDEOptions::AddIncludes(const std::vector<std::string>& includes)
{
2020-08-30 11:54:41 +02:00
cm::append(this->Includes, includes);
2018-04-23 21:13:27 +02:00
}
std::vector<std::string> const& cmIDEOptions::GetIncludes() const
{
return this->Includes;
}
void cmIDEOptions::AddFlag(std::string const& flag, std::string const& value)
2009-10-04 10:30:41 +03:00
{
this->FlagMap[flag] = value;
}
2011-06-19 15:41:06 +03:00
2018-04-23 21:13:27 +02:00
void cmIDEOptions::AddFlag(std::string const& flag,
2015-04-27 22:25:09 +02:00
std::vector<std::string> const& value)
{
this->FlagMap[flag] = value;
}
void cmIDEOptions::AppendFlag(std::string const& flag,
std::string const& value)
{
this->FlagMap[flag].push_back(value);
}
void cmIDEOptions::AppendFlag(std::string const& flag,
std::vector<std::string> const& value)
{
FlagValue& fv = this->FlagMap[flag];
std::copy(value.begin(), value.end(), std::back_inserter(fv));
}
2017-07-20 19:35:53 +02:00
void cmIDEOptions::AppendFlagString(std::string const& flag,
std::string const& value)
{
this->FlagMap[flag].append_with_space(value);
}
2018-04-23 21:13:27 +02:00
void cmIDEOptions::RemoveFlag(std::string const& flag)
2011-06-19 15:41:06 +03:00
{
this->FlagMap.erase(flag);
}
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
bool cmIDEOptions::HasFlag(std::string const& flag) const
{
return this->FlagMap.find(flag) != this->FlagMap.end();
}
2018-04-23 21:13:27 +02:00
const char* cmIDEOptions::GetFlag(std::string const& flag) const
2013-03-16 19:13:01 +02:00
{
2015-04-27 22:25:09 +02:00
// This method works only for single-valued flags!
2018-04-23 21:13:27 +02:00
std::map<std::string, FlagValue>::const_iterator i =
this->FlagMap.find(flag);
if (i != this->FlagMap.cend() && i->second.size() == 1) {
2015-04-27 22:25:09 +02:00
return i->second[0].c_str();
2016-07-09 11:21:54 +02:00
}
2018-04-23 21:13:27 +02:00
return nullptr;
2013-03-16 19:13:01 +02:00
}