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