CMake/Source/cmIDEOptions.cxx

254 lines
7.4 KiB
C++
Raw Normal View History

Simplify CMake per-source license notices Per-source copyright/license notice headers that spell out copyright holder names and years are hard to maintain and often out-of-date or plain wrong. Precise contributor information is already maintained automatically by the version control tool. Ultimately it is the receiver of a file who is responsible for determining its licensing status, and per-source notices are merely a convenience. Therefore it is simpler and more accurate for each source to have a generic notice of the license name and references to more detailed information on copyright holders and full license terms. Our `Copyright.txt` file now contains a list of Contributors whose names appeared source-level copyright notices. It also references version control history for more precise information. Therefore we no longer need to spell out the list of Contributors in each source file notice. Replace CMake per-source copyright/license notice headers with a short description of the license and links to `Copyright.txt` and online information available from "https://cmake.org/licensing". The online URL also handles cases of modules being copied out of our source into other projects, so we can drop our notices about replacing links with full license text. Run the `Utilities/Scripts/filter-notices.bash` script to perform the majority of the replacements mechanically. Manually fix up shebang lines and trailing newlines in a few files. Manually update the notices in a few files that the script does not handle.
2016-09-27 19:01:08 +00:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmIDEOptions.h"
#include "cmsys/String.h"
2016-11-25 21:54:58 +00:00
#include <iterator>
#include <string.h>
#include "cmAlgorithms.h"
2016-11-25 21:54:58 +00:00
#include "cmIDEFlagTable.h"
#include "cmSystemTools.h"
cmIDEOptions::cmIDEOptions()
{
this->DoingDefine = false;
this->AllowDefine = true;
this->DoingInclude = false;
this->AllowSlash = false;
this->DoingFollowing = 0;
for (int i = 0; i < FlagTableCount; ++i) {
this->FlagTable[i] = 0;
}
}
cmIDEOptions::~cmIDEOptions()
{
}
2018-02-26 16:24:45 +00:00
void cmIDEOptions::HandleFlag(std::string const& flag)
{
// If the last option was -D then this option is the definition.
if (this->DoingDefine) {
this->DoingDefine = false;
this->Defines.push_back(flag);
return;
}
// 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;
}
// If the last option expected a following value, this is it.
if (this->DoingFollowing) {
this->FlagMapUpdate(this->DoingFollowing, flag);
this->DoingFollowing = 0;
return;
}
// Look for known arguments.
2018-02-26 16:24:45 +00:00
size_t len = flag.length();
if (len > 0 && (flag[0] == '-' || (this->AllowSlash && flag[0] == '/'))) {
// Look for preprocessor definitions.
2018-02-26 16:24:45 +00:00
if (this->AllowDefine && len > 1 && flag[1] == 'D') {
if (len <= 2) {
// The next argument will have the definition.
this->DoingDefine = true;
} else {
// Store this definition.
2018-02-26 16:24:45 +00:00
this->Defines.push_back(flag.substr(2));
}
return;
}
// Look for include directory.
2018-02-26 16:24:45 +00:00
if (this->AllowInclude && len > 1 && flag[1] == 'I') {
if (len <= 2) {
// The next argument will have the include directory.
this->DoingInclude = true;
} else {
// Store this include directory.
2018-02-26 16:24:45 +00:00
this->Includes.push_back(flag.substr(2));
}
return;
}
// Look through the available flag tables.
bool flag_handled = false;
for (int i = 0; i < FlagTableCount && this->FlagTable[i]; ++i) {
if (this->CheckFlagTable(this->FlagTable[i], flag, flag_handled)) {
return;
}
}
// If any map entry handled the flag we are done.
if (flag_handled) {
return;
}
}
// This option is not known. Store it in the output flags.
this->StoreUnknownFlag(flag);
}
bool cmIDEOptions::CheckFlagTable(cmIDEFlagTable const* table,
2018-02-26 16:24:45 +00:00
std::string const& flag, bool& flag_handled)
{
2018-02-26 16:24:45 +00:00
const char* pf = flag.c_str() + 1;
// Look for an entry in the flag table matching this flag.
for (cmIDEFlagTable const* entry = table; !entry->IDEName.empty(); ++entry) {
bool entry_found = false;
if (entry->special & cmIDEFlagTable::UserValue) {
// 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.
int n = static_cast<int>(entry->commandFlag.length());
if ((strncmp(pf, entry->commandFlag.c_str(), n) == 0 ||
(entry->special & cmIDEFlagTable::CaseInsensitive &&
cmsysString_strncasecmp(pf, entry->commandFlag.c_str(), n))) &&
(!(entry->special & cmIDEFlagTable::UserRequired) ||
2018-02-26 16:24:45 +00:00
static_cast<int>(strlen(pf)) > n)) {
this->FlagMapUpdate(entry, std::string(pf + n));
entry_found = true;
}
} else if (strcmp(pf, entry->commandFlag.c_str()) == 0 ||
(entry->special & cmIDEFlagTable::CaseInsensitive &&
cmsysString_strcasecmp(pf, entry->commandFlag.c_str()) == 0)) {
if (entry->special & cmIDEFlagTable::UserFollowing) {
// This flag expects a value in the following argument.
this->DoingFollowing = entry;
} else {
// This flag table entry provides a fixed value.
this->FlagMap[entry->IDEName] = entry->value;
}
entry_found = true;
}
// If the flag has been handled by an entry not requesting a
// search continuation we are done.
if (entry_found && !(entry->special & cmIDEFlagTable::Continue)) {
return true;
}
// If the entry was found the flag has been handled.
flag_handled = flag_handled || entry_found;
}
return false;
}
void cmIDEOptions::FlagMapUpdate(cmIDEFlagTable const* entry,
2018-02-26 16:24:45 +00:00
std::string const& new_value)
{
if (entry->special & cmIDEFlagTable::UserIgnored) {
// Ignore the user-specified value.
this->FlagMap[entry->IDEName] = entry->value;
} else if (entry->special & cmIDEFlagTable::SemicolonAppendable) {
this->FlagMap[entry->IDEName].push_back(new_value);
} else if (entry->special & cmIDEFlagTable::SpaceAppendable) {
this->FlagMap[entry->IDEName].append_with_space(new_value);
} else if (entry->special & cmIDEFlagTable::CommaAppendable) {
this->FlagMap[entry->IDEName].append_with_comma(new_value);
} else {
// Use the user-specified value.
this->FlagMap[entry->IDEName] = new_value;
}
}
void cmIDEOptions::AddDefine(const std::string& def)
{
this->Defines.push_back(def);
}
2018-02-26 16:24:45 +00:00
void cmIDEOptions::AddDefines(std::string const& defines)
{
2018-02-26 16:24:45 +00:00
if (!defines.empty()) {
// Expand the list of definitions.
cmSystemTools::ExpandListArgument(defines, this->Defines);
}
}
void cmIDEOptions::AddDefines(const std::vector<std::string>& defines)
{
cmAppend(this->Defines, defines);
}
2017-03-28 16:55:14 +00:00
std::vector<std::string> const& cmIDEOptions::GetDefines() const
{
return this->Defines;
}
void cmIDEOptions::AddInclude(const std::string& include)
{
this->Includes.push_back(include);
}
2018-02-26 16:24:45 +00:00
void cmIDEOptions::AddIncludes(std::string const& includes)
{
2018-02-26 16:24:45 +00:00
if (!includes.empty()) {
// Expand the list of includes.
cmSystemTools::ExpandListArgument(includes, this->Includes);
}
}
void cmIDEOptions::AddIncludes(const std::vector<std::string>& includes)
{
cmAppend(this->Includes, includes);
}
std::vector<std::string> const& cmIDEOptions::GetIncludes() const
{
return this->Includes;
}
void cmIDEOptions::AddFlag(std::string const& flag, std::string const& value)
{
this->FlagMap[flag] = value;
}
void cmIDEOptions::AddFlag(std::string const& flag,
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));
}
void cmIDEOptions::AppendFlagString(std::string const& flag,
std::string const& value)
{
this->FlagMap[flag].append_with_space(value);
}
void cmIDEOptions::RemoveFlag(std::string const& flag)
{
this->FlagMap.erase(flag);
}
bool cmIDEOptions::HasFlag(std::string const& flag) const
{
return this->FlagMap.find(flag) != this->FlagMap.end();
}
const char* cmIDEOptions::GetFlag(std::string const& flag) const
{
// This method works only for single-valued flags!
std::map<std::string, FlagValue>::const_iterator i =
this->FlagMap.find(flag);
if (i != this->FlagMap.cend() && i->second.size() == 1) {
return i->second[0].c_str();
}
return nullptr;
}