cmGeneratorExpressionEvaluationFile.cxx 8.8 KB

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