cmInstallScriptGenerator.cxx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmInstallScriptGenerator.h"
  4. #include <memory>
  5. #include <ostream>
  6. #include <vector>
  7. #include "cmGeneratorExpression.h"
  8. #include "cmLocalGenerator.h"
  9. #include "cmMessageType.h"
  10. #include "cmPolicies.h"
  11. #include "cmScriptGenerator.h"
  12. cmInstallScriptGenerator::cmInstallScriptGenerator(const char* script,
  13. bool code,
  14. const char* component,
  15. bool exclude_from_all)
  16. : cmInstallGenerator(nullptr, std::vector<std::string>(), component,
  17. MessageDefault, exclude_from_all)
  18. , Script(script)
  19. , Code(code)
  20. , AllowGenex(false)
  21. {
  22. // We need per-config actions if the script has generator expressions.
  23. if (cmGeneratorExpression::Find(Script) != std::string::npos) {
  24. this->ActionsPerConfig = true;
  25. }
  26. }
  27. cmInstallScriptGenerator::~cmInstallScriptGenerator() = default;
  28. bool cmInstallScriptGenerator::Compute(cmLocalGenerator* lg)
  29. {
  30. this->LocalGenerator = lg;
  31. if (this->ActionsPerConfig) {
  32. switch (this->LocalGenerator->GetPolicyStatus(cmPolicies::CMP0087)) {
  33. case cmPolicies::WARN:
  34. this->LocalGenerator->IssueMessage(
  35. MessageType::AUTHOR_WARNING,
  36. cmPolicies::GetPolicyWarning(cmPolicies::CMP0087));
  37. CM_FALLTHROUGH;
  38. case cmPolicies::OLD:
  39. break;
  40. case cmPolicies::NEW:
  41. case cmPolicies::REQUIRED_ALWAYS:
  42. case cmPolicies::REQUIRED_IF_USED:
  43. this->AllowGenex = true;
  44. break;
  45. }
  46. }
  47. return true;
  48. }
  49. void cmInstallScriptGenerator::AddScriptInstallRule(std::ostream& os,
  50. Indent indent,
  51. std::string const& script)
  52. {
  53. if (this->Code) {
  54. os << indent << script << "\n";
  55. } else {
  56. os << indent << "include(\"" << script << "\")\n";
  57. }
  58. }
  59. void cmInstallScriptGenerator::GenerateScriptActions(std::ostream& os,
  60. Indent indent)
  61. {
  62. if (this->AllowGenex && this->ActionsPerConfig) {
  63. this->cmInstallGenerator::GenerateScriptActions(os, indent);
  64. } else {
  65. this->AddScriptInstallRule(os, indent, this->Script);
  66. }
  67. }
  68. void cmInstallScriptGenerator::GenerateScriptForConfig(
  69. std::ostream& os, const std::string& config, Indent indent)
  70. {
  71. if (this->AllowGenex) {
  72. cmGeneratorExpression ge;
  73. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  74. ge.Parse(this->Script);
  75. this->AddScriptInstallRule(os, indent,
  76. cge->Evaluate(this->LocalGenerator, config));
  77. } else {
  78. this->AddScriptInstallRule(os, indent, this->Script);
  79. }
  80. }