cmGeneratorExpressionEvaluationFile.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. CM_AUTO_PTR<cmCompiledGeneratorExpression> outputFileExpr,
  21. CM_AUTO_PTR<cmCompiledGeneratorExpression> condition, bool inputIsContent)
  22. : Input(input)
  23. , OutputFileExpr(outputFileExpr)
  24. , Condition(condition)
  25. , InputIsContent(inputIsContent)
  26. {
  27. }
  28. void cmGeneratorExpressionEvaluationFile::Generate(
  29. cmLocalGenerator* lg, const std::string& config, const std::string& lang,
  30. cmCompiledGeneratorExpression* inputExpression,
  31. std::map<std::string, std::string>& outputFiles, mode_t perm)
  32. {
  33. std::string rawCondition = this->Condition->GetInput();
  34. if (!rawCondition.empty()) {
  35. std::string condResult = this->Condition->Evaluate(
  36. lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, lang);
  37. if (condResult == "0") {
  38. return;
  39. }
  40. if (condResult != "1") {
  41. std::ostringstream e;
  42. e << "Evaluation file condition \"" << rawCondition
  43. << "\" did "
  44. "not evaluate to valid content. Got \""
  45. << condResult << "\".";
  46. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  47. return;
  48. }
  49. }
  50. const std::string outputFileName = this->OutputFileExpr->Evaluate(
  51. lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, lang);
  52. const std::string outputContent = inputExpression->Evaluate(
  53. lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, lang);
  54. std::map<std::string, std::string>::iterator it =
  55. outputFiles.find(outputFileName);
  56. if (it != outputFiles.end()) {
  57. if (it->second == outputContent) {
  58. return;
  59. }
  60. std::ostringstream e;
  61. e << "Evaluation file to be written multiple times for different "
  62. "configurations or languages with different content:\n "
  63. << outputFileName;
  64. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  65. return;
  66. }
  67. lg->GetMakefile()->AddCMakeOutputFile(outputFileName);
  68. this->Files.push_back(outputFileName);
  69. outputFiles[outputFileName] = outputContent;
  70. cmGeneratedFileStream fout(outputFileName.c_str());
  71. fout.SetCopyIfDifferent(true);
  72. fout << outputContent;
  73. if (fout.Close() && perm) {
  74. cmSystemTools::SetPermissions(outputFileName.c_str(), perm);
  75. }
  76. }
  77. void cmGeneratorExpressionEvaluationFile::CreateOutputFile(
  78. cmLocalGenerator* lg, std::string const& config)
  79. {
  80. std::vector<std::string> enabledLanguages;
  81. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  82. gg->GetEnabledLanguages(enabledLanguages);
  83. for (std::vector<std::string>::const_iterator le = enabledLanguages.begin();
  84. le != enabledLanguages.end(); ++le) {
  85. std::string name = this->OutputFileExpr->Evaluate(
  86. lg, config, false, CM_NULLPTR, CM_NULLPTR, CM_NULLPTR, *le);
  87. cmSourceFile* sf = lg->GetMakefile()->GetOrCreateSource(name);
  88. sf->SetProperty("GENERATED", "1");
  89. gg->SetFilenameTargetDepends(
  90. sf, this->OutputFileExpr->GetSourceSensitiveTargets());
  91. }
  92. }
  93. void cmGeneratorExpressionEvaluationFile::Generate(cmLocalGenerator* lg)
  94. {
  95. mode_t perm = 0;
  96. std::string inputContent;
  97. if (this->InputIsContent) {
  98. inputContent = this->Input;
  99. } else {
  100. lg->GetMakefile()->AddCMakeDependFile(this->Input);
  101. cmSystemTools::GetPermissions(this->Input.c_str(), perm);
  102. cmsys::ifstream fin(this->Input.c_str());
  103. if (!fin) {
  104. std::ostringstream e;
  105. e << "Evaluation file \"" << this->Input << "\" cannot be read.";
  106. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  107. return;
  108. }
  109. std::string line;
  110. std::string sep;
  111. while (cmSystemTools::GetLineFromStream(fin, line)) {
  112. inputContent += sep + line;
  113. sep = "\n";
  114. }
  115. inputContent += sep;
  116. }
  117. cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
  118. cmGeneratorExpression contentGE(lfbt);
  119. CM_AUTO_PTR<cmCompiledGeneratorExpression> inputExpression =
  120. contentGE.Parse(inputContent);
  121. std::map<std::string, std::string> outputFiles;
  122. std::vector<std::string> allConfigs;
  123. lg->GetMakefile()->GetConfigurations(allConfigs);
  124. if (allConfigs.empty()) {
  125. allConfigs.push_back("");
  126. }
  127. std::vector<std::string> enabledLanguages;
  128. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  129. gg->GetEnabledLanguages(enabledLanguages);
  130. for (std::vector<std::string>::const_iterator le = enabledLanguages.begin();
  131. le != enabledLanguages.end(); ++le) {
  132. for (std::vector<std::string>::const_iterator li = allConfigs.begin();
  133. li != allConfigs.end(); ++li) {
  134. this->Generate(lg, *li, *le, inputExpression.get(), outputFiles, perm);
  135. if (cmSystemTools::GetFatalErrorOccured()) {
  136. return;
  137. }
  138. }
  139. }
  140. }