CMake/Source/cmGeneratorExpressionEvalua...

159 lines
5.0 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 22:01:08 +03:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmGeneratorExpressionEvaluationFile.h"
#include "cmGeneratedFileStream.h"
#include "cmGlobalGenerator.h"
#include "cmListFileCache.h"
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
#include "cmSourceFile.h"
#include "cmSystemTools.h"
#include "cmake.h"
#include <cmConfigure.h>
#include <cmsys/FStream.hxx>
#include <sstream>
#include <utility>
cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
const std::string& input,
CM_AUTO_PTR<cmCompiledGeneratorExpression> outputFileExpr,
CM_AUTO_PTR<cmCompiledGeneratorExpression> condition, bool inputIsContent)
: Input(input)
, OutputFileExpr(outputFileExpr)
, Condition(condition)
, InputIsContent(inputIsContent)
{
}
void cmGeneratorExpressionEvaluationFile::Generate(
cmLocalGenerator* lg, const std::string& config, const std::string& lang,
cmCompiledGeneratorExpression* inputExpression,
std::map<std::string, std::string>& outputFiles, mode_t perm)
{
std::string rawCondition = this->Condition->GetInput();
if (!rawCondition.empty()) {
2016-06-27 23:44:16 +03:00
std::string condResult = this->Condition->Evaluate(
lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, lang);
if (condResult == "0") {
return;
}
if (condResult != "1") {
std::ostringstream e;
e << "Evaluation file condition \"" << rawCondition
<< "\" did "
"not evaluate to valid content. Got \""
<< condResult << "\".";
lg->IssueMessage(cmake::FATAL_ERROR, e.str());
return;
}
}
2016-06-27 23:44:16 +03:00
const std::string outputFileName = this->OutputFileExpr->Evaluate(
lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, lang);
const std::string outputContent = inputExpression->Evaluate(
lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, lang);
std::map<std::string, std::string>::iterator it =
outputFiles.find(outputFileName);
if (it != outputFiles.end()) {
if (it->second == outputContent) {
return;
}
std::ostringstream e;
e << "Evaluation file to be written multiple times for different "
"configurations or languages with different content:\n "
<< outputFileName;
lg->IssueMessage(cmake::FATAL_ERROR, e.str());
return;
}
lg->GetMakefile()->AddCMakeOutputFile(outputFileName);
this->Files.push_back(outputFileName);
outputFiles[outputFileName] = outputContent;
cmGeneratedFileStream fout(outputFileName.c_str());
fout.SetCopyIfDifferent(true);
fout << outputContent;
if (fout.Close() && perm) {
cmSystemTools::SetPermissions(outputFileName.c_str(), perm);
}
}
void cmGeneratorExpressionEvaluationFile::CreateOutputFile(
cmLocalGenerator* lg, std::string const& config)
{
std::vector<std::string> enabledLanguages;
cmGlobalGenerator* gg = lg->GetGlobalGenerator();
gg->GetEnabledLanguages(enabledLanguages);
for (std::vector<std::string>::const_iterator le = enabledLanguages.begin();
le != enabledLanguages.end(); ++le) {
2016-06-27 23:44:16 +03:00
std::string name = this->OutputFileExpr->Evaluate(
lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, *le);
cmSourceFile* sf = lg->GetMakefile()->GetOrCreateSource(name);
sf->SetProperty("GENERATED", "1");
gg->SetFilenameTargetDepends(
sf, this->OutputFileExpr->GetSourceSensitiveTargets());
}
}
void cmGeneratorExpressionEvaluationFile::Generate(cmLocalGenerator* lg)
{
mode_t perm = 0;
std::string inputContent;
if (this->InputIsContent) {
inputContent = this->Input;
} else {
lg->GetMakefile()->AddCMakeDependFile(this->Input);
cmSystemTools::GetPermissions(this->Input.c_str(), perm);
cmsys::ifstream fin(this->Input.c_str());
if (!fin) {
std::ostringstream e;
e << "Evaluation file \"" << this->Input << "\" cannot be read.";
lg->IssueMessage(cmake::FATAL_ERROR, e.str());
return;
}
std::string line;
std::string sep;
while (cmSystemTools::GetLineFromStream(fin, line)) {
inputContent += sep + line;
sep = "\n";
}
inputContent += sep;
}
cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
cmGeneratorExpression contentGE(lfbt);
CM_AUTO_PTR<cmCompiledGeneratorExpression> inputExpression =
contentGE.Parse(inputContent);
std::map<std::string, std::string> outputFiles;
std::vector<std::string> allConfigs;
lg->GetMakefile()->GetConfigurations(allConfigs);
if (allConfigs.empty()) {
allConfigs.push_back("");
}
std::vector<std::string> enabledLanguages;
cmGlobalGenerator* gg = lg->GetGlobalGenerator();
gg->GetEnabledLanguages(enabledLanguages);
for (std::vector<std::string>::const_iterator le = enabledLanguages.begin();
le != enabledLanguages.end(); ++le) {
for (std::vector<std::string>::const_iterator li = allConfigs.begin();
li != allConfigs.end(); ++li) {
this->Generate(lg, *li, *le, inputExpression.get(), outputFiles, perm);
if (cmSystemTools::GetFatalErrorOccured()) {
return;
}
}
}
}