cmInstallScriptGenerator.cxx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <ostream>
  5. #include <utility>
  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(
  13. std::string script, bool code, std::string const& component,
  14. bool exclude_from_all, cmListFileBacktrace backtrace)
  15. : cmInstallGenerator("", std::vector<std::string>(), component,
  16. MessageDefault, exclude_from_all, std::move(backtrace))
  17. , Script(std::move(script))
  18. , Code(code)
  19. , AllowGenex(false)
  20. {
  21. // We need per-config actions if the script has generator expressions.
  22. if (cmGeneratorExpression::Find(this->Script) != std::string::npos) {
  23. this->ActionsPerConfig = true;
  24. }
  25. }
  26. cmInstallScriptGenerator::~cmInstallScriptGenerator() = default;
  27. bool cmInstallScriptGenerator::Compute(cmLocalGenerator* lg)
  28. {
  29. this->LocalGenerator = lg;
  30. if (this->ActionsPerConfig) {
  31. switch (this->LocalGenerator->GetPolicyStatus(cmPolicies::CMP0087)) {
  32. case cmPolicies::WARN:
  33. this->LocalGenerator->IssueMessage(
  34. MessageType::AUTHOR_WARNING,
  35. cmPolicies::GetPolicyWarning(cmPolicies::CMP0087));
  36. CM_FALLTHROUGH;
  37. case cmPolicies::OLD:
  38. break;
  39. case cmPolicies::NEW:
  40. case cmPolicies::REQUIRED_ALWAYS:
  41. case cmPolicies::REQUIRED_IF_USED:
  42. this->AllowGenex = true;
  43. break;
  44. }
  45. }
  46. return true;
  47. }
  48. std::string cmInstallScriptGenerator::GetScript(
  49. std::string const& config) const
  50. {
  51. std::string script;
  52. if (this->AllowGenex && this->ActionsPerConfig) {
  53. script = cmGeneratorExpression::Evaluate(this->Script,
  54. this->LocalGenerator, config);
  55. } else {
  56. script = this->Script;
  57. }
  58. return script;
  59. }
  60. void cmInstallScriptGenerator::AddScriptInstallRule(
  61. std::ostream& os, Indent indent, std::string const& script) const
  62. {
  63. if (this->Code) {
  64. os << indent << script << "\n";
  65. } else {
  66. os << indent << "include(\"" << script << "\")\n";
  67. }
  68. }
  69. void cmInstallScriptGenerator::GenerateScriptActions(std::ostream& os,
  70. Indent indent)
  71. {
  72. if (this->AllowGenex && this->ActionsPerConfig) {
  73. this->cmInstallGenerator::GenerateScriptActions(os, indent);
  74. } else {
  75. this->AddScriptInstallRule(os, indent, this->Script);
  76. }
  77. }
  78. void cmInstallScriptGenerator::GenerateScriptForConfig(
  79. std::ostream& os, const std::string& config, Indent indent)
  80. {
  81. this->AddScriptInstallRule(os, indent, this->GetScript(config));
  82. }