1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2013 Stephen Kelly <steveire@gmail.com>
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
13 #include "cmGeneratorExpressionEvaluationFile.h"
15 #include "cmMakefile.h"
19 //----------------------------------------------------------------------------
20 cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
21 const std::string &input,
22 cmsys::auto_ptr<cmCompiledGeneratorExpression> outputFileExpr,
24 cmsys::auto_ptr<cmCompiledGeneratorExpression> condition,
27 OutputFileExpr(outputFileExpr),
30 InputIsContent(inputIsContent)
34 //----------------------------------------------------------------------------
35 void cmGeneratorExpressionEvaluationFile::Generate(const char *config,
36 cmCompiledGeneratorExpression* inputExpression,
37 std::map<std::string, std::string> &outputFiles)
39 std::string rawCondition = this->Condition->GetInput();
40 if (!rawCondition.empty())
42 std::string condResult = this->Condition->Evaluate(this->Makefile, config);
43 if (condResult == "0")
47 if (condResult != "1")
50 e << "Evaluation file condition \"" << rawCondition << "\" did "
51 "not evaluate to valid content. Got \"" << condResult << "\".";
52 this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
57 const std::string outputFileName
58 = this->OutputFileExpr->Evaluate(this->Makefile, config);
59 const std::string outputContent
60 = inputExpression->Evaluate(this->Makefile, config);
62 std::map<std::string, std::string>::iterator it
63 = outputFiles.find(outputFileName);
65 if(it != outputFiles.end())
67 if (it->second == outputContent)
72 e << "Evaluation file to be written multiple times for different "
73 "configurations with different content:\n " << outputFileName;
74 this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
78 this->Files.push_back(outputFileName);
79 outputFiles[outputFileName] = outputContent;
81 std::ofstream fout(outputFileName.c_str());
86 e << "Evaluation file \"" << outputFileName << "\" cannot be written.";
87 this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
91 fout << outputContent;
96 //----------------------------------------------------------------------------
97 void cmGeneratorExpressionEvaluationFile::Generate()
99 std::string inputContent;
100 if (this->InputIsContent)
102 inputContent = this->Input;
106 std::ifstream fin(this->Input.c_str());
110 e << "Evaluation file \"" << this->Input << "\" cannot be read.";
111 this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
117 while(cmSystemTools::GetLineFromStream(fin, line))
119 inputContent += sep + line;
125 cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
126 cmGeneratorExpression contentGE(lfbt);
127 cmsys::auto_ptr<cmCompiledGeneratorExpression> inputExpression
128 = contentGE.Parse(inputContent);
130 std::map<std::string, std::string> outputFiles;
132 std::vector<std::string> allConfigs;
133 this->Makefile->GetConfigurations(allConfigs);
135 if (allConfigs.empty())
137 this->Generate(0, inputExpression.get(), outputFiles);
141 for(std::vector<std::string>::const_iterator li = allConfigs.begin();
142 li != allConfigs.end(); ++li)
144 this->Generate(li->c_str(), inputExpression.get(), outputFiles);
145 if(cmSystemTools::GetFatalErrorOccured())