123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /*============================================================================
- CMake - Cross Platform Makefile Generator
- Copyright 2013 Stephen Kelly <[email protected]>
- Distributed under the OSI-approved BSD License (the "License");
- see accompanying file Copyright.txt for details.
- This software is distributed WITHOUT ANY WARRANTY; without even the
- implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- See the License for more information.
- ============================================================================*/
- #include "cmGeneratorExpressionEvaluationFile.h"
- #include "cmMakefile.h"
- #include <cmsys/FStream.hxx>
- #include <assert.h>
- //----------------------------------------------------------------------------
- cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
- const std::string &input,
- cmsys::auto_ptr<cmCompiledGeneratorExpression> outputFileExpr,
- cmMakefile *makefile,
- cmsys::auto_ptr<cmCompiledGeneratorExpression> condition,
- bool inputIsContent)
- : Input(input),
- OutputFileExpr(outputFileExpr),
- Makefile(makefile),
- Condition(condition),
- InputIsContent(inputIsContent)
- {
- }
- //----------------------------------------------------------------------------
- void cmGeneratorExpressionEvaluationFile::Generate(const std::string& config,
- cmCompiledGeneratorExpression* inputExpression,
- std::map<std::string, std::string> &outputFiles)
- {
- std::string rawCondition = this->Condition->GetInput();
- if (!rawCondition.empty())
- {
- std::string condResult = this->Condition->Evaluate(this->Makefile, config);
- if (condResult == "0")
- {
- return;
- }
- if (condResult != "1")
- {
- cmOStringStream e;
- e << "Evaluation file condition \"" << rawCondition << "\" did "
- "not evaluate to valid content. Got \"" << condResult << "\".";
- this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
- return;
- }
- }
- const std::string outputFileName
- = this->OutputFileExpr->Evaluate(this->Makefile, config);
- const std::string outputContent
- = inputExpression->Evaluate(this->Makefile, config);
- std::map<std::string, std::string>::iterator it
- = outputFiles.find(outputFileName);
- if(it != outputFiles.end())
- {
- if (it->second == outputContent)
- {
- return;
- }
- cmOStringStream e;
- e << "Evaluation file to be written multiple times for different "
- "configurations with different content:\n " << outputFileName;
- this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
- return;
- }
- this->Files.push_back(outputFileName);
- outputFiles[outputFileName] = outputContent;
- cmsys::ofstream fout(outputFileName.c_str());
- if(!fout)
- {
- cmOStringStream e;
- e << "Evaluation file \"" << outputFileName << "\" cannot be written.";
- this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
- return;
- }
- fout << outputContent;
- fout.close();
- }
- //----------------------------------------------------------------------------
- void cmGeneratorExpressionEvaluationFile::Generate()
- {
- std::string inputContent;
- if (this->InputIsContent)
- {
- inputContent = this->Input;
- }
- else
- {
- cmsys::ifstream fin(this->Input.c_str());
- if(!fin)
- {
- cmOStringStream e;
- e << "Evaluation file \"" << this->Input << "\" cannot be read.";
- this->Makefile->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);
- cmsys::auto_ptr<cmCompiledGeneratorExpression> inputExpression
- = contentGE.Parse(inputContent);
- std::map<std::string, std::string> outputFiles;
- std::vector<std::string> allConfigs;
- this->Makefile->GetConfigurations(allConfigs);
- if (allConfigs.empty())
- {
- this->Generate("", inputExpression.get(), outputFiles);
- }
- else
- {
- for(std::vector<std::string>::const_iterator li = allConfigs.begin();
- li != allConfigs.end(); ++li)
- {
- this->Generate(*li, inputExpression.get(), outputFiles);
- if(cmSystemTools::GetFatalErrorOccured())
- {
- return;
- }
- }
- }
- }
|