cmGeneratorExpressionEvaluationFile.cxx 8.0 KB

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