cmGeneratorExpressionEvaluationFile.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <memory>
  5. #include <sstream>
  6. #include <utility>
  7. #include "cmsys/FStream.hxx"
  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 =
  37. this->Condition->Evaluate(lg, config, 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. const std::string outputFileName = this->GetOutputFileName(lg, config, lang);
  52. const std::string& outputContent =
  53. inputExpression->Evaluate(lg, config, nullptr, nullptr, nullptr, lang);
  54. auto it = 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 with different "
  61. "content. "
  62. "This is generally caused by the content evaluating the "
  63. "configuration type, language, or location of object files:\n "
  64. << outputFileName;
  65. lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
  66. return;
  67. }
  68. lg->GetMakefile()->AddCMakeOutputFile(outputFileName);
  69. this->Files.push_back(outputFileName);
  70. outputFiles[outputFileName] = outputContent;
  71. cmGeneratedFileStream fout(outputFileName);
  72. fout.SetCopyIfDifferent(true);
  73. fout << outputContent;
  74. if (fout.Close() && perm) {
  75. cmSystemTools::SetPermissions(outputFileName.c_str(), perm);
  76. }
  77. }
  78. void cmGeneratorExpressionEvaluationFile::CreateOutputFile(
  79. cmLocalGenerator* lg, std::string const& config)
  80. {
  81. std::vector<std::string> enabledLanguages;
  82. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  83. gg->GetEnabledLanguages(enabledLanguages);
  84. for (std::string const& le : enabledLanguages) {
  85. std::string const name = this->GetOutputFileName(lg, config, le);
  86. cmSourceFile* sf = lg->GetMakefile()->GetOrCreateSource(
  87. name, false, cmSourceFileLocationKind::Known);
  88. // Tell TraceDependencies that the file is not expected to exist
  89. // on disk yet. We generate it after that runs.
  90. sf->SetProperty("GENERATED", "1");
  91. // Tell the build system generators that there is no build rule
  92. // to generate the file.
  93. sf->SetProperty("__CMAKE_GENERATED_BY_CMAKE", "1");
  94. gg->SetFilenameTargetDepends(
  95. sf, this->OutputFileExpr->GetSourceSensitiveTargets());
  96. }
  97. }
  98. void cmGeneratorExpressionEvaluationFile::Generate(cmLocalGenerator* lg)
  99. {
  100. mode_t perm = 0;
  101. std::string inputContent;
  102. if (this->InputIsContent) {
  103. inputContent = this->Input;
  104. } else {
  105. const std::string inputFileName = this->GetInputFileName(lg);
  106. lg->GetMakefile()->AddCMakeDependFile(inputFileName);
  107. cmSystemTools::GetPermissions(inputFileName.c_str(), perm);
  108. cmsys::ifstream fin(inputFileName.c_str());
  109. if (!fin) {
  110. std::ostringstream e;
  111. e << "Evaluation file \"" << inputFileName << "\" cannot be read.";
  112. lg->IssueMessage(MessageType::FATAL_ERROR, e.str());
  113. return;
  114. }
  115. std::string line;
  116. std::string sep;
  117. while (cmSystemTools::GetLineFromStream(fin, line)) {
  118. inputContent += sep + line;
  119. sep = "\n";
  120. }
  121. inputContent += sep;
  122. }
  123. cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
  124. cmGeneratorExpression contentGE(lfbt);
  125. std::unique_ptr<cmCompiledGeneratorExpression> inputExpression =
  126. contentGE.Parse(inputContent);
  127. std::map<std::string, std::string> outputFiles;
  128. std::vector<std::string> allConfigs =
  129. lg->GetMakefile()->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  130. std::vector<std::string> enabledLanguages;
  131. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  132. gg->GetEnabledLanguages(enabledLanguages);
  133. for (std::string const& le : enabledLanguages) {
  134. for (std::string const& li : allConfigs) {
  135. this->Generate(lg, li, le, inputExpression.get(), outputFiles, perm);
  136. if (cmSystemTools::GetFatalErrorOccured()) {
  137. return;
  138. }
  139. }
  140. }
  141. }
  142. std::string cmGeneratorExpressionEvaluationFile::GetInputFileName(
  143. cmLocalGenerator* lg)
  144. {
  145. std::string inputFileName = this->Input;
  146. if (cmSystemTools::FileIsFullPath(inputFileName)) {
  147. inputFileName = cmSystemTools::CollapseFullPath(inputFileName);
  148. } else {
  149. inputFileName = this->FixRelativePath(inputFileName, PathForInput, lg);
  150. }
  151. return inputFileName;
  152. }
  153. std::string cmGeneratorExpressionEvaluationFile::GetOutputFileName(
  154. cmLocalGenerator* lg, const std::string& config, const std::string& lang)
  155. {
  156. std::string outputFileName = this->OutputFileExpr->Evaluate(
  157. lg, config, nullptr, nullptr, nullptr, lang);
  158. if (cmSystemTools::FileIsFullPath(outputFileName)) {
  159. outputFileName = cmSystemTools::CollapseFullPath(outputFileName);
  160. } else {
  161. outputFileName = this->FixRelativePath(outputFileName, PathForOutput, lg);
  162. }
  163. return outputFileName;
  164. }
  165. std::string cmGeneratorExpressionEvaluationFile::FixRelativePath(
  166. std::string const& relativePath, PathRole role, cmLocalGenerator* lg)
  167. {
  168. std::string resultPath;
  169. switch (this->PolicyStatusCMP0070) {
  170. case cmPolicies::WARN: {
  171. std::string arg;
  172. switch (role) {
  173. case PathForInput:
  174. arg = "INPUT";
  175. break;
  176. case PathForOutput:
  177. arg = "OUTPUT";
  178. break;
  179. }
  180. std::ostringstream w;
  181. /* clang-format off */
  182. w <<
  183. cmPolicies::GetPolicyWarning(cmPolicies::CMP0070) << "\n"
  184. "file(GENERATE) given relative " << arg << " path:\n"
  185. " " << relativePath << "\n"
  186. "This is not defined behavior unless CMP0070 is set to NEW. "
  187. "For compatibility with older versions of CMake, the previous "
  188. "undefined behavior will be used."
  189. ;
  190. /* clang-format on */
  191. lg->IssueMessage(MessageType::AUTHOR_WARNING, w.str());
  192. }
  193. CM_FALLTHROUGH;
  194. case cmPolicies::OLD:
  195. // OLD behavior is to use the relative path unchanged,
  196. // which ends up being used relative to the working dir.
  197. resultPath = relativePath;
  198. break;
  199. case cmPolicies::REQUIRED_IF_USED:
  200. case cmPolicies::REQUIRED_ALWAYS:
  201. case cmPolicies::NEW:
  202. // NEW behavior is to interpret the relative path with respect
  203. // to the current source or binary directory.
  204. switch (role) {
  205. case PathForInput:
  206. resultPath = cmSystemTools::CollapseFullPath(
  207. relativePath, lg->GetCurrentSourceDirectory());
  208. break;
  209. case PathForOutput:
  210. resultPath = cmSystemTools::CollapseFullPath(
  211. relativePath, lg->GetCurrentBinaryDirectory());
  212. break;
  213. }
  214. break;
  215. }
  216. return resultPath;
  217. }