cmGeneratorExpressionEvaluationFile.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2013 Stephen Kelly <[email protected]>
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmGeneratorExpressionEvaluationFile.h"
  11. #include "cmMakefile.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmSourceFile.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include <cmsys/FStream.hxx>
  17. #include <assert.h>
  18. //----------------------------------------------------------------------------
  19. cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
  20. const std::string &input,
  21. cmsys::auto_ptr<cmCompiledGeneratorExpression> outputFileExpr,
  22. cmMakefile *makefile,
  23. cmsys::auto_ptr<cmCompiledGeneratorExpression> condition,
  24. bool inputIsContent)
  25. : Input(input),
  26. OutputFileExpr(outputFileExpr),
  27. Makefile(makefile),
  28. Condition(condition),
  29. InputIsContent(inputIsContent)
  30. {
  31. }
  32. //----------------------------------------------------------------------------
  33. void cmGeneratorExpressionEvaluationFile::Generate(const std::string& config,
  34. const std::string& lang,
  35. cmCompiledGeneratorExpression* inputExpression,
  36. std::map<std::string, std::string> &outputFiles, mode_t perm)
  37. {
  38. std::string rawCondition = this->Condition->GetInput();
  39. if (!rawCondition.empty())
  40. {
  41. std::string condResult = this->Condition->Evaluate(this->Makefile, config,
  42. false, 0, 0, 0, lang);
  43. if (condResult == "0")
  44. {
  45. return;
  46. }
  47. if (condResult != "1")
  48. {
  49. std::ostringstream e;
  50. e << "Evaluation file condition \"" << rawCondition << "\" did "
  51. "not evaluate to valid content. Got \"" << condResult << "\".";
  52. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  53. return;
  54. }
  55. }
  56. const std::string outputFileName
  57. = this->OutputFileExpr->Evaluate(this->Makefile, config,
  58. false, 0, 0, 0, lang);
  59. const std::string outputContent
  60. = inputExpression->Evaluate(this->Makefile, config,
  61. false, 0, 0, 0, lang);
  62. std::map<std::string, std::string>::iterator it
  63. = outputFiles.find(outputFileName);
  64. if(it != outputFiles.end())
  65. {
  66. if (it->second == outputContent)
  67. {
  68. return;
  69. }
  70. std::ostringstream e;
  71. e << "Evaluation file to be written multiple times for different "
  72. "configurations or languages with different content:\n "
  73. << outputFileName;
  74. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  75. return;
  76. }
  77. this->Makefile->AddCMakeOutputFile(outputFileName.c_str());
  78. this->Files.push_back(outputFileName);
  79. outputFiles[outputFileName] = outputContent;
  80. cmGeneratedFileStream fout(outputFileName.c_str());
  81. fout.SetCopyIfDifferent(true);
  82. fout << outputContent;
  83. if (fout.Close() && perm)
  84. {
  85. cmSystemTools::SetPermissions(outputFileName.c_str(), perm);
  86. }
  87. }
  88. //----------------------------------------------------------------------------
  89. void cmGeneratorExpressionEvaluationFile::CreateOutputFile(
  90. std::string const& config)
  91. {
  92. std::vector<std::string> enabledLanguages;
  93. cmGlobalGenerator *gg = this->Makefile->GetGlobalGenerator();
  94. gg->GetEnabledLanguages(enabledLanguages);
  95. for(std::vector<std::string>::const_iterator le = enabledLanguages.begin();
  96. le != enabledLanguages.end(); ++le)
  97. {
  98. std::string name = this->OutputFileExpr->Evaluate(this->Makefile, config,
  99. false, 0, 0, 0, *le);
  100. cmSourceFile* sf = this->Makefile->GetOrCreateSource(name);
  101. sf->SetProperty("GENERATED", "1");
  102. gg->SetFilenameTargetDepends(sf,
  103. this->OutputFileExpr->GetSourceSensitiveTargets());
  104. }
  105. }
  106. //----------------------------------------------------------------------------
  107. void cmGeneratorExpressionEvaluationFile::Generate()
  108. {
  109. mode_t perm = 0;
  110. std::string inputContent;
  111. if (this->InputIsContent)
  112. {
  113. inputContent = this->Input;
  114. }
  115. else
  116. {
  117. this->Makefile->AddCMakeDependFile(this->Input.c_str());
  118. cmSystemTools::GetPermissions(this->Input.c_str(), perm);
  119. cmsys::ifstream fin(this->Input.c_str());
  120. if(!fin)
  121. {
  122. std::ostringstream e;
  123. e << "Evaluation file \"" << this->Input << "\" cannot be read.";
  124. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  125. return;
  126. }
  127. std::string line;
  128. std::string sep;
  129. while(cmSystemTools::GetLineFromStream(fin, line))
  130. {
  131. inputContent += sep + line;
  132. sep = "\n";
  133. }
  134. inputContent += sep;
  135. }
  136. cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
  137. cmGeneratorExpression contentGE(lfbt);
  138. cmsys::auto_ptr<cmCompiledGeneratorExpression> inputExpression
  139. = contentGE.Parse(inputContent);
  140. std::map<std::string, std::string> outputFiles;
  141. std::vector<std::string> allConfigs;
  142. this->Makefile->GetConfigurations(allConfigs);
  143. if (allConfigs.empty())
  144. {
  145. allConfigs.push_back("");
  146. }
  147. std::vector<std::string> enabledLanguages;
  148. cmGlobalGenerator *gg = this->Makefile->GetGlobalGenerator();
  149. gg->GetEnabledLanguages(enabledLanguages);
  150. for(std::vector<std::string>::const_iterator le = enabledLanguages.begin();
  151. le != enabledLanguages.end(); ++le)
  152. {
  153. for(std::vector<std::string>::const_iterator li = allConfigs.begin();
  154. li != allConfigs.end(); ++li)
  155. {
  156. this->Generate(*li, *le, inputExpression.get(), outputFiles, perm);
  157. if(cmSystemTools::GetFatalErrorOccured())
  158. {
  159. return;
  160. }
  161. }
  162. }
  163. }