cmGeneratorExpressionEvaluationFile.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "cmsys/FStream.hxx"
  5. #include <memory> // IWYU pragma: keep
  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 "cmake.h"
  16. cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
  17. const std::string& input,
  18. std::unique_ptr<cmCompiledGeneratorExpression> outputFileExpr,
  19. std::unique_ptr<cmCompiledGeneratorExpression> condition,
  20. bool inputIsContent, cmPolicies::PolicyStatus policyStatusCMP0070)
  21. : Input(input)
  22. , OutputFileExpr(std::move(outputFileExpr))
  23. , Condition(std::move(condition))
  24. , InputIsContent(inputIsContent)
  25. , PolicyStatusCMP0070(policyStatusCMP0070)
  26. {
  27. }
  28. void cmGeneratorExpressionEvaluationFile::Generate(
  29. cmLocalGenerator* lg, const std::string& config, const std::string& lang,
  30. cmCompiledGeneratorExpression* inputExpression,
  31. std::map<std::string, std::string>& outputFiles, mode_t perm)
  32. {
  33. std::string rawCondition = this->Condition->GetInput();
  34. if (!rawCondition.empty()) {
  35. std::string condResult = this->Condition->Evaluate(
  36. lg, config, false, nullptr, nullptr, nullptr, lang);
  37. if (condResult == "0") {
  38. return;
  39. }
  40. if (condResult != "1") {
  41. std::ostringstream e;
  42. e << "Evaluation file condition \"" << rawCondition
  43. << "\" did "
  44. "not evaluate to valid content. Got \""
  45. << condResult << "\".";
  46. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  47. return;
  48. }
  49. }
  50. std::string outputFileName = this->OutputFileExpr->Evaluate(
  51. lg, config, false, nullptr, nullptr, nullptr, lang);
  52. const std::string outputContent = inputExpression->Evaluate(
  53. lg, config, false, nullptr, nullptr, nullptr, lang);
  54. if (cmSystemTools::FileIsFullPath(outputFileName)) {
  55. outputFileName = cmSystemTools::CollapseFullPath(outputFileName);
  56. } else {
  57. outputFileName = this->FixRelativePath(outputFileName, PathForOutput, lg);
  58. }
  59. std::map<std::string, std::string>::iterator it =
  60. outputFiles.find(outputFileName);
  61. if (it != outputFiles.end()) {
  62. if (it->second == outputContent) {
  63. return;
  64. }
  65. std::ostringstream e;
  66. e << "Evaluation file to be written multiple times with different "
  67. "content. "
  68. "This is generally caused by the content evaluating the "
  69. "configuration type, language, or location of object files:\n "
  70. << outputFileName;
  71. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  72. return;
  73. }
  74. lg->GetMakefile()->AddCMakeOutputFile(outputFileName);
  75. this->Files.push_back(outputFileName);
  76. outputFiles[outputFileName] = outputContent;
  77. cmGeneratedFileStream fout(outputFileName.c_str());
  78. fout.SetCopyIfDifferent(true);
  79. fout << outputContent;
  80. if (fout.Close() && perm) {
  81. cmSystemTools::SetPermissions(outputFileName.c_str(), perm);
  82. }
  83. }
  84. void cmGeneratorExpressionEvaluationFile::CreateOutputFile(
  85. cmLocalGenerator* lg, std::string const& config)
  86. {
  87. std::vector<std::string> enabledLanguages;
  88. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  89. gg->GetEnabledLanguages(enabledLanguages);
  90. for (std::string const& le : enabledLanguages) {
  91. std::string name = this->OutputFileExpr->Evaluate(
  92. lg, config, false, nullptr, nullptr, nullptr, le);
  93. cmSourceFile* sf = lg->GetMakefile()->GetOrCreateSource(name);
  94. sf->SetProperty("GENERATED", "1");
  95. gg->SetFilenameTargetDepends(
  96. sf, this->OutputFileExpr->GetSourceSensitiveTargets());
  97. }
  98. }
  99. void cmGeneratorExpressionEvaluationFile::Generate(cmLocalGenerator* lg)
  100. {
  101. mode_t perm = 0;
  102. std::string inputContent;
  103. if (this->InputIsContent) {
  104. inputContent = this->Input;
  105. } else {
  106. std::string inputFileName = this->Input;
  107. if (cmSystemTools::FileIsFullPath(inputFileName)) {
  108. inputFileName = cmSystemTools::CollapseFullPath(inputFileName);
  109. } else {
  110. inputFileName = this->FixRelativePath(inputFileName, PathForInput, lg);
  111. }
  112. lg->GetMakefile()->AddCMakeDependFile(inputFileName);
  113. cmSystemTools::GetPermissions(inputFileName.c_str(), perm);
  114. cmsys::ifstream fin(inputFileName.c_str());
  115. if (!fin) {
  116. std::ostringstream e;
  117. e << "Evaluation file \"" << inputFileName << "\" cannot be read.";
  118. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  119. return;
  120. }
  121. std::string line;
  122. std::string sep;
  123. while (cmSystemTools::GetLineFromStream(fin, line)) {
  124. inputContent += sep + line;
  125. sep = "\n";
  126. }
  127. inputContent += sep;
  128. }
  129. cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
  130. cmGeneratorExpression contentGE(lfbt);
  131. std::unique_ptr<cmCompiledGeneratorExpression> inputExpression =
  132. contentGE.Parse(inputContent);
  133. std::map<std::string, std::string> outputFiles;
  134. std::vector<std::string> allConfigs;
  135. lg->GetMakefile()->GetConfigurations(allConfigs);
  136. if (allConfigs.empty()) {
  137. allConfigs.push_back("");
  138. }
  139. std::vector<std::string> enabledLanguages;
  140. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  141. gg->GetEnabledLanguages(enabledLanguages);
  142. for (std::string const& le : enabledLanguages) {
  143. for (std::string const& li : allConfigs) {
  144. this->Generate(lg, li, le, inputExpression.get(), outputFiles, perm);
  145. if (cmSystemTools::GetFatalErrorOccured()) {
  146. return;
  147. }
  148. }
  149. }
  150. }
  151. std::string cmGeneratorExpressionEvaluationFile::FixRelativePath(
  152. std::string const& relativePath, PathRole role, cmLocalGenerator* lg)
  153. {
  154. std::string resultPath;
  155. switch (this->PolicyStatusCMP0070) {
  156. case cmPolicies::WARN: {
  157. std::string arg;
  158. switch (role) {
  159. case PathForInput:
  160. arg = "INPUT";
  161. break;
  162. case PathForOutput:
  163. arg = "OUTPUT";
  164. break;
  165. }
  166. std::ostringstream w;
  167. /* clang-format off */
  168. w <<
  169. cmPolicies::GetPolicyWarning(cmPolicies::CMP0070) << "\n"
  170. "file(GENERATE) given relative " << arg << " path:\n"
  171. " " << relativePath << "\n"
  172. "This is not defined behavior unless CMP0070 is set to NEW. "
  173. "For compatibility with older versions of CMake, the previous "
  174. "undefined behavior will be used."
  175. ;
  176. /* clang-format on */
  177. lg->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  178. }
  179. CM_FALLTHROUGH;
  180. case cmPolicies::OLD:
  181. // OLD behavior is to use the relative path unchanged,
  182. // which ends up being used relative to the working dir.
  183. resultPath = relativePath;
  184. break;
  185. case cmPolicies::REQUIRED_IF_USED:
  186. case cmPolicies::REQUIRED_ALWAYS:
  187. case cmPolicies::NEW:
  188. // NEW behavior is to interpret the relative path with respect
  189. // to the current source or binary directory.
  190. switch (role) {
  191. case PathForInput:
  192. resultPath = cmSystemTools::CollapseFullPath(
  193. relativePath, lg->GetCurrentSourceDirectory());
  194. break;
  195. case PathForOutput:
  196. resultPath = cmSystemTools::CollapseFullPath(
  197. relativePath, lg->GetCurrentBinaryDirectory());
  198. break;
  199. }
  200. break;
  201. }
  202. return resultPath;
  203. }