1
0

cmGeneratorExpressionEvaluationFile.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. 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. if (cmSystemTools::FileIsFullPath(outputFileName)) {
  54. outputFileName = cmSystemTools::CollapseFullPath(outputFileName);
  55. }
  56. std::map<std::string, std::string>::iterator it =
  57. outputFiles.find(outputFileName);
  58. if (it != outputFiles.end()) {
  59. if (it->second == outputContent) {
  60. return;
  61. }
  62. std::ostringstream e;
  63. e << "Evaluation file to be written multiple times with different "
  64. "content. "
  65. "This is generally caused by the content evaluating the "
  66. "configuration type, language, or location of object files:\n "
  67. << outputFileName;
  68. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  69. return;
  70. }
  71. lg->GetMakefile()->AddCMakeOutputFile(outputFileName);
  72. this->Files.push_back(outputFileName);
  73. outputFiles[outputFileName] = outputContent;
  74. cmGeneratedFileStream fout(outputFileName.c_str());
  75. fout.SetCopyIfDifferent(true);
  76. fout << outputContent;
  77. if (fout.Close() && perm) {
  78. cmSystemTools::SetPermissions(outputFileName.c_str(), perm);
  79. }
  80. }
  81. void cmGeneratorExpressionEvaluationFile::CreateOutputFile(
  82. cmLocalGenerator* lg, std::string const& config)
  83. {
  84. std::vector<std::string> enabledLanguages;
  85. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  86. gg->GetEnabledLanguages(enabledLanguages);
  87. for (std::vector<std::string>::const_iterator le = enabledLanguages.begin();
  88. le != enabledLanguages.end(); ++le) {
  89. std::string name = this->OutputFileExpr->Evaluate(
  90. lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, *le);
  91. cmSourceFile* sf = lg->GetMakefile()->GetOrCreateSource(name);
  92. sf->SetProperty("GENERATED", "1");
  93. gg->SetFilenameTargetDepends(
  94. sf, this->OutputFileExpr->GetSourceSensitiveTargets());
  95. }
  96. }
  97. void cmGeneratorExpressionEvaluationFile::Generate(cmLocalGenerator* lg)
  98. {
  99. mode_t perm = 0;
  100. std::string inputContent;
  101. if (this->InputIsContent) {
  102. inputContent = this->Input;
  103. } else {
  104. std::string inputFileName = this->Input;
  105. if (cmSystemTools::FileIsFullPath(inputFileName)) {
  106. inputFileName = cmSystemTools::CollapseFullPath(inputFileName);
  107. }
  108. lg->GetMakefile()->AddCMakeDependFile(inputFileName);
  109. cmSystemTools::GetPermissions(inputFileName.c_str(), perm);
  110. cmsys::ifstream fin(inputFileName.c_str());
  111. if (!fin) {
  112. std::ostringstream e;
  113. e << "Evaluation file \"" << inputFileName << "\" cannot be read.";
  114. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  115. return;
  116. }
  117. std::string line;
  118. std::string sep;
  119. while (cmSystemTools::GetLineFromStream(fin, line)) {
  120. inputContent += sep + line;
  121. sep = "\n";
  122. }
  123. inputContent += sep;
  124. }
  125. cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
  126. cmGeneratorExpression contentGE(lfbt);
  127. CM_AUTO_PTR<cmCompiledGeneratorExpression> inputExpression =
  128. contentGE.Parse(inputContent);
  129. std::map<std::string, std::string> outputFiles;
  130. std::vector<std::string> allConfigs;
  131. lg->GetMakefile()->GetConfigurations(allConfigs);
  132. if (allConfigs.empty()) {
  133. allConfigs.push_back("");
  134. }
  135. std::vector<std::string> enabledLanguages;
  136. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  137. gg->GetEnabledLanguages(enabledLanguages);
  138. for (std::vector<std::string>::const_iterator le = enabledLanguages.begin();
  139. le != enabledLanguages.end(); ++le) {
  140. for (std::vector<std::string>::const_iterator li = allConfigs.begin();
  141. li != allConfigs.end(); ++li) {
  142. this->Generate(lg, *li, *le, inputExpression.get(), outputFiles, perm);
  143. if (cmSystemTools::GetFatalErrorOccured()) {
  144. return;
  145. }
  146. }
  147. }
  148. }