cmGeneratorExpressionEvaluationFile.cxx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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>
  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 "cmMessageType.h"
  14. #include "cmSourceFile.h"
  15. #include "cmSourceFileLocationKind.h"
  16. #include "cmSystemTools.h"
  17. cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
  18. std::string input,
  19. std::unique_ptr<cmCompiledGeneratorExpression> outputFileExpr,
  20. std::unique_ptr<cmCompiledGeneratorExpression> condition,
  21. bool inputIsContent, cmPolicies::PolicyStatus policyStatusCMP0070)
  22. : Input(std::move(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(MessageType::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. auto it = 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(MessageType::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);
  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(
  94. name, false, cmSourceFileLocationKind::Known);
  95. // Tell TraceDependencies that the file is not expected to exist
  96. // on disk yet. We generate it after that runs.
  97. sf->SetProperty("GENERATED", "1");
  98. // Tell the build system generators that there is no build rule
  99. // to generate the file.
  100. sf->SetProperty("__CMAKE_GENERATED_BY_CMAKE", "1");
  101. gg->SetFilenameTargetDepends(
  102. sf, this->OutputFileExpr->GetSourceSensitiveTargets());
  103. }
  104. }
  105. void cmGeneratorExpressionEvaluationFile::Generate(cmLocalGenerator* lg)
  106. {
  107. mode_t perm = 0;
  108. std::string inputContent;
  109. if (this->InputIsContent) {
  110. inputContent = this->Input;
  111. } else {
  112. std::string inputFileName = this->Input;
  113. if (cmSystemTools::FileIsFullPath(inputFileName)) {
  114. inputFileName = cmSystemTools::CollapseFullPath(inputFileName);
  115. } else {
  116. inputFileName = this->FixRelativePath(inputFileName, PathForInput, lg);
  117. }
  118. lg->GetMakefile()->AddCMakeDependFile(inputFileName);
  119. cmSystemTools::GetPermissions(inputFileName.c_str(), perm);
  120. cmsys::ifstream fin(inputFileName.c_str());
  121. if (!fin) {
  122. std::ostringstream e;
  123. e << "Evaluation file \"" << inputFileName << "\" cannot be read.";
  124. lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
  125. return;
  126. }
  127. std::string line;
  128. std::string sep;
  129. while (cmSystemTools::GetLineFromStream(fin, line)) {
  130. inputContent += sep + line;
  131. sep = "\n";
  132. }
  133. inputContent += sep;
  134. }
  135. cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
  136. cmGeneratorExpression contentGE(lfbt);
  137. std::unique_ptr<cmCompiledGeneratorExpression> inputExpression =
  138. contentGE.Parse(inputContent);
  139. std::map<std::string, std::string> outputFiles;
  140. std::vector<std::string> allConfigs;
  141. lg->GetMakefile()->GetConfigurations(allConfigs);
  142. if (allConfigs.empty()) {
  143. allConfigs.emplace_back();
  144. }
  145. std::vector<std::string> enabledLanguages;
  146. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  147. gg->GetEnabledLanguages(enabledLanguages);
  148. for (std::string const& le : enabledLanguages) {
  149. for (std::string const& li : allConfigs) {
  150. this->Generate(lg, li, le, inputExpression.get(), outputFiles, perm);
  151. if (cmSystemTools::GetFatalErrorOccured()) {
  152. return;
  153. }
  154. }
  155. }
  156. }
  157. std::string cmGeneratorExpressionEvaluationFile::FixRelativePath(
  158. std::string const& relativePath, PathRole role, cmLocalGenerator* lg)
  159. {
  160. std::string resultPath;
  161. switch (this->PolicyStatusCMP0070) {
  162. case cmPolicies::WARN: {
  163. std::string arg;
  164. switch (role) {
  165. case PathForInput:
  166. arg = "INPUT";
  167. break;
  168. case PathForOutput:
  169. arg = "OUTPUT";
  170. break;
  171. }
  172. std::ostringstream w;
  173. /* clang-format off */
  174. w <<
  175. cmPolicies::GetPolicyWarning(cmPolicies::CMP0070) << "\n"
  176. "file(GENERATE) given relative " << arg << " path:\n"
  177. " " << relativePath << "\n"
  178. "This is not defined behavior unless CMP0070 is set to NEW. "
  179. "For compatibility with older versions of CMake, the previous "
  180. "undefined behavior will be used."
  181. ;
  182. /* clang-format on */
  183. lg->IssueMessage(MessageType::AUTHOR_WARNING, w.str());
  184. }
  185. CM_FALLTHROUGH;
  186. case cmPolicies::OLD:
  187. // OLD behavior is to use the relative path unchanged,
  188. // which ends up being used relative to the working dir.
  189. resultPath = relativePath;
  190. break;
  191. case cmPolicies::REQUIRED_IF_USED:
  192. case cmPolicies::REQUIRED_ALWAYS:
  193. case cmPolicies::NEW:
  194. // NEW behavior is to interpret the relative path with respect
  195. // to the current source or binary directory.
  196. switch (role) {
  197. case PathForInput:
  198. resultPath = cmSystemTools::CollapseFullPath(
  199. relativePath, lg->GetCurrentSourceDirectory());
  200. break;
  201. case PathForOutput:
  202. resultPath = cmSystemTools::CollapseFullPath(
  203. relativePath, lg->GetCurrentBinaryDirectory());
  204. break;
  205. }
  206. break;
  207. }
  208. return resultPath;
  209. }