cmGeneratorExpressionEvaluationFile.cxx 7.3 KB

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