cmGeneratorExpressionEvaluationFile.cxx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. cmsys::auto_ptr<cmCompiledGeneratorExpression> condition,
  23. bool inputIsContent)
  24. : Input(input),
  25. OutputFileExpr(outputFileExpr),
  26. Condition(condition),
  27. InputIsContent(inputIsContent)
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. void cmGeneratorExpressionEvaluationFile::Generate(cmLocalGenerator* lg,
  32. const std::string& config,
  33. const std::string& lang,
  34. cmCompiledGeneratorExpression* inputExpression,
  35. std::map<std::string, std::string> &outputFiles, mode_t perm)
  36. {
  37. std::string rawCondition = this->Condition->GetInput();
  38. if (!rawCondition.empty())
  39. {
  40. std::string condResult = this->Condition->Evaluate(lg->GetMakefile(),
  41. 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. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  53. return;
  54. }
  55. }
  56. const std::string outputFileName
  57. = this->OutputFileExpr->Evaluate(lg->GetMakefile(), config,
  58. false, 0, 0, 0, lang);
  59. const std::string outputContent
  60. = inputExpression->Evaluate(lg->GetMakefile(),
  61. config,
  62. false, 0, 0, 0, lang);
  63. std::map<std::string, std::string>::iterator it
  64. = outputFiles.find(outputFileName);
  65. if(it != outputFiles.end())
  66. {
  67. if (it->second == outputContent)
  68. {
  69. return;
  70. }
  71. std::ostringstream e;
  72. e << "Evaluation file to be written multiple times for different "
  73. "configurations or languages with different content:\n "
  74. << outputFileName;
  75. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  76. return;
  77. }
  78. lg->GetMakefile()->AddCMakeOutputFile(outputFileName.c_str());
  79. this->Files.push_back(outputFileName);
  80. outputFiles[outputFileName] = outputContent;
  81. cmGeneratedFileStream fout(outputFileName.c_str());
  82. fout.SetCopyIfDifferent(true);
  83. fout << outputContent;
  84. if (fout.Close() && perm)
  85. {
  86. cmSystemTools::SetPermissions(outputFileName.c_str(), perm);
  87. }
  88. }
  89. //----------------------------------------------------------------------------
  90. void cmGeneratorExpressionEvaluationFile::CreateOutputFile(
  91. cmLocalGenerator *lg, std::string const& config)
  92. {
  93. std::vector<std::string> enabledLanguages;
  94. cmGlobalGenerator *gg = lg->GetGlobalGenerator();
  95. gg->GetEnabledLanguages(enabledLanguages);
  96. for(std::vector<std::string>::const_iterator le = enabledLanguages.begin();
  97. le != enabledLanguages.end(); ++le)
  98. {
  99. std::string name = this->OutputFileExpr->Evaluate(lg->GetMakefile(),
  100. config,
  101. false, 0, 0, 0, *le);
  102. cmSourceFile* sf = lg->GetMakefile()->GetOrCreateSource(name);
  103. sf->SetProperty("GENERATED", "1");
  104. gg->SetFilenameTargetDepends(sf,
  105. this->OutputFileExpr->GetSourceSensitiveTargets());
  106. }
  107. }
  108. //----------------------------------------------------------------------------
  109. void cmGeneratorExpressionEvaluationFile::Generate(cmLocalGenerator *lg)
  110. {
  111. mode_t perm = 0;
  112. std::string inputContent;
  113. if (this->InputIsContent)
  114. {
  115. inputContent = this->Input;
  116. }
  117. else
  118. {
  119. lg->GetMakefile()->AddCMakeDependFile(this->Input.c_str());
  120. cmSystemTools::GetPermissions(this->Input.c_str(), perm);
  121. cmsys::ifstream fin(this->Input.c_str());
  122. if(!fin)
  123. {
  124. std::ostringstream e;
  125. e << "Evaluation file \"" << this->Input << "\" cannot be read.";
  126. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  127. return;
  128. }
  129. std::string line;
  130. std::string sep;
  131. while(cmSystemTools::GetLineFromStream(fin, line))
  132. {
  133. inputContent += sep + line;
  134. sep = "\n";
  135. }
  136. inputContent += sep;
  137. }
  138. cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
  139. cmGeneratorExpression contentGE(lfbt);
  140. cmsys::auto_ptr<cmCompiledGeneratorExpression> inputExpression
  141. = contentGE.Parse(inputContent);
  142. std::map<std::string, std::string> outputFiles;
  143. std::vector<std::string> allConfigs;
  144. lg->GetMakefile()->GetConfigurations(allConfigs);
  145. if (allConfigs.empty())
  146. {
  147. allConfigs.push_back("");
  148. }
  149. std::vector<std::string> enabledLanguages;
  150. cmGlobalGenerator *gg = lg->GetGlobalGenerator();
  151. gg->GetEnabledLanguages(enabledLanguages);
  152. for(std::vector<std::string>::const_iterator le = enabledLanguages.begin();
  153. le != enabledLanguages.end(); ++le)
  154. {
  155. for(std::vector<std::string>::const_iterator li = allConfigs.begin();
  156. li != allConfigs.end(); ++li)
  157. {
  158. this->Generate(lg, *li, *le, inputExpression.get(), outputFiles, perm);
  159. if(cmSystemTools::GetFatalErrorOccured())
  160. {
  161. return;
  162. }
  163. }
  164. }
  165. }