cmGeneratorExpressionEvaluationFile.cxx 5.1 KB

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