cmGeneratorExpressionEvaluationFile.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "cmMakefile.h"
  12. #include <cmsys/FStream.hxx>
  13. #include <assert.h>
  14. //----------------------------------------------------------------------------
  15. cmGeneratorExpressionEvaluationFile::cmGeneratorExpressionEvaluationFile(
  16. const std::string &input,
  17. cmsys::auto_ptr<cmCompiledGeneratorExpression> outputFileExpr,
  18. cmMakefile *makefile,
  19. cmsys::auto_ptr<cmCompiledGeneratorExpression> condition,
  20. bool inputIsContent)
  21. : Input(input),
  22. OutputFileExpr(outputFileExpr),
  23. Makefile(makefile),
  24. Condition(condition),
  25. InputIsContent(inputIsContent)
  26. {
  27. }
  28. //----------------------------------------------------------------------------
  29. void cmGeneratorExpressionEvaluationFile::Generate(const std::string& config,
  30. cmCompiledGeneratorExpression* inputExpression,
  31. std::map<std::string, std::string> &outputFiles)
  32. {
  33. std::string rawCondition = this->Condition->GetInput();
  34. if (!rawCondition.empty())
  35. {
  36. std::string condResult = this->Condition->Evaluate(this->Makefile, config);
  37. if (condResult == "0")
  38. {
  39. return;
  40. }
  41. if (condResult != "1")
  42. {
  43. cmOStringStream e;
  44. e << "Evaluation file condition \"" << rawCondition << "\" did "
  45. "not evaluate to valid content. Got \"" << condResult << "\".";
  46. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  47. return;
  48. }
  49. }
  50. const std::string outputFileName
  51. = this->OutputFileExpr->Evaluate(this->Makefile, config);
  52. const std::string outputContent
  53. = inputExpression->Evaluate(this->Makefile, config);
  54. std::map<std::string, std::string>::iterator it
  55. = outputFiles.find(outputFileName);
  56. if(it != outputFiles.end())
  57. {
  58. if (it->second == outputContent)
  59. {
  60. return;
  61. }
  62. cmOStringStream e;
  63. e << "Evaluation file to be written multiple times for different "
  64. "configurations with different content:\n " << outputFileName;
  65. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  66. return;
  67. }
  68. this->Files.push_back(outputFileName);
  69. outputFiles[outputFileName] = outputContent;
  70. cmsys::ofstream fout(outputFileName.c_str());
  71. if(!fout)
  72. {
  73. cmOStringStream e;
  74. e << "Evaluation file \"" << outputFileName << "\" cannot be written.";
  75. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  76. return;
  77. }
  78. fout << outputContent;
  79. fout.close();
  80. }
  81. //----------------------------------------------------------------------------
  82. void cmGeneratorExpressionEvaluationFile::Generate()
  83. {
  84. std::string inputContent;
  85. if (this->InputIsContent)
  86. {
  87. inputContent = this->Input;
  88. }
  89. else
  90. {
  91. cmsys::ifstream fin(this->Input.c_str());
  92. if(!fin)
  93. {
  94. cmOStringStream e;
  95. e << "Evaluation file \"" << this->Input << "\" cannot be read.";
  96. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  97. return;
  98. }
  99. std::string line;
  100. std::string sep;
  101. while(cmSystemTools::GetLineFromStream(fin, line))
  102. {
  103. inputContent += sep + line;
  104. sep = "\n";
  105. }
  106. inputContent += sep;
  107. }
  108. cmListFileBacktrace lfbt = this->OutputFileExpr->GetBacktrace();
  109. cmGeneratorExpression contentGE(lfbt);
  110. cmsys::auto_ptr<cmCompiledGeneratorExpression> inputExpression
  111. = contentGE.Parse(inputContent);
  112. std::map<std::string, std::string> outputFiles;
  113. std::vector<std::string> allConfigs;
  114. this->Makefile->GetConfigurations(allConfigs);
  115. if (allConfigs.empty())
  116. {
  117. this->Generate("", inputExpression.get(), outputFiles);
  118. }
  119. else
  120. {
  121. for(std::vector<std::string>::const_iterator li = allConfigs.begin();
  122. li != allConfigs.end(); ++li)
  123. {
  124. this->Generate(*li, inputExpression.get(), outputFiles);
  125. if(cmSystemTools::GetFatalErrorOccured())
  126. {
  127. return;
  128. }
  129. }
  130. }
  131. }