cmGeneratorExpressionEvaluationFile.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2013 Stephen Kelly <[email protected]>
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmGeneratorExpressionEvaluationFile.h"
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmListFileCache.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmSourceFile.h"
  17. #include "cmSystemTools.h"
  18. #include "cmake.h"
  19. #include <cmConfigure.h>
  20. #include <cmsys/FStream.hxx>
  21. #include <sstream>
  22. #include <utility>
  23. cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
  24. const std::string& input,
  25. CM_AUTO_PTR<cmCompiledGeneratorExpression> outputFileExpr,
  26. CM_AUTO_PTR<cmCompiledGeneratorExpression> condition, bool inputIsContent)
  27. : Input(input)
  28. , OutputFileExpr(outputFileExpr)
  29. , Condition(condition)
  30. , InputIsContent(inputIsContent)
  31. {
  32. }
  33. void cmGeneratorExpressionEvaluationFile::Generate(
  34. cmLocalGenerator* lg, const std::string& config, const std::string& lang,
  35. cmCompiledGeneratorExpression* inputExpression,
  36. std::map<std::string, std::string>& outputFiles, mode_t perm)
  37. {
  38. std::string rawCondition = this->Condition->GetInput();
  39. if (!rawCondition.empty()) {
  40. std::string condResult = this->Condition->Evaluate(
  41. lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_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(cmake::FATAL_ERROR, e.str());
  52. return;
  53. }
  54. }
  55. const std::string outputFileName = this->OutputFileExpr->Evaluate(
  56. lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, lang);
  57. const std::string outputContent = inputExpression->Evaluate(
  58. lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, lang);
  59. std::map<std::string, std::string>::iterator it =
  60. 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 for different "
  67. "configurations or languages with different content:\n "
  68. << outputFileName;
  69. lg->IssueMessage(cmake::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.c_str());
  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. gg->GetEnabledLanguages(enabledLanguages);
  88. for (std::vector<std::string>::const_iterator le = enabledLanguages.begin();
  89. le != enabledLanguages.end(); ++le) {
  90. std::string name = this->OutputFileExpr->Evaluate(
  91. lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, *le);
  92. cmSourceFile* sf = lg->GetMakefile()->GetOrCreateSource(name);
  93. sf->SetProperty("GENERATED", "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. lg->GetMakefile()->AddCMakeDependFile(this->Input);
  106. cmSystemTools::GetPermissions(this->Input.c_str(), perm);
  107. cmsys::ifstream fin(this->Input.c_str());
  108. if (!fin) {
  109. std::ostringstream e;
  110. e << "Evaluation file \"" << this->Input << "\" cannot be read.";
  111. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  112. return;
  113. }
  114. std::string line;
  115. std::string sep;
  116. while (cmSystemTools::GetLineFromStream(fin, line)) {
  117. inputContent += sep + line;
  118. sep = "\n";
  119. }
  120. inputContent += sep;
  121. }
  122. cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
  123. cmGeneratorExpression contentGE(lfbt);
  124. CM_AUTO_PTR<cmCompiledGeneratorExpression> inputExpression =
  125. contentGE.Parse(inputContent);
  126. std::map<std::string, std::string> outputFiles;
  127. std::vector<std::string> allConfigs;
  128. lg->GetMakefile()->GetConfigurations(allConfigs);
  129. if (allConfigs.empty()) {
  130. allConfigs.push_back("");
  131. }
  132. std::vector<std::string> enabledLanguages;
  133. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  134. gg->GetEnabledLanguages(enabledLanguages);
  135. for (std::vector<std::string>::const_iterator le = enabledLanguages.begin();
  136. le != enabledLanguages.end(); ++le) {
  137. for (std::vector<std::string>::const_iterator li = allConfigs.begin();
  138. li != allConfigs.end(); ++li) {
  139. this->Generate(lg, *li, *le, inputExpression.get(), outputFiles, perm);
  140. if (cmSystemTools::GetFatalErrorOccured()) {
  141. return;
  142. }
  143. }
  144. }
  145. }