cmGeneratorExpressionEvaluationFile.cxx 7.6 KB

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