cmGeneratorExpressionEvaluationFile.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "cmLocalGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmSourceFile.h"
  16. #include <cmsys/FStream.hxx>
  17. #include <assert.h>
  18. cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
  19. const std::string& input,
  20. cmsys::auto_ptr<cmCompiledGeneratorExpression> outputFileExpr,
  21. cmsys::auto_ptr<cmCompiledGeneratorExpression> condition,
  22. bool inputIsContent)
  23. : Input(input)
  24. , OutputFileExpr(outputFileExpr)
  25. , Condition(condition)
  26. , InputIsContent(inputIsContent)
  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, false, 0, 0, 0, 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(cmake::FATAL_ERROR, e.str());
  48. return;
  49. }
  50. }
  51. const std::string outputFileName =
  52. this->OutputFileExpr->Evaluate(lg, config, false, 0, 0, 0, lang);
  53. const std::string outputContent =
  54. inputExpression->Evaluate(lg, config, false, 0, 0, 0, lang);
  55. std::map<std::string, std::string>::iterator it =
  56. outputFiles.find(outputFileName);
  57. if (it != outputFiles.end()) {
  58. if (it->second == outputContent) {
  59. return;
  60. }
  61. std::ostringstream e;
  62. e << "Evaluation file to be written multiple times for different "
  63. "configurations or languages with different content:\n "
  64. << outputFileName;
  65. lg->IssueMessage(cmake::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.c_str());
  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::vector<std::string>::const_iterator le = enabledLanguages.begin();
  85. le != enabledLanguages.end(); ++le) {
  86. std::string name =
  87. this->OutputFileExpr->Evaluate(lg, config, false, 0, 0, 0, *le);
  88. cmSourceFile* sf = lg->GetMakefile()->GetOrCreateSource(name);
  89. sf->SetProperty("GENERATED", "1");
  90. gg->SetFilenameTargetDepends(
  91. sf, this->OutputFileExpr->GetSourceSensitiveTargets());
  92. }
  93. }
  94. void cmGeneratorExpressionEvaluationFile::Generate(cmLocalGenerator* lg)
  95. {
  96. mode_t perm = 0;
  97. std::string inputContent;
  98. if (this->InputIsContent) {
  99. inputContent = this->Input;
  100. } else {
  101. lg->GetMakefile()->AddCMakeDependFile(this->Input);
  102. cmSystemTools::GetPermissions(this->Input.c_str(), perm);
  103. cmsys::ifstream fin(this->Input.c_str());
  104. if (!fin) {
  105. std::ostringstream e;
  106. e << "Evaluation file \"" << this->Input << "\" cannot be read.";
  107. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  108. return;
  109. }
  110. std::string line;
  111. std::string sep;
  112. while (cmSystemTools::GetLineFromStream(fin, line)) {
  113. inputContent += sep + line;
  114. sep = "\n";
  115. }
  116. inputContent += sep;
  117. }
  118. cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
  119. cmGeneratorExpression contentGE(lfbt);
  120. cmsys::auto_ptr<cmCompiledGeneratorExpression> inputExpression =
  121. contentGE.Parse(inputContent);
  122. std::map<std::string, std::string> outputFiles;
  123. std::vector<std::string> allConfigs;
  124. lg->GetMakefile()->GetConfigurations(allConfigs);
  125. if (allConfigs.empty()) {
  126. allConfigs.push_back("");
  127. }
  128. std::vector<std::string> enabledLanguages;
  129. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  130. gg->GetEnabledLanguages(enabledLanguages);
  131. for (std::vector<std::string>::const_iterator le = enabledLanguages.begin();
  132. le != enabledLanguages.end(); ++le) {
  133. for (std::vector<std::string>::const_iterator li = allConfigs.begin();
  134. li != allConfigs.end(); ++li) {
  135. this->Generate(lg, *li, *le, inputExpression.get(), outputFiles, perm);
  136. if (cmSystemTools::GetFatalErrorOccured()) {
  137. return;
  138. }
  139. }
  140. }
  141. }