cmGeneratorExpressionEvaluationFile.cxx 4.3 KB

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